From 369fcfa32a0448463e1593269a25000f94d9a23d Mon Sep 17 00:00:00 2001 From: Andrew Kutz <101085+akutz@users.noreply.github.com> Date: Mon, 22 Aug 2022 14:08:39 -0500 Subject: [PATCH 5/8] DataSourceVMware: fix var use before init (#1674) Reference:https://github.com/canonical/cloud-init/commit/9f0efc474ea430c75cd0abec3e2da719d4934346 Conflict:change tests/unittests/test_datasource/test_vmware.py not tests/unittests/sources/test_vmware.py This patch fixes an issue in the DataSourceVMware code where the variable ipv6_ready was used in a logging statement before it was initialized. Now the variable is initialized, avoiding the panic. LP: #1987005 --- cloudinit/sources/DataSourceVMware.py | 7 +- .../unittests/test_datasource/test_vmware.py | 74 +++++++++++++++++++ 2 files changed, 79 insertions(+), 2 deletions(-) diff --git a/cloudinit/sources/DataSourceVMware.py b/cloudinit/sources/DataSourceVMware.py index 22ca63d..197c926 100644 --- a/cloudinit/sources/DataSourceVMware.py +++ b/cloudinit/sources/DataSourceVMware.py @@ -812,7 +812,7 @@ def wait_on_network(metadata): wait_on_ipv6 = util.translate_bool(wait_on_ipv6_val) # Get information about the host. - host_info = None + host_info, ipv4_ready, ipv6_ready = None, False, False while host_info is None: # This loop + sleep results in two logs every second while waiting # for either ipv4 or ipv6 up. Do we really need to log each iteration @@ -857,7 +857,10 @@ def main(): except Exception: pass metadata = { - "wait-on-network": {"ipv4": True, "ipv6": "false"}, + WAIT_ON_NETWORK: { + WAIT_ON_NETWORK_IPV4: True, + WAIT_ON_NETWORK_IPV6: False, + }, "network": {"config": {"dhcp": True}}, } host_info = wait_on_network(metadata) diff --git a/tests/unittests/test_datasource/test_vmware.py b/tests/unittests/test_datasource/test_vmware.py index 52f910b..35be74b 100644 --- a/tests/unittests/test_datasource/test_vmware.py +++ b/tests/unittests/test_datasource/test_vmware.py @@ -75,6 +75,8 @@ class TestDataSourceVMware(CiTestCase): Test common functionality that is not transport specific. """ + with_logs = True + def setUp(self): super(TestDataSourceVMware, self).setUp() self.tmp = self.tmp_dir() @@ -93,6 +95,78 @@ class TestDataSourceVMware(CiTestCase): self.assertTrue(host_info["local_hostname"]) self.assertTrue(host_info[DataSourceVMware.LOCAL_IPV4]) + @mock.patch("cloudinit.sources.DataSourceVMware.get_host_info") + def test_wait_on_network(self, m_fn): + metadata = { + DataSourceVMware.WAIT_ON_NETWORK: { + DataSourceVMware.WAIT_ON_NETWORK_IPV4: True, + DataSourceVMware.WAIT_ON_NETWORK_IPV6: False, + }, + } + m_fn.side_effect = [ + { + "hostname": "host.cloudinit.test", + "local-hostname": "host.cloudinit.test", + "local_hostname": "host.cloudinit.test", + "network": { + "interfaces": { + "by-ipv4": {}, + "by-ipv6": {}, + "by-mac": { + "aa:bb:cc:dd:ee:ff": {"ipv4": [], "ipv6": []} + }, + }, + }, + }, + { + "hostname": "host.cloudinit.test", + "local-hostname": "host.cloudinit.test", + "local-ipv4": "10.10.10.1", + "local_hostname": "host.cloudinit.test", + "network": { + "interfaces": { + "by-ipv4": { + "10.10.10.1": { + "mac": "aa:bb:cc:dd:ee:ff", + "netmask": "255.255.255.0", + } + }, + "by-mac": { + "aa:bb:cc:dd:ee:ff": { + "ipv4": [ + { + "addr": "10.10.10.1", + "broadcast": "10.10.10.255", + "netmask": "255.255.255.0", + } + ], + "ipv6": [], + } + }, + }, + }, + }, + ] + + host_info = DataSourceVMware.wait_on_network(metadata) + + logs = self.logs.getvalue() + expected_logs = [ + "DEBUG: waiting on network: wait4=True, " + + "ready4=False, wait6=False, ready6=False\n", + "DEBUG: waiting on network complete\n", + ] + for log in expected_logs: + self.assertIn(log, logs) + + 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") + class TestDataSourceVMwareEnvVars(FilesystemMockingTestCase): """ -- 2.40.0