From 27f4190426c9584af9e44a913995c4101c162724 Mon Sep 17 00:00:00 2001 From: narrieta Date: Wed, 9 Aug 2023 07:47:42 -0700 Subject: [PATCH 1/4] Reorganize file structure of unit tests --- tests/common/dhcp/test_dhcp.py | 2 +- tests/common/mock_cgroup_environment.py | 2 +- tests/common/mock_environment.py | 2 +- tests/common/osutil/test_alpine.py | 2 +- tests/common/osutil/test_arch.py | 2 +- tests/common/osutil/test_bigip.py | 2 +- tests/common/osutil/test_clearlinux.py | 2 +- tests/common/osutil/test_coreos.py | 2 +- tests/common/osutil/test_default.py | 2 +- tests/common/osutil/test_default_osutil.py | 2 +- tests/common/osutil/test_factory.py | 2 +- tests/common/osutil/test_freebsd.py | 2 +- tests/common/osutil/test_nsbsd.py | 2 +- tests/common/osutil/test_openbsd.py | 2 +- tests/common/osutil/test_openwrt.py | 2 +- tests/common/osutil/test_photonos.py | 2 +- tests/common/osutil/test_redhat.py | 2 +- tests/common/osutil/test_suse.py | 2 +- tests/common/osutil/test_ubuntu.py | 2 +- tests/{distro => common/protocol}/__init__.py | 0 .../protocol/test_datacontract.py | 0 ...sions_goal_state_from_extensions_config.py | 18 +-- ..._extensions_goal_state_from_vm_settings.py | 20 +-- .../{ => common}/protocol/test_goal_state.py | 46 +++--- .../protocol/test_healthservice.py | 4 +- .../{ => common}/protocol/test_hostplugin.py | 20 +-- .../protocol/test_image_info_matcher.py | 0 tests/{ => common}/protocol/test_imds.py | 4 +- .../test_metadata_server_migration_util.py | 2 +- .../protocol/test_protocol_util.py | 2 +- tests/{ => common}/protocol/test_wire.py | 86 +++++----- tests/common/test_agent_supported_feature.py | 2 +- tests/common/test_cgroupapi.py | 4 +- tests/common/test_cgroupconfigurator.py | 4 +- tests/common/test_cgroups.py | 2 +- tests/common/test_cgroupstelemetry.py | 2 +- tests/common/test_conf.py | 2 +- tests/common/test_errorstate.py | 2 +- tests/common/test_event.py | 16 +- tests/common/test_logcollector.py | 2 +- tests/common/test_logger.py | 2 +- tests/common/test_persist_firewall_rules.py | 2 +- tests/common/test_singletonperthread.py | 2 +- tests/common/test_telemetryevent.py | 2 +- tests/common/test_version.py | 2 +- tests/{protocol => common/utils}/__init__.py | 0 tests/{ => common}/utils/test_archive.py | 2 +- tests/{ => common}/utils/test_crypt_util.py | 2 +- .../utils/test_extension_process_util.py | 2 +- tests/{ => common}/utils/test_file_util.py | 2 +- .../utils/test_flexible_version.py | 0 tests/{ => common}/utils/test_network_util.py | 2 +- tests/{ => common}/utils/test_passwords.txt | 0 tests/{ => common}/utils/test_rest_util.py | 2 +- tests/{ => common}/utils/test_shell_util.py | 4 +- tests/{ => common}/utils/test_text_util.py | 2 +- tests/daemon/test_daemon.py | 2 +- tests/daemon/test_resourcedisk.py | 123 ++++++++++++++- tests/{distro => daemon}/test_scvmm.py | 2 +- tests/distro/test_resourceDisk.py | 148 ------------------ tests/ga/test_agent_update_handler.py | 8 +- tests/ga/test_collect_logs.py | 8 +- tests/ga/test_collect_telemetry_events.py | 4 +- tests/ga/test_env.py | 2 +- tests/ga/test_extension.py | 138 ++++++++-------- tests/ga/test_exthandlers.py | 8 +- .../ga/test_exthandlers_download_extension.py | 8 +- .../ga/test_exthandlers_exthandlerinstance.py | 2 +- tests/ga/test_guestagent.py | 12 +- tests/ga/test_monitor.py | 8 +- tests/ga/test_multi_config_extension.py | 10 +- tests/ga/test_periodic_operation.py | 2 +- tests/ga/test_remoteaccess.py | 10 +- tests/ga/test_remoteaccess_handler.py | 6 +- tests/ga/test_report_status.py | 18 +-- tests/ga/test_send_telemetry_events.py | 10 +- tests/ga/test_update.py | 48 +++--- tests/{utils => lib}/cgroups_tools.py | 0 tests/{utils => lib}/event_logger_tools.py | 10 +- tests/{ga => lib}/extension_emulator.py | 10 +- .../http_request_predicates.py} | 0 tests/{utils => lib}/miscellaneous_tools.py | 0 .../mocks.py => lib/mock_update_handler.py} | 2 +- .../mocks.py => lib/mock_wire_protocol.py} | 10 +- tests/{ => lib}/tools.py | 4 +- .../wire_protocol_data.py} | 4 +- tests/pa/test_deprovision.py | 2 +- tests/pa/test_provision.py | 2 +- tests/test_agent.py | 2 +- tests/utils/__init__.py | 16 -- 90 files changed, 451 insertions(+), 492 deletions(-) rename tests/{distro => common/protocol}/__init__.py (100%) rename tests/{ => common}/protocol/test_datacontract.py (100%) rename tests/{ => common}/protocol/test_extensions_goal_state_from_extensions_config.py (85%) rename tests/{ => common}/protocol/test_extensions_goal_state_from_vm_settings.py (93%) rename tests/{ => common}/protocol/test_goal_state.py (93%) rename tests/{ => common}/protocol/test_healthservice.py (99%) rename tests/{ => common}/protocol/test_hostplugin.py (98%) rename tests/{ => common}/protocol/test_image_info_matcher.py (100%) rename tests/{ => common}/protocol/test_imds.py (99%) rename tests/{ => common}/protocol/test_metadata_server_migration_util.py (99%) rename tests/{ => common}/protocol/test_protocol_util.py (99%) rename tests/{ => common}/protocol/test_wire.py (94%) rename tests/{protocol => common/utils}/__init__.py (100%) rename tests/{ => common}/utils/test_archive.py (99%) rename tests/{ => common}/utils/test_crypt_util.py (96%) rename tests/{ => common}/utils/test_extension_process_util.py (99%) rename tests/{ => common}/utils/test_file_util.py (99%) rename tests/{ => common}/utils/test_flexible_version.py (100%) rename tests/{ => common}/utils/test_network_util.py (99%) rename tests/{ => common}/utils/test_passwords.txt (100%) rename tests/{ => common}/utils/test_rest_util.py (99%) rename tests/{ => common}/utils/test_shell_util.py (99%) rename tests/{ => common}/utils/test_text_util.py (99%) rename tests/{distro => daemon}/test_scvmm.py (98%) delete mode 100644 tests/distro/test_resourceDisk.py rename tests/{utils => lib}/cgroups_tools.py (100%) rename tests/{utils => lib}/event_logger_tools.py (89%) rename tests/{ga => lib}/extension_emulator.py (98%) rename tests/{protocol/HttpRequestPredicates.py => lib/http_request_predicates.py} (100%) rename tests/{utils => lib}/miscellaneous_tools.py (100%) rename tests/{ga/mocks.py => lib/mock_update_handler.py} (99%) rename tests/{protocol/mocks.py => lib/mock_wire_protocol.py} (96%) rename tests/{ => lib}/tools.py (99%) rename tests/{protocol/mockwiredata.py => lib/wire_protocol_data.py} (99%) delete mode 100644 tests/utils/__init__.py diff --git a/tests/common/dhcp/test_dhcp.py b/tests/common/dhcp/test_dhcp.py index b4eece5c2e..dda28985ae 100644 --- a/tests/common/dhcp/test_dhcp.py +++ b/tests/common/dhcp/test_dhcp.py @@ -18,7 +18,7 @@ import mock import azurelinuxagent.common.dhcp as dhcp import azurelinuxagent.common.osutil.default as osutil -from tests.tools import AgentTestCase, open_patch, patch +from tests.lib.tools import AgentTestCase, open_patch, patch class TestDHCP(AgentTestCase): diff --git a/tests/common/mock_cgroup_environment.py b/tests/common/mock_cgroup_environment.py index e38471060e..03c0d02bf6 100644 --- a/tests/common/mock_cgroup_environment.py +++ b/tests/common/mock_cgroup_environment.py @@ -17,7 +17,7 @@ # import contextlib import os -from tests.tools import patch, data_dir +from tests.lib.tools import patch, data_dir from tests.common.mock_environment import MockEnvironment, MockCommand _MOCKED_COMMANDS = [ diff --git a/tests/common/mock_environment.py b/tests/common/mock_environment.py index bedce0900d..8f5682cf8e 100644 --- a/tests/common/mock_environment.py +++ b/tests/common/mock_environment.py @@ -22,7 +22,7 @@ from azurelinuxagent.common.future import ustr from azurelinuxagent.common.utils import fileutil -from tests.tools import patch, patch_builtin +from tests.lib.tools import patch, patch_builtin class MockCommand: diff --git a/tests/common/osutil/test_alpine.py b/tests/common/osutil/test_alpine.py index d2eb361148..ec669cf3eb 100644 --- a/tests/common/osutil/test_alpine.py +++ b/tests/common/osutil/test_alpine.py @@ -17,7 +17,7 @@ import unittest from azurelinuxagent.common.osutil.alpine import AlpineOSUtil -from tests.tools import AgentTestCase +from tests.lib.tools import AgentTestCase from .test_default import osutil_get_dhcp_pid_should_return_a_list_of_pids diff --git a/tests/common/osutil/test_arch.py b/tests/common/osutil/test_arch.py index 1133eae270..67ada5e547 100644 --- a/tests/common/osutil/test_arch.py +++ b/tests/common/osutil/test_arch.py @@ -17,7 +17,7 @@ import unittest from azurelinuxagent.common.osutil.arch import ArchUtil -from tests.tools import AgentTestCase +from tests.lib.tools import AgentTestCase from .test_default import osutil_get_dhcp_pid_should_return_a_list_of_pids diff --git a/tests/common/osutil/test_bigip.py b/tests/common/osutil/test_bigip.py index 421d4d920a..7312f3ae57 100644 --- a/tests/common/osutil/test_bigip.py +++ b/tests/common/osutil/test_bigip.py @@ -26,7 +26,7 @@ import azurelinuxagent.common.utils.shellutil as shellutil from azurelinuxagent.common.exception import OSUtilError from azurelinuxagent.common.osutil.bigip import BigIpOSUtil -from tests.tools import AgentTestCase, patch +from tests.lib.tools import AgentTestCase, patch from .test_default import osutil_get_dhcp_pid_should_return_a_list_of_pids diff --git a/tests/common/osutil/test_clearlinux.py b/tests/common/osutil/test_clearlinux.py index a7d75722fa..4824c95511 100644 --- a/tests/common/osutil/test_clearlinux.py +++ b/tests/common/osutil/test_clearlinux.py @@ -17,7 +17,7 @@ import unittest from azurelinuxagent.common.osutil.clearlinux import ClearLinuxUtil -from tests.tools import AgentTestCase +from tests.lib.tools import AgentTestCase from .test_default import osutil_get_dhcp_pid_should_return_a_list_of_pids diff --git a/tests/common/osutil/test_coreos.py b/tests/common/osutil/test_coreos.py index dca06fa155..36e3987389 100644 --- a/tests/common/osutil/test_coreos.py +++ b/tests/common/osutil/test_coreos.py @@ -17,7 +17,7 @@ import unittest from azurelinuxagent.common.osutil.coreos import CoreOSUtil -from tests.tools import AgentTestCase +from tests.lib.tools import AgentTestCase from .test_default import osutil_get_dhcp_pid_should_return_a_list_of_pids diff --git a/tests/common/osutil/test_default.py b/tests/common/osutil/test_default.py index ab4fa5c999..a89bef35d6 100644 --- a/tests/common/osutil/test_default.py +++ b/tests/common/osutil/test_default.py @@ -35,7 +35,7 @@ from azurelinuxagent.common.utils.flexible_version import FlexibleVersion from azurelinuxagent.common.utils.networkutil import AddFirewallRules from tests.common.mock_environment import MockEnvironment -from tests.tools import AgentTestCase, patch, open_patch, load_data, data_dir, is_python_version_26_or_34, skip_if_predicate_true +from tests.lib.tools import AgentTestCase, patch, open_patch, load_data, data_dir, is_python_version_26_or_34, skip_if_predicate_true actual_get_proc_net_route = 'azurelinuxagent.common.osutil.default.DefaultOSUtil._get_proc_net_route' diff --git a/tests/common/osutil/test_default_osutil.py b/tests/common/osutil/test_default_osutil.py index 070f1d653c..1b94dd5ca8 100644 --- a/tests/common/osutil/test_default_osutil.py +++ b/tests/common/osutil/test_default_osutil.py @@ -16,7 +16,7 @@ # from azurelinuxagent.common.osutil.default import DefaultOSUtil, shellutil # pylint: disable=unused-import -from tests.tools import AgentTestCase, patch # pylint: disable=unused-import +from tests.lib.tools import AgentTestCase, patch # pylint: disable=unused-import class DefaultOsUtilTestCase(AgentTestCase): diff --git a/tests/common/osutil/test_factory.py b/tests/common/osutil/test_factory.py index 7bd729c3b3..fbe5e24d3d 100644 --- a/tests/common/osutil/test_factory.py +++ b/tests/common/osutil/test_factory.py @@ -34,7 +34,7 @@ from azurelinuxagent.common.osutil.suse import SUSEOSUtil, SUSE11OSUtil from azurelinuxagent.common.osutil.ubuntu import UbuntuOSUtil, Ubuntu12OSUtil, Ubuntu14OSUtil, \ UbuntuSnappyOSUtil, Ubuntu16OSUtil, Ubuntu18OSUtil -from tests.tools import AgentTestCase, patch +from tests.lib.tools import AgentTestCase, patch class TestOsUtilFactory(AgentTestCase): diff --git a/tests/common/osutil/test_freebsd.py b/tests/common/osutil/test_freebsd.py index 3851836013..0236b47190 100644 --- a/tests/common/osutil/test_freebsd.py +++ b/tests/common/osutil/test_freebsd.py @@ -20,7 +20,7 @@ import azurelinuxagent.common.utils.shellutil as shellutil from azurelinuxagent.common.osutil.freebsd import FreeBSDOSUtil from azurelinuxagent.common.utils import textutil -from tests.tools import AgentTestCase, patch +from tests.lib.tools import AgentTestCase, patch from .test_default import osutil_get_dhcp_pid_should_return_a_list_of_pids diff --git a/tests/common/osutil/test_nsbsd.py b/tests/common/osutil/test_nsbsd.py index 4e97f7444b..37d79e61ad 100644 --- a/tests/common/osutil/test_nsbsd.py +++ b/tests/common/osutil/test_nsbsd.py @@ -19,7 +19,7 @@ from azurelinuxagent.common.osutil.nsbsd import NSBSDOSUtil from azurelinuxagent.common.utils.fileutil import read_file -from tests.tools import AgentTestCase, patch +from tests.lib.tools import AgentTestCase, patch class TestNSBSDOSUtil(AgentTestCase): diff --git a/tests/common/osutil/test_openbsd.py b/tests/common/osutil/test_openbsd.py index e82a1d8e49..666e4efabe 100644 --- a/tests/common/osutil/test_openbsd.py +++ b/tests/common/osutil/test_openbsd.py @@ -17,7 +17,7 @@ import unittest from azurelinuxagent.common.osutil.openbsd import OpenBSDOSUtil -from tests.tools import AgentTestCase +from tests.lib.tools import AgentTestCase from .test_default import osutil_get_dhcp_pid_should_return_a_list_of_pids diff --git a/tests/common/osutil/test_openwrt.py b/tests/common/osutil/test_openwrt.py index 05620ff4d0..e204cae1fb 100644 --- a/tests/common/osutil/test_openwrt.py +++ b/tests/common/osutil/test_openwrt.py @@ -17,7 +17,7 @@ import unittest from azurelinuxagent.common.osutil.openwrt import OpenWRTOSUtil -from tests.tools import AgentTestCase +from tests.lib.tools import AgentTestCase from .test_default import osutil_get_dhcp_pid_should_return_a_list_of_pids diff --git a/tests/common/osutil/test_photonos.py b/tests/common/osutil/test_photonos.py index f63e7c2f9c..506025e2eb 100644 --- a/tests/common/osutil/test_photonos.py +++ b/tests/common/osutil/test_photonos.py @@ -18,7 +18,7 @@ import unittest from azurelinuxagent.common.osutil.photonos import PhotonOSUtil -from tests.tools import AgentTestCase +from tests.lib.tools import AgentTestCase from .test_default import osutil_get_dhcp_pid_should_return_a_list_of_pids diff --git a/tests/common/osutil/test_redhat.py b/tests/common/osutil/test_redhat.py index dfd5e07a85..3c4787fc15 100644 --- a/tests/common/osutil/test_redhat.py +++ b/tests/common/osutil/test_redhat.py @@ -17,7 +17,7 @@ import unittest from azurelinuxagent.common.osutil.redhat import Redhat6xOSUtil -from tests.tools import AgentTestCase +from tests.lib.tools import AgentTestCase from .test_default import osutil_get_dhcp_pid_should_return_a_list_of_pids diff --git a/tests/common/osutil/test_suse.py b/tests/common/osutil/test_suse.py index 8fd6141bea..1e752ca72e 100644 --- a/tests/common/osutil/test_suse.py +++ b/tests/common/osutil/test_suse.py @@ -17,7 +17,7 @@ import unittest from azurelinuxagent.common.osutil.suse import SUSE11OSUtil -from tests.tools import AgentTestCase +from tests.lib.tools import AgentTestCase from .test_default import osutil_get_dhcp_pid_should_return_a_list_of_pids diff --git a/tests/common/osutil/test_ubuntu.py b/tests/common/osutil/test_ubuntu.py index f386fb7c76..24ce7b7f6b 100644 --- a/tests/common/osutil/test_ubuntu.py +++ b/tests/common/osutil/test_ubuntu.py @@ -18,7 +18,7 @@ import unittest from azurelinuxagent.common.osutil.ubuntu import Ubuntu12OSUtil, Ubuntu18OSUtil -from tests.tools import AgentTestCase +from tests.lib.tools import AgentTestCase from .test_default import osutil_get_dhcp_pid_should_return_a_list_of_pids diff --git a/tests/distro/__init__.py b/tests/common/protocol/__init__.py similarity index 100% rename from tests/distro/__init__.py rename to tests/common/protocol/__init__.py diff --git a/tests/protocol/test_datacontract.py b/tests/common/protocol/test_datacontract.py similarity index 100% rename from tests/protocol/test_datacontract.py rename to tests/common/protocol/test_datacontract.py diff --git a/tests/protocol/test_extensions_goal_state_from_extensions_config.py b/tests/common/protocol/test_extensions_goal_state_from_extensions_config.py similarity index 85% rename from tests/protocol/test_extensions_goal_state_from_extensions_config.py rename to tests/common/protocol/test_extensions_goal_state_from_extensions_config.py index 5af0aa288f..61380a46f7 100644 --- a/tests/protocol/test_extensions_goal_state_from_extensions_config.py +++ b/tests/common/protocol/test_extensions_goal_state_from_extensions_config.py @@ -2,20 +2,20 @@ # Licensed under the Apache License. from azurelinuxagent.common.AgentGlobals import AgentGlobals from azurelinuxagent.common.protocol.extensions_goal_state import GoalStateChannel -from tests.protocol.mocks import mockwiredata, mock_wire_protocol -from tests.tools import AgentTestCase +from tests.lib.mock_wire_protocol import wire_protocol_data, mock_wire_protocol +from tests.lib.tools import AgentTestCase class ExtensionsGoalStateFromExtensionsConfigTestCase(AgentTestCase): def test_it_should_parse_in_vm_metadata(self): - with mock_wire_protocol(mockwiredata.DATA_FILE_IN_VM_META_DATA) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE_IN_VM_META_DATA) as protocol: extensions_goal_state = protocol.get_goal_state().extensions_goal_state self.assertEqual("555e551c-600e-4fb4-90ba-8ab8ec28eccc", extensions_goal_state.activity_id, "Incorrect activity Id") self.assertEqual("400de90b-522e-491f-9d89-ec944661f531", extensions_goal_state.correlation_id, "Incorrect correlation Id") self.assertEqual('2020-11-09T17:48:50.412125Z', extensions_goal_state.created_on_timestamp, "Incorrect GS Creation time") def test_it_should_use_default_values_when_in_vm_metadata_is_missing(self): - data_file = mockwiredata.DATA_FILE.copy() + data_file = wire_protocol_data.DATA_FILE.copy() data_file["ext_conf"] = "wire/ext_conf-no_gs_metadata.xml" with mock_wire_protocol(data_file) as protocol: extensions_goal_state = protocol.get_goal_state().extensions_goal_state @@ -24,14 +24,14 @@ def test_it_should_use_default_values_when_in_vm_metadata_is_missing(self): self.assertEqual('1900-01-01T00:00:00.000000Z', extensions_goal_state.created_on_timestamp, "Incorrect GS Creation time") def test_it_should_use_default_values_when_in_vm_metadata_is_invalid(self): - with mock_wire_protocol(mockwiredata.DATA_FILE_INVALID_VM_META_DATA) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE_INVALID_VM_META_DATA) as protocol: extensions_goal_state = protocol.get_goal_state().extensions_goal_state self.assertEqual(AgentGlobals.GUID_ZERO, extensions_goal_state.activity_id, "Incorrect activity Id") self.assertEqual(AgentGlobals.GUID_ZERO, extensions_goal_state.correlation_id, "Incorrect correlation Id") self.assertEqual('1900-01-01T00:00:00.000000Z', extensions_goal_state.created_on_timestamp, "Incorrect GS Creation time") def test_it_should_parse_missing_status_upload_blob_as_none(self): - data_file = mockwiredata.DATA_FILE.copy() + data_file = wire_protocol_data.DATA_FILE.copy() data_file["ext_conf"] = "hostgaplugin/ext_conf-no_status_upload_blob.xml" with mock_wire_protocol(data_file) as protocol: extensions_goal_state = protocol.get_goal_state().extensions_goal_state @@ -40,14 +40,14 @@ def test_it_should_parse_missing_status_upload_blob_as_none(self): self.assertEqual("BlockBlob", extensions_goal_state.status_upload_blob_type, "Expected status upload blob to be Block") def test_it_should_default_to_block_blob_when_the_status_blob_type_is_not_valid(self): - data_file = mockwiredata.DATA_FILE.copy() + data_file = wire_protocol_data.DATA_FILE.copy() data_file["ext_conf"] = "hostgaplugin/ext_conf-invalid_blob_type.xml" with mock_wire_protocol(data_file) as protocol: extensions_goal_state = protocol.get_goal_state().extensions_goal_state self.assertEqual("BlockBlob", extensions_goal_state.status_upload_blob_type, 'Expected BlockBlob for an invalid statusBlobType') def test_it_should_parse_empty_depends_on_as_dependency_level_0(self): - data_file = mockwiredata.DATA_FILE_VM_SETTINGS.copy() + data_file = wire_protocol_data.DATA_FILE_VM_SETTINGS.copy() data_file["vm_settings"] = "hostgaplugin/vm_settings-empty_depends_on.json" data_file["ext_conf"] = "hostgaplugin/ext_conf-empty_depends_on.xml" with mock_wire_protocol(data_file) as protocol: @@ -56,7 +56,7 @@ def test_it_should_parse_empty_depends_on_as_dependency_level_0(self): self.assertEqual(0, extensions[0].settings[0].dependencyLevel, "Incorrect dependencyLevel") def test_its_source_channel_should_be_wire_server(self): - with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE) as protocol: extensions_goal_state = protocol.get_goal_state().extensions_goal_state self.assertEqual(GoalStateChannel.WireServer, extensions_goal_state.channel, "The channel is incorrect") diff --git a/tests/protocol/test_extensions_goal_state_from_vm_settings.py b/tests/common/protocol/test_extensions_goal_state_from_vm_settings.py similarity index 93% rename from tests/protocol/test_extensions_goal_state_from_vm_settings.py rename to tests/common/protocol/test_extensions_goal_state_from_vm_settings.py index 1100b05bf9..bea1063f70 100644 --- a/tests/protocol/test_extensions_goal_state_from_vm_settings.py +++ b/tests/common/protocol/test_extensions_goal_state_from_vm_settings.py @@ -5,13 +5,13 @@ from azurelinuxagent.common.protocol.goal_state import GoalState from azurelinuxagent.common.protocol.extensions_goal_state import GoalStateChannel from azurelinuxagent.common.protocol.extensions_goal_state_from_vm_settings import _CaseFoldedDict -from tests.protocol.mocks import mockwiredata, mock_wire_protocol -from tests.tools import AgentTestCase +from tests.lib.mock_wire_protocol import wire_protocol_data, mock_wire_protocol +from tests.lib.tools import AgentTestCase class ExtensionsGoalStateFromVmSettingsTestCase(AgentTestCase): def test_it_should_parse_vm_settings(self): - with mock_wire_protocol(mockwiredata.DATA_FILE_VM_SETTINGS) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE_VM_SETTINGS) as protocol: extensions_goal_state = protocol.get_goal_state().extensions_goal_state def assert_property(name, value): @@ -49,13 +49,13 @@ def assert_property(name, value): self.assertEqual(1, extensions_goal_state.extensions[3].settings[1].dependencyLevel, "Incorrect dependency level (multi-config)") def test_it_should_parse_requested_version_properly(self): - with mock_wire_protocol(mockwiredata.DATA_FILE_VM_SETTINGS) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE_VM_SETTINGS) as protocol: goal_state = GoalState(protocol.client) families = goal_state.extensions_goal_state.agent_families for family in families: self.assertEqual(family.requested_version_string, "0.0.0.0", "Version should be None") - data_file = mockwiredata.DATA_FILE_VM_SETTINGS.copy() + data_file = wire_protocol_data.DATA_FILE_VM_SETTINGS.copy() data_file["vm_settings"] = "hostgaplugin/vm_settings-requested_version.json" with mock_wire_protocol(data_file) as protocol: protocol.mock_wire_data.set_etag(888) @@ -65,7 +65,7 @@ def test_it_should_parse_requested_version_properly(self): self.assertEqual(family.requested_version_string, "9.9.9.9", "Version should be 9.9.9.9") def test_it_should_parse_missing_status_upload_blob_as_none(self): - data_file = mockwiredata.DATA_FILE_VM_SETTINGS.copy() + data_file = wire_protocol_data.DATA_FILE_VM_SETTINGS.copy() data_file["vm_settings"] = "hostgaplugin/vm_settings-no_status_upload_blob.json" with mock_wire_protocol(data_file) as protocol: extensions_goal_state = protocol.get_goal_state().extensions_goal_state @@ -74,7 +74,7 @@ def test_it_should_parse_missing_status_upload_blob_as_none(self): self.assertEqual("BlockBlob", extensions_goal_state.status_upload_blob_type, "Expected status upload blob to be Block") def test_it_should_parse_missing_agent_manifests_as_empty(self): - data_file = mockwiredata.DATA_FILE_VM_SETTINGS.copy() + data_file = wire_protocol_data.DATA_FILE_VM_SETTINGS.copy() data_file["vm_settings"] = "hostgaplugin/vm_settings-no_manifests.json" with mock_wire_protocol(data_file) as protocol: extensions_goal_state = protocol.get_goal_state().extensions_goal_state @@ -82,7 +82,7 @@ def test_it_should_parse_missing_agent_manifests_as_empty(self): self.assertListEqual([], extensions_goal_state.agent_families[0].uris, "Expected an empty list of agent manifests") def test_it_should_parse_missing_extension_manifests_as_empty(self): - data_file = mockwiredata.DATA_FILE_VM_SETTINGS.copy() + data_file = wire_protocol_data.DATA_FILE_VM_SETTINGS.copy() data_file["vm_settings"] = "hostgaplugin/vm_settings-no_manifests.json" with mock_wire_protocol(data_file) as protocol: extensions_goal_state = protocol.get_goal_state().extensions_goal_state @@ -98,7 +98,7 @@ def test_it_should_parse_missing_extension_manifests_as_empty(self): extensions_goal_state.extensions[2].manifest_uris, "Incorrect list of manifests for {0}".format(extensions_goal_state.extensions[2])) def test_it_should_default_to_block_blob_when_the_status_blob_type_is_not_valid(self): - data_file = mockwiredata.DATA_FILE_VM_SETTINGS.copy() + data_file = wire_protocol_data.DATA_FILE_VM_SETTINGS.copy() data_file["vm_settings"] = "hostgaplugin/vm_settings-invalid_blob_type.json" with mock_wire_protocol(data_file) as protocol: extensions_goal_state = protocol.get_goal_state().extensions_goal_state @@ -106,7 +106,7 @@ def test_it_should_default_to_block_blob_when_the_status_blob_type_is_not_valid( self.assertEqual("BlockBlob", extensions_goal_state.status_upload_blob_type, 'Expected BlockBlob for an invalid statusBlobType') def test_its_source_channel_should_be_host_ga_plugin(self): - with mock_wire_protocol(mockwiredata.DATA_FILE_VM_SETTINGS) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE_VM_SETTINGS) as protocol: extensions_goal_state = protocol.get_goal_state().extensions_goal_state self.assertEqual(GoalStateChannel.HostGAPlugin, extensions_goal_state.channel, "The channel is incorrect") diff --git a/tests/protocol/test_goal_state.py b/tests/common/protocol/test_goal_state.py similarity index 93% rename from tests/protocol/test_goal_state.py rename to tests/common/protocol/test_goal_state.py index 61653b2af6..3f00a62d8b 100644 --- a/tests/protocol/test_goal_state.py +++ b/tests/common/protocol/test_goal_state.py @@ -19,15 +19,15 @@ from azurelinuxagent.common.exception import ProtocolError from azurelinuxagent.common.utils import fileutil from azurelinuxagent.common.utils.archive import ARCHIVE_DIRECTORY_NAME -from tests.protocol.mocks import mock_wire_protocol, MockHttpResponse -from tests.protocol import mockwiredata -from tests.protocol.HttpRequestPredicates import HttpRequestPredicates -from tests.tools import AgentTestCase, patch, load_data +from tests.lib.mock_wire_protocol import mock_wire_protocol, MockHttpResponse +from tests.lib import wire_protocol_data +from tests.lib.http_request_predicates import HttpRequestPredicates +from tests.lib.tools import AgentTestCase, patch, load_data class GoalStateTestCase(AgentTestCase, HttpRequestPredicates): def test_it_should_use_vm_settings_by_default(self): - with mock_wire_protocol(mockwiredata.DATA_FILE_VM_SETTINGS) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE_VM_SETTINGS) as protocol: protocol.mock_wire_data.set_etag(888) extensions_goal_state = GoalState(protocol.client).extensions_goal_state self.assertTrue( @@ -41,7 +41,7 @@ def _assert_is_extensions_goal_state_from_extensions_config(self, extensions_goa def test_it_should_use_extensions_config_when_fast_track_is_disabled(self): with patch("azurelinuxagent.common.conf.get_enable_fast_track", return_value=False): - with mock_wire_protocol(mockwiredata.DATA_FILE_VM_SETTINGS) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE_VM_SETTINGS) as protocol: self._assert_is_extensions_goal_state_from_extensions_config(GoalState(protocol.client).extensions_goal_state) def test_it_should_use_extensions_config_when_fast_track_is_not_supported(self): @@ -50,11 +50,11 @@ def http_get_handler(url, *_, **__): return MockHttpResponse(httpclient.NOT_FOUND) return None - with mock_wire_protocol(mockwiredata.DATA_FILE_VM_SETTINGS, http_get_handler=http_get_handler) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE_VM_SETTINGS, http_get_handler=http_get_handler) as protocol: self._assert_is_extensions_goal_state_from_extensions_config(GoalState(protocol.client).extensions_goal_state) def test_it_should_use_extensions_config_when_the_host_ga_plugin_version_is_not_supported(self): - data_file = mockwiredata.DATA_FILE_VM_SETTINGS.copy() + data_file = wire_protocol_data.DATA_FILE_VM_SETTINGS.copy() data_file["vm_settings"] = "hostgaplugin/vm_settings-unsupported_version.json" with mock_wire_protocol(data_file) as protocol: @@ -63,7 +63,7 @@ def test_it_should_use_extensions_config_when_the_host_ga_plugin_version_is_not_ def test_it_should_retry_get_vm_settings_on_resource_gone_error(self): # Requests to the hostgaplugin incude the Container ID and the RoleConfigName as headers; when the hostgaplugin returns GONE (HTTP status 410) the agent # needs to get a new goal state and retry the request with updated values for the Container ID and RoleConfigName headers. - with mock_wire_protocol(mockwiredata.DATA_FILE_VM_SETTINGS) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE_VM_SETTINGS) as protocol: # Do not mock the vmSettings request at the level of azurelinuxagent.common.utils.restutil.http_request. The GONE status is handled # in the internal _http_request, which we mock below. protocol.do_not_mock = lambda method, url: method == "GET" and self.is_host_plugin_vm_settings_request(url) @@ -89,8 +89,8 @@ def http_get_vm_settings(_method, _host, _relative_url, _timeout, **kwargs): self.assertEqual("GET_VM_SETTINGS_TEST_ROLE_CONFIG_NAME", request_headers[1][hostplugin._HEADER_HOST_CONFIG_NAME], "The retry request did not include the expected header for the RoleConfigName") def test_fetch_goal_state_should_raise_on_incomplete_goal_state(self): - with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: - protocol.mock_wire_data.data_files = mockwiredata.DATA_FILE_NOOP_GS + with mock_wire_protocol(wire_protocol_data.DATA_FILE) as protocol: + protocol.mock_wire_data.data_files = wire_protocol_data.DATA_FILE_NOOP_GS protocol.mock_wire_data.reload() protocol.mock_wire_data.set_incarnation(2) @@ -101,14 +101,14 @@ def test_fetch_goal_state_should_raise_on_incomplete_goal_state(self): def test_fetching_the_goal_state_should_save_the_shared_config(self): # SharedConfig.xml is used by other components (Azsec and Singularity/HPC Infiniband); verify that we do not delete it - with mock_wire_protocol(mockwiredata.DATA_FILE_VM_SETTINGS) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE_VM_SETTINGS) as protocol: _ = GoalState(protocol.client) shared_config = os.path.join(conf.get_lib_dir(), 'SharedConfig.xml') self.assertTrue(os.path.exists(shared_config), "{0} should have been created".format(shared_config)) def test_fetching_the_goal_state_should_save_the_goal_state_to_the_history_directory(self): - with mock_wire_protocol(mockwiredata.DATA_FILE_VM_SETTINGS) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE_VM_SETTINGS) as protocol: protocol.mock_wire_data.set_incarnation(999) protocol.mock_wire_data.set_etag(888) @@ -132,7 +132,7 @@ def _assert_directory_contents(self, directory, expected_files): self.assertEqual(expected_files, actual_files, "The expected files were not saved to {0}".format(directory)) def test_update_should_create_new_history_subdirectories(self): - with mock_wire_protocol(mockwiredata.DATA_FILE_VM_SETTINGS) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE_VM_SETTINGS) as protocol: protocol.mock_wire_data.set_incarnation(123) protocol.mock_wire_data.set_etag(654) @@ -160,7 +160,7 @@ def http_get_handler(url, *_, **__): self._find_history_subdirectory("234-987"), ["VmSettings.json", "Certificates.json"]) def test_it_should_redact_the_protected_settings_when_saving_to_the_history_directory(self): - with mock_wire_protocol(mockwiredata.DATA_FILE_VM_SETTINGS) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE_VM_SETTINGS) as protocol: protocol.mock_wire_data.set_incarnation(888) protocol.mock_wire_data.set_etag(888) @@ -195,11 +195,11 @@ def test_it_should_redact_the_protected_settings_when_saving_to_the_history_dire "Could not find the expected number of redacted settings in {0}.\nExpected {1}.\n{2}".format(file_name, len(protected_settings), file_contents)) def test_it_should_save_vm_settings_on_parse_errors(self): - with mock_wire_protocol(mockwiredata.DATA_FILE_VM_SETTINGS) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE_VM_SETTINGS) as protocol: invalid_vm_settings_file = "hostgaplugin/vm_settings-parse_error.json" - data_file = mockwiredata.DATA_FILE_VM_SETTINGS.copy() + data_file = wire_protocol_data.DATA_FILE_VM_SETTINGS.copy() data_file["vm_settings"] = invalid_vm_settings_file - protocol.mock_wire_data = mockwiredata.WireProtocolData(data_file) + protocol.mock_wire_data = wire_protocol_data.WireProtocolData(data_file) with self.assertRaises(ProtocolError): # the parsing error will cause an exception _ = GoalState(protocol.client) @@ -228,7 +228,7 @@ def _create_protocol_ws_and_hgap_in_sync(): Creates a mock protocol in which the HostGAPlugin and the WireServer are in sync, both of them returning the same Fabric goal state. """ - data_file = mockwiredata.DATA_FILE_VM_SETTINGS.copy() + data_file = wire_protocol_data.DATA_FILE_VM_SETTINGS.copy() with mock_wire_protocol(data_file) as protocol: timestamp = datetime.datetime.utcnow() @@ -372,7 +372,7 @@ def http_get_handler(url, *_, **__): self.assertTrue(goal_state.extensions_goal_state.is_outdated, "The updated goal state should be marked as outdated") def test_it_should_raise_when_the_tenant_certificate_is_missing(self): - data_file = mockwiredata.DATA_FILE_VM_SETTINGS.copy() + data_file = wire_protocol_data.DATA_FILE_VM_SETTINGS.copy() with mock_wire_protocol(data_file) as protocol: data_file["vm_settings"] = "hostgaplugin/vm_settings-missing_cert.json" @@ -386,7 +386,7 @@ def test_it_should_raise_when_the_tenant_certificate_is_missing(self): self.assertIn(expected_message, str(context.exception)) def test_it_should_download_certs_on_a_new_fast_track_goal_state(self): - data_file = mockwiredata.DATA_FILE_VM_SETTINGS.copy() + data_file = wire_protocol_data.DATA_FILE_VM_SETTINGS.copy() with mock_wire_protocol(data_file) as protocol: goal_state = GoalState(protocol.client) @@ -410,7 +410,7 @@ def test_it_should_download_certs_on_a_new_fast_track_goal_state(self): self.assertTrue(os.path.isfile(crt_path)) def test_it_should_download_certs_on_a_new_fabric_goal_state(self): - data_file = mockwiredata.DATA_FILE_VM_SETTINGS.copy() + data_file = wire_protocol_data.DATA_FILE_VM_SETTINGS.copy() with mock_wire_protocol(data_file) as protocol: protocol.mock_wire_data.set_vm_settings_source(GoalStateSource.Fabric) @@ -457,7 +457,7 @@ def http_get_handler(url, *_, **__): return None http_get_handler.certificate_requests = 0 - with mock_wire_protocol(mockwiredata.DATA_FILE_VM_SETTINGS) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE_VM_SETTINGS) as protocol: protocol.set_http_handlers(http_get_handler=http_get_handler) protocol.mock_wire_data.reset_call_counts() diff --git a/tests/protocol/test_healthservice.py b/tests/common/protocol/test_healthservice.py similarity index 99% rename from tests/protocol/test_healthservice.py rename to tests/common/protocol/test_healthservice.py index cb523a78f8..d9ba17755d 100644 --- a/tests/protocol/test_healthservice.py +++ b/tests/common/protocol/test_healthservice.py @@ -18,8 +18,8 @@ from azurelinuxagent.common.exception import HttpError from azurelinuxagent.common.protocol.healthservice import Observation, HealthService from azurelinuxagent.common.utils import restutil -from tests.protocol.test_hostplugin import MockResponse -from tests.tools import AgentTestCase, patch +from tests.common.protocol.test_hostplugin import MockResponse +from tests.lib.tools import AgentTestCase, patch class TestHealthService(AgentTestCase): diff --git a/tests/protocol/test_hostplugin.py b/tests/common/protocol/test_hostplugin.py similarity index 98% rename from tests/protocol/test_hostplugin.py rename to tests/common/protocol/test_hostplugin.py index 47e6871bea..4c97c73fd9 100644 --- a/tests/protocol/test_hostplugin.py +++ b/tests/common/protocol/test_hostplugin.py @@ -34,10 +34,10 @@ from azurelinuxagent.common.protocol.goal_state import GoalState from azurelinuxagent.common.utils import restutil from azurelinuxagent.common.version import AGENT_VERSION, AGENT_NAME -from tests.protocol.mocks import mock_wire_protocol, mockwiredata, MockHttpResponse -from tests.protocol.HttpRequestPredicates import HttpRequestPredicates -from tests.protocol.mockwiredata import DATA_FILE, DATA_FILE_NO_EXT -from tests.tools import AgentTestCase, PY_VERSION_MAJOR, Mock, patch +from tests.lib.mock_wire_protocol import mock_wire_protocol, wire_protocol_data, MockHttpResponse +from tests.lib.http_request_predicates import HttpRequestPredicates +from tests.lib.wire_protocol_data import DATA_FILE, DATA_FILE_NO_EXT +from tests.lib.tools import AgentTestCase, PY_VERSION_MAJOR, Mock, patch hostplugin_status_url = "http://168.63.129.16:32526/status" @@ -852,7 +852,7 @@ def http_get_handler(url, *_, **__): return MockHttpResponse(httpclient.INTERNAL_SERVER_ERROR, body="TEST ERROR") return None - with mock_wire_protocol(mockwiredata.DATA_FILE_VM_SETTINGS) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE_VM_SETTINGS) as protocol: protocol.set_http_handlers(http_get_handler=http_get_handler) with self.assertRaisesRegexCM(ProtocolError, r'GET vmSettings \[correlation ID: .* eTag: .*\]: \[HTTP Failed\] \[500: None].*TEST ERROR.*'): protocol.client.get_host_plugin().fetch_vm_settings() @@ -875,7 +875,7 @@ def http_get_handler(url, *_, **__): return mock_response return None - with mock_wire_protocol(mockwiredata.DATA_FILE_VM_SETTINGS, http_get_handler=http_get_handler) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE_VM_SETTINGS, http_get_handler=http_get_handler) as protocol: mock_response = MockHttpResponse(httpclient.INTERNAL_SERVER_ERROR) self._fetch_vm_settings_ignoring_errors(protocol) @@ -913,7 +913,7 @@ def http_get_handler(url, *_, **__): self.assertEqual(expected, summary, "The count of errors is incorrect") def test_it_should_limit_the_number_of_errors_it_reports(self): - with mock_wire_protocol(mockwiredata.DATA_FILE_VM_SETTINGS) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE_VM_SETTINGS) as protocol: def http_get_handler(url, *_, **__): if self.is_host_plugin_vm_settings_request(url): return MockHttpResponse(httpclient.BAD_GATEWAY) # HostGAPlugin returns 502 for internal errors @@ -941,7 +941,7 @@ def get_telemetry_messages(): self.assertEqual(1, len(telemetry_messages), "Expected additional errors to be reported to telemetry in the next period (got: {0})".format(telemetry_messages)) def test_it_should_stop_issuing_vm_settings_requests_when_api_is_not_supported(self): - with mock_wire_protocol(mockwiredata.DATA_FILE_VM_SETTINGS) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE_VM_SETTINGS) as protocol: def http_get_handler(url, *_, **__): if self.is_host_plugin_vm_settings_request(url): return MockHttpResponse(httpclient.NOT_FOUND) # HostGAPlugin returns 404 if the API is not supported @@ -969,7 +969,7 @@ def http_get_handler(url, *_, **__): return MockHttpResponse(httpclient.NOT_FOUND) # HostGAPlugin returns 404 if the API is not supported return None - with mock_wire_protocol(mockwiredata.DATA_FILE_VM_SETTINGS) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE_VM_SETTINGS) as protocol: host_ga_plugin = protocol.client.get_host_plugin() # Do an initial call to ensure the API is supported @@ -984,7 +984,7 @@ def http_get_handler(url, *_, **__): self.assertEqual(vm_settings.created_on_timestamp, cm.exception.timestamp) def test_it_should_save_the_timestamp_of_the_most_recent_fast_track_goal_state(self): - with mock_wire_protocol(mockwiredata.DATA_FILE_VM_SETTINGS) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE_VM_SETTINGS) as protocol: host_ga_plugin = protocol.client.get_host_plugin() vm_settings, _ = host_ga_plugin.fetch_vm_settings() diff --git a/tests/protocol/test_image_info_matcher.py b/tests/common/protocol/test_image_info_matcher.py similarity index 100% rename from tests/protocol/test_image_info_matcher.py rename to tests/common/protocol/test_image_info_matcher.py diff --git a/tests/protocol/test_imds.py b/tests/common/protocol/test_imds.py similarity index 99% rename from tests/protocol/test_imds.py rename to tests/common/protocol/test_imds.py index 1f8e428c1f..efc705ffab 100644 --- a/tests/protocol/test_imds.py +++ b/tests/common/protocol/test_imds.py @@ -26,8 +26,8 @@ from azurelinuxagent.common.exception import HttpError, ResourceGoneError from azurelinuxagent.common.future import ustr, httpclient from azurelinuxagent.common.utils import restutil -from tests.protocol.mocks import MockHttpResponse -from tests.tools import AgentTestCase, data_dir, MagicMock, Mock, patch +from tests.lib.mock_wire_protocol import MockHttpResponse +from tests.lib.tools import AgentTestCase, data_dir, MagicMock, Mock, patch def get_mock_compute_response(): diff --git a/tests/protocol/test_metadata_server_migration_util.py b/tests/common/protocol/test_metadata_server_migration_util.py similarity index 99% rename from tests/protocol/test_metadata_server_migration_util.py rename to tests/common/protocol/test_metadata_server_migration_util.py index 5950b43f17..70ef053335 100644 --- a/tests/protocol/test_metadata_server_migration_util.py +++ b/tests/common/protocol/test_metadata_server_migration_util.py @@ -27,7 +27,7 @@ _LEGACY_METADATA_SERVER_P7B_FILE_NAME, \ _KNOWN_METADATASERVER_IP from azurelinuxagent.common.utils.restutil import KNOWN_WIRESERVER_IP -from tests.tools import AgentTestCase, patch, MagicMock +from tests.lib.tools import AgentTestCase, patch, MagicMock class TestMetadataServerMigrationUtil(AgentTestCase): @patch('azurelinuxagent.common.conf.get_lib_dir') diff --git a/tests/protocol/test_protocol_util.py b/tests/common/protocol/test_protocol_util.py similarity index 99% rename from tests/protocol/test_protocol_util.py rename to tests/common/protocol/test_protocol_util.py index 3529e95d42..06683cef25 100644 --- a/tests/protocol/test_protocol_util.py +++ b/tests/common/protocol/test_protocol_util.py @@ -30,7 +30,7 @@ from azurelinuxagent.common.protocol.util import get_protocol_util, ProtocolUtil, PROTOCOL_FILE_NAME, \ WIRE_PROTOCOL_NAME, ENDPOINT_FILE_NAME from azurelinuxagent.common.utils.restutil import KNOWN_WIRESERVER_IP -from tests.tools import AgentTestCase, MagicMock, Mock, patch, clear_singleton_instances +from tests.lib.tools import AgentTestCase, MagicMock, Mock, patch, clear_singleton_instances @patch("time.sleep") diff --git a/tests/protocol/test_wire.py b/tests/common/protocol/test_wire.py similarity index 94% rename from tests/protocol/test_wire.py rename to tests/common/protocol/test_wire.py index cc73b0fb45..9ce8339e94 100644 --- a/tests/protocol/test_wire.py +++ b/tests/common/protocol/test_wire.py @@ -40,12 +40,12 @@ from azurelinuxagent.common.version import CURRENT_VERSION, DISTRO_NAME, DISTRO_VERSION from azurelinuxagent.ga.exthandlers import get_exthandlers_handler from tests.ga.test_monitor import random_generator -from tests.protocol import mockwiredata -from tests.protocol.mocks import mock_wire_protocol, MockHttpResponse -from tests.protocol.HttpRequestPredicates import HttpRequestPredicates -from tests.protocol.mockwiredata import DATA_FILE_NO_EXT, DATA_FILE -from tests.protocol.mockwiredata import WireProtocolData -from tests.tools import patch, AgentTestCase, load_bin_data +from tests.lib import wire_protocol_data +from tests.lib.mock_wire_protocol import mock_wire_protocol, MockHttpResponse +from tests.lib.http_request_predicates import HttpRequestPredicates +from tests.lib.wire_protocol_data import DATA_FILE_NO_EXT, DATA_FILE +from tests.lib.wire_protocol_data import WireProtocolData +from tests.lib.tools import patch, AgentTestCase, load_bin_data data_with_bom = b'\xef\xbb\xbfhehe' testurl = 'http://foo' @@ -120,37 +120,37 @@ def _yield_events(): def test_getters(self, *args): """Normal case""" - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE) self._test_getters(test_data, True, *args) def test_getters_no_ext(self, *args): """Provision with agent is not checked""" - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_NO_EXT) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE_NO_EXT) self._test_getters(test_data, True, *args) def test_getters_ext_no_settings(self, *args): """Extensions without any settings""" - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_EXT_NO_SETTINGS) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE_EXT_NO_SETTINGS) self._test_getters(test_data, True, *args) def test_getters_ext_no_public(self, *args): """Extensions without any public settings""" - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_EXT_NO_PUBLIC) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE_EXT_NO_PUBLIC) self._test_getters(test_data, True, *args) def test_getters_ext_no_cert_format(self, *args): """Certificate format not specified""" - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_NO_CERT_FORMAT) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE_NO_CERT_FORMAT) self._test_getters(test_data, True, *args) def test_getters_ext_cert_format_not_pfx(self, *args): """Certificate format is not Pkcs7BlobWithPfxContents specified""" - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_CERT_FORMAT_NOT_PFX) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE_CERT_FORMAT_NOT_PFX) self._test_getters(test_data, False, *args) @patch("azurelinuxagent.common.protocol.healthservice.HealthService.report_host_plugin_extension_artifact") def test_getters_with_stale_goal_state(self, patch_report, *args): - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE) test_data.emulate_stale_goal_state = True self._test_getters(test_data, True, *args) @@ -202,7 +202,7 @@ def test_call_storage_kwargs(self, *args): # pylint: disable=unused-argument self.assertTrue(c == (True if i != 3 else False)) def test_status_blob_parsing(self, *args): # pylint: disable=unused-argument - with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE) as protocol: extensions_goal_state = protocol.get_goal_state().extensions_goal_state self.assertIsInstance(extensions_goal_state, ExtensionsGoalStateFromExtensionsConfig) self.assertEqual(extensions_goal_state.status_upload_blob, @@ -212,7 +212,7 @@ def test_status_blob_parsing(self, *args): # pylint: disable=unused-argument self.assertEqual(protocol.get_goal_state().extensions_goal_state.status_upload_blob_type, u'BlockBlob') def test_get_host_ga_plugin(self, *args): # pylint: disable=unused-argument - with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE) as protocol: host_plugin = protocol.client.get_host_plugin() goal_state = protocol.client.get_goal_state() self.assertEqual(goal_state.container_id, host_plugin.container_id) @@ -223,7 +223,7 @@ def http_put_handler(url, *_, **__): # pylint: disable=inconsistent-return-stat if protocol.get_endpoint() in url and url.endswith('/status'): return MockHttpResponse(200) - with mock_wire_protocol(mockwiredata.DATA_FILE, http_put_handler=http_put_handler) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE, http_put_handler=http_put_handler) as protocol: HostPluginProtocol.is_default_channel = False protocol.client.status_blob.vm_status = VMStatus(message="Ready", status="Ready") @@ -254,14 +254,14 @@ def test_upload_status_blob_reports_prepare_error(self, *_): self.assertEqual(1, mock_prepare.call_count) def test_get_in_vm_artifacts_profile_blob_not_available(self, *_): - data_file = mockwiredata.DATA_FILE.copy() + data_file = wire_protocol_data.DATA_FILE.copy() data_file["ext_conf"] = "wire/ext_conf_in_vm_empty_artifacts_profile.xml" with mock_wire_protocol(data_file) as protocol: self.assertFalse(protocol.get_goal_state().extensions_goal_state.on_hold) def test_it_should_set_on_hold_to_false_when_the_in_vm_artifacts_profile_is_not_valid(self, *_): - with mock_wire_protocol(mockwiredata.DATA_FILE_IN_VM_ARTIFACTS_PROFILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE_IN_VM_ARTIFACTS_PROFILE) as protocol: extensions_on_hold = protocol.get_goal_state().extensions_goal_state.on_hold self.assertTrue(extensions_on_hold, "Extensions should be on hold in the test data") @@ -485,7 +485,7 @@ def test_get_ext_conf_without_extensions_should_retrieve_vmagent_manifests_info( # Basic test for extensions_goal_state when extensions are not present in the config. The test verifies that # extensions_goal_state fetches the correct data by comparing the returned data with the test data provided the # mock_wire_protocol. - with mock_wire_protocol(mockwiredata.DATA_FILE_NO_EXT) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE_NO_EXT) as protocol: extensions_goal_state = protocol.get_goal_state().extensions_goal_state ext_handlers_names = [ext_handler.name for ext_handler in extensions_goal_state.extensions] @@ -500,7 +500,7 @@ def test_get_ext_conf_without_extensions_should_retrieve_vmagent_manifests_info( def test_get_ext_conf_with_extensions_should_retrieve_ext_handlers_and_vmagent_manifests_info(self): # Basic test for extensions_goal_state when extensions are present in the config. The test verifies that extensions_goal_state # fetches the correct data by comparing the returned data with the test data provided the mock_wire_protocol. - with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE) as protocol: extensions_goal_state = protocol.get_goal_state().extensions_goal_state ext_handlers_names = [ext_handler.name for ext_handler in extensions_goal_state.extensions] @@ -527,7 +527,7 @@ def http_get_handler(url, *_, **__): return MockHttpResponse(200, body=load_bin_data("ga/fake_extension.zip")) return None - with mock_wire_protocol(mockwiredata.DATA_FILE, http_get_handler=http_get_handler) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE, http_get_handler=http_get_handler) as protocol: protocol.client.download_zip_package("extension package", [extension_url], target_file, target_directory, use_verify_header=False) self.assertTrue(os.path.exists(target_directory), "The extension package was not downloaded") @@ -545,7 +545,7 @@ def http_get_handler(url, *_, **__): self.fail('The host channel should not have been used') return None - with mock_wire_protocol(mockwiredata.DATA_FILE, http_get_handler=http_get_handler) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE, http_get_handler=http_get_handler) as protocol: HostPluginProtocol.is_default_channel = False protocol.client.download_zip_package("extension package", [extension_url], target_file, target_directory, use_verify_header=False) @@ -568,7 +568,7 @@ def http_get_handler(url, *_, **kwargs): return MockHttpResponse(200, body=load_bin_data("ga/fake_extension.zip")) return None - with mock_wire_protocol(mockwiredata.DATA_FILE, http_get_handler=http_get_handler) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE, http_get_handler=http_get_handler) as protocol: HostPluginProtocol.is_default_channel = False protocol.client.download_zip_package("extension package", [extension_url], target_file, target_directory, use_verify_header=False) @@ -599,7 +599,7 @@ def http_get_handler(url, *_, **kwargs): return None http_get_handler.goal_state_requests = 0 - with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE) as protocol: HostPluginProtocol.is_default_channel = False try: @@ -633,7 +633,7 @@ def http_get_handler(url, *_, **kwargs): protocol.track_url(url) # keep track of goal state requests return None - with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE) as protocol: HostPluginProtocol.is_default_channel = False # initialization of the host plugin triggers a request for the goal state; do it here before we start tracking those requests. @@ -661,7 +661,7 @@ def http_get_handler(url, *_, **kwargs): return MockHttpResponse(status=200, body=b"NOT A ZIP") return None - with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE) as protocol: protocol.set_http_handlers(http_get_handler=http_get_handler) with self.assertRaises(ExtensionDownloadError): @@ -681,7 +681,7 @@ def http_get_handler(url, *_, **__): self.fail('The Host GA Plugin should not have been invoked') return None - with mock_wire_protocol(mockwiredata.DATA_FILE, http_get_handler=http_get_handler) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE, http_get_handler=http_get_handler) as protocol: HostPluginProtocol.is_default_channel = False manifest = protocol.client.fetch_manifest("test", [manifest_url], use_verify_header=False) @@ -703,7 +703,7 @@ def http_get_handler(url, *_, **kwargs): return MockHttpResponse(200, body=manifest_xml.encode('utf-8')) return None - with mock_wire_protocol(mockwiredata.DATA_FILE, http_get_handler=http_get_handler) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE, http_get_handler=http_get_handler) as protocol: HostPluginProtocol.is_default_channel = False try: @@ -736,7 +736,7 @@ def http_get_handler(url, *_, **kwargs): return None http_get_handler.goal_state_requests = 0 - with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE) as protocol: HostPluginProtocol.is_default_channel = False try: @@ -768,7 +768,7 @@ def http_get_handler(url, *_, **kwargs): return None # Everything fails. Goal state should have been updated and host channel should not have been set as default. - with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE) as protocol: HostPluginProtocol.is_default_channel = False # initialization of the host plugin triggers a request for the goal state; do it here before we start @@ -796,7 +796,7 @@ def http_get_handler(url, *_, **__): protocol.track_url(url) return None - with mock_wire_protocol(mockwiredata.DATA_FILE_IN_VM_ARTIFACTS_PROFILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE_IN_VM_ARTIFACTS_PROFILE) as protocol: protocol.set_http_handlers(http_get_handler=http_get_handler) HostPluginProtocol.is_default_channel = False @@ -814,7 +814,7 @@ def http_get_handler(url, *_, **kwargs): protocol.track_url(url) return None - with mock_wire_protocol(mockwiredata.DATA_FILE_IN_VM_ARTIFACTS_PROFILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE_IN_VM_ARTIFACTS_PROFILE) as protocol: protocol.set_http_handlers(http_get_handler=http_get_handler) HostPluginProtocol.is_default_channel = False @@ -843,7 +843,7 @@ def http_get_handler(url, *_, **kwargs): return None http_get_handler.host_plugin_calls = 0 - with mock_wire_protocol(mockwiredata.DATA_FILE_IN_VM_ARTIFACTS_PROFILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE_IN_VM_ARTIFACTS_PROFILE) as protocol: HostPluginProtocol.is_default_channel = False try: @@ -876,7 +876,7 @@ def http_get_handler(url, *_, **kwargs): return None http_get_handler.host_plugin_calls = 0 - with mock_wire_protocol(mockwiredata.DATA_FILE_IN_VM_ARTIFACTS_PROFILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE_IN_VM_ARTIFACTS_PROFILE) as protocol: HostPluginProtocol.is_default_channel = False # initialization of the host plugin triggers a request for the goal state; do it here before we start tracking those requests. @@ -917,7 +917,7 @@ def host_func(*_): return direct_func, host_func def test_download_using_appropriate_channel_should_not_invoke_secondary_when_primary_channel_succeeds(self): - with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE) as protocol: # Scenario #1: Direct channel default HostPluginProtocol.is_default_channel = False @@ -943,7 +943,7 @@ def test_download_using_appropriate_channel_should_not_invoke_secondary_when_pri self.assertTrue(HostPluginProtocol.is_default_channel) def test_download_using_appropriate_channel_should_not_change_default_channel_if_none_succeeds(self): - with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE) as protocol: # Scenario #1: Direct channel is default HostPluginProtocol.is_default_channel = False direct_func, host_func = self._set_and_fail_helper_channel_functions(fail_direct=True, fail_host=True) @@ -969,7 +969,7 @@ def test_download_using_appropriate_channel_should_not_change_default_channel_if self.assertTrue(HostPluginProtocol.is_default_channel) def test_download_using_appropriate_channel_should_change_default_channel_when_secondary_succeeds(self): - with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE) as protocol: # Scenario #1: Direct channel is default HostPluginProtocol.is_default_channel = False direct_func, host_func = self._set_and_fail_helper_channel_functions(fail_direct=True, fail_host=False) @@ -1015,7 +1015,7 @@ class UpdateGoalStateTestCase(HttpRequestPredicates, AgentTestCase): """ def test_it_should_update_the_goal_state_and_the_host_plugin_when_the_incarnation_changes(self): - with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE) as protocol: protocol.client.get_host_plugin() # if the incarnation changes the behavior is the same for forced and non-forced updates @@ -1072,7 +1072,7 @@ def test_it_should_update_the_goal_state_and_the_host_plugin_when_the_incarnatio self.assertEqual(protocol.client.get_host_plugin().role_config_name, new_role_config_name) def test_non_forced_update_should_not_update_the_goal_state_but_should_update_the_host_plugin_when_the_incarnation_does_not_change(self): - with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE) as protocol: protocol.client.get_host_plugin() # The container id, role config name and shared config can change without the incarnation changing; capture the initial @@ -1096,7 +1096,7 @@ def test_non_forced_update_should_not_update_the_goal_state_but_should_update_th self.assertEqual(protocol.client.get_host_plugin().role_config_name, new_role_config_name) def test_forced_update_should_update_the_goal_state_and_the_host_plugin_when_the_incarnation_does_not_change(self): - with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE) as protocol: protocol.client.get_host_plugin() # The container id, role config name and shared config can change without the incarnation changing @@ -1119,7 +1119,7 @@ def test_forced_update_should_update_the_goal_state_and_the_host_plugin_when_the self.assertEqual(protocol.client.get_host_plugin().role_config_name, new_role_config_name) def test_reset_should_init_provided_goal_state_properties(self): - with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE) as protocol: protocol.client.reset_goal_state(goal_state_properties=GoalStateProperties.All & ~GoalStateProperties.Certificates) with self.assertRaises(ProtocolError) as context: @@ -1129,7 +1129,7 @@ def test_reset_should_init_provided_goal_state_properties(self): self.assertIn(expected_message, str(context.exception)) def test_reset_should_init_the_goal_state(self): - with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE) as protocol: new_container_id = str(uuid.uuid4()) new_role_config_name = str(uuid.uuid4()) protocol.mock_wire_data.set_container_id(new_container_id) @@ -1146,7 +1146,7 @@ class UpdateHostPluginFromGoalStateTestCase(AgentTestCase): Tests for WireClient.update_host_plugin_from_goal_state() """ def test_it_should_update_the_host_plugin_with_or_without_incarnation_changes(self): - with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE) as protocol: protocol.client.get_host_plugin() # the behavior should be the same whether the incarnation changes or not diff --git a/tests/common/test_agent_supported_feature.py b/tests/common/test_agent_supported_feature.py index c2d3b1981e..6a49dd8875 100644 --- a/tests/common/test_agent_supported_feature.py +++ b/tests/common/test_agent_supported_feature.py @@ -18,7 +18,7 @@ from azurelinuxagent.common.agent_supported_feature import SupportedFeatureNames, \ get_agent_supported_features_list_for_crp, get_supported_feature_by_name, \ get_agent_supported_features_list_for_extensions -from tests.tools import AgentTestCase, patch +from tests.lib.tools import AgentTestCase, patch class TestAgentSupportedFeature(AgentTestCase): diff --git a/tests/common/test_cgroupapi.py b/tests/common/test_cgroupapi.py index a31d57d722..7715d3cf8c 100644 --- a/tests/common/test_cgroupapi.py +++ b/tests/common/test_cgroupapi.py @@ -27,8 +27,8 @@ from azurelinuxagent.common.osutil import systemd from azurelinuxagent.common.utils import fileutil from tests.common.mock_cgroup_environment import mock_cgroup_environment -from tests.tools import AgentTestCase, patch, mock_sleep -from tests.utils.cgroups_tools import CGroupsTools +from tests.lib.tools import AgentTestCase, patch, mock_sleep +from tests.lib.cgroups_tools import CGroupsTools class _MockedFileSystemTestCase(AgentTestCase): def setUp(self): diff --git a/tests/common/test_cgroupconfigurator.py b/tests/common/test_cgroupconfigurator.py index 7e2dc45b44..2713b445d6 100644 --- a/tests/common/test_cgroupconfigurator.py +++ b/tests/common/test_cgroupconfigurator.py @@ -39,8 +39,8 @@ from azurelinuxagent.common.utils import shellutil, fileutil from tests.common.mock_environment import MockCommand from tests.common.mock_cgroup_environment import mock_cgroup_environment, UnitFilePaths -from tests.tools import AgentTestCase, patch, mock_sleep, i_am_root, data_dir, is_python_version_26_or_34, skip_if_predicate_true -from tests.utils.miscellaneous_tools import format_processes, wait_for +from tests.lib.tools import AgentTestCase, patch, mock_sleep, i_am_root, data_dir, is_python_version_26_or_34, skip_if_predicate_true +from tests.lib.miscellaneous_tools import format_processes, wait_for class CGroupConfiguratorSystemdTestCase(AgentTestCase): diff --git a/tests/common/test_cgroups.py b/tests/common/test_cgroups.py index 7f549e5b8d..58e179d82a 100644 --- a/tests/common/test_cgroups.py +++ b/tests/common/test_cgroups.py @@ -26,7 +26,7 @@ from azurelinuxagent.common.exception import CGroupsException from azurelinuxagent.common.osutil import get_osutil from azurelinuxagent.common.utils import fileutil -from tests.tools import AgentTestCase, patch, data_dir +from tests.lib.tools import AgentTestCase, patch, data_dir def consume_cpu_time(): diff --git a/tests/common/test_cgroupstelemetry.py b/tests/common/test_cgroupstelemetry.py index fe1ff299a3..e9b163437b 100644 --- a/tests/common/test_cgroupstelemetry.py +++ b/tests/common/test_cgroupstelemetry.py @@ -22,7 +22,7 @@ from azurelinuxagent.common.cgroup import CpuCgroup, MemoryCgroup from azurelinuxagent.common.cgroupstelemetry import CGroupsTelemetry from azurelinuxagent.common.utils import fileutil -from tests.tools import AgentTestCase, data_dir, patch +from tests.lib.tools import AgentTestCase, data_dir, patch def raise_ioerror(*_): diff --git a/tests/common/test_conf.py b/tests/common/test_conf.py index ebc57ffed9..972b289a79 100644 --- a/tests/common/test_conf.py +++ b/tests/common/test_conf.py @@ -19,7 +19,7 @@ import azurelinuxagent.common.conf as conf from azurelinuxagent.common.utils import fileutil -from tests.tools import AgentTestCase, data_dir +from tests.lib.tools import AgentTestCase, data_dir class TestConf(AgentTestCase): diff --git a/tests/common/test_errorstate.py b/tests/common/test_errorstate.py index 263d95ed7d..c51682b700 100644 --- a/tests/common/test_errorstate.py +++ b/tests/common/test_errorstate.py @@ -2,7 +2,7 @@ from datetime import timedelta, datetime from azurelinuxagent.common.errorstate import ErrorState -from tests.tools import Mock, patch +from tests.lib.tools import Mock, patch class TestErrorState(unittest.TestCase): diff --git a/tests/common/test_event.py b/tests/common/test_event.py index de5ad7353a..28f2e3860d 100644 --- a/tests/common/test_event.py +++ b/tests/common/test_event.py @@ -41,11 +41,11 @@ GuestAgentExtensionEventsSchema, GuestAgentPerfCounterEventsSchema from azurelinuxagent.common.version import CURRENT_AGENT, CURRENT_VERSION, AGENT_EXECUTION_MODE from azurelinuxagent.ga.collect_telemetry_events import _CollectAndEnqueueEvents -from tests.protocol import mockwiredata -from tests.protocol.mocks import mock_wire_protocol, MockHttpResponse -from tests.protocol.HttpRequestPredicates import HttpRequestPredicates -from tests.tools import AgentTestCase, data_dir, load_data, patch, skip_if_predicate_true, is_python_version_26_or_34 -from tests.utils.event_logger_tools import EventLoggerTools +from tests.lib import wire_protocol_data +from tests.lib.mock_wire_protocol import mock_wire_protocol, MockHttpResponse +from tests.lib.http_request_predicates import HttpRequestPredicates +from tests.lib.tools import AgentTestCase, data_dir, load_data, patch, skip_if_predicate_true, is_python_version_26_or_34 +from tests.lib.event_logger_tools import EventLoggerTools class TestEvent(HttpRequestPredicates, AgentTestCase): @@ -155,7 +155,7 @@ def create_event_and_return_container_id(): # pylint: disable=inconsistent-retu self.fail("Could not find Contained ID on event") - with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE) as protocol: contained_id = create_event_and_return_container_id() # The expect value comes from DATA_FILE self.assertEqual(contained_id, 'c6d5526c-5ac2-4200-b6e2-56f2b70c5ab2', "Incorrect container ID") @@ -787,7 +787,7 @@ def http_post_handler(url, body, **__): return None http_post_handler.request_body = None - with mock_wire_protocol(mockwiredata.DATA_FILE, http_post_handler=http_post_handler) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE, http_post_handler=http_post_handler) as protocol: event_file_path = self._create_test_event_file("event_with_callstack.waagent.tld") expected_message = get_event_message_from_event_file(event_file_path) @@ -807,7 +807,7 @@ def http_post_handler(url, body, **__): return None http_post_handler.request_body = None - with mock_wire_protocol(mockwiredata.DATA_FILE, http_post_handler=http_post_handler) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE, http_post_handler=http_post_handler) as protocol: test_messages = [ 'Non-English message - 此文字不是英文的', "Ξεσκεπάζω τὴν ψυχοφθόρα βδελυγμία", diff --git a/tests/common/test_logcollector.py b/tests/common/test_logcollector.py index 521e0f23ed..1ef2474c34 100644 --- a/tests/common/test_logcollector.py +++ b/tests/common/test_logcollector.py @@ -25,7 +25,7 @@ from azurelinuxagent.common.logcollector import LogCollector from azurelinuxagent.common.utils import fileutil from azurelinuxagent.common.utils.fileutil import rm_dirs, mkdir, rm_files -from tests.tools import AgentTestCase, is_python_version_26, patch, skip_if_predicate_true, data_dir +from tests.lib.tools import AgentTestCase, is_python_version_26, patch, skip_if_predicate_true, data_dir SMALL_FILE_SIZE = 1 * 1024 * 1024 # 1 MB LARGE_FILE_SIZE = 5 * 1024 * 1024 # 5 MB diff --git a/tests/common/test_logger.py b/tests/common/test_logger.py index a10ea47c6a..d792eb8577 100644 --- a/tests/common/test_logger.py +++ b/tests/common/test_logger.py @@ -24,7 +24,7 @@ import azurelinuxagent.common.logger as logger from azurelinuxagent.common.utils import fileutil -from tests.tools import AgentTestCase, MagicMock, patch, skip_if_predicate_true +from tests.lib.tools import AgentTestCase, MagicMock, patch, skip_if_predicate_true _MSG_INFO = "This is our test info logging message {0} {1}" _MSG_WARN = "This is our test warn logging message {0} {1}" diff --git a/tests/common/test_persist_firewall_rules.py b/tests/common/test_persist_firewall_rules.py index 307c8536e7..da414c952d 100644 --- a/tests/common/test_persist_firewall_rules.py +++ b/tests/common/test_persist_firewall_rules.py @@ -28,7 +28,7 @@ from azurelinuxagent.common.persist_firewall_rules import PersistFirewallRulesHandler from azurelinuxagent.common.utils import fileutil, shellutil from azurelinuxagent.common.utils.networkutil import AddFirewallRules, FirewallCmdDirectCommands -from tests.tools import AgentTestCase, MagicMock, patch +from tests.lib.tools import AgentTestCase, MagicMock, patch class TestPersistFirewallRulesHandler(AgentTestCase): diff --git a/tests/common/test_singletonperthread.py b/tests/common/test_singletonperthread.py index 39d8c99178..7b1972635e 100644 --- a/tests/common/test_singletonperthread.py +++ b/tests/common/test_singletonperthread.py @@ -3,7 +3,7 @@ from threading import Thread, currentThread from azurelinuxagent.common.singletonperthread import SingletonPerThread -from tests.tools import AgentTestCase, clear_singleton_instances +from tests.lib.tools import AgentTestCase, clear_singleton_instances class TestClassToTestSingletonPerThread(SingletonPerThread): diff --git a/tests/common/test_telemetryevent.py b/tests/common/test_telemetryevent.py index e23ab68446..ce232dab0b 100644 --- a/tests/common/test_telemetryevent.py +++ b/tests/common/test_telemetryevent.py @@ -16,7 +16,7 @@ # from azurelinuxagent.common.telemetryevent import TelemetryEvent, TelemetryEventParam, GuestAgentExtensionEventsSchema, \ CommonTelemetryEventSchema -from tests.tools import AgentTestCase +from tests.lib.tools import AgentTestCase def get_test_event(name="DummyExtension", op="Unknown", is_success=True, duration=0, version="foo", evt_type="", is_internal=False, diff --git a/tests/common/test_version.py b/tests/common/test_version.py index 625c0bcfc3..bdc07c0046 100644 --- a/tests/common/test_version.py +++ b/tests/common/test_version.py @@ -30,7 +30,7 @@ get_f5_platform, get_distro, get_lis_version, PY_VERSION_MAJOR, \ PY_VERSION_MINOR, get_daemon_version, set_daemon_version, __DAEMON_VERSION_ENV_VARIABLE as DAEMON_VERSION_ENV_VARIABLE from azurelinuxagent.common.utils.flexible_version import FlexibleVersion -from tests.tools import AgentTestCase, open_patch, patch +from tests.lib.tools import AgentTestCase, open_patch, patch def freebsd_system(): diff --git a/tests/protocol/__init__.py b/tests/common/utils/__init__.py similarity index 100% rename from tests/protocol/__init__.py rename to tests/common/utils/__init__.py diff --git a/tests/utils/test_archive.py b/tests/common/utils/test_archive.py similarity index 99% rename from tests/utils/test_archive.py rename to tests/common/utils/test_archive.py index 54766862f8..e65fef1e70 100644 --- a/tests/utils/test_archive.py +++ b/tests/common/utils/test_archive.py @@ -9,7 +9,7 @@ from azurelinuxagent.common import conf from azurelinuxagent.common.utils import fileutil, timeutil from azurelinuxagent.common.utils.archive import GoalStateHistory, StateArchiver, _MAX_ARCHIVED_STATES, ARCHIVE_DIRECTORY_NAME -from tests.tools import AgentTestCase, patch +from tests.lib.tools import AgentTestCase, patch debug = False if os.environ.get('DEBUG') == '1': diff --git a/tests/utils/test_crypt_util.py b/tests/common/utils/test_crypt_util.py similarity index 96% rename from tests/utils/test_crypt_util.py rename to tests/common/utils/test_crypt_util.py index 4c8ab2e375..c724c246c8 100644 --- a/tests/utils/test_crypt_util.py +++ b/tests/common/utils/test_crypt_util.py @@ -21,7 +21,7 @@ import azurelinuxagent.common.conf as conf from azurelinuxagent.common.exception import CryptError from azurelinuxagent.common.utils.cryptutil import CryptUtil -from tests.tools import AgentTestCase, data_dir, load_data, is_python_version_26, skip_if_predicate_true +from tests.lib.tools import AgentTestCase, data_dir, load_data, is_python_version_26, skip_if_predicate_true class TestCryptoUtilOperations(AgentTestCase): diff --git a/tests/utils/test_extension_process_util.py b/tests/common/utils/test_extension_process_util.py similarity index 99% rename from tests/utils/test_extension_process_util.py rename to tests/common/utils/test_extension_process_util.py index a74c4ff733..7f2d4aadf6 100644 --- a/tests/utils/test_extension_process_util.py +++ b/tests/common/utils/test_extension_process_util.py @@ -24,7 +24,7 @@ from azurelinuxagent.common.future import ustr from azurelinuxagent.common.utils.extensionprocessutil import format_stdout_stderr, read_output, \ wait_for_process_completion_or_timeout, handle_process_completion -from tests.tools import AgentTestCase, patch, data_dir +from tests.lib.tools import AgentTestCase, patch, data_dir class TestProcessUtils(AgentTestCase): diff --git a/tests/utils/test_file_util.py b/tests/common/utils/test_file_util.py similarity index 99% rename from tests/utils/test_file_util.py rename to tests/common/utils/test_file_util.py index 2dfa3bf966..f1514e5d04 100644 --- a/tests/utils/test_file_util.py +++ b/tests/common/utils/test_file_util.py @@ -27,7 +27,7 @@ import azurelinuxagent.common.utils.fileutil as fileutil from azurelinuxagent.common.future import ustr -from tests.tools import AgentTestCase, patch +from tests.lib.tools import AgentTestCase, patch class TestFileOperations(AgentTestCase): diff --git a/tests/utils/test_flexible_version.py b/tests/common/utils/test_flexible_version.py similarity index 100% rename from tests/utils/test_flexible_version.py rename to tests/common/utils/test_flexible_version.py diff --git a/tests/utils/test_network_util.py b/tests/common/utils/test_network_util.py similarity index 99% rename from tests/utils/test_network_util.py rename to tests/common/utils/test_network_util.py index 4c3f5d0144..e08f1aab3e 100644 --- a/tests/utils/test_network_util.py +++ b/tests/common/utils/test_network_util.py @@ -19,7 +19,7 @@ from mock.mock import patch import azurelinuxagent.common.utils.networkutil as networkutil -from tests.tools import AgentTestCase +from tests.lib.tools import AgentTestCase class TestNetworkOperations(AgentTestCase): diff --git a/tests/utils/test_passwords.txt b/tests/common/utils/test_passwords.txt similarity index 100% rename from tests/utils/test_passwords.txt rename to tests/common/utils/test_passwords.txt diff --git a/tests/utils/test_rest_util.py b/tests/common/utils/test_rest_util.py similarity index 99% rename from tests/utils/test_rest_util.py rename to tests/common/utils/test_rest_util.py index a0b00f6cd9..efcebb082f 100644 --- a/tests/utils/test_rest_util.py +++ b/tests/common/utils/test_rest_util.py @@ -22,7 +22,7 @@ import azurelinuxagent.common.utils.restutil as restutil from azurelinuxagent.common.utils.restutil import HTTP_USER_AGENT from azurelinuxagent.common.future import httpclient, ustr -from tests.tools import AgentTestCase, call, Mock, MagicMock, patch +from tests.lib.tools import AgentTestCase, call, Mock, MagicMock, patch class TestIOErrorCounter(AgentTestCase): diff --git a/tests/utils/test_shell_util.py b/tests/common/utils/test_shell_util.py similarity index 99% rename from tests/utils/test_shell_util.py rename to tests/common/utils/test_shell_util.py index 83082bf7e7..3c6afc60e6 100644 --- a/tests/utils/test_shell_util.py +++ b/tests/common/utils/test_shell_util.py @@ -24,8 +24,8 @@ from azurelinuxagent.common.future import ustr import azurelinuxagent.common.utils.shellutil as shellutil -from tests.tools import AgentTestCase, patch -from tests.utils.miscellaneous_tools import wait_for, format_processes +from tests.lib.tools import AgentTestCase, patch +from tests.lib.miscellaneous_tools import wait_for, format_processes class ShellQuoteTestCase(AgentTestCase): diff --git a/tests/utils/test_text_util.py b/tests/common/utils/test_text_util.py similarity index 99% rename from tests/utils/test_text_util.py rename to tests/common/utils/test_text_util.py index ff129c40be..5029cfb921 100644 --- a/tests/utils/test_text_util.py +++ b/tests/common/utils/test_text_util.py @@ -22,7 +22,7 @@ import azurelinuxagent.common.utils.textutil as textutil from azurelinuxagent.common.future import ustr -from tests.tools import AgentTestCase +from tests.lib.tools import AgentTestCase class TestTextUtil(AgentTestCase): diff --git a/tests/daemon/test_daemon.py b/tests/daemon/test_daemon.py index b5a75902ba..4b34ddec73 100644 --- a/tests/daemon/test_daemon.py +++ b/tests/daemon/test_daemon.py @@ -22,7 +22,7 @@ import azurelinuxagent.common.conf as conf from azurelinuxagent.daemon.main import OPENSSL_FIPS_ENVIRONMENT, get_daemon_handler from azurelinuxagent.pa.provision.default import ProvisionHandler -from tests.tools import AgentTestCase, Mock, patch +from tests.lib.tools import AgentTestCase, Mock, patch class MockDaemonCall(object): diff --git a/tests/daemon/test_resourcedisk.py b/tests/daemon/test_resourcedisk.py index 301ac695e4..c072d59001 100644 --- a/tests/daemon/test_resourcedisk.py +++ b/tests/daemon/test_resourcedisk.py @@ -15,10 +15,15 @@ # Requires Python 2.6+ and Openssl 1.0+ # +import os +import stat +import sys import unittest -from tests.tools import AgentTestCase, patch, DEFAULT +from tests.lib.tools import AgentTestCase, patch, DEFAULT +from azurelinuxagent.daemon.resourcedisk import get_resourcedisk_handler from azurelinuxagent.daemon.resourcedisk.default import ResourceDiskHandler +from azurelinuxagent.common.utils import shellutil class TestResourceDisk(AgentTestCase): @@ -80,6 +85,122 @@ def run_side_effect(*args, **kwargs): # pylint: disable=unused-argument size_mb=size_mb ) +class TestResourceDisk(AgentTestCase): + def test_mkfile(self): + # setup + test_file = os.path.join(self.tmp_dir, 'test_file') + file_size = 1024 * 128 + if os.path.exists(test_file): + os.remove(test_file) + + # execute + get_resourcedisk_handler().mkfile(test_file, file_size) + + # assert + assert os.path.exists(test_file) + + # only the owner should have access + mode = os.stat(test_file).st_mode & ( + stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) + assert mode == stat.S_IRUSR | stat.S_IWUSR + + # cleanup + os.remove(test_file) + + def test_mkfile_dd_fallback(self): + with patch.object(shellutil, "run") as run_patch: + # setup + run_patch.return_value = 1 + test_file = os.path.join(self.tmp_dir, 'test_file') + file_size = 1024 * 128 + + # execute + if sys.version_info >= (3, 3): + with patch("os.posix_fallocate", + side_effect=Exception('failure')): + get_resourcedisk_handler().mkfile(test_file, file_size) + else: + get_resourcedisk_handler().mkfile(test_file, file_size) + + # assert + assert run_patch.call_count > 1 + assert "fallocate" in run_patch.call_args_list[0][0][0] + assert "dd if" in run_patch.call_args_list[-1][0][0] + + def test_mkfile_xfs_fs(self): + # setup + test_file = os.path.join(self.tmp_dir, 'test_file') + file_size = 1024 * 128 + if os.path.exists(test_file): + os.remove(test_file) + + # execute + resource_disk_handler = get_resourcedisk_handler() + resource_disk_handler.fs = 'xfs' + + with patch.object(shellutil, "run") as run_patch: + resource_disk_handler.mkfile(test_file, file_size) + + # assert + if sys.version_info >= (3, 3): + with patch("os.posix_fallocate") as posix_fallocate: + self.assertEqual(0, posix_fallocate.call_count) + + assert run_patch.call_count == 1 + assert "dd if" in run_patch.call_args_list[0][0][0] + + def test_change_partition_type(self): + resource_handler = get_resourcedisk_handler() + # test when sfdisk --part-type does not exist + with patch.object(shellutil, "run_get_output", + side_effect=[[1, ''], [0, '']]) as run_patch: + resource_handler.change_partition_type( + suppress_message=True, option_str='') + + # assert + assert run_patch.call_count == 2 + assert "sfdisk --part-type" in run_patch.call_args_list[0][0][0] + assert "sfdisk -c" in run_patch.call_args_list[1][0][0] + + # test when sfdisk --part-type exists + with patch.object(shellutil, "run_get_output", + side_effect=[[0, '']]) as run_patch: + resource_handler.change_partition_type( + suppress_message=True, option_str='') + + # assert + assert run_patch.call_count == 1 + assert "sfdisk --part-type" in run_patch.call_args_list[0][0][0] + + def test_check_existing_swap_file(self): + test_file = os.path.join(self.tmp_dir, 'test_swap_file') + file_size = 1024 * 128 + if os.path.exists(test_file): + os.remove(test_file) + + with open(test_file, "wb") as file: # pylint: disable=redefined-builtin + file.write(bytearray(file_size)) + + os.chmod(test_file, stat.S_ISUID | stat.S_ISGID | stat.S_IRUSR | + stat.S_IWUSR | stat.S_IRWXG | stat.S_IRWXO) # 0o6677 + + def swap_on(_): # mimic the output of "swapon -s" + return [ + "Filename Type Size Used Priority", + "{0} partition 16498684 0 -2".format(test_file) + ] + + with patch.object(shellutil, "run_get_output", side_effect=swap_on): + get_resourcedisk_handler().check_existing_swap_file( + test_file, test_file, file_size) + + # it should remove access from group, others + mode = os.stat(test_file).st_mode & (stat.S_ISUID | stat.S_ISGID | + stat.S_IRWXU | stat.S_IWUSR | stat.S_IRWXG | stat.S_IRWXO) # 0o6777 + assert mode == stat.S_ISUID | stat.S_ISGID | stat.S_IRUSR | stat.S_IWUSR # 0o6600 + + os.remove(test_file) + if __name__ == '__main__': unittest.main() diff --git a/tests/distro/test_scvmm.py b/tests/daemon/test_scvmm.py similarity index 98% rename from tests/distro/test_scvmm.py rename to tests/daemon/test_scvmm.py index 109a960522..275f3f6e37 100644 --- a/tests/distro/test_scvmm.py +++ b/tests/daemon/test_scvmm.py @@ -26,7 +26,7 @@ from azurelinuxagent.common import conf from azurelinuxagent.common.osutil.default import DefaultOSUtil from azurelinuxagent.common.utils import fileutil -from tests.tools import AgentTestCase, Mock, patch +from tests.lib.tools import AgentTestCase, Mock, patch class TestSCVMM(AgentTestCase): diff --git a/tests/distro/test_resourceDisk.py b/tests/distro/test_resourceDisk.py deleted file mode 100644 index 04acd39152..0000000000 --- a/tests/distro/test_resourceDisk.py +++ /dev/null @@ -1,148 +0,0 @@ -# Copyright 2018 Microsoft Corporation -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -# Requires Python 2.6+ and Openssl 1.0+ -# -# Implements parts of RFC 2131, 1541, 1497 and -# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx -# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx - -import os -import stat -import sys -import unittest -from azurelinuxagent.common.utils import shellutil -from azurelinuxagent.daemon.resourcedisk import get_resourcedisk_handler -from tests.tools import AgentTestCase, patch - - -class TestResourceDisk(AgentTestCase): - def test_mkfile(self): - # setup - test_file = os.path.join(self.tmp_dir, 'test_file') - file_size = 1024 * 128 - if os.path.exists(test_file): - os.remove(test_file) - - # execute - get_resourcedisk_handler().mkfile(test_file, file_size) - - # assert - assert os.path.exists(test_file) - - # only the owner should have access - mode = os.stat(test_file).st_mode & ( - stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) - assert mode == stat.S_IRUSR | stat.S_IWUSR - - # cleanup - os.remove(test_file) - - def test_mkfile_dd_fallback(self): - with patch.object(shellutil, "run") as run_patch: - # setup - run_patch.return_value = 1 - test_file = os.path.join(self.tmp_dir, 'test_file') - file_size = 1024 * 128 - - # execute - if sys.version_info >= (3, 3): - with patch("os.posix_fallocate", - side_effect=Exception('failure')): - get_resourcedisk_handler().mkfile(test_file, file_size) - else: - get_resourcedisk_handler().mkfile(test_file, file_size) - - # assert - assert run_patch.call_count > 1 - assert "fallocate" in run_patch.call_args_list[0][0][0] - assert "dd if" in run_patch.call_args_list[-1][0][0] - - def test_mkfile_xfs_fs(self): - # setup - test_file = os.path.join(self.tmp_dir, 'test_file') - file_size = 1024 * 128 - if os.path.exists(test_file): - os.remove(test_file) - - # execute - resource_disk_handler = get_resourcedisk_handler() - resource_disk_handler.fs = 'xfs' - - with patch.object(shellutil, "run") as run_patch: - resource_disk_handler.mkfile(test_file, file_size) - - # assert - if sys.version_info >= (3, 3): - with patch("os.posix_fallocate") as posix_fallocate: - self.assertEqual(0, posix_fallocate.call_count) - - assert run_patch.call_count == 1 - assert "dd if" in run_patch.call_args_list[0][0][0] - - def test_change_partition_type(self): - resource_handler = get_resourcedisk_handler() - # test when sfdisk --part-type does not exist - with patch.object(shellutil, "run_get_output", - side_effect=[[1, ''], [0, '']]) as run_patch: - resource_handler.change_partition_type( - suppress_message=True, option_str='') - - # assert - assert run_patch.call_count == 2 - assert "sfdisk --part-type" in run_patch.call_args_list[0][0][0] - assert "sfdisk -c" in run_patch.call_args_list[1][0][0] - - # test when sfdisk --part-type exists - with patch.object(shellutil, "run_get_output", - side_effect=[[0, '']]) as run_patch: - resource_handler.change_partition_type( - suppress_message=True, option_str='') - - # assert - assert run_patch.call_count == 1 - assert "sfdisk --part-type" in run_patch.call_args_list[0][0][0] - - def test_check_existing_swap_file(self): - test_file = os.path.join(self.tmp_dir, 'test_swap_file') - file_size = 1024 * 128 - if os.path.exists(test_file): - os.remove(test_file) - - with open(test_file, "wb") as file: # pylint: disable=redefined-builtin - file.write(bytearray(file_size)) - - os.chmod(test_file, stat.S_ISUID | stat.S_ISGID | stat.S_IRUSR | - stat.S_IWUSR | stat.S_IRWXG | stat.S_IRWXO) # 0o6677 - - def swap_on(_): # mimic the output of "swapon -s" - return [ - "Filename Type Size Used Priority", - "{0} partition 16498684 0 -2".format(test_file) - ] - - with patch.object(shellutil, "run_get_output", side_effect=swap_on): - get_resourcedisk_handler().check_existing_swap_file( - test_file, test_file, file_size) - - # it should remove access from group, others - mode = os.stat(test_file).st_mode & (stat.S_ISUID | stat.S_ISGID | - stat.S_IRWXU | stat.S_IWUSR | stat.S_IRWXG | stat.S_IRWXO) # 0o6777 - assert mode == stat.S_ISUID | stat.S_ISGID | stat.S_IRUSR | stat.S_IWUSR # 0o6600 - - os.remove(test_file) - - -if __name__ == '__main__': - unittest.main() diff --git a/tests/ga/test_agent_update_handler.py b/tests/ga/test_agent_update_handler.py index 35ec714264..a78471df08 100644 --- a/tests/ga/test_agent_update_handler.py +++ b/tests/ga/test_agent_update_handler.py @@ -13,10 +13,10 @@ from azurelinuxagent.ga.agent_update_handler import get_agent_update_handler from azurelinuxagent.ga.guestagent import GAUpdateReportState from tests.ga.test_update import UpdateTestCase -from tests.protocol.HttpRequestPredicates import HttpRequestPredicates -from tests.protocol.mocks import mock_wire_protocol, MockHttpResponse -from tests.protocol.mockwiredata import DATA_FILE -from tests.tools import clear_singleton_instances, load_bin_data, patch +from tests.lib.http_request_predicates import HttpRequestPredicates +from tests.lib.mock_wire_protocol import mock_wire_protocol, MockHttpResponse +from tests.lib.wire_protocol_data import DATA_FILE +from tests.lib.tools import clear_singleton_instances, load_bin_data, patch class TestAgentUpdate(UpdateTestCase): diff --git a/tests/ga/test_collect_logs.py b/tests/ga/test_collect_logs.py index 14593726d6..fa0add0153 100644 --- a/tests/ga/test_collect_logs.py +++ b/tests/ga/test_collect_logs.py @@ -25,10 +25,10 @@ from azurelinuxagent.common.utils import fileutil from azurelinuxagent.ga.collect_logs import get_collect_logs_handler, is_log_collection_allowed, \ get_log_collector_monitor_handler -from tests.protocol.mocks import mock_wire_protocol, MockHttpResponse -from tests.protocol.HttpRequestPredicates import HttpRequestPredicates -from tests.protocol.mockwiredata import DATA_FILE -from tests.tools import Mock, MagicMock, patch, AgentTestCase, clear_singleton_instances, skip_if_predicate_true, \ +from tests.lib.mock_wire_protocol import mock_wire_protocol, MockHttpResponse +from tests.lib.http_request_predicates import HttpRequestPredicates +from tests.lib.wire_protocol_data import DATA_FILE +from tests.lib.tools import Mock, MagicMock, patch, AgentTestCase, clear_singleton_instances, skip_if_predicate_true, \ is_python_version_26, data_dir diff --git a/tests/ga/test_collect_telemetry_events.py b/tests/ga/test_collect_telemetry_events.py index bdd763effb..509af2ceff 100644 --- a/tests/ga/test_collect_telemetry_events.py +++ b/tests/ga/test_collect_telemetry_events.py @@ -36,8 +36,8 @@ CommonTelemetryEventSchema from azurelinuxagent.common.utils import fileutil from azurelinuxagent.ga.collect_telemetry_events import ExtensionEventSchema, _ProcessExtensionEvents -from tests.protocol.HttpRequestPredicates import HttpRequestPredicates -from tests.tools import AgentTestCase, clear_singleton_instances, data_dir +from tests.lib.http_request_predicates import HttpRequestPredicates +from tests.lib.tools import AgentTestCase, clear_singleton_instances, data_dir class TestExtensionTelemetryHandler(AgentTestCase, HttpRequestPredicates): diff --git a/tests/ga/test_env.py b/tests/ga/test_env.py index aa4b74ab1c..29ca6fec1e 100644 --- a/tests/ga/test_env.py +++ b/tests/ga/test_env.py @@ -19,7 +19,7 @@ from azurelinuxagent.common.osutil import get_osutil from azurelinuxagent.common.osutil.default import DefaultOSUtil, shellutil from azurelinuxagent.ga.env import MonitorDhcpClientRestart -from tests.tools import AgentTestCase, patch +from tests.lib.tools import AgentTestCase, patch class MonitorDhcpClientRestartTestCase(AgentTestCase): diff --git a/tests/ga/test_extension.py b/tests/ga/test_extension.py index 76dde881d2..201868fc68 100644 --- a/tests/ga/test_extension.py +++ b/tests/ga/test_extension.py @@ -50,12 +50,12 @@ get_exthandlers_handler, ExtCommandEnvVariable, HandlerManifest, NOT_RUN, \ ExtensionStatusValue, HANDLER_COMPLETE_NAME_PATTERN, HandlerEnvironment, GoalStateStatus -from tests.protocol import mockwiredata -from tests.protocol.mocks import mock_wire_protocol, MockHttpResponse -from tests.protocol.HttpRequestPredicates import HttpRequestPredicates -from tests.protocol.mockwiredata import DATA_FILE, DATA_FILE_EXT_ADDITIONAL_LOCATIONS -from tests.tools import AgentTestCase, data_dir, MagicMock, Mock, patch, mock_sleep -from tests.ga.extension_emulator import Actions, ExtensionCommandNames, extension_emulator, \ +from tests.lib import wire_protocol_data +from tests.lib.mock_wire_protocol import mock_wire_protocol, MockHttpResponse +from tests.lib.http_request_predicates import HttpRequestPredicates +from tests.lib.wire_protocol_data import DATA_FILE, DATA_FILE_EXT_ADDITIONAL_LOCATIONS +from tests.lib.tools import AgentTestCase, data_dir, MagicMock, Mock, patch, mock_sleep +from tests.lib.extension_emulator import Actions, ExtensionCommandNames, extension_emulator, \ enable_invocations, generate_put_handler # Mocking the original sleep to reduce test execution time @@ -137,7 +137,7 @@ def mock_http_put(url, *args, **_): yield exthandlers_handler, protocol, no_of_extensions def test_cleanup_leaves_installed_extensions(self): - with self._setup_test_env(mockwiredata.DATA_FILE_MULTIPLE_EXT) as (exthandlers_handler, protocol, no_of_exts): + with self._setup_test_env(wire_protocol_data.DATA_FILE_MULTIPLE_EXT) as (exthandlers_handler, protocol, no_of_exts): exthandlers_handler.run() exthandlers_handler.report_ext_handlers_status() @@ -147,7 +147,7 @@ def test_cleanup_leaves_installed_extensions(self): version="1.0.0") def test_cleanup_removes_uninstalled_extensions(self): - with self._setup_test_env(mockwiredata.DATA_FILE_MULTIPLE_EXT) as (exthandlers_handler, protocol, no_of_exts): + with self._setup_test_env(wire_protocol_data.DATA_FILE_MULTIPLE_EXT) as (exthandlers_handler, protocol, no_of_exts): exthandlers_handler.run() exthandlers_handler.report_ext_handlers_status() self._assert_ext_handler_status(protocol.aggregate_status, "Ready", expected_ext_handler_count=no_of_exts, @@ -167,7 +167,7 @@ def test_cleanup_removes_uninstalled_extensions(self): self.assertEqual(0, TestExtensionCleanup._count_extension_directories(), "All extension directories should be removed") def test_cleanup_removes_orphaned_packages(self): - data_file = mockwiredata.DATA_FILE_NO_EXT.copy() + data_file = wire_protocol_data.DATA_FILE_NO_EXT.copy() data_file["ext_conf"] = "wire/ext_conf_no_extensions-no_status_blob.xml" no_of_orphaned_packages = 5 @@ -197,7 +197,7 @@ def test_cleanup_leaves_failed_extensions(self): def mock_fail_popen(*args, **kwargs): # pylint: disable=unused-argument return original_popen("fail_this_command", **kwargs) - with self._setup_test_env(mockwiredata.DATA_FILE_EXT_SINGLE) as (exthandlers_handler, protocol, no_of_exts): + with self._setup_test_env(wire_protocol_data.DATA_FILE_EXT_SINGLE) as (exthandlers_handler, protocol, no_of_exts): with patch("azurelinuxagent.common.cgroupapi.subprocess.Popen", mock_fail_popen): exthandlers_handler.run() exthandlers_handler.report_ext_handlers_status() @@ -235,7 +235,7 @@ def assert_extension_seq_no(expected_seq_no): self.assertEqual(expected_seq_no, handler_status['runtimeSettingsStatus']['sequenceNumber'], "Sequence number mismatch") - with self._setup_test_env(mockwiredata.DATA_FILE_MULTIPLE_EXT) as (exthandlers_handler, protocol, orig_no_of_exts): + with self._setup_test_env(wire_protocol_data.DATA_FILE_MULTIPLE_EXT) as (exthandlers_handler, protocol, orig_no_of_exts): # Run 1 - GS has no required features and contains 5 extensions exthandlers_handler.run() exthandlers_handler.report_ext_handlers_status() @@ -249,7 +249,7 @@ def assert_extension_seq_no(expected_seq_no): # Run 2 - Change the GS to one with Required features not supported by the agent # This ExtensionConfig has 1 extension - ExampleHandlerLinuxWithRequiredFeatures - protocol.mock_wire_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_REQUIRED_FEATURES) + protocol.mock_wire_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE_REQUIRED_FEATURES) protocol.mock_wire_data.set_incarnation(2) protocol.mock_wire_data.set_extensions_config_sequence_number(random.randint(10, 100)) protocol.client.update_goal_state() @@ -272,7 +272,7 @@ def assert_extension_seq_no(expected_seq_no): # Run 3 - Run a GS with no Required Features and ensure we execute all extensions properly # This ExtensionConfig has 1 extension - OSTCExtensions.ExampleHandlerLinux - protocol.mock_wire_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) + protocol.mock_wire_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE) protocol.mock_wire_data.set_incarnation(3) extension_seq_no = random.randint(10, 100) protocol.mock_wire_data.set_extensions_config_sequence_number(extension_seq_no) @@ -495,7 +495,7 @@ def _set_up_update_test_and_update_gs(self, patch_command, *args): :param args: Any additional args passed to the function, needed for creating a mock for handler and protocol :return: test_data, exthandlers_handler, protocol """ - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_EXT_SINGLE) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE_EXT_SINGLE) exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter # Ensure initial install and enable is successful @@ -524,7 +524,7 @@ def _create_extension_handlers_handler(protocol): def test_ext_handler(self, *args): # Test enable scenario. - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE) exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter exthandlers_handler.run() @@ -612,7 +612,7 @@ def _assert_handler_status_and_manifest_download_count(protocol, test_data, mani self.assertEqual(test_data.call_counts['manifest.xml'], manifest_count, "We should have downloaded extension manifest {0} times".format(manifest_count)) - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE) exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter exthandlers_handler.run() exthandlers_handler.report_ext_handlers_status() @@ -634,7 +634,7 @@ def test_it_should_fail_handler_on_bad_extension_config_and_report_error(self, m for bad_config_file_path in os.listdir(invalid_config_dir): bad_conf = DATA_FILE.copy() bad_conf["ext_conf"] = os.path.join(invalid_config_dir, bad_config_file_path) - test_data = mockwiredata.WireProtocolData(bad_conf) + test_data = wire_protocol_data.WireProtocolData(bad_conf) exthandlers_handler, protocol = self._create_mock(test_data, mock_get, mock_crypt_util, *args) with patch('azurelinuxagent.ga.exthandlers.add_event') as patch_add_event: @@ -651,7 +651,7 @@ def test_it_should_process_valid_extensions_if_present(self, mock_get, mock_cryp bad_conf = DATA_FILE.copy() bad_conf["ext_conf"] = os.path.join("wire", "ext_conf_invalid_and_valid_handlers.xml") - test_data = mockwiredata.WireProtocolData(bad_conf) + test_data = wire_protocol_data.WireProtocolData(bad_conf) exthandlers_handler, protocol = self._create_mock(test_data, mock_get, mock_crypt_util, *args) exthandlers_handler.run() @@ -675,7 +675,7 @@ def test_it_should_process_valid_extensions_if_present(self, mock_get, mock_cryp def test_it_should_ignore_case_when_parsing_plugin_settings(self, mock_get, mock_crypt_util, *args): - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_CASE_MISMATCH_EXT) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE_CASE_MISMATCH_EXT) exthandlers_handler, protocol = self._create_mock(test_data, mock_get, mock_crypt_util, *args) exthandlers_handler.run() @@ -704,7 +704,7 @@ def test_it_should_ignore_case_when_parsing_plugin_settings(self, mock_get, mock self.assertEqual(0, len(expected_ext_handlers), "All handlers not reported") def test_ext_handler_no_settings(self, *args): - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_EXT_NO_SETTINGS) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE_EXT_NO_SETTINGS) exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter test_ext = extension_emulator(name="OSTCExtensions.ExampleHandlerLinux") @@ -734,7 +734,7 @@ def test_ext_handler_no_settings(self, *args): ) def test_ext_handler_no_public_settings(self, *args): - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_EXT_NO_PUBLIC) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE_EXT_NO_PUBLIC) exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter exthandlers_handler.run() @@ -742,7 +742,7 @@ def test_ext_handler_no_public_settings(self, *args): self._assert_handler_status(protocol.report_vm_status, "Ready", 1, "1.0.0") def test_ext_handler_no_ext(self, *args): - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_NO_EXT) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE_NO_EXT) exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter # Assert no extension handler status @@ -752,7 +752,7 @@ def test_ext_handler_no_ext(self, *args): def test_ext_handler_sequencing(self, *args): # Test enable scenario. - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_EXT_SEQUENCING) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE_EXT_SEQUENCING) exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter dep_ext_level_2 = extension_emulator(name="OSTCExtensions.ExampleHandlerLinux") @@ -881,7 +881,7 @@ def test_it_should_process_sequencing_properly_even_if_no_settings_for_dependent self, mock_get, mock_crypt, *args): test_data_file = DATA_FILE.copy() test_data_file["ext_conf"] = "wire/ext_conf_dependencies_with_empty_settings.xml" - test_data = mockwiredata.WireProtocolData(test_data_file) + test_data = wire_protocol_data.WireProtocolData(test_data_file) exthandlers_handler, protocol = self._create_mock(test_data, mock_get, mock_crypt, *args) ext_1 = extension_emulator(name="OSTCExtensions.ExampleHandlerLinux") @@ -910,7 +910,7 @@ def test_it_should_process_sequencing_properly_even_if_no_settings_for_dependent ) def test_ext_handler_sequencing_should_fail_if_handler_failed(self, mock_get, mock_crypt, *args): - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_EXT_SEQUENCING) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE_EXT_SEQUENCING) exthandlers_handler, protocol = self._create_mock(test_data, mock_get, mock_crypt, *args) original_popen = subprocess.Popen @@ -986,7 +986,7 @@ def mock_fail_extension_commands(args, **kwargs): ) def test_ext_handler_sequencing_default_dependency_level(self, *args): - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE) exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=unused-variable,no-value-for-parameter exthandlers_handler.run() exthandlers_handler.report_ext_handlers_status() @@ -995,7 +995,7 @@ def test_ext_handler_sequencing_default_dependency_level(self, *args): self.assertEqual(exthandlers_handler.ext_handlers[0].settings[0].dependencyLevel, 0) def test_ext_handler_sequencing_invalid_dependency_level(self, *args): - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_EXT_SEQUENCING) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE_EXT_SEQUENCING) test_data.set_incarnation(2) test_data.set_extensions_config_sequence_number(1) test_data.ext_conf = test_data.ext_conf.replace("dependencyLevel=\"1\"", @@ -1012,7 +1012,7 @@ def test_ext_handler_sequencing_invalid_dependency_level(self, *args): def test_ext_handler_rollingupgrade(self, *args): # Test enable scenario. - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_EXT_ROLLINGUPGRADE) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE_EXT_ROLLINGUPGRADE) exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter exthandlers_handler.run() @@ -1123,7 +1123,7 @@ def test_it_should_create_extension_events_dir_and_set_handler_environment_only_ with patch("azurelinuxagent.common.agent_supported_feature._ETPFeature.is_supported", enable_extensions): # Create new object for each run to force re-installation of extensions as we # only create handler_environment on installation - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_MULTIPLE_EXT) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE_MULTIPLE_EXT) exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter exthandlers_handler.run() @@ -1154,7 +1154,7 @@ def test_it_should_create_extension_events_dir_and_set_handler_environment_only_ shutil.rmtree(tmp_lib_dir, ignore_errors=True) def test_it_should_not_delete_extension_events_directory_on_extension_uninstall(self, *args): - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE) exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter with patch("azurelinuxagent.common.agent_supported_feature._ETPFeature.is_supported", True): @@ -1178,7 +1178,7 @@ def test_it_should_not_delete_extension_events_directory_on_extension_uninstall( self.assertTrue(os.path.exists(ehi.get_extension_events_dir()), "Events directory should still exist") def test_it_should_uninstall_unregistered_extensions_properly(self, *args): - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE) exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter exthandlers_handler.run() exthandlers_handler.report_ext_handlers_status() @@ -1205,7 +1205,7 @@ def test_it_should_uninstall_unregistered_extensions_properly(self, *args): @patch('azurelinuxagent.common.errorstate.ErrorState.is_triggered') @patch('azurelinuxagent.ga.exthandlers.add_event') def test_ext_handler_report_status_permanent(self, mock_add_event, mock_error_state, *args): - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE) exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter protocol.report_vm_status = Mock(side_effect=ProtocolError) @@ -1221,7 +1221,7 @@ def test_ext_handler_report_status_permanent(self, mock_add_event, mock_error_st @patch('azurelinuxagent.ga.exthandlers.add_event') def test_ext_handler_report_status_resource_gone(self, mock_add_event, *args): - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE) exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter protocol.report_vm_status = Mock(side_effect=ResourceGoneError) @@ -1236,7 +1236,7 @@ def test_ext_handler_report_status_resource_gone(self, mock_add_event, *args): @patch('azurelinuxagent.common.errorstate.ErrorState.is_triggered') @patch('azurelinuxagent.ga.exthandlers.add_event') def test_ext_handler_download_failure_permanent_ProtocolError(self, mock_add_event, mock_error_state, *args): - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE) exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter protocol.get_goal_state().fetch_extension_manifest = Mock(side_effect=ProtocolError) @@ -1254,7 +1254,7 @@ def test_ext_handler_download_failure_permanent_ProtocolError(self, mock_add_eve @patch('azurelinuxagent.ga.exthandlers.fileutil') def test_ext_handler_io_error(self, mock_fileutil, *args): - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE) exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=unused-variable,no-value-for-parameter mock_fileutil.write_file.return_value = IOError("Mock IO Error") @@ -1277,7 +1277,7 @@ def _assert_ext_status(self, vm_agent_status, expected_status, self.assertIn(expected_msg, ext_status.message) def test_it_should_initialise_and_use_command_execution_log_for_extensions(self, mock_get, mock_crypt_util, *args): - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE) exthandlers_handler, protocol = self._create_mock(test_data, mock_get, mock_crypt_util, *args) exthandlers_handler.run() exthandlers_handler.report_ext_handlers_status() @@ -1290,7 +1290,7 @@ def test_it_should_initialise_and_use_command_execution_log_for_extensions(self, self.assertGreater(os.path.getsize(command_execution_log), 0, "The file should not be empty") def test_ext_handler_no_reporting_status(self, *args): - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE) exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter exthandlers_handler.run() exthandlers_handler.report_ext_handlers_status() @@ -1317,7 +1317,7 @@ def test_wait_for_handler_completion_no_status(self, mock_http_get, mock_crypt_u Expected to retry and eventually report failure for all dependent extensions. """ exthandlers_handler, protocol = self._create_mock( - mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_EXT_SEQUENCING), mock_http_get, mock_crypt_util, *args) + wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE_EXT_SEQUENCING), mock_http_get, mock_crypt_util, *args) original_popen = subprocess.Popen @@ -1371,7 +1371,7 @@ def mock_popen(cmd, *_, **kwargs): aks_test_mock = DATA_FILE.copy() aks_test_mock["ext_conf"] = "wire/ext_conf_aks_extension.xml" - exthandlers_handler, protocol = self._create_mock(mockwiredata.WireProtocolData(aks_test_mock), + exthandlers_handler, protocol = self._create_mock(wire_protocol_data.WireProtocolData(aks_test_mock), mock_http_get, mock_crypt_util, *args) with patch('azurelinuxagent.common.cgroupapi.subprocess.Popen', side_effect=mock_popen): @@ -1405,7 +1405,7 @@ def test_it_should_include_part_of_status_in_ext_handler_message(self, mock_http debugging. """ exthandlers_handler, protocol = self._create_mock( - mockwiredata.WireProtocolData(mockwiredata.DATA_FILE), mock_http_get, mock_crypt_util, *args) + wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE), mock_http_get, mock_crypt_util, *args) original_popen = subprocess.Popen @@ -1440,7 +1440,7 @@ def test_wait_for_handler_completion_success_status(self, mock_http_get, mock_cr Testing depends-on scenario on a successful case. Expected to report the status for both extensions properly. """ exthandlers_handler, protocol = self._create_mock( - mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_EXT_SEQUENCING), mock_http_get, mock_crypt_util, *args) + wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE_EXT_SEQUENCING), mock_http_get, mock_crypt_util, *args) exthandlers_handler.run() exthandlers_handler.report_ext_handlers_status() @@ -1463,7 +1463,7 @@ def test_wait_for_handler_completion_error_status(self, mock_http_get, mock_cryp Expected to return False. """ exthandlers_handler, protocol = self._create_mock( - mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_EXT_SEQUENCING), mock_http_get, mock_crypt_util, *args) + wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE_EXT_SEQUENCING), mock_http_get, mock_crypt_util, *args) original_popen = subprocess.Popen @@ -1491,7 +1491,7 @@ def test_get_ext_handling_status(self, *args): Testing get_ext_handling_status() function with various cases and verifying against the expected values """ - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE) exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=unused-variable,no-value-for-parameter handler_name = "Handler" @@ -1534,7 +1534,7 @@ def test_is_ext_handling_complete(self, *args): Testing is_ext_handling_complete() with various input and verifying against the expected output values. """ - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE) exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=unused-variable,no-value-for-parameter handler_name = "Handler" @@ -1572,18 +1572,18 @@ def test_ext_handler_version_decide_autoupgrade_internalversion(self, *args): config_version = '1.3.0' decision_version = '1.3.0' if autoupgrade: - datafile = mockwiredata.DATA_FILE_EXT_AUTOUPGRADE_INTERNALVERSION + datafile = wire_protocol_data.DATA_FILE_EXT_AUTOUPGRADE_INTERNALVERSION else: - datafile = mockwiredata.DATA_FILE_EXT_INTERNALVERSION + datafile = wire_protocol_data.DATA_FILE_EXT_INTERNALVERSION else: config_version = '1.0.0' decision_version = '1.0.0' if autoupgrade: - datafile = mockwiredata.DATA_FILE_EXT_AUTOUPGRADE + datafile = wire_protocol_data.DATA_FILE_EXT_AUTOUPGRADE else: - datafile = mockwiredata.DATA_FILE + datafile = wire_protocol_data.DATA_FILE - _, protocol = self._create_mock(mockwiredata.WireProtocolData(datafile), *args) # pylint: disable=no-value-for-parameter + _, protocol = self._create_mock(wire_protocol_data.WireProtocolData(datafile), *args) # pylint: disable=no-value-for-parameter ext_handlers = protocol.get_goal_state().extensions_goal_state.extensions self.assertEqual(1, len(ext_handlers)) ext_handler = ext_handlers[0] @@ -1616,7 +1616,7 @@ def test_ext_handler_version_decide_between_minor_versions(self, *args): (None, '4.1', '4.1.0.0'), ] - _, protocol = self._create_mock(mockwiredata.WireProtocolData(mockwiredata.DATA_FILE), *args) # pylint: disable=no-value-for-parameter + _, protocol = self._create_mock(wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE), *args) # pylint: disable=no-value-for-parameter version_uri = 'http://mock-goal-state/Microsoft.OSTCExtensions_ExampleHandlerLinux_asiaeast_manifest.xml' for (installed_version, config_version, expected_version) in cases: @@ -1635,7 +1635,7 @@ def test_ext_handler_version_decide_between_minor_versions(self, *args): @patch('azurelinuxagent.common.conf.get_extensions_enabled', return_value=False) def test_extensions_disabled(self, _, *args): # test status is reported for no extensions - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_NO_EXT) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE_NO_EXT) exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter exthandlers_handler.run() @@ -1644,7 +1644,7 @@ def test_extensions_disabled(self, _, *args): self._assert_no_handler_status(protocol.report_vm_status) # test status is reported, but extensions are not processed - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE) exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter exthandlers_handler.run() exthandlers_handler.report_ext_handlers_status() @@ -1653,7 +1653,7 @@ def test_extensions_disabled(self, _, *args): def test_extensions_deleted(self, *args): # Ensure initial enable is successful - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_EXT_DELETION) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE_EXT_DELETION) exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter exthandlers_handler.run() @@ -1682,7 +1682,7 @@ def test_install_failure(self, patch_get_install_command, patch_install, *args): """ When extension install fails, the operation should not be retried. """ - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_EXT_SINGLE) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE_EXT_SINGLE) exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter # Ensure initial install is unsuccessful @@ -1700,7 +1700,7 @@ def test_install_failure_check_exception_handling(self, patch_get_install_comman """ When extension install fails, the operation should be reported to our telemetry service. """ - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_EXT_SINGLE) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE_EXT_SINGLE) exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter # Ensure install is unsuccessful @@ -1717,7 +1717,7 @@ def test_enable_failure_check_exception_handling(self, patch_get_enable_command, """ When extension enable fails, the operation should be reported. """ - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_EXT_SINGLE) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE_EXT_SINGLE) exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter # Ensure initial install is successful, but enable fails @@ -1737,7 +1737,7 @@ def test_disable_failure_with_exception_handling(self, patch_get_disable_command When extension disable fails, the operation should be reported. """ # Ensure initial install and enable is successful, but disable fails - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_EXT_SINGLE) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE_EXT_SINGLE) exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter patch_get_disable_command.call_count = 0 patch_get_disable_command.return_value = "exit 1" @@ -1768,7 +1768,7 @@ def test_uninstall_failure(self, patch_get_uninstall_command, *args): When extension uninstall fails, the operation should not be retried. """ # Ensure initial install and enable is successful, but uninstall fails - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_EXT_SINGLE) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE_EXT_SINGLE) exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter patch_get_uninstall_command.call_count = 0 patch_get_uninstall_command.return_value = "exit 1" @@ -2131,7 +2131,7 @@ def test_uninstall_rc_env_var_should_report_not_run_for_non_update_calls_to_exth self._assert_ext_status(protocol.report_vm_status, "success", 0) def test_ext_path_and_version_env_variables_set_for_ever_operation(self, *args): - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_EXT_SINGLE) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE_EXT_SINGLE) exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter with patch.object(CGroupConfigurator.get_instance(), "start_extension_command") as patch_start_cmd: @@ -2150,7 +2150,7 @@ def test_ext_path_and_version_env_variables_set_for_ever_operation(self, *args): @patch("azurelinuxagent.common.cgroupconfigurator.handle_process_completion", side_effect="Process Successful") def test_ext_sequence_no_should_be_set_for_every_command_call(self, _, *args): - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_MULTIPLE_EXT) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE_MULTIPLE_EXT) exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter with patch("subprocess.Popen") as patch_popen: @@ -2205,7 +2205,7 @@ def test_ext_sequence_no_should_be_set_from_within_extension(self, *args): os.mkdir(base_dir) self.create_script(os.path.join(base_dir, test_file_name), test_file) - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_EXT_SINGLE) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE_EXT_SINGLE) exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=unused-variable,no-value-for-parameter expected_seq_no = 0 @@ -2297,7 +2297,7 @@ def test_correct_exit_code_should_be_set_on_uninstall_cmd_failure(self, *args): self.assertIn("%s=%s" % (ExtCommandEnvVariable.UninstallReturnCode, exit_code), enable_kwargs['message']) def test_it_should_persist_goal_state_aggregate_status_until_new_incarnation(self, mock_get, mock_crypt_util, *args): - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE) exthandlers_handler, protocol = self._create_mock(test_data, mock_get, mock_crypt_util, *args) exthandlers_handler.run() @@ -2325,7 +2325,7 @@ def test_it_should_persist_goal_state_aggregate_status_until_new_incarnation(sel self.assertEqual(new_gs_aggregate_status.in_svd_seq_no, "2", "Incorrect seq no") def test_it_should_parse_required_features_properly(self, mock_get, mock_crypt_util, *args): - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_REQUIRED_FEATURES) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE_REQUIRED_FEATURES) _, protocol = self._create_mock(test_data, mock_get, mock_crypt_util, *args) required_features = protocol.get_goal_state().extensions_goal_state.required_features @@ -2334,7 +2334,7 @@ def test_it_should_parse_required_features_properly(self, mock_get, mock_crypt_u self.assertEqual(feature, "TestRequiredFeature{0}".format(i+1), "Name mismatch") def test_it_should_fail_goal_state_if_required_features_not_supported(self, mock_get, mock_crypt_util, *args): - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_REQUIRED_FEATURES) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE_REQUIRED_FEATURES) exthandlers_handler, protocol = self._create_mock(test_data, mock_get, mock_crypt_util, *args) exthandlers_handler.run() @@ -2355,7 +2355,7 @@ def test_it_should_fail_goal_state_if_required_features_not_supported(self, mock class TestExtensionSequencing(AgentTestCase): def _create_mock(self, mock_http_get, MockCryptUtil): - test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) + test_data = wire_protocol_data.WireProtocolData(wire_protocol_data.DATA_FILE) # Mock protocol to return test data mock_http_get.side_effect = test_data.mock_http_get @@ -3211,7 +3211,7 @@ def tearDown(self): @patch('time.gmtime', MagicMock(return_value=time.gmtime(0))) @patch("azurelinuxagent.common.version.get_daemon_version", return_value=FlexibleVersion("0.0.0.0")) def test_ext_handler_reporting_status_file(self, _): - with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE) as protocol: def mock_http_put(url, *args, **_): if HttpRequestPredicates.is_host_plugin_status_request(url): @@ -3346,7 +3346,7 @@ def http_get_handler(url, *_, **kwargs): mock_in_vm_artifacts_profile_response = MockHttpResponse(200, body='{ "onHold": false }'.encode('utf-8')) - with mock_wire_protocol(mockwiredata.DATA_FILE_IN_VM_ARTIFACTS_PROFILE, http_get_handler=http_get_handler) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE_IN_VM_ARTIFACTS_PROFILE, http_get_handler=http_get_handler) as protocol: protocol.report_vm_status = MagicMock() exthandlers_handler = get_exthandlers_handler(protocol) @@ -3388,7 +3388,7 @@ def http_get_handler(url, *_, **kwargs): def test_it_should_process_extensions_appropriately_on_artifact_hold(self): with patch('time.sleep', side_effect=lambda _: mock_sleep(0.001)): with patch("azurelinuxagent.common.conf.get_enable_overprovisioning", return_value=True): - with mock_wire_protocol(mockwiredata.DATA_FILE_IN_VM_ARTIFACTS_PROFILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE_IN_VM_ARTIFACTS_PROFILE) as protocol: protocol.report_vm_status = MagicMock() exthandlers_handler = get_exthandlers_handler(protocol) # @@ -3427,7 +3427,7 @@ def test_it_should_redact_access_tokens_in_extension_output(self): TWO:HTTPS://bar.blob.core.com/foo/bar/foo.txt? TWO https://bar.com/foo?uid=2018&sr=b THREE''' - with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE) as protocol: exthandlers_handler = get_exthandlers_handler(protocol) original_popen = subprocess.Popen diff --git a/tests/ga/test_exthandlers.py b/tests/ga/test_exthandlers.py index 67b0771779..10303ce7a1 100644 --- a/tests/ga/test_exthandlers.py +++ b/tests/ga/test_exthandlers.py @@ -32,8 +32,8 @@ read_output from azurelinuxagent.ga.exthandlers import parse_ext_status, ExtHandlerInstance, ExtCommandEnvVariable, \ ExtensionStatusError, _DEFAULT_SEQ_NO, get_exthandlers_handler, ExtHandlerState -from tests.protocol.mocks import mock_wire_protocol, mockwiredata -from tests.tools import AgentTestCase, patch, mock_sleep, clear_singleton_instances +from tests.lib.mock_wire_protocol import mock_wire_protocol, wire_protocol_data +from tests.lib.tools import AgentTestCase, patch, mock_sleep, clear_singleton_instances class TestExtHandlers(AgentTestCase): @@ -247,7 +247,7 @@ def test_extension_sequence_number(self): expected_sequence_number=-1) def test_it_should_report_error_if_plugin_settings_version_mismatch(self): - with mock_wire_protocol(mockwiredata.DATA_FILE_PLUGIN_SETTINGS_MISMATCH) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE_PLUGIN_SETTINGS_MISMATCH) as protocol: with patch("azurelinuxagent.common.protocol.extensions_goal_state_from_extensions_config.add_event") as mock_add_event: # Forcing update of GoalState to allow the ExtConfig to report an event protocol.mock_wire_data.set_incarnation(2) @@ -292,7 +292,7 @@ def heartbeat_with_message(): return {'code': 0, 'formattedMessage': {'lang': 'en-US', 'message': 'This is a heartbeat message'}, 'status': 'ready'} - with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE) as protocol: with patch("azurelinuxagent.common.protocol.wire.WireProtocol.report_vm_status", return_value=None): with patch("azurelinuxagent.ga.exthandlers.ExtHandlerInstance.collect_heartbeat", side_effect=heartbeat_with_message): diff --git a/tests/ga/test_exthandlers_download_extension.py b/tests/ga/test_exthandlers_download_extension.py index 556254fa3b..b3ed96a89a 100644 --- a/tests/ga/test_exthandlers_download_extension.py +++ b/tests/ga/test_exthandlers_download_extension.py @@ -10,9 +10,9 @@ from azurelinuxagent.common.protocol.restapi import Extension, ExtHandlerPackage from azurelinuxagent.common.protocol.wire import WireProtocol from azurelinuxagent.ga.exthandlers import ExtHandlerInstance, ExtHandlerState -from tests.protocol import mockwiredata -from tests.protocol.mocks import mock_wire_protocol -from tests.tools import AgentTestCase, patch, Mock +from tests.lib import wire_protocol_data +from tests.lib.mock_wire_protocol import mock_wire_protocol +from tests.lib.tools import AgentTestCase, patch, Mock class DownloadExtensionTestCase(AgentTestCase): @@ -42,7 +42,7 @@ def setUp(self): protocol.client.get_artifact_request = Mock(return_value=(None, None)) # create a dummy goal state, since downloads are done via the GoalState class - with mock_wire_protocol(mockwiredata.DATA_FILE) as p: + with mock_wire_protocol(wire_protocol_data.DATA_FILE) as p: goal_state = p.get_goal_state() goal_state._wire_client = protocol.client protocol.client._goal_state = goal_state diff --git a/tests/ga/test_exthandlers_exthandlerinstance.py b/tests/ga/test_exthandlers_exthandlerinstance.py index 6295d68d27..846bb89e92 100644 --- a/tests/ga/test_exthandlers_exthandlerinstance.py +++ b/tests/ga/test_exthandlers_exthandlerinstance.py @@ -7,7 +7,7 @@ from azurelinuxagent.common.protocol.restapi import Extension, ExtHandlerPackage from azurelinuxagent.ga.exthandlers import ExtHandlerInstance -from tests.tools import AgentTestCase, patch +from tests.lib.tools import AgentTestCase, patch class ExtHandlerInstanceTestCase(AgentTestCase): diff --git a/tests/ga/test_guestagent.py b/tests/ga/test_guestagent.py index 81e248bb04..a127341d15 100644 --- a/tests/ga/test_guestagent.py +++ b/tests/ga/test_guestagent.py @@ -9,9 +9,9 @@ from azurelinuxagent.common.protocol.restapi import ExtHandlerPackage from azurelinuxagent.common.version import AGENT_NAME from tests.ga.test_update import UpdateTestCase, EMPTY_MANIFEST, WITH_ERROR, NO_ERROR -from tests.protocol import mockwiredata -from tests.protocol.mocks import MockHttpResponse, mock_wire_protocol -from tests.tools import load_bin_data, patch +from tests.lib import wire_protocol_data +from tests.lib.mock_wire_protocol import MockHttpResponse, mock_wire_protocol +from tests.lib.tools import load_bin_data, patch class TestGuestAgent(UpdateTestCase): @@ -155,7 +155,7 @@ def http_get_handler(uri, *_, **__): pkg = ExtHandlerPackage(version=str(self._get_agent_version())) pkg.uris.append(agent_uri) - with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE) as protocol: protocol.set_http_handlers(http_get_handler=http_get_handler) agent = GuestAgent.from_agent_package(pkg, protocol, False) @@ -176,7 +176,7 @@ def http_get_handler(uri, *_, **__): pkg = ExtHandlerPackage(version=str(self._get_agent_version())) pkg.uris.append(agent_uri) - with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE) as protocol: protocol.set_http_handlers(http_get_handler=http_get_handler) with patch("azurelinuxagent.ga.guestagent.add_event") as add_event: agent = GuestAgent.from_agent_package(pkg, protocol, False) @@ -201,7 +201,7 @@ def http_get_handler(uri, *_, **__): pkg = ExtHandlerPackage(version="9.9.9.9") pkg.uris.append(agent_uri) - with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE) as protocol: protocol.set_http_handlers(http_get_handler=http_get_handler) agent = GuestAgent.from_agent_package(pkg, protocol, False) diff --git a/tests/ga/test_monitor.py b/tests/ga/test_monitor.py index 5853b23eff..9b11d81114 100644 --- a/tests/ga/test_monitor.py +++ b/tests/ga/test_monitor.py @@ -30,10 +30,10 @@ from azurelinuxagent.ga.monitor import get_monitor_handler, PeriodicOperation, SendImdsHeartbeat, \ ResetPeriodicLogMessages, SendHostPluginHeartbeat, PollResourceUsage, \ ReportNetworkErrors, ReportNetworkConfigurationChanges, PollSystemWideResourceUsage -from tests.protocol.mocks import mock_wire_protocol, MockHttpResponse -from tests.protocol.HttpRequestPredicates import HttpRequestPredicates -from tests.protocol.mockwiredata import DATA_FILE -from tests.tools import Mock, MagicMock, patch, AgentTestCase, clear_singleton_instances +from tests.lib.mock_wire_protocol import mock_wire_protocol, MockHttpResponse +from tests.lib.http_request_predicates import HttpRequestPredicates +from tests.lib.wire_protocol_data import DATA_FILE +from tests.lib.tools import Mock, MagicMock, patch, AgentTestCase, clear_singleton_instances def random_generator(size=6, chars=string.ascii_uppercase + string.digits + string.ascii_lowercase): diff --git a/tests/ga/test_multi_config_extension.py b/tests/ga/test_multi_config_extension.py index 365052f5d4..66b366fd0a 100644 --- a/tests/ga/test_multi_config_extension.py +++ b/tests/ga/test_multi_config_extension.py @@ -13,12 +13,12 @@ from azurelinuxagent.common.utils import fileutil from azurelinuxagent.ga.exthandlers import get_exthandlers_handler, ExtensionStatusValue, ExtCommandEnvVariable, \ GoalStateStatus, ExtHandlerInstance -from tests.ga.extension_emulator import enable_invocations, extension_emulator, ExtensionCommandNames, Actions, \ +from tests.lib.extension_emulator import enable_invocations, extension_emulator, ExtensionCommandNames, Actions, \ extract_extension_info_from_command -from tests.protocol.mocks import mock_wire_protocol, MockHttpResponse -from tests.protocol.HttpRequestPredicates import HttpRequestPredicates -from tests.protocol.mockwiredata import DATA_FILE, WireProtocolData -from tests.tools import AgentTestCase, mock_sleep, patch +from tests.lib.mock_wire_protocol import mock_wire_protocol, MockHttpResponse +from tests.lib.http_request_predicates import HttpRequestPredicates +from tests.lib.wire_protocol_data import DATA_FILE, WireProtocolData +from tests.lib.tools import AgentTestCase, mock_sleep, patch class TestMultiConfigExtensionsConfigParsing(AgentTestCase): diff --git a/tests/ga/test_periodic_operation.py b/tests/ga/test_periodic_operation.py index 8fd8d32dc8..65980a1470 100644 --- a/tests/ga/test_periodic_operation.py +++ b/tests/ga/test_periodic_operation.py @@ -17,7 +17,7 @@ import datetime import time from azurelinuxagent.ga.monitor import PeriodicOperation -from tests.tools import AgentTestCase, patch, PropertyMock +from tests.lib.tools import AgentTestCase, patch, PropertyMock class TestPeriodicOperation(AgentTestCase): diff --git a/tests/ga/test_remoteaccess.py b/tests/ga/test_remoteaccess.py index 069931a157..f0e2ff2665 100644 --- a/tests/ga/test_remoteaccess.py +++ b/tests/ga/test_remoteaccess.py @@ -17,9 +17,9 @@ import xml from azurelinuxagent.common.protocol.goal_state import GoalState, RemoteAccess # pylint: disable=unused-import -from tests.tools import AgentTestCase, load_data, patch, Mock # pylint: disable=unused-import -from tests.protocol import mockwiredata -from tests.protocol.mocks import mock_wire_protocol +from tests.lib.tools import AgentTestCase, load_data, patch, Mock # pylint: disable=unused-import +from tests.lib import wire_protocol_data +from tests.lib.mock_wire_protocol import mock_wire_protocol class TestRemoteAccess(AgentTestCase): @@ -34,7 +34,7 @@ def test_parse_remote_access(self): self.assertEqual("2019-01-01", remote_access.user_list.users[0].expiration, "Expiration does not match.") def test_goal_state_with_no_remote_access(self): - with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE) as protocol: self.assertIsNone(protocol.client.get_remote_access()) def test_parse_two_remote_access_accounts(self): @@ -75,7 +75,7 @@ def test_parse_zero_remote_access_accounts(self): self.assertEqual(0, len(remote_access.user_list.users), "User count does not match.") def test_update_remote_access_conf_remote_access(self): - with mock_wire_protocol(mockwiredata.DATA_FILE_REMOTE_ACCESS) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE_REMOTE_ACCESS) as protocol: self.assertIsNotNone(protocol.client.get_remote_access()) self.assertEqual(1, len(protocol.client.get_remote_access().user_list.users)) self.assertEqual('testAccount', protocol.client.get_remote_access().user_list.users[0].name) diff --git a/tests/ga/test_remoteaccess_handler.py b/tests/ga/test_remoteaccess_handler.py index 37187702e3..d4f1579260 100644 --- a/tests/ga/test_remoteaccess_handler.py +++ b/tests/ga/test_remoteaccess_handler.py @@ -22,9 +22,9 @@ from azurelinuxagent.common.protocol.util import ProtocolUtil from azurelinuxagent.common.protocol.wire import WireProtocol from azurelinuxagent.ga.remoteaccess import RemoteAccessHandler -from tests.tools import AgentTestCase, load_data, patch, clear_singleton_instances -from tests.protocol.mocks import mock_wire_protocol -from tests.protocol.mockwiredata import DATA_FILE, DATA_FILE_REMOTE_ACCESS +from tests.lib.tools import AgentTestCase, load_data, patch, clear_singleton_instances +from tests.lib.mock_wire_protocol import mock_wire_protocol +from tests.lib.wire_protocol_data import DATA_FILE, DATA_FILE_REMOTE_ACCESS class MockOSUtil(DefaultOSUtil): diff --git a/tests/ga/test_report_status.py b/tests/ga/test_report_status.py index 1dcfe33edc..370bcb60f8 100644 --- a/tests/ga/test_report_status.py +++ b/tests/ga/test_report_status.py @@ -7,11 +7,11 @@ from azurelinuxagent.ga.agent_update_handler import get_agent_update_handler from azurelinuxagent.ga.exthandlers import ExtHandlersHandler from azurelinuxagent.ga.update import get_update_handler -from tests.ga.mocks import mock_update_handler -from tests.protocol.mocks import mock_wire_protocol, MockHttpResponse -from tests.tools import AgentTestCase, patch -from tests.protocol import mockwiredata -from tests.protocol.HttpRequestPredicates import HttpRequestPredicates +from tests.lib.mock_update_handler import mock_update_handler +from tests.lib.mock_wire_protocol import mock_wire_protocol, MockHttpResponse +from tests.lib.tools import AgentTestCase, patch +from tests.lib import wire_protocol_data +from tests.lib.http_request_predicates import HttpRequestPredicates class ReportStatusTestCase(AgentTestCase): @@ -32,7 +32,7 @@ def http_get_handler(url, *_, **__): def on_new_iteration(iteration): fail_goal_state_request[0] = iteration == 2 - with mock_wire_protocol(mockwiredata.DATA_FILE, http_get_handler=http_get_handler) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE, http_get_handler=http_get_handler) as protocol: exthandlers_handler = ExtHandlersHandler(protocol) with patch.object(exthandlers_handler, "run", wraps=exthandlers_handler.run) as exthandlers_handler_run: with mock_update_handler(protocol, iterations=2, on_new_iteration=on_new_iteration, exthandlers_handler=exthandlers_handler) as update_handler: @@ -75,7 +75,7 @@ def remove_timestamps(x): self.assertEqual(first_status, second_status) def test_report_status_should_log_errors_only_once_per_goal_state(self): - with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE) as protocol: with patch("azurelinuxagent.common.conf.get_autoupdate_enabled", return_value=False): # skip agent update with patch("azurelinuxagent.ga.update.logger.warn") as logger_warn: with patch("azurelinuxagent.common.version.get_daemon_version", return_value=FlexibleVersion("2.2.53")): @@ -101,7 +101,7 @@ def test_report_status_should_log_errors_only_once_per_goal_state(self): self.assertEqual(2, len(get_warnings()), "UpdateHandler._report_status() should continue reporting errors after a new goal state") def test_update_handler_should_add_fast_track_to_supported_features_when_it_is_supported(self): - with mock_wire_protocol(mockwiredata.DATA_FILE_VM_SETTINGS) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE_VM_SETTINGS) as protocol: self._test_supported_features_includes_fast_track(protocol, True) def test_update_handler_should_not_add_fast_track_to_supported_features_when_it_is_not_supported(self): @@ -110,7 +110,7 @@ def http_get_handler(url, *_, **__): return MockHttpResponse(status=404) return None - with mock_wire_protocol(mockwiredata.DATA_FILE_VM_SETTINGS, http_get_handler=http_get_handler) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE_VM_SETTINGS, http_get_handler=http_get_handler) as protocol: self._test_supported_features_includes_fast_track(protocol, False) def _test_supported_features_includes_fast_track(self, protocol, expected): diff --git a/tests/ga/test_send_telemetry_events.py b/tests/ga/test_send_telemetry_events.py index 005a07b09c..c9e04a38ca 100644 --- a/tests/ga/test_send_telemetry_events.py +++ b/tests/ga/test_send_telemetry_events.py @@ -42,11 +42,11 @@ from azurelinuxagent.ga.collect_telemetry_events import _CollectAndEnqueueEvents from azurelinuxagent.ga.send_telemetry_events import get_send_telemetry_events_handler from tests.ga.test_monitor import random_generator -from tests.protocol.mocks import MockHttpResponse, mock_wire_protocol -from tests.protocol.HttpRequestPredicates import HttpRequestPredicates -from tests.protocol.mockwiredata import DATA_FILE -from tests.tools import AgentTestCase, clear_singleton_instances, mock_sleep -from tests.utils.event_logger_tools import EventLoggerTools +from tests.lib.mock_wire_protocol import MockHttpResponse, mock_wire_protocol +from tests.lib.http_request_predicates import HttpRequestPredicates +from tests.lib.wire_protocol_data import DATA_FILE +from tests.lib.tools import AgentTestCase, clear_singleton_instances, mock_sleep +from tests.lib.event_logger_tools import EventLoggerTools class TestSendTelemetryEventsHandler(AgentTestCase, HttpRequestPredicates): diff --git a/tests/ga/test_update.py b/tests/ga/test_update.py index e342fec7d3..ef2e3e66e5 100644 --- a/tests/ga/test_update.py +++ b/tests/ga/test_update.py @@ -49,13 +49,13 @@ get_update_handler, ORPHAN_POLL_INTERVAL, AGENT_PARTITION_FILE, ORPHAN_WAIT_INTERVAL, \ CHILD_LAUNCH_RESTART_MAX, CHILD_HEALTH_INTERVAL, GOAL_STATE_PERIOD_EXTENSIONS_DISABLED, UpdateHandler, \ READONLY_FILE_GLOBS, ExtensionsSummary -from tests.ga.mocks import mock_update_handler -from tests.protocol.mocks import mock_wire_protocol, MockHttpResponse -from tests.protocol.mockwiredata import DATA_FILE, DATA_FILE_MULTIPLE_EXT, DATA_FILE_VM_SETTINGS -from tests.tools import AgentTestCase, AgentTestCaseWithGetVmSizeMock, data_dir, DEFAULT, patch, load_bin_data, Mock, MagicMock, \ +from tests.lib.mock_update_handler import mock_update_handler +from tests.lib.mock_wire_protocol import mock_wire_protocol, MockHttpResponse +from tests.lib.wire_protocol_data import DATA_FILE, DATA_FILE_MULTIPLE_EXT, DATA_FILE_VM_SETTINGS +from tests.lib.tools import AgentTestCase, AgentTestCaseWithGetVmSizeMock, data_dir, DEFAULT, patch, load_bin_data, Mock, MagicMock, \ clear_singleton_instances, is_python_version_26_or_34, skip_if_predicate_true -from tests.protocol import mockwiredata -from tests.protocol.HttpRequestPredicates import HttpRequestPredicates +from tests.lib import wire_protocol_data +from tests.lib.http_request_predicates import HttpRequestPredicates NO_ERROR = { @@ -1504,7 +1504,7 @@ def __assert_ga_version_in_status(self, aggregate_status, version=str(CURRENT_VE "Guest Agent should be reported as Ready") def test_it_should_upgrade_agent_on_process_start_if_auto_upgrade_enabled(self): - data_file = mockwiredata.DATA_FILE.copy() + data_file = wire_protocol_data.DATA_FILE.copy() data_file["ext_conf"] = "wire/ext_conf_requested_version.xml" with self.__get_update_handler(test_data=data_file, iterations=10) as (update_handler, mock_telemetry): update_handler.run(debug=True) @@ -1543,7 +1543,7 @@ def test_it_should_not_auto_upgrade_if_auto_update_disabled(self): "New agent directory should not be found") def test_it_should_download_only_requested_version_if_available(self): - data_file = mockwiredata.DATA_FILE.copy() + data_file = wire_protocol_data.DATA_FILE.copy() data_file["ext_conf"] = "wire/ext_conf_requested_version.xml" with self.__get_update_handler(test_data=data_file) as (update_handler, mock_telemetry): update_handler.run(debug=True) @@ -1553,7 +1553,7 @@ def test_it_should_download_only_requested_version_if_available(self): self.__assert_agent_directories_exist_and_others_dont_exist(versions=["9.9.9.10"]) def test_it_should_download_largest_version_if_ga_versioning_disabled(self): - data_file = mockwiredata.DATA_FILE.copy() + data_file = wire_protocol_data.DATA_FILE.copy() data_file["ext_conf"] = "wire/ext_conf_requested_version.xml" with self.__get_update_handler(test_data=data_file) as (update_handler, mock_telemetry): with patch.object(conf, "get_enable_ga_versioning", return_value=False): @@ -1564,7 +1564,7 @@ def test_it_should_download_largest_version_if_ga_versioning_disabled(self): self.__assert_agent_directories_exist_and_others_dont_exist(versions=["99999.0.0.0"]) def test_it_should_cleanup_all_agents_except_requested_version_and_current_version(self): - data_file = mockwiredata.DATA_FILE.copy() + data_file = wire_protocol_data.DATA_FILE.copy() data_file["ext_conf"] = "wire/ext_conf_requested_version.xml" # Set the test environment by adding 20 random agents to the agent directory @@ -1580,7 +1580,7 @@ def test_it_should_cleanup_all_agents_except_requested_version_and_current_versi def test_it_should_not_update_if_requested_version_not_found_in_manifest(self): self.prepare_agents(1) - data_file = mockwiredata.DATA_FILE.copy() + data_file = wire_protocol_data.DATA_FILE.copy() data_file["ext_conf"] = "wire/ext_conf_requested_version_missing_in_manifest.xml" with self.__get_update_handler(test_data=data_file) as (update_handler, mock_telemetry): update_handler.run(debug=True) @@ -1628,7 +1628,7 @@ def reload_conf(url, protocol): reload_conf.call_count = 0 reload_conf.incarnation = 2 - data_file = mockwiredata.DATA_FILE.copy() + data_file = wire_protocol_data.DATA_FILE.copy() data_file["ext_conf"] = "wire/ext_conf_requested_version.xml" with self.__get_update_handler(iterations=no_of_iterations, test_data=data_file, reload_conf=reload_conf) as (update_handler, mock_telemetry): update_handler._protocol.mock_wire_data.set_extension_config_requested_version(str(CURRENT_VERSION)) @@ -1673,7 +1673,7 @@ def reload_conf(url, protocol): reload_conf.call_count = 0 reload_conf.incarnation = 2 - data_file = mockwiredata.DATA_FILE.copy() + data_file = wire_protocol_data.DATA_FILE.copy() data_file["ext_conf"] = "wire/ext_conf_requested_version.xml" with self.__get_update_handler(iterations=no_of_iterations, test_data=data_file, reload_conf=reload_conf) as (update_handler, mock_telemetry): update_handler._protocol.mock_wire_data.set_extension_config_requested_version(str(CURRENT_VERSION)) @@ -1712,7 +1712,7 @@ def reload_conf(url, protocol): reload_conf.call_count = 0 reload_conf.incarnation = 2 - data_file = mockwiredata.DATA_FILE.copy() + data_file = wire_protocol_data.DATA_FILE.copy() # This is to fail the agent update at first attempt so that agent doesn't go through update data_file["ga_manifest"] = "wire/ga_manifest_no_uris.xml" with self.__get_update_handler(iterations=no_of_iterations, test_data=data_file, reload_conf=reload_conf, @@ -1752,7 +1752,7 @@ def reload_conf(url, protocol): reload_conf.call_count = 0 reload_conf.incarnation = 2 - data_file = mockwiredata.DATA_FILE.copy() + data_file = wire_protocol_data.DATA_FILE.copy() data_file["ga_manifest"] = "wire/ga_manifest_no_uris.xml" with self.__get_update_handler(iterations=no_of_iterations, test_data=data_file, reload_conf=reload_conf, hotfix_frequency=0.001, normal_frequency=0.001) as (update_handler, mock_telemetry): @@ -1765,7 +1765,7 @@ def reload_conf(url, protocol): self.__assert_agent_directories_exist_and_others_dont_exist(versions=["99999.0.0.0", str(CURRENT_VERSION)]) def test_it_should_not_download_anything_if_requested_version_is_current_version(self): - data_file = mockwiredata.DATA_FILE.copy() + data_file = wire_protocol_data.DATA_FILE.copy() data_file["ext_conf"] = "wire/ext_conf_requested_version.xml" # Set the test environment by adding 20 random agents to the agent directory @@ -1803,7 +1803,7 @@ def reload_conf(url, protocol): reload_conf.call_count = 0 - data_file = mockwiredata.DATA_FILE.copy() + data_file = wire_protocol_data.DATA_FILE.copy() data_file['ga_manifest'] = "wire/ga_manifest_no_upgrade.xml" with self.__get_update_handler(iterations=no_of_iterations, test_data=data_file, reload_conf=reload_conf) as (update_handler, mock_telemetry): update_handler._protocol.mock_wire_data.set_ga_manifest_version_version(str(CURRENT_VERSION)) @@ -1824,7 +1824,7 @@ def test_it_should_mark_current_agent_as_bad_version_on_downgrade(self): "The current agent should not be blacklisted") downgraded_version = "2.5.0" - data_file = mockwiredata.DATA_FILE.copy() + data_file = wire_protocol_data.DATA_FILE.copy() data_file["ext_conf"] = "wire/ext_conf_requested_version.xml" with self.__get_update_handler(test_data=data_file) as (update_handler, mock_telemetry): update_handler._protocol.mock_wire_data.set_extension_config_requested_version(downgraded_version) @@ -2047,11 +2047,11 @@ class TryUpdateGoalStateTestCase(HttpRequestPredicates, AgentTestCase): """ def test_it_should_return_true_on_success(self): update_handler = get_update_handler() - with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE) as protocol: self.assertTrue(update_handler._try_update_goal_state(protocol), "try_update_goal_state should have succeeded") def test_it_should_return_false_on_failure(self): - with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE) as protocol: def http_get_handler(url, *_, **__): if self.is_goal_state_request(url): return HttpError('Exception to fake an error retrieving the goal state') @@ -2063,7 +2063,7 @@ def http_get_handler(url, *_, **__): def test_it_should_update_the_goal_state(self): update_handler = get_update_handler() - with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE) as protocol: protocol.mock_wire_data.set_incarnation(12345) # the first goal state should produce an update @@ -2080,7 +2080,7 @@ def test_it_should_update_the_goal_state(self): self.assertEqual(update_handler._goal_state.incarnation, '6789', "The goal state was not updated (received unexpected incarnation)") def test_it_should_log_errors_only_when_the_error_state_changes(self): - with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE) as protocol: def http_get_handler(url, *_, **__): if self.is_goal_state_request(url): if fail_goal_state_request: @@ -2259,7 +2259,7 @@ def _prepare_fast_track_goal_state(): invokes HostPluginProtocol.fetch_vm_settings() to save the Fast Track status to disk """ # Do a query for the vmSettings; this would retrieve a FastTrack goal state and keep track of its timestamp - mock_wire_data_file = mockwiredata.DATA_FILE_VM_SETTINGS.copy() + mock_wire_data_file = wire_protocol_data.DATA_FILE_VM_SETTINGS.copy() with mock_wire_protocol(mock_wire_data_file) as protocol: protocol.mock_wire_data.set_etag("0123456789") _ = protocol.client.get_host_plugin().fetch_vm_settings() @@ -2359,7 +2359,7 @@ class HeartbeatTestCase(AgentTestCase): @patch("azurelinuxagent.ga.update.add_event") def test_telemetry_heartbeat_creates_event(self, patch_add_event, patch_info, *_): - with mock_wire_protocol(mockwiredata.DATA_FILE) as mock_protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE) as mock_protocol: update_handler = get_update_handler() update_handler.last_telemetry_heartbeat = datetime.utcnow() - timedelta(hours=1) diff --git a/tests/utils/cgroups_tools.py b/tests/lib/cgroups_tools.py similarity index 100% rename from tests/utils/cgroups_tools.py rename to tests/lib/cgroups_tools.py diff --git a/tests/utils/event_logger_tools.py b/tests/lib/event_logger_tools.py similarity index 89% rename from tests/utils/event_logger_tools.py rename to tests/lib/event_logger_tools.py index 626d71d9ef..5150cebd54 100644 --- a/tests/utils/event_logger_tools.py +++ b/tests/lib/event_logger_tools.py @@ -19,9 +19,9 @@ import platform import azurelinuxagent.common.event as event from azurelinuxagent.common.version import DISTRO_NAME, DISTRO_VERSION, DISTRO_CODE_NAME -import tests.tools as tools -from tests.protocol import mockwiredata -from tests.protocol.mocks import mock_wire_protocol +import tests.lib.tools as tools +from tests.lib import wire_protocol_data +from tests.lib.mock_wire_protocol import mock_wire_protocol class EventLoggerTools(object): @@ -37,7 +37,7 @@ class EventLoggerTools(object): def initialize_event_logger(event_dir): """ Initializes the event logger using mock data for the common parameters; the goal state fields are taken - from mockwiredata.DATA_FILE and the IMDS fields from mock_imds_data. + from wire_protocol_data.DATA_FILE and the IMDS fields from mock_imds_data. """ if not os.path.exists(event_dir): os.mkdir(event_dir) @@ -53,7 +53,7 @@ def initialize_event_logger(event_dir): mock_imds_client = tools.Mock() mock_imds_client.get_compute = tools.Mock(return_value=mock_imds_info) - with mock_wire_protocol(mockwiredata.DATA_FILE) as mock_protocol: + with mock_wire_protocol(wire_protocol_data.DATA_FILE) as mock_protocol: with tools.patch("azurelinuxagent.common.event.get_imds_client", return_value=mock_imds_client): event.initialize_event_logger_vminfo_common_parameters(mock_protocol) diff --git a/tests/ga/extension_emulator.py b/tests/lib/extension_emulator.py similarity index 98% rename from tests/ga/extension_emulator.py rename to tests/lib/extension_emulator.py index dafd365dff..5b4f69d142 100644 --- a/tests/ga/extension_emulator.py +++ b/tests/lib/extension_emulator.py @@ -27,10 +27,10 @@ from azurelinuxagent.common.utils import fileutil from azurelinuxagent.ga.exthandlers import ExtHandlerInstance, ExtCommandEnvVariable -from tests.tools import Mock, patch -from tests.protocol.mockwiredata import WireProtocolData -from tests.protocol.mocks import MockHttpResponse -from tests.protocol.HttpRequestPredicates import HttpRequestPredicates +from tests.lib.tools import Mock, patch +from tests.lib.wire_protocol_data import WireProtocolData +from tests.lib.mock_wire_protocol import MockHttpResponse +from tests.lib.http_request_predicates import HttpRequestPredicates class ExtensionCommandNames(object): @@ -107,7 +107,7 @@ def enable_invocations(*emulators): def generate_put_handler(*emulators): """ Create a HTTP handler to store status blobs for each provided emulator. - For use with tests.protocol.mocks.mock_wire_protocol. + For use with tests.lib.mocks.mock_wire_protocol. """ def mock_put_handler(url, *args, **_): diff --git a/tests/protocol/HttpRequestPredicates.py b/tests/lib/http_request_predicates.py similarity index 100% rename from tests/protocol/HttpRequestPredicates.py rename to tests/lib/http_request_predicates.py diff --git a/tests/utils/miscellaneous_tools.py b/tests/lib/miscellaneous_tools.py similarity index 100% rename from tests/utils/miscellaneous_tools.py rename to tests/lib/miscellaneous_tools.py diff --git a/tests/ga/mocks.py b/tests/lib/mock_update_handler.py similarity index 99% rename from tests/ga/mocks.py rename to tests/lib/mock_update_handler.py index 588825f780..f0b311abe2 100644 --- a/tests/ga/mocks.py +++ b/tests/lib/mock_update_handler.py @@ -23,7 +23,7 @@ from azurelinuxagent.ga.exthandlers import ExtHandlersHandler from azurelinuxagent.ga.remoteaccess import RemoteAccessHandler from azurelinuxagent.ga.update import UpdateHandler, get_update_handler -from tests.tools import patch, Mock, mock_sleep +from tests.lib.tools import patch, Mock, mock_sleep @contextlib.contextmanager diff --git a/tests/protocol/mocks.py b/tests/lib/mock_wire_protocol.py similarity index 96% rename from tests/protocol/mocks.py rename to tests/lib/mock_wire_protocol.py index b74138888b..4e3521fef2 100644 --- a/tests/protocol/mocks.py +++ b/tests/lib/mock_wire_protocol.py @@ -17,18 +17,18 @@ import contextlib from azurelinuxagent.common.protocol.wire import WireProtocol from azurelinuxagent.common.utils import restutil -from tests.tools import patch -from tests.protocol import mockwiredata +from tests.lib.tools import patch +from tests.lib import wire_protocol_data @contextlib.contextmanager def mock_wire_protocol(mock_wire_data_file, http_get_handler=None, http_post_handler=None, http_put_handler=None, do_not_mock=lambda method, url: False, fail_on_unknown_request=True): """ Creates a WireProtocol object that handles requests to the WireServer, the Host GA Plugin, and some requests to storage (requests that provide mock data - in mockwiredata.py). + in wire_protocol_data.py). The data returned by those requests is read from the files specified by 'mock_wire_data_file' (which must follow the structure of the data - files defined in tests/protocol/mockwiredata.py). + files defined in tests/protocol/wire_protocol_data.py). The caller can also provide handler functions for specific HTTP methods using the http_*_handler arguments. The return value of the handler function is interpreted similarly to the "return_value" argument of patch(): if it is an exception the exception is raised or, if it is @@ -135,7 +135,7 @@ def stop(): # create the protocol object # protocol = WireProtocol(restutil.KNOWN_WIRESERVER_IP) - protocol.mock_wire_data = mockwiredata.WireProtocolData(mock_wire_data_file) + protocol.mock_wire_data = wire_protocol_data.WireProtocolData(mock_wire_data_file) protocol.start = start protocol.stop = stop protocol.track_url = lambda url: tracked_urls.append(url) # pylint: disable=unnecessary-lambda diff --git a/tests/tools.py b/tests/lib/tools.py similarity index 99% rename from tests/tools.py rename to tests/lib/tools.py index 85d460d374..008be8552a 100644 --- a/tests/tools.py +++ b/tests/lib/tools.py @@ -38,6 +38,8 @@ from azurelinuxagent.common.utils import fileutil from azurelinuxagent.common.version import PY_VERSION_MAJOR +import tests + try: from unittest.mock import Mock, patch, MagicMock, ANY, DEFAULT, call, PropertyMock # pylint: disable=unused-import @@ -46,7 +48,7 @@ except ImportError: from mock import Mock, patch, MagicMock, ANY, DEFAULT, call, PropertyMock -test_dir = os.path.dirname(os.path.abspath(__file__)) +test_dir = tests.__path__[0] data_dir = os.path.join(test_dir, "data") debug = False diff --git a/tests/protocol/mockwiredata.py b/tests/lib/wire_protocol_data.py similarity index 99% rename from tests/protocol/mockwiredata.py rename to tests/lib/wire_protocol_data.py index c3beabf566..2bc18e34f1 100644 --- a/tests/protocol/mockwiredata.py +++ b/tests/lib/wire_protocol_data.py @@ -21,8 +21,8 @@ from azurelinuxagent.common.utils import timeutil from azurelinuxagent.common.utils.textutil import parse_doc, find, findall -from tests.protocol.HttpRequestPredicates import HttpRequestPredicates -from tests.tools import load_bin_data, load_data, MagicMock, Mock +from tests.lib.http_request_predicates import HttpRequestPredicates +from tests.lib.tools import load_bin_data, load_data, MagicMock, Mock from azurelinuxagent.common.protocol.imds import IMDS_ENDPOINT from azurelinuxagent.common.exception import HttpError, ResourceGoneError from azurelinuxagent.common.future import httpclient diff --git a/tests/pa/test_deprovision.py b/tests/pa/test_deprovision.py index 8680366a30..9970a249e4 100644 --- a/tests/pa/test_deprovision.py +++ b/tests/pa/test_deprovision.py @@ -23,7 +23,7 @@ from azurelinuxagent.pa.deprovision import get_deprovision_handler from azurelinuxagent.pa.deprovision.default import DeprovisionHandler -from tests.tools import AgentTestCase, distros, Mock, patch +from tests.lib.tools import AgentTestCase, distros, Mock, patch class TestDeprovision(AgentTestCase): diff --git a/tests/pa/test_provision.py b/tests/pa/test_provision.py index 59de0e97b3..66c525dff4 100644 --- a/tests/pa/test_provision.py +++ b/tests/pa/test_provision.py @@ -28,7 +28,7 @@ from azurelinuxagent.pa.provision.cloudinit import CloudInitProvisionHandler from azurelinuxagent.pa.provision.default import ProvisionHandler from azurelinuxagent.common.utils import fileutil -from tests.tools import AgentTestCase, distros, load_data, MagicMock, Mock, patch +from tests.lib.tools import AgentTestCase, distros, load_data, MagicMock, Mock, patch class TestProvision(AgentTestCase): diff --git a/tests/test_agent.py b/tests/test_agent.py index abbc090ba8..14053de41f 100644 --- a/tests/test_agent.py +++ b/tests/test_agent.py @@ -22,7 +22,7 @@ from azurelinuxagent.common.cgroupapi import SystemdCgroupsApi from azurelinuxagent.common.utils import fileutil from azurelinuxagent.ga.collect_logs import CollectLogsHandler -from tests.tools import AgentTestCase, data_dir, Mock, patch +from tests.lib.tools import AgentTestCase, data_dir, Mock, patch EXPECTED_CONFIGURATION = \ """AutoUpdate.Enabled = True diff --git a/tests/utils/__init__.py b/tests/utils/__init__.py deleted file mode 100644 index 24121bff93..0000000000 --- a/tests/utils/__init__.py +++ /dev/null @@ -1,16 +0,0 @@ -# Copyright 2018 Microsoft Corporation -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -# Requires Python 2.6+ and Openssl 1.0+ -# From cec41975928fc3cf6610006a8735a72ed7659347 Mon Sep 17 00:00:00 2001 From: narrieta Date: Wed, 9 Aug 2023 08:00:24 -0700 Subject: [PATCH 2/4] remove duplicate --- tests/daemon/test_resourcedisk.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/daemon/test_resourcedisk.py b/tests/daemon/test_resourcedisk.py index c072d59001..0927414424 100644 --- a/tests/daemon/test_resourcedisk.py +++ b/tests/daemon/test_resourcedisk.py @@ -85,7 +85,6 @@ def run_side_effect(*args, **kwargs): # pylint: disable=unused-argument size_mb=size_mb ) -class TestResourceDisk(AgentTestCase): def test_mkfile(self): # setup test_file = os.path.join(self.tmp_dir, 'test_file') From c487965e585240baf7271e32e43027044d0d8858 Mon Sep 17 00:00:00 2001 From: narrieta Date: Wed, 9 Aug 2023 09:24:40 -0700 Subject: [PATCH 3/4] add init --- tests/lib/__init__.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 tests/lib/__init__.py diff --git a/tests/lib/__init__.py b/tests/lib/__init__.py new file mode 100644 index 0000000000..24121bff93 --- /dev/null +++ b/tests/lib/__init__.py @@ -0,0 +1,16 @@ +# Copyright 2018 Microsoft Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Requires Python 2.6+ and Openssl 1.0+ +# From 1c7640e436362b86ad1c54ee14842a6ffa3baf9f Mon Sep 17 00:00:00 2001 From: narrieta Date: Wed, 9 Aug 2023 09:46:00 -0700 Subject: [PATCH 4/4] mocks --- tests/common/osutil/test_default.py | 2 +- tests/common/test_cgroupapi.py | 2 +- tests/common/test_cgroupconfigurator.py | 4 ++-- tests/{common => lib}/mock_cgroup_environment.py | 3 ++- tests/{common => lib}/mock_command.py | 0 tests/{common => lib}/mock_environment.py | 0 6 files changed, 6 insertions(+), 5 deletions(-) rename tests/{common => lib}/mock_cgroup_environment.py (98%) rename tests/{common => lib}/mock_command.py (100%) rename tests/{common => lib}/mock_environment.py (100%) diff --git a/tests/common/osutil/test_default.py b/tests/common/osutil/test_default.py index a89bef35d6..42a7050522 100644 --- a/tests/common/osutil/test_default.py +++ b/tests/common/osutil/test_default.py @@ -34,7 +34,7 @@ from azurelinuxagent.common.utils import fileutil from azurelinuxagent.common.utils.flexible_version import FlexibleVersion from azurelinuxagent.common.utils.networkutil import AddFirewallRules -from tests.common.mock_environment import MockEnvironment +from tests.lib.mock_environment import MockEnvironment from tests.lib.tools import AgentTestCase, patch, open_patch, load_data, data_dir, is_python_version_26_or_34, skip_if_predicate_true actual_get_proc_net_route = 'azurelinuxagent.common.osutil.default.DefaultOSUtil._get_proc_net_route' diff --git a/tests/common/test_cgroupapi.py b/tests/common/test_cgroupapi.py index 7715d3cf8c..a7b47b44f6 100644 --- a/tests/common/test_cgroupapi.py +++ b/tests/common/test_cgroupapi.py @@ -26,7 +26,7 @@ from azurelinuxagent.common.cgroupstelemetry import CGroupsTelemetry from azurelinuxagent.common.osutil import systemd from azurelinuxagent.common.utils import fileutil -from tests.common.mock_cgroup_environment import mock_cgroup_environment +from tests.lib.mock_cgroup_environment import mock_cgroup_environment from tests.lib.tools import AgentTestCase, patch, mock_sleep from tests.lib.cgroups_tools import CGroupsTools diff --git a/tests/common/test_cgroupconfigurator.py b/tests/common/test_cgroupconfigurator.py index 2713b445d6..ebf7bac0e0 100644 --- a/tests/common/test_cgroupconfigurator.py +++ b/tests/common/test_cgroupconfigurator.py @@ -37,8 +37,8 @@ AgentMemoryExceededException from azurelinuxagent.common.future import ustr from azurelinuxagent.common.utils import shellutil, fileutil -from tests.common.mock_environment import MockCommand -from tests.common.mock_cgroup_environment import mock_cgroup_environment, UnitFilePaths +from tests.lib.mock_environment import MockCommand +from tests.lib.mock_cgroup_environment import mock_cgroup_environment, UnitFilePaths from tests.lib.tools import AgentTestCase, patch, mock_sleep, i_am_root, data_dir, is_python_version_26_or_34, skip_if_predicate_true from tests.lib.miscellaneous_tools import format_processes, wait_for diff --git a/tests/common/mock_cgroup_environment.py b/tests/lib/mock_cgroup_environment.py similarity index 98% rename from tests/common/mock_cgroup_environment.py rename to tests/lib/mock_cgroup_environment.py index 03c0d02bf6..408e1c15cc 100644 --- a/tests/common/mock_cgroup_environment.py +++ b/tests/lib/mock_cgroup_environment.py @@ -18,7 +18,7 @@ import contextlib import os from tests.lib.tools import patch, data_dir -from tests.common.mock_environment import MockEnvironment, MockCommand +from tests.lib.mock_environment import MockEnvironment, MockCommand _MOCKED_COMMANDS = [ MockCommand(r"^systemctl --version$", @@ -104,6 +104,7 @@ class UnitFilePaths: extension_service_memory_accounting = '/lib/systemd/system/extension.service.d/13-MemoryAccounting.conf' extension_service_memory_limit = '/lib/systemd/system/extension.service.d/14-MemoryLimit.conf' + @contextlib.contextmanager def mock_cgroup_environment(tmp_dir): """ diff --git a/tests/common/mock_command.py b/tests/lib/mock_command.py similarity index 100% rename from tests/common/mock_command.py rename to tests/lib/mock_command.py diff --git a/tests/common/mock_environment.py b/tests/lib/mock_environment.py similarity index 100% rename from tests/common/mock_environment.py rename to tests/lib/mock_environment.py