summaryrefslogtreecommitdiff
path: root/tests/unittests/test_handler/test_handler_ntp.py
diff options
context:
space:
mode:
authorJames Falcon <james.falcon@canonical.com>2021-11-22 16:56:41 -0600
committerGitHub <noreply@github.com>2021-11-22 16:56:41 -0600
commit31daf6670aeeba1d452c70bc0d4d04139652be36 (patch)
treee892ab1389fdcd36c77ea985711b09e5d113dcdb /tests/unittests/test_handler/test_handler_ntp.py
parent1343584dc03c50c80eabb8199c4e7d0d6fb4bd56 (diff)
downloadvyos-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_handler/test_handler_ntp.py')
-rw-r--r--tests/unittests/test_handler/test_handler_ntp.py27
1 files changed, 14 insertions, 13 deletions
diff --git a/tests/unittests/test_handler/test_handler_ntp.py b/tests/unittests/test_handler/test_handler_ntp.py
index b9e2ba57..b34a18cb 100644
--- a/tests/unittests/test_handler/test_handler_ntp.py
+++ b/tests/unittests/test_handler/test_handler_ntp.py
@@ -37,8 +37,6 @@ class TestNtp(FilesystemMockingTestCase):
self.new_root = self.tmp_dir()
self.add_patch('cloudinit.util.system_is_snappy', 'm_snappy')
self.m_snappy.return_value = False
- self.add_patch('cloudinit.util.system_info', 'm_sysinfo')
- self.m_sysinfo.return_value = {'dist': ('Distro', '99.1', 'Codename')}
self.new_root = self.reRoot()
self._get_cloud = partial(
get_cloud,
@@ -510,35 +508,38 @@ class TestNtp(FilesystemMockingTestCase):
self.assertEqual(expected_content, util.load_file(confpath))
- def test_opensuse_picks_chrony(self):
+ @mock.patch('cloudinit.util.system_info')
+ def test_opensuse_picks_chrony(self, m_sysinfo):
"""Test opensuse picks chrony or ntp on certain distro versions"""
# < 15.0 => ntp
- self.m_sysinfo.return_value = {'dist':
- ('openSUSE', '13.2', 'Harlequin')}
+ m_sysinfo.return_value = {
+ 'dist': ('openSUSE', '13.2', 'Harlequin')
+ }
mycloud = self._get_cloud('opensuse')
expected_client = mycloud.distro.preferred_ntp_clients[0]
self.assertEqual('ntp', expected_client)
# >= 15.0 and not openSUSE => chrony
- self.m_sysinfo.return_value = {'dist':
- ('SLES', '15.0',
- 'SUSE Linux Enterprise Server 15')}
+ m_sysinfo.return_value = {
+ 'dist': ('SLES', '15.0', 'SUSE Linux Enterprise Server 15')
+ }
mycloud = self._get_cloud('sles')
expected_client = mycloud.distro.preferred_ntp_clients[0]
self.assertEqual('chrony', expected_client)
# >= 15.0 and openSUSE and ver != 42 => chrony
- self.m_sysinfo.return_value = {'dist': ('openSUSE Tumbleweed',
- '20180326',
- 'timbleweed')}
+ m_sysinfo.return_value = {
+ 'dist': ('openSUSE Tumbleweed', '20180326', 'timbleweed')
+ }
mycloud = self._get_cloud('opensuse')
expected_client = mycloud.distro.preferred_ntp_clients[0]
self.assertEqual('chrony', expected_client)
- def test_ubuntu_xenial_picks_ntp(self):
+ @mock.patch('cloudinit.util.system_info')
+ def test_ubuntu_xenial_picks_ntp(self, m_sysinfo):
"""Test Ubuntu picks ntp on xenial release"""
- self.m_sysinfo.return_value = {'dist': ('Ubuntu', '16.04', 'xenial')}
+ m_sysinfo.return_value = {'dist': ('Ubuntu', '16.04', 'xenial')}
mycloud = self._get_cloud('ubuntu')
expected_client = mycloud.distro.preferred_ntp_clients[0]
self.assertEqual('ntp', expected_client)