diff options
author | James Falcon <james.falcon@canonical.com> | 2021-11-22 16:56:41 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-22 16:56:41 -0600 |
commit | 31daf6670aeeba1d452c70bc0d4d04139652be36 (patch) | |
tree | e892ab1389fdcd36c77ea985711b09e5d113dcdb /tests/unittests/test_datasource | |
parent | 1343584dc03c50c80eabb8199c4e7d0d6fb4bd56 (diff) | |
download | vyos-cloud-init-31daf6670aeeba1d452c70bc0d4d04139652be36.tar.gz vyos-cloud-init-31daf6670aeeba1d452c70bc0d4d04139652be36.zip |
testing: monkeypatch system_info call in unit tests (SC-533) (#1117)
testing: monkeypatch system_info call in unit tests
system_info can make calls that read or write from the filesystem, which
should require special mocking. It is also decorated with 'lru_cache',
which means test authors often don't realize they need to be mocking.
Also, we don't actually want the results from the user's local
machine, so monkeypatching it across all tests should be reasonable.
Additionally, moved some of 'system_info` into a helper function to
reduce the surface area of the monkeypatch, added tests for the new
function (and fixed a bug as a result), and removed related mocks that
should be no longer needed.
Diffstat (limited to 'tests/unittests/test_datasource')
-rw-r--r-- | tests/unittests/test_datasource/test_azure.py | 13 | ||||
-rw-r--r-- | tests/unittests/test_datasource/test_azure_helper.py | 4 | ||||
-rw-r--r-- | tests/unittests/test_datasource/test_configdrive.py | 9 |
3 files changed, 8 insertions, 18 deletions
diff --git a/tests/unittests/test_datasource/test_azure.py b/tests/unittests/test_datasource/test_azure.py index cbc9665d..d0274049 100644 --- a/tests/unittests/test_datasource/test_azure.py +++ b/tests/unittests/test_datasource/test_azure.py @@ -1789,9 +1789,8 @@ scbus-1 on xpt0 bus 0 config_driver=True) @mock.patch(MOCKPATH + 'net.get_interfaces', autospec=True) - @mock.patch(MOCKPATH + 'util.is_FreeBSD') def test_blacklist_through_distro( - self, m_is_freebsd, m_net_get_interfaces): + self, m_net_get_interfaces): """Verify Azure DS updates blacklist drivers in the distro's networking object.""" odata = {'HostName': "myhost", 'UserName': "myuser"} @@ -1805,7 +1804,6 @@ scbus-1 on xpt0 bus 0 self.assertEqual(distro.networking.blacklist_drivers, dsaz.BLACKLIST_DRIVERS) - m_is_freebsd.return_value = False distro.networking.get_interfaces_by_mac() m_net_get_interfaces.assert_called_with( blacklist_drivers=dsaz.BLACKLIST_DRIVERS) @@ -3219,7 +3217,6 @@ class TestPreprovisioningPollIMDS(CiTestCase): @mock.patch(MOCKPATH + 'DataSourceAzure._report_ready', mock.MagicMock()) @mock.patch(MOCKPATH + 'subp.subp', mock.MagicMock()) @mock.patch(MOCKPATH + 'util.write_file', mock.MagicMock()) -@mock.patch(MOCKPATH + 'util.is_FreeBSD') @mock.patch('cloudinit.sources.helpers.netlink.' 'wait_for_media_disconnect_connect') @mock.patch('cloudinit.net.dhcp.EphemeralIPv4Network', autospec=True) @@ -3236,10 +3233,8 @@ class TestAzureDataSourcePreprovisioning(CiTestCase): def test_poll_imds_returns_ovf_env(self, m_request, m_dhcp, m_net, - m_media_switch, - m_is_bsd): + m_media_switch): """The _poll_imds method should return the ovf_env.xml.""" - m_is_bsd.return_value = False m_media_switch.return_value = None m_dhcp.return_value = [{ 'interface': 'eth9', 'fixed-address': '192.168.2.9', @@ -3268,10 +3263,8 @@ class TestAzureDataSourcePreprovisioning(CiTestCase): def test__reprovision_calls__poll_imds(self, m_request, m_dhcp, m_net, - m_media_switch, - m_is_bsd): + m_media_switch): """The _reprovision method should call poll IMDS.""" - m_is_bsd.return_value = False m_media_switch.return_value = None m_dhcp.return_value = [{ 'interface': 'eth9', 'fixed-address': '192.168.2.9', diff --git a/tests/unittests/test_datasource/test_azure_helper.py b/tests/unittests/test_datasource/test_azure_helper.py index 552c7905..ab4f0b50 100644 --- a/tests/unittests/test_datasource/test_azure_helper.py +++ b/tests/unittests/test_datasource/test_azure_helper.py @@ -129,9 +129,7 @@ class TestFindEndpoint(CiTestCase): self.dhcp_options.return_value = {"eth0": {"unknown_245": "5:4:3:2"}} self.assertEqual('5.4.3.2', wa_shim.find_endpoint(None)) - @mock.patch('cloudinit.sources.helpers.azure.util.is_FreeBSD') - def test_latest_lease_used(self, m_is_freebsd): - m_is_freebsd.return_value = False # To avoid hitting load_file + def test_latest_lease_used(self): encoded_addresses = ['5:4:3:2', '4:3:2:1'] file_content = '\n'.join([self._build_lease_content(encoded_address) for encoded_address in encoded_addresses]) diff --git a/tests/unittests/test_datasource/test_configdrive.py b/tests/unittests/test_datasource/test_configdrive.py index 51097231..be13165c 100644 --- a/tests/unittests/test_datasource/test_configdrive.py +++ b/tests/unittests/test_datasource/test_configdrive.py @@ -486,11 +486,10 @@ class TestConfigDriveDataSource(CiTestCase): None, helpers.Paths({})) with mock.patch(M_PATH + 'find_candidate_devs') as m_find_devs: - with mock.patch(M_PATH + 'util.is_FreeBSD', return_value=False): - with mock.patch(M_PATH + 'util.mount_cb'): - with mock.patch(M_PATH + 'on_first_boot'): - m_find_devs.return_value = ['/dev/anything'] - self.assertEqual(True, cfg_ds.get_data()) + with mock.patch(M_PATH + 'util.mount_cb'): + with mock.patch(M_PATH + 'on_first_boot'): + m_find_devs.return_value = ['/dev/anything'] + self.assertEqual(True, cfg_ds.get_data()) self.assertEqual('config-disk (/dev/anything)', cfg_ds.subplatform) |