diff options
Diffstat (limited to 'tests/unittests/distros/test__init__.py')
-rw-r--r-- | tests/unittests/distros/test__init__.py | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/tests/unittests/distros/test__init__.py b/tests/unittests/distros/test__init__.py index 7c5187fd..ea017d58 100644 --- a/tests/unittests/distros/test__init__.py +++ b/tests/unittests/distros/test__init__.py @@ -221,6 +221,102 @@ class TestGenericDistro(helpers.FilesystemMockingTestCase): ["pw", "usermod", "myuser", "-p", "01-Jan-1970"] ) + @mock.patch("cloudinit.distros.uses_systemd") + @mock.patch( + "cloudinit.distros.subp.which", + ) + @mock.patch( + "cloudinit.distros.subp.subp", + ) + def test_virtualization_detected(self, m_subp, m_which, m_uses_systemd): + m_uses_systemd.return_value = True + m_which.return_value = "/usr/bin/systemd-detect-virt" + m_subp.return_value = ("kvm", None) + + cls = distros.fetch("ubuntu") + d = cls("ubuntu", {}, None) + self.assertTrue(d.is_virtual) + + @mock.patch("cloudinit.distros.uses_systemd") + @mock.patch( + "cloudinit.distros.subp.subp", + ) + def test_virtualization_not_detected(self, m_subp, m_uses_systemd): + m_uses_systemd.return_value = True + m_subp.return_value = ("none", None) + + cls = distros.fetch("ubuntu") + d = cls("ubuntu", {}, None) + self.assertFalse(d.is_virtual) + + @mock.patch("cloudinit.distros.uses_systemd") + def test_virtualization_unknown(self, m_uses_systemd): + m_uses_systemd.return_value = True + + from cloudinit.subp import ProcessExecutionError + + cls = distros.fetch("ubuntu") + d = cls("ubuntu", {}, None) + with mock.patch( + "cloudinit.distros.subp.which", + return_value=None, + ): + self.assertIsNone( + d.is_virtual, + "Reflect unknown state when detection" + " binary cannot be found", + ) + + with mock.patch( + "cloudinit.distros.subp.subp", + side_effect=ProcessExecutionError(), + ): + self.assertIsNone( + d.is_virtual, "Reflect unknown state on ProcessExecutionError" + ) + + def test_virtualization_on_freebsd(self): + # This test function is a bit unusual: + # We need to first mock away the `ifconfig -a` subp call + # Then, we can use side-effects to get the results of two subp calls + # needed for is_container()/virtual() which is_virtual depends on. + # We also have to clear cache between each of those assertions. + + cls = distros.fetch("freebsd") + with mock.patch( + "cloudinit.distros.subp.subp", return_value=("", None) + ): + d = cls("freebsd", {}, None) + # This mock is called by `sysctl -n security.jail.jailed` + with mock.patch( + "cloudinit.distros.subp.subp", + side_effect=[("0\n", None), ("literaly any truthy value", None)], + ): + self.assertFalse(d.is_container()) + d.is_container.cache_clear() + self.assertTrue(d.is_container()) + d.is_container.cache_clear() + + # This mock is called by `sysctl -n kern.vm_guest` + with mock.patch( + "cloudinit.distros.subp.subp", + # fmt: off + side_effect=[ + ("0\n", None), ("hv\n", None), # virtual + ("0\n", None), ("none\n", None), # physical + ("0\n", None), ("hv\n", None) # virtual + ], + # fmt: on + ): + self.assertEqual(d.virtual(), "microsoft") + d.is_container.cache_clear() + d.virtual.cache_clear() + self.assertEqual(d.virtual(), "none") + d.is_container.cache_clear() + d.virtual.cache_clear() + + self.assertTrue(d.is_virtual) + class TestGetPackageMirrors: def return_first(self, mlist): |