diff options
Diffstat (limited to 'tests/protocol')
-rw-r--r-- | tests/protocol/mockwiredata.py | 2 | ||||
-rw-r--r-- | tests/protocol/test_hostplugin.py | 4 | ||||
-rw-r--r-- | tests/protocol/test_metadata.py | 124 | ||||
-rw-r--r-- | tests/protocol/test_wire.py | 53 |
4 files changed, 152 insertions, 31 deletions
diff --git a/tests/protocol/mockwiredata.py b/tests/protocol/mockwiredata.py index c789de5..4e45623 100644 --- a/tests/protocol/mockwiredata.py +++ b/tests/protocol/mockwiredata.py @@ -30,7 +30,7 @@ DATA_FILE = { "ga_manifest" : "wire/ga_manifest.xml", "trans_prv": "wire/trans_prv", "trans_cert": "wire/trans_cert", - "test_ext": "ext/sample_ext.zip" + "test_ext": "ext/sample_ext-1.2.0.zip" } DATA_FILE_NO_EXT = DATA_FILE.copy() diff --git a/tests/protocol/test_hostplugin.py b/tests/protocol/test_hostplugin.py index ef91998..e203615 100644 --- a/tests/protocol/test_hostplugin.py +++ b/tests/protocol/test_hostplugin.py @@ -33,6 +33,7 @@ import azurelinuxagent.common.protocol.wire as wire import azurelinuxagent.common.protocol.hostplugin as hostplugin from azurelinuxagent.common import event +from azurelinuxagent.common.exception import ProtocolError, HttpError from azurelinuxagent.common.protocol.hostplugin import API_VERSION from azurelinuxagent.common.utils import restutil @@ -128,8 +129,10 @@ class TestHostPlugin(AgentTestCase): wire_protocol_client.get_goal_state = Mock(return_value=test_goal_state) wire_protocol_client.ext_conf = wire.ExtensionsConfig(None) wire_protocol_client.ext_conf.status_upload_blob = sas_url + wire_protocol_client.ext_conf.status_upload_blob_type = page_blob_type wire_protocol_client.status_blob.set_vm_status(status) wire_protocol_client.upload_status_blob() + self.assertEqual(patch_upload.call_count, 1) self.assertTrue(patch_put.call_count == 1, "Fallback was not engaged") self.assertTrue(patch_put.call_args[0][0] == sas_url) @@ -156,6 +159,7 @@ class TestHostPlugin(AgentTestCase): client.get_goal_state = Mock(return_value=test_goal_state) client.ext_conf = wire.ExtensionsConfig(None) client.ext_conf.status_upload_blob = sas_url + client.ext_conf.status_upload_blob_type = page_blob_type client.status_blob.set_vm_status(status) client.upload_status_blob() self.assertTrue(patch_put.call_count == 1, 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)) diff --git a/tests/protocol/test_wire.py b/tests/protocol/test_wire.py index dda7a2b..ba9fc7d 100644 --- a/tests/protocol/test_wire.py +++ b/tests/protocol/test_wire.py @@ -152,11 +152,12 @@ class TestWireProtocolGetters(AgentTestCase): wire_protocol_client = WireProtocol(wireserver_url).client wire_protocol_client.ext_conf = ExtensionsConfig(None) wire_protocol_client.ext_conf.status_upload_blob = testurl + wire_protocol_client.ext_conf.status_upload_blob_type = testtype wire_protocol_client.status_blob.vm_status = vmstatus with patch.object(WireClient, "get_goal_state") as patch_get_goal_state: with patch.object(HostPluginProtocol, "put_vm_status") as patch_host_ga_plugin_upload: - with patch.object(StatusBlob, "upload", return_value=True) as patch_default_upload: + with patch.object(StatusBlob, "upload") as patch_default_upload: HostPluginProtocol.set_default_channel(False) wire_protocol_client.upload_status_blob() @@ -190,7 +191,25 @@ class TestWireProtocolGetters(AgentTestCase): self.assertTrue(HostPluginProtocol.is_default_channel()) HostPluginProtocol.set_default_channel(False) - def test_upload_status_blob_error_reporting(self, *args): + def test_upload_status_blob_unknown_type_assumes_block(self, *args): + vmstatus = VMStatus(message="Ready", status="Ready") + wire_protocol_client = WireProtocol(wireserver_url).client + wire_protocol_client.ext_conf = ExtensionsConfig(None) + wire_protocol_client.ext_conf.status_upload_blob = testurl + wire_protocol_client.ext_conf.status_upload_blob_type = "NotALegalType" + wire_protocol_client.status_blob.vm_status = vmstatus + + with patch.object(WireClient, "get_goal_state") as patch_get_goal_state: + with patch.object(StatusBlob, "prepare") as patch_prepare: + with patch.object(StatusBlob, "upload") as patch_default_upload: + HostPluginProtocol.set_default_channel(False) + wire_protocol_client.upload_status_blob() + + patch_prepare.assert_called_once_with("BlockBlob") + patch_default_upload.assert_called_once_with(testurl) + patch_get_goal_state.assert_not_called() + + def test_upload_status_blob_reports_prepare_error(self, *args): vmstatus = VMStatus(message="Ready", status="Ready") wire_protocol_client = WireProtocol(wireserver_url).client wire_protocol_client.ext_conf = ExtensionsConfig(None) @@ -199,29 +218,13 @@ class TestWireProtocolGetters(AgentTestCase): wire_protocol_client.status_blob.vm_status = vmstatus goal_state = GoalState(WireProtocolData(DATA_FILE).goal_state) - with patch.object(HostPluginProtocol, - "ensure_initialized", - return_value=True): - with patch.object(StatusBlob, - "put_block_blob", - side_effect=HttpError("error")): - with patch.object(StatusBlob, - "get_blob_type", - return_value='BlockBlob'): - with patch.object(HostPluginProtocol, - "put_vm_status"): - with patch.object(WireClient, - "report_blob_type", - side_effect=MagicMock()): - with patch.object(event, - "add_event") as patch_add_event: - HostPluginProtocol.set_default_channel(False) - wire_protocol_client.get_goal_state = Mock(return_value=goal_state) - wire_protocol_client.upload_status_blob() - wire_protocol_client.get_goal_state.assert_called_once() - self.assertTrue(patch_add_event.call_count == 1) - self.assertTrue(patch_add_event.call_args_list[0][1]['op'] == 'ReportStatus') - self.assertFalse(HostPluginProtocol.is_default_channel()) + with patch.object(StatusBlob, "prepare", + side_effect=Exception) as mock_prepare: + with patch.object(WireClient, "report_status_event") as mock_event: + wire_protocol_client.upload_status_blob() + + mock_prepare.assert_called_once() + mock_event.assert_called_once() def test_get_in_vm_artifacts_profile_blob_not_available(self, *args): wire_protocol_client = WireProtocol(wireserver_url).client |