summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Schweikert <rjschwei@suse.com>2022-03-08 20:38:20 -0500
committerGitHub <noreply@github.com>2022-03-08 19:38:20 -0600
commitafaa7da4e6eab7a98e4643bc2ebd849d53c540f5 (patch)
treefbd99554c4194256a18485cf0bd282a053138cc9
parentdee8231d69ebad4e16a88977c24fd2d8ad3df14b (diff)
downloadcloud-init-git-afaa7da4e6eab7a98e4643bc2ebd849d53c540f5.tar.gz
Make VMWare data source test host independent and expand testing (#1308)
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.
-rw-r--r--tests/unittests/sources/test_vmware.py60
1 files changed, 55 insertions, 5 deletions
diff --git a/tests/unittests/sources/test_vmware.py b/tests/unittests/sources/test_vmware.py
index dd331349..3579041a 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 @@ runcmd:
@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,49 @@ class TestDataSourceVMware(CiTestCase):
ret = ds.get_data()
self.assertFalse(ret)
- def test_get_host_info(self):
+ @mock.patch("cloudinit.sources.DataSourceVMware.get_default_ip_addrs")
+ def test_get_host_info_ipv4(self, m_fn_ipaddr):
+ m_fn_ipaddr.return_value = ("10.10.10.1", None)
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.get_default_ip_addrs")
+ def test_get_host_info_ipv6(self, m_fn_ipaddr):
+ m_fn_ipaddr.return_value = (None, "2001:db8::::::8888")
+ 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.get_default_ip_addrs")
+ def test_get_host_info_dual(self, m_fn_ipaddr):
+ m_fn_ipaddr.return_value = ("10.10.10.1", "2001:db8::::::8888")
+ 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):