diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/unittests/sources/azure/test_identity.py | 165 | ||||
-rw-r--r-- | tests/unittests/sources/test_azure.py | 40 | ||||
-rw-r--r-- | tests/unittests/sources/test_azure_helper.py | 58 |
3 files changed, 192 insertions, 71 deletions
diff --git a/tests/unittests/sources/azure/test_identity.py b/tests/unittests/sources/azure/test_identity.py new file mode 100644 index 00000000..53b1e18f --- /dev/null +++ b/tests/unittests/sources/azure/test_identity.py @@ -0,0 +1,165 @@ +# This file is part of cloud-init. See LICENSE file for license information. + +from unittest import mock + +import pytest + +from cloudinit.sources.azure import identity + + +@pytest.fixture(autouse=True) +def mock_read_dmi_data(): + with mock.patch.object( + identity.dmi, "read_dmi_data", return_value=None + ) as m: + yield m + + +@pytest.fixture(autouse=True) +def mock_os_path_exists(): + with mock.patch.object(identity.os.path, "exists") as m: + yield m + + +class TestByteSwapSystemUuid: + @pytest.mark.parametrize( + "system_uuid,swapped_uuid", + [ + ( + "527c2691-029f-fe4c-b1f4-a4da7ebac2cf", + "91267c52-9f02-4cfe-b1f4-a4da7ebac2cf", + ), + ( + "527C2691-029F-FE4C-B1F4-A4DA7EBAC2CD", + "91267c52-9f02-4cfe-b1f4-a4da7ebac2cd", + ), + ], + ) + def test_values(self, system_uuid, swapped_uuid): + assert identity.byte_swap_system_uuid(system_uuid) == swapped_uuid + + @pytest.mark.parametrize( + "system_uuid", + [ + "", + "g", + "91267c52-9f02-4cfe-b1f4-a4da7ebac2c", + "91267c52-9f02-4cfe-b1f4-a4da7ebac2ccc", + "-----", + ], + ) + def test_errors(self, system_uuid): + with pytest.raises(ValueError) as exc_info: + identity.byte_swap_system_uuid(system_uuid) + + assert exc_info.value.args[0] == "badly formed hexadecimal UUID string" + + +class TestConvertSystemUuidToVmId: + def test_gen1(self, monkeypatch): + system_uuid = "527c2691-029f-fe4c-b1f4-a4da7ebac2cf" + monkeypatch.setattr(identity, "is_vm_gen1", lambda: True) + + swapped_uuid = "91267c52-9f02-4cfe-b1f4-a4da7ebac2cf" + assert ( + identity.convert_system_uuid_to_vm_id(system_uuid) == swapped_uuid + ) + + def test_gen2(self, monkeypatch): + system_uuid = "527c2691-029f-fe4c-b1f4-a4da7ebac2cf" + monkeypatch.setattr(identity, "is_vm_gen1", lambda: False) + + assert ( + identity.convert_system_uuid_to_vm_id(system_uuid) == system_uuid + ) + + +class TestIsVmGen1: + def test_gen1(self, mock_os_path_exists) -> None: + mock_os_path_exists.side_effect = lambda _: False + + assert identity.is_vm_gen1() is True + + def test_gen2_freebsd(self, mock_os_path_exists) -> None: + mock_os_path_exists.side_effect = lambda x: x == "/dev/efi" + + assert identity.is_vm_gen1() is False + + def test_gen2_linux(self, mock_os_path_exists) -> None: + mock_os_path_exists.side_effect = lambda x: x == "/sys/firmware/efi" + + assert identity.is_vm_gen1() is False + + +class TestQuerySystemUuid: + @pytest.mark.parametrize( + "system_uuid", + [ + "527c2691-029f-fe4c-b1f4-a4da7ebac2cf", + "527C2691-029F-FE4C-B1F4-A4DA7EBAC2CF", + ], + ) + def test_values(self, mock_read_dmi_data, system_uuid): + mock_read_dmi_data.return_value = system_uuid + + assert identity.query_system_uuid() == system_uuid.lower() + assert mock_read_dmi_data.mock_calls == [mock.call("system-uuid")] + + def test_errors(self, mock_read_dmi_data): + mock_read_dmi_data.return_value = None + + with pytest.raises(RuntimeError) as exc_info: + identity.query_system_uuid() + + assert exc_info.value.args[0] == "failed to read system-uuid" + + +class TestQueryVmId: + def test_gen1(self, monkeypatch): + system_uuid = "527c2691-029f-fe4c-b1f4-a4da7ebac2cf" + swapped_uuid = "91267c52-9f02-4cfe-b1f4-a4da7ebac2cf" + monkeypatch.setattr(identity, "query_system_uuid", lambda: system_uuid) + monkeypatch.setattr(identity, "is_vm_gen1", lambda: True) + + assert identity.query_vm_id() == swapped_uuid + + def test_gen2(self, monkeypatch): + system_uuid = "527c2691-029f-fe4c-b1f4-a4da7ebac2cf" + monkeypatch.setattr(identity, "query_system_uuid", lambda: system_uuid) + monkeypatch.setattr(identity, "is_vm_gen1", lambda: False) + + assert identity.query_vm_id() == system_uuid + + +class TestChassisAssetTag: + def test_true_azure_cloud(self, caplog, mock_read_dmi_data): + mock_read_dmi_data.return_value = ( + identity.ChassisAssetTag.AZURE_CLOUD.value + ) + + asset_tag = identity.ChassisAssetTag.query_system() + + assert asset_tag == identity.ChassisAssetTag.AZURE_CLOUD + assert caplog.record_tuples == [ + ( + "cloudinit.sources.azure.identity", + 10, + "Azure chassis asset tag: " + "'7783-7084-3265-9085-8269-3286-77' (AZURE_CLOUD)", + ) + ] + + @pytest.mark.parametrize("tag", [None, "", "notazure"]) + def test_false_on_nonazure_chassis(self, caplog, mock_read_dmi_data, tag): + mock_read_dmi_data.return_value = tag + + asset_tag = identity.ChassisAssetTag.query_system() + + assert asset_tag is None + assert caplog.record_tuples == [ + ( + "cloudinit.sources.azure.identity", + 10, + "Non-Azure chassis asset tag: %r" % tag, + ) + ] diff --git a/tests/unittests/sources/test_azure.py b/tests/unittests/sources/test_azure.py index 0648f08c..3c36c9c6 100644 --- a/tests/unittests/sources/test_azure.py +++ b/tests/unittests/sources/test_azure.py @@ -11,11 +11,12 @@ from pathlib import Path import pytest import requests -from cloudinit import distros, helpers, subp, url_helper +from cloudinit import distros, dmi, helpers, subp, url_helper from cloudinit.net import dhcp from cloudinit.sources import UNSET from cloudinit.sources import DataSourceAzure as dsaz from cloudinit.sources import InvalidMetaDataException +from cloudinit.sources.azure import identity from cloudinit.sources.helpers import netlink from cloudinit.util import ( MountFailedError, @@ -81,9 +82,9 @@ def mock_azure_report_failure_to_fabric(): @pytest.fixture def mock_chassis_asset_tag(): with mock.patch.object( - dsaz.ChassisAssetTag, + identity.ChassisAssetTag, "query_system", - return_value=dsaz.ChassisAssetTag.AZURE_CLOUD.value, + return_value=identity.ChassisAssetTag.AZURE_CLOUD.value, ) as m: yield m @@ -110,13 +111,14 @@ def mock_time(): def mock_dmi_read_dmi_data(): def fake_read(key: str) -> str: if key == "system-uuid": - return "fake-system-uuid" + return "50109936-ef07-47fe-ac82-890c853f60d5" elif key == "chassis-asset-tag": return "7783-7084-3265-9085-8269-3286-77" raise RuntimeError() - with mock.patch( - MOCKPATH + "dmi.read_dmi_data", + with mock.patch.object( + dmi, + "read_dmi_data", side_effect=fake_read, autospec=True, ) as m: @@ -1029,7 +1031,7 @@ scbus-1 on xpt0 bus 0 ), (dsaz.subp, "which", lambda x: True), ( - dsaz.dmi, + dmi, "read_dmi_data", self.m_read_dmi_data, ), @@ -3147,7 +3149,7 @@ class TestIsPlatformViable: @pytest.mark.parametrize( "tag", [ - dsaz.ChassisAssetTag.AZURE_CLOUD.value, + identity.ChassisAssetTag.AZURE_CLOUD.value, ], ) def test_true_on_azure_chassis( @@ -3422,7 +3424,7 @@ class TestInstanceId: def test_fallback(self, azure_ds, mock_dmi_read_dmi_data): id = azure_ds.get_instance_id() - assert id == "fake-system-uuid" + assert id == "50109936-ef07-47fe-ac82-890c853f60d5" class TestProvisioning: @@ -3543,7 +3545,10 @@ class TestProvisioning: mock.call("chassis-asset-tag"), mock.call("system-uuid"), ] - assert self.azure_ds.metadata["instance-id"] == "fake-system-uuid" + assert ( + self.azure_ds.metadata["instance-id"] + == "50109936-ef07-47fe-ac82-890c853f60d5" + ) # Verify IMDS metadata. assert self.azure_ds.metadata["imds"] == self.imds_md @@ -3633,7 +3638,10 @@ class TestProvisioning: mock.call("chassis-asset-tag"), mock.call("system-uuid"), ] - assert self.azure_ds.metadata["instance-id"] == "fake-system-uuid" + assert ( + self.azure_ds.metadata["instance-id"] + == "50109936-ef07-47fe-ac82-890c853f60d5" + ) # Verify IMDS metadata. assert self.azure_ds.metadata["imds"] == self.imds_md @@ -3749,7 +3757,10 @@ class TestProvisioning: mock.call("chassis-asset-tag"), mock.call("system-uuid"), ] - assert self.azure_ds.metadata["instance-id"] == "fake-system-uuid" + assert ( + self.azure_ds.metadata["instance-id"] + == "50109936-ef07-47fe-ac82-890c853f60d5" + ) # Verify IMDS metadata. assert self.azure_ds.metadata["imds"] == self.imds_md @@ -3903,7 +3914,10 @@ class TestProvisioning: mock.call("chassis-asset-tag"), mock.call("system-uuid"), ] - assert self.azure_ds.metadata["instance-id"] == "fake-system-uuid" + assert ( + self.azure_ds.metadata["instance-id"] + == "50109936-ef07-47fe-ac82-890c853f60d5" + ) # Verify IMDS metadata. assert self.azure_ds.metadata["imds"] == self.imds_md diff --git a/tests/unittests/sources/test_azure_helper.py b/tests/unittests/sources/test_azure_helper.py index 38a57b99..ac2126e8 100644 --- a/tests/unittests/sources/test_azure_helper.py +++ b/tests/unittests/sources/test_azure_helper.py @@ -196,28 +196,6 @@ class TestGoalStateParsing(CiTestCase): goal_state = self._get_goal_state(instance_id=instance_id) self.assertEqual(instance_id, goal_state.instance_id) - def test_instance_id_byte_swap(self): - """Return true when previous_iid is byteswapped current_iid""" - previous_iid = "D0DF4C54-4ECB-4A4B-9954-5BDF3ED5C3B8" - current_iid = "544CDFD0-CB4E-4B4A-9954-5BDF3ED5C3B8" - self.assertTrue( - azure_helper.is_byte_swapped(previous_iid, current_iid) - ) - - def test_instance_id_no_byte_swap_same_instance_id(self): - previous_iid = "D0DF4C54-4ECB-4A4B-9954-5BDF3ED5C3B8" - current_iid = "D0DF4C54-4ECB-4A4B-9954-5BDF3ED5C3B8" - self.assertFalse( - azure_helper.is_byte_swapped(previous_iid, current_iid) - ) - - def test_instance_id_no_byte_swap_diff_instance_id(self): - previous_iid = "D0DF4C54-4ECB-4A4B-9954-5BDF3ED5C3B8" - current_iid = "G0DF4C54-4ECB-4A4B-9954-5BDF3ED5C3B8" - self.assertFalse( - azure_helper.is_byte_swapped(previous_iid, current_iid) - ) - def test_certificates_xml_parsed_and_fetched_correctly(self): m_azure_endpoint_client = mock.MagicMock() certificates_url = "TestCertificatesUrl" @@ -1417,42 +1395,6 @@ class TestGetMetadataGoalStateXMLAndReportFailureToFabric(CiTestCase): ) -class TestChassisAssetTag: - def test_true_azure_cloud(self, caplog, mock_dmi_read_dmi_data): - mock_dmi_read_dmi_data.return_value = ( - azure_helper.ChassisAssetTag.AZURE_CLOUD.value - ) - - asset_tag = azure_helper.ChassisAssetTag.query_system() - - assert asset_tag == azure_helper.ChassisAssetTag.AZURE_CLOUD - assert caplog.record_tuples == [ - ( - "cloudinit.sources.helpers.azure", - 10, - "Azure chassis asset tag: " - "'7783-7084-3265-9085-8269-3286-77' (AZURE_CLOUD)", - ) - ] - - @pytest.mark.parametrize("tag", [None, "", "notazure"]) - def test_false_on_nonazure_chassis( - self, caplog, mock_dmi_read_dmi_data, tag - ): - mock_dmi_read_dmi_data.return_value = tag - - asset_tag = azure_helper.ChassisAssetTag.query_system() - - assert asset_tag is None - assert caplog.record_tuples == [ - ( - "cloudinit.sources.helpers.azure", - 10, - "Non-Azure chassis asset tag: %r" % tag, - ) - ] - - class TestOvfEnvXml: @pytest.mark.parametrize( "ovf,expected", |