From 68614a755ad6d931eea01c486763ac6478987f43 Mon Sep 17 00:00:00 2001 From: Robert Schweikert Date: Sat, 26 Feb 2022 06:15:52 -0500 Subject: [PATCH 1/3] Make VMWare data source test host independent and expand testing - At present the test to get the host info is dependent on the network of the host on which the test is run. If there is no network the test will fail. Make the test host independent by applying appropriate mocks and expand the test to cover ipv4, ipv6, and dual stack setups. --- tests/unittests/sources/test_vmware.py | 72 +++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/tests/unittests/sources/test_vmware.py b/tests/unittests/sources/test_vmware.py index dd331349ac3..1b10b4cfb6f 100644 --- a/tests/unittests/sources/test_vmware.py +++ b/tests/unittests/sources/test_vmware.py @@ -83,13 +83,83 @@ def test_no_data_access_method(self): ret = ds.get_data() self.assertFalse(ret) - def test_get_host_info(self): + @mock.patch( + "cloudinit.sources.DataSourceVMware.netifaces.interfaces" + ) + @mock.patch( + "cloudinit.sources.DataSourceVMware.get_default_ip_addrs" + ) + @mock.patch( + "cloudinit.sources.DataSourceVMware.getfqdn" + ) + def test_get_host_info_ipv4(self, m_fn_fqdn, m_fn_ipaddr, m_fn_ifaces): + m_fn_fqdn.return_value = 'host.cloudinit.test' + m_fn_ipaddr.return_value = ('10.10.10.1', None) + m_fn_ifaces.return_value = [] host_info = DataSourceVMware.get_host_info() self.assertTrue(host_info) self.assertTrue(host_info["hostname"]) + self.assertTrue(host_info["hostname"] == 'host.cloudinit.test') self.assertTrue(host_info["local-hostname"]) self.assertTrue(host_info["local_hostname"]) self.assertTrue(host_info[DataSourceVMware.LOCAL_IPV4]) + self.assertTrue(host_info[DataSourceVMware.LOCAL_IPV4] == '10.10.10.1') + self.assertFalse(host_info.get(DataSourceVMware.LOCAL_IPV6)) + + @mock.patch( + "cloudinit.sources.DataSourceVMware.netifaces.interfaces" + ) + @mock.patch( + "cloudinit.sources.DataSourceVMware.get_default_ip_addrs" + ) + @mock.patch( + "cloudinit.sources.DataSourceVMware.getfqdn" + ) + def test_get_host_info_ipv6(self, m_fn_fqdn, m_fn_ipaddr, m_fn_ifaces): + m_fn_fqdn.return_value = 'host.cloudinit.test' + m_fn_ipaddr.return_value = ( + None, '2001:db8::::::8888' + ) + m_fn_ifaces.return_value = [] + host_info = DataSourceVMware.get_host_info() + self.assertTrue(host_info) + self.assertTrue(host_info["hostname"]) + self.assertTrue(host_info["hostname"] == 'host.cloudinit.test') + self.assertTrue(host_info["local-hostname"]) + self.assertTrue(host_info["local_hostname"]) + self.assertTrue(host_info[DataSourceVMware.LOCAL_IPV6]) + self.assertTrue( + host_info[DataSourceVMware.LOCAL_IPV6] == '2001:db8::::::8888' + ) + self.assertFalse(host_info.get(DataSourceVMware.LOCAL_IPV4)) + + @mock.patch( + "cloudinit.sources.DataSourceVMware.netifaces.interfaces" + ) + @mock.patch( + "cloudinit.sources.DataSourceVMware.get_default_ip_addrs" + ) + @mock.patch( + "cloudinit.sources.DataSourceVMware.getfqdn" + ) + def test_get_host_info_dual(self, m_fn_fqdn, m_fn_ipaddr, m_fn_ifaces): + m_fn_fqdn.return_value = 'host.cloudinit.test' + m_fn_ipaddr.return_value = ( + '10.10.10.1', '2001:db8::::::8888' + ) + m_fn_ifaces.return_value = [] + host_info = DataSourceVMware.get_host_info() + self.assertTrue(host_info) + self.assertTrue(host_info["hostname"]) + self.assertTrue(host_info["hostname"] == 'host.cloudinit.test') + self.assertTrue(host_info["local-hostname"]) + self.assertTrue(host_info["local_hostname"]) + self.assertTrue(host_info[DataSourceVMware.LOCAL_IPV4]) + self.assertTrue(host_info[DataSourceVMware.LOCAL_IPV4] == '10.10.10.1') + self.assertTrue(host_info[DataSourceVMware.LOCAL_IPV6]) + self.assertTrue( + host_info[DataSourceVMware.LOCAL_IPV6] == '2001:db8::::::8888' + ) class TestDataSourceVMwareEnvVars(FilesystemMockingTestCase): From 46f5d23244396521447e3d7597f852b7e5edf53d Mon Sep 17 00:00:00 2001 From: Robert Schweikert Date: Mon, 28 Feb 2022 09:52:05 -0500 Subject: [PATCH 2/3] Fix formatting error --- tests/unittests/sources/test_vmware.py | 66 +++++++++----------------- 1 file changed, 22 insertions(+), 44 deletions(-) diff --git a/tests/unittests/sources/test_vmware.py b/tests/unittests/sources/test_vmware.py index 1b10b4cfb6f..7b661aaf1fc 100644 --- a/tests/unittests/sources/test_vmware.py +++ b/tests/unittests/sources/test_vmware.py @@ -83,82 +83,60 @@ def test_no_data_access_method(self): ret = ds.get_data() self.assertFalse(ret) - @mock.patch( - "cloudinit.sources.DataSourceVMware.netifaces.interfaces" - ) - @mock.patch( - "cloudinit.sources.DataSourceVMware.get_default_ip_addrs" - ) - @mock.patch( - "cloudinit.sources.DataSourceVMware.getfqdn" - ) + @mock.patch("cloudinit.sources.DataSourceVMware.netifaces.interfaces") + @mock.patch("cloudinit.sources.DataSourceVMware.get_default_ip_addrs") + @mock.patch("cloudinit.sources.DataSourceVMware.getfqdn") def test_get_host_info_ipv4(self, m_fn_fqdn, m_fn_ipaddr, m_fn_ifaces): - m_fn_fqdn.return_value = 'host.cloudinit.test' - m_fn_ipaddr.return_value = ('10.10.10.1', None) + m_fn_fqdn.return_value = "host.cloudinit.test" + m_fn_ipaddr.return_value = ("10.10.10.1", None) m_fn_ifaces.return_value = [] host_info = DataSourceVMware.get_host_info() self.assertTrue(host_info) self.assertTrue(host_info["hostname"]) - self.assertTrue(host_info["hostname"] == 'host.cloudinit.test') + self.assertTrue(host_info["hostname"] == "host.cloudinit.test") self.assertTrue(host_info["local-hostname"]) self.assertTrue(host_info["local_hostname"]) self.assertTrue(host_info[DataSourceVMware.LOCAL_IPV4]) - self.assertTrue(host_info[DataSourceVMware.LOCAL_IPV4] == '10.10.10.1') + self.assertTrue(host_info[DataSourceVMware.LOCAL_IPV4] == "10.10.10.1") self.assertFalse(host_info.get(DataSourceVMware.LOCAL_IPV6)) - @mock.patch( - "cloudinit.sources.DataSourceVMware.netifaces.interfaces" - ) - @mock.patch( - "cloudinit.sources.DataSourceVMware.get_default_ip_addrs" - ) - @mock.patch( - "cloudinit.sources.DataSourceVMware.getfqdn" - ) + @mock.patch("cloudinit.sources.DataSourceVMware.netifaces.interfaces") + @mock.patch("cloudinit.sources.DataSourceVMware.get_default_ip_addrs") + @mock.patch("cloudinit.sources.DataSourceVMware.getfqdn") def test_get_host_info_ipv6(self, m_fn_fqdn, m_fn_ipaddr, m_fn_ifaces): - m_fn_fqdn.return_value = 'host.cloudinit.test' - m_fn_ipaddr.return_value = ( - None, '2001:db8::::::8888' - ) + m_fn_fqdn.return_value = "host.cloudinit.test" + m_fn_ipaddr.return_value = (None, "2001:db8::::::8888") m_fn_ifaces.return_value = [] host_info = DataSourceVMware.get_host_info() self.assertTrue(host_info) self.assertTrue(host_info["hostname"]) - self.assertTrue(host_info["hostname"] == 'host.cloudinit.test') + self.assertTrue(host_info["hostname"] == "host.cloudinit.test") self.assertTrue(host_info["local-hostname"]) self.assertTrue(host_info["local_hostname"]) self.assertTrue(host_info[DataSourceVMware.LOCAL_IPV6]) self.assertTrue( - host_info[DataSourceVMware.LOCAL_IPV6] == '2001:db8::::::8888' + host_info[DataSourceVMware.LOCAL_IPV6] == "2001:db8::::::8888" ) self.assertFalse(host_info.get(DataSourceVMware.LOCAL_IPV4)) - @mock.patch( - "cloudinit.sources.DataSourceVMware.netifaces.interfaces" - ) - @mock.patch( - "cloudinit.sources.DataSourceVMware.get_default_ip_addrs" - ) - @mock.patch( - "cloudinit.sources.DataSourceVMware.getfqdn" - ) + @mock.patch("cloudinit.sources.DataSourceVMware.netifaces.interfaces") + @mock.patch("cloudinit.sources.DataSourceVMware.get_default_ip_addrs") + @mock.patch("cloudinit.sources.DataSourceVMware.getfqdn") def test_get_host_info_dual(self, m_fn_fqdn, m_fn_ipaddr, m_fn_ifaces): - m_fn_fqdn.return_value = 'host.cloudinit.test' - m_fn_ipaddr.return_value = ( - '10.10.10.1', '2001:db8::::::8888' - ) + m_fn_fqdn.return_value = "host.cloudinit.test" + m_fn_ipaddr.return_value = ("10.10.10.1", "2001:db8::::::8888") m_fn_ifaces.return_value = [] host_info = DataSourceVMware.get_host_info() self.assertTrue(host_info) self.assertTrue(host_info["hostname"]) - self.assertTrue(host_info["hostname"] == 'host.cloudinit.test') + self.assertTrue(host_info["hostname"] == "host.cloudinit.test") self.assertTrue(host_info["local-hostname"]) self.assertTrue(host_info["local_hostname"]) self.assertTrue(host_info[DataSourceVMware.LOCAL_IPV4]) - self.assertTrue(host_info[DataSourceVMware.LOCAL_IPV4] == '10.10.10.1') + self.assertTrue(host_info[DataSourceVMware.LOCAL_IPV4] == "10.10.10.1") self.assertTrue(host_info[DataSourceVMware.LOCAL_IPV6]) self.assertTrue( - host_info[DataSourceVMware.LOCAL_IPV6] == '2001:db8::::::8888' + host_info[DataSourceVMware.LOCAL_IPV6] == "2001:db8::::::8888" ) From df77d97ae3bcc35a10f2674ea687ee4905721ff7 Mon Sep 17 00:00:00 2001 From: James Falcon Date: Tue, 8 Mar 2022 19:12:51 -0600 Subject: [PATCH 3/3] Move some patches into fixture --- tests/unittests/sources/test_vmware.py | 40 ++++++++++++++------------ 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/tests/unittests/sources/test_vmware.py b/tests/unittests/sources/test_vmware.py index 7b661aaf1fc..3579041ab6b 100644 --- a/tests/unittests/sources/test_vmware.py +++ b/tests/unittests/sources/test_vmware.py @@ -7,6 +7,7 @@ import base64 import gzip import os +from contextlib import ExitStack import pytest @@ -59,13 +60,26 @@ @pytest.fixture(autouse=True) def common_patches(): - with mock.patch("cloudinit.util.platform.platform", return_value="Linux"): - with mock.patch.multiple( + mocks = [ + mock.patch("cloudinit.util.platform.platform", return_value="Linux"), + mock.patch.multiple( "cloudinit.dmi", is_container=mock.Mock(return_value=False), is_FreeBSD=mock.Mock(return_value=False), - ): - yield + ), + mock.patch( + "cloudinit.sources.DataSourceVMware.netifaces.interfaces", + return_value=[], + ), + mock.patch( + "cloudinit.sources.DataSourceVMware.getfqdn", + return_value="host.cloudinit.test", + ), + ] + with ExitStack() as stack: + for some_mock in mocks: + stack.enter_context(some_mock) + yield class TestDataSourceVMware(CiTestCase): @@ -83,13 +97,9 @@ def test_no_data_access_method(self): ret = ds.get_data() self.assertFalse(ret) - @mock.patch("cloudinit.sources.DataSourceVMware.netifaces.interfaces") @mock.patch("cloudinit.sources.DataSourceVMware.get_default_ip_addrs") - @mock.patch("cloudinit.sources.DataSourceVMware.getfqdn") - def test_get_host_info_ipv4(self, m_fn_fqdn, m_fn_ipaddr, m_fn_ifaces): - m_fn_fqdn.return_value = "host.cloudinit.test" + def test_get_host_info_ipv4(self, m_fn_ipaddr): m_fn_ipaddr.return_value = ("10.10.10.1", None) - m_fn_ifaces.return_value = [] host_info = DataSourceVMware.get_host_info() self.assertTrue(host_info) self.assertTrue(host_info["hostname"]) @@ -100,13 +110,9 @@ def test_get_host_info_ipv4(self, m_fn_fqdn, m_fn_ipaddr, m_fn_ifaces): self.assertTrue(host_info[DataSourceVMware.LOCAL_IPV4] == "10.10.10.1") self.assertFalse(host_info.get(DataSourceVMware.LOCAL_IPV6)) - @mock.patch("cloudinit.sources.DataSourceVMware.netifaces.interfaces") @mock.patch("cloudinit.sources.DataSourceVMware.get_default_ip_addrs") - @mock.patch("cloudinit.sources.DataSourceVMware.getfqdn") - def test_get_host_info_ipv6(self, m_fn_fqdn, m_fn_ipaddr, m_fn_ifaces): - m_fn_fqdn.return_value = "host.cloudinit.test" + def test_get_host_info_ipv6(self, m_fn_ipaddr): m_fn_ipaddr.return_value = (None, "2001:db8::::::8888") - m_fn_ifaces.return_value = [] host_info = DataSourceVMware.get_host_info() self.assertTrue(host_info) self.assertTrue(host_info["hostname"]) @@ -119,13 +125,9 @@ def test_get_host_info_ipv6(self, m_fn_fqdn, m_fn_ipaddr, m_fn_ifaces): ) self.assertFalse(host_info.get(DataSourceVMware.LOCAL_IPV4)) - @mock.patch("cloudinit.sources.DataSourceVMware.netifaces.interfaces") @mock.patch("cloudinit.sources.DataSourceVMware.get_default_ip_addrs") - @mock.patch("cloudinit.sources.DataSourceVMware.getfqdn") - def test_get_host_info_dual(self, m_fn_fqdn, m_fn_ipaddr, m_fn_ifaces): - m_fn_fqdn.return_value = "host.cloudinit.test" + def test_get_host_info_dual(self, m_fn_ipaddr): m_fn_ipaddr.return_value = ("10.10.10.1", "2001:db8::::::8888") - m_fn_ifaces.return_value = [] host_info = DataSourceVMware.get_host_info() self.assertTrue(host_info) self.assertTrue(host_info["hostname"])