summaryrefslogtreecommitdiff
path: root/tests/protocol/test_metadata.py
diff options
context:
space:
mode:
authorƁukasz 'sil2100' Zemczak <lukasz.zemczak@ubuntu.com>2017-05-18 19:58:02 +0200
committerusd-importer <ubuntu-server@lists.ubuntu.com>2017-05-31 09:53:12 +0000
commit4fb0b5a09b26135ade285844da5d7dfe582a8d4c (patch)
tree09b1e5867d6e7501118cdd0af0012b51fc216530 /tests/protocol/test_metadata.py
parent473ad6fbfe0b9c3b362b530492928303f2b4c7f3 (diff)
downloadvyos-walinuxagent-4fb0b5a09b26135ade285844da5d7dfe582a8d4c.tar.gz
vyos-walinuxagent-4fb0b5a09b26135ade285844da5d7dfe582a8d4c.zip
Import patches-unapplied version 2.2.12-0ubuntu1 to ubuntu/artful-proposed
Imported using git-ubuntu import. Changelog parent: 473ad6fbfe0b9c3b362b530492928303f2b4c7f3 New changelog entries: * New upstream release (LP: #1690854). - Refreshed debian/patches/disable_import_test.patch.
Diffstat (limited to 'tests/protocol/test_metadata.py')
-rw-r--r--tests/protocol/test_metadata.py124
1 files changed, 119 insertions, 5 deletions
diff --git a/tests/protocol/test_metadata.py b/tests/protocol/test_metadata.py
index f390f7a..ee4ba3e 100644
--- a/tests/protocol/test_metadata.py
+++ b/tests/protocol/test_metadata.py
@@ -15,14 +15,23 @@
# Requires Python 2.4+ and Openssl 1.0+
#
-from tests.tools import *
-from tests.protocol.mockmetadata import *
+import json
+
+from azurelinuxagent.common.future import ustr
+
from azurelinuxagent.common.utils.restutil import httpclient
-from azurelinuxagent.common.protocol.metadata import MetadataProtocol
+from azurelinuxagent.common.protocol.metadata import *
+from azurelinuxagent.common.protocol.restapi import *
+
+from tests.protocol.mockmetadata import *
+from tests.tools import *
-@patch("time.sleep")
-@patch("azurelinuxagent.common.protocol.metadata.restutil")
class TestMetadataProtocolGetters(AgentTestCase):
+ def load_json(self, path):
+ return json.loads(ustr(load_data(path)), encoding="utf-8")
+
+ @patch("time.sleep")
+ @patch("azurelinuxagent.common.protocol.metadata.restutil")
def _test_getters(self, test_data, mock_restutil ,_):
mock_restutil.http_get.side_effect = test_data.mock_http_get
@@ -43,3 +52,108 @@ class TestMetadataProtocolGetters(AgentTestCase):
self._test_getters(test_data, *args)
+ @patch("azurelinuxagent.common.protocol.metadata.MetadataProtocol.update_goal_state")
+ @patch("azurelinuxagent.common.protocol.metadata.MetadataProtocol._get_data")
+ def test_get_vmagents_manifests(self, mock_get, mock_update):
+ data = self.load_json("metadata/vmagent_manifests.json")
+ mock_get.return_value = data, 42
+
+ protocol = MetadataProtocol()
+ manifests, etag = protocol.get_vmagent_manifests()
+
+ self.assertEqual(mock_update.call_count, 1)
+ self.assertEqual(mock_get.call_count, 1)
+
+ manifests_uri = BASE_URI.format(
+ METADATA_ENDPOINT,
+ "vmAgentVersions",
+ APIVERSION)
+ self.assertEqual(mock_get.call_args[0][0], manifests_uri)
+
+ self.assertEqual(etag, 42)
+ self.assertNotEqual(None, manifests)
+ self.assertEqual(len(manifests.vmAgentManifests), 1)
+
+ manifest = manifests.vmAgentManifests[0]
+ self.assertEqual(manifest.family, conf.get_autoupdate_gafamily())
+ self.assertEqual(len(manifest.versionsManifestUris), 2)
+
+ # Same etag returns the same data
+ data = self.load_json("metadata/vmagent_manifests_invalid1.json")
+ mock_get.return_value = data, 42
+ next_manifests, etag = protocol.get_vmagent_manifests()
+
+ self.assertEqual(etag, 42)
+ self.assertEqual(manifests, next_manifests)
+
+ # New etag returns new data
+ mock_get.return_value = data, 43
+ self.assertRaises(ProtocolError, protocol.get_vmagent_manifests)
+
+ @patch("azurelinuxagent.common.protocol.metadata.MetadataProtocol.update_goal_state")
+ @patch("azurelinuxagent.common.protocol.metadata.MetadataProtocol._get_data")
+ def test_get_vmagents_manifests_raises(self, mock_get, mock_update):
+ data = self.load_json("metadata/vmagent_manifests_invalid1.json")
+ mock_get.return_value = data, 42
+
+ protocol = MetadataProtocol()
+ self.assertRaises(ProtocolError, protocol.get_vmagent_manifests)
+
+ data = self.load_json("metadata/vmagent_manifests_invalid2.json")
+ mock_get.return_value = data, 43
+ self.assertRaises(ProtocolError, protocol.get_vmagent_manifests)
+
+ @patch("azurelinuxagent.common.protocol.metadata.MetadataProtocol.update_goal_state")
+ @patch("azurelinuxagent.common.protocol.metadata.MetadataProtocol._get_data")
+ def test_get_vmagent_pkgs(self, mock_get, mock_update):
+ data = self.load_json("metadata/vmagent_manifests.json")
+ mock_get.return_value = data, 42
+
+ protocol = MetadataProtocol()
+ manifests, etag = protocol.get_vmagent_manifests()
+ manifest = manifests.vmAgentManifests[0]
+
+ data = self.load_json("metadata/vmagent_manifest1.json")
+ mock_get.return_value = data, 42
+ pkgs = protocol.get_vmagent_pkgs(manifest)
+
+ self.assertNotEqual(None, pkgs)
+ self.assertEqual(len(pkgs.versions), 2)
+
+ for pkg in pkgs.versions:
+ self.assertNotEqual(None, pkg.version)
+ self.assertTrue(len(pkg.uris) > 0)
+
+ for uri in pkg.uris:
+ self.assertTrue(uri.uri.endswith("zip"))
+
+ @patch("azurelinuxagent.common.protocol.metadata.MetadataProtocol._post_data")
+ def test_report_event(self, mock_post):
+ events = TelemetryEventList()
+
+ data = self.load_json("events/1478123456789000.tld")
+ event = TelemetryEvent()
+ set_properties("event", event, data)
+ events.events.append(event)
+
+ data = self.load_json("events/1478123456789001.tld")
+ event = TelemetryEvent()
+ set_properties("event", event, data)
+ events.events.append(event)
+
+ data = self.load_json("events/1479766858966718.tld")
+ event = TelemetryEvent()
+ set_properties("event", event, data)
+ events.events.append(event)
+
+ protocol = MetadataProtocol()
+ protocol.report_event(events)
+
+ events_uri = BASE_URI.format(
+ METADATA_ENDPOINT,
+ "status/telemetry",
+ APIVERSION)
+
+ self.assertEqual(mock_post.call_count, 1)
+ self.assertEqual(mock_post.call_args[0][0], events_uri)
+ self.assertEqual(mock_post.call_args[0][1], get_properties(events))