From 31daf6670aeeba1d452c70bc0d4d04139652be36 Mon Sep 17 00:00:00 2001 From: James Falcon Date: Mon, 22 Nov 2021 16:56:41 -0600 Subject: 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. --- tests/unittests/test_distros/test_netconfig.py | 7 ++++--- tests/unittests/test_distros/test_user_data_normalize.py | 2 -- 2 files changed, 4 insertions(+), 5 deletions(-) (limited to 'tests/unittests/test_distros') diff --git a/tests/unittests/test_distros/test_netconfig.py b/tests/unittests/test_distros/test_netconfig.py index d09e46af..e4eba179 100644 --- a/tests/unittests/test_distros/test_netconfig.py +++ b/tests/unittests/test_distros/test_netconfig.py @@ -241,8 +241,6 @@ class TestNetCfgDistroBase(FilesystemMockingTestCase): def setUp(self): super(TestNetCfgDistroBase, self).setUp() self.add_patch('cloudinit.util.system_is_snappy', 'm_snappy') - self.add_patch('cloudinit.util.system_info', 'm_sysinfo') - self.m_sysinfo.return_value = {'dist': ('Distro', '99.1', 'Codename')} def _get_distro(self, dname, renderers=None): cls = distros.fetch(dname) @@ -783,7 +781,10 @@ class TestNetCfgDistroArch(TestNetCfgDistroBase): """), } - with mock.patch('cloudinit.util.is_FreeBSD', return_value=False): + with mock.patch( + 'cloudinit.net.netplan.get_devicelist', + return_value=[] + ): self._apply_and_verify(self.distro.apply_network_config, V1_NET_CFG, expected_cfgs=expected_cfgs.copy(), diff --git a/tests/unittests/test_distros/test_user_data_normalize.py b/tests/unittests/test_distros/test_user_data_normalize.py index fa48410a..50c86942 100644 --- a/tests/unittests/test_distros/test_user_data_normalize.py +++ b/tests/unittests/test_distros/test_user_data_normalize.py @@ -26,8 +26,6 @@ class TestUGNormalize(TestCase): def setUp(self): super(TestUGNormalize, self).setUp() self.add_patch('cloudinit.util.system_is_snappy', 'm_snappy') - self.add_patch('cloudinit.util.system_info', 'm_sysinfo') - self.m_sysinfo.return_value = {'dist': ('Distro', '99.1', 'Codename')} def _make_distro(self, dtype, def_user=None): cfg = dict(settings.CFG_BUILTIN) -- cgit v1.2.3