testing: monkeypatch system_info call in unit tests (SC-533)#1117
Conversation
| "variant": "ubuntu" | ||
| } | ||
|
|
||
| util.system_info = my_system_info |
There was a problem hiding this comment.
main change is here
| ({'system': 'linux', 'dist': ('debian',)}, 'debian'), | ||
| ({'system': 'linux', 'dist': ('eurolinux',)}, 'eurolinux'), | ||
| ({'system': 'linux', 'dist': ('fedora',)}, 'fedora'), | ||
| ({'system': 'linux', 'dist': ('openEuler',)}, 'openeuler'), |
There was a problem hiding this comment.
Discovered a bug here. We were checking for openEuler after .lower() had been called
| @mock.patch("cloudinit.util.get_linux_distro") | ||
| def test_sysconfig_available_uses_variant_mapping(self, m_distro, m_avail): | ||
| @mock.patch("cloudinit.util.system_info") | ||
| def test_sysconfig_available_uses_variant_mapping(self, m_info, m_avail): |
There was a problem hiding this comment.
This test was checking some of the "variant" determining behavior, so I moved the bit of code into it's own function and am testing that separately now.
There was a problem hiding this comment.
Thx for the explanation here.
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 author's often don't realize they need to be mocking. Additionally, 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.
096cd6d to
e7ae6ef
Compare
blackboxsw
left a comment
There was a problem hiding this comment.
Thanks for this @TheRealFalcon think we have a couple others too to drop as well?
diff --git a/cloudinit/analyze/tests/test_boot.py b/cloudinit/analyze/tests/test_boot.py
index 37788214..6b3afb5e 100644
--- a/cloudinit/analyze/tests/test_boot.py
+++ b/cloudinit/analyze/tests/test_boot.py
@@ -12,12 +12,8 @@ class TestDistroChecker(CiTestCase):
def test_blank_distro(self):
self.assertEqual(err_code, dist_check_timestamp())
- @mock.patch('cloudinit.util.system_info', return_value={'dist': ('', '',
- '')})
- @mock.patch('cloudinit.util.get_linux_distro', return_value=('', '', ''))
@mock.patch('cloudinit.util.is_FreeBSD', return_value=True)
- def test_freebsd_gentoo_cant_find(self, m_sys_info,
- m_get_linux_distro, m_is_FreeBSD):
+ def test_freebsd_gentoo_cant_find(self, m_is_FreeBSD):
self.assertEqual(err_code, dist_check_timestamp())
@mock.patch('cloudinit.subp.subp', return_value=(0, 1))
diff --git a/tests/unittests/test_distros/test_netconfig.py b/tests/unittests/test_distros/test_netconfig.py
index 4c65644d..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)
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)
diff --git a/tests/unittests/test_handler/test_handler_ntp.py b/tests/unittests/test_handler/test_handler_ntp.py
index b9e2ba57..46365009 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 = par| def _get_variant(info): | ||
| system = info['system'].lower() | ||
| var = 'unknown' | ||
| variant = 'unknown' |
There was a problem hiding this comment.
Yeah thanks on readability.
| @mock.patch("cloudinit.util.get_linux_distro") | ||
| def test_sysconfig_available_uses_variant_mapping(self, m_distro, m_avail): | ||
| @mock.patch("cloudinit.util.system_info") | ||
| def test_sysconfig_available_uses_variant_mapping(self, m_info, m_avail): |
There was a problem hiding this comment.
Thx for the explanation here.
that expects distro version number.
|
Thanks @blackboxsw . Updated based on comments. |
blackboxsw
left a comment
There was a problem hiding this comment.
approved pending flake8
Proposed Commit Message
Additional Context
Test Steps
Checklist: