From 705daeb7d0867c95f3503199de577b3eba6fcf89 Mon Sep 17 00:00:00 2001 From: Alessandro Pilotti Date: Tue, 4 Sep 2018 13:50:06 +0300 Subject: [PATCH] Include the NIC names in get_dhcp_hosts_in_use This is needed to identify the NIC when multiple adapters share the same MAC address, e.g. when bonds or VLAN NICs are present. Change-Id: I5dc63fd06e03c005e1f2190e84444720723a5de6 Partially-Implements: blueprint json-network-config --- cloudbaseinit/metadata/services/cloudstack.py | 2 +- cloudbaseinit/osutils/windows.py | 3 ++- cloudbaseinit/plugins/common/mtu.py | 2 +- cloudbaseinit/plugins/common/ntpclient.py | 2 +- cloudbaseinit/tests/metadata/services/test_cloudstack.py | 9 +++++---- cloudbaseinit/tests/osutils/test_windows.py | 9 ++++++--- cloudbaseinit/tests/plugins/common/test_mtu.py | 7 +++++-- cloudbaseinit/tests/plugins/common/test_ntpclient.py | 4 ++-- 8 files changed, 23 insertions(+), 15 deletions(-) diff --git a/cloudbaseinit/metadata/services/cloudstack.py b/cloudbaseinit/metadata/services/cloudstack.py index 30cdd10c..b2bd99e8 100644 --- a/cloudbaseinit/metadata/services/cloudstack.py +++ b/cloudbaseinit/metadata/services/cloudstack.py @@ -90,7 +90,7 @@ class CloudStack(base.BaseHTTPMetadataService): if not dhcp_servers: LOG.debug('No DHCP server was found.') return False - for _, ip_address in dhcp_servers: + for _, _, ip_address in dhcp_servers: LOG.debug('Testing: %s', ip_address) if self._test_api('http://%s/' % ip_address): return True diff --git a/cloudbaseinit/osutils/windows.py b/cloudbaseinit/osutils/windows.py index 135ed964..f66163cf 100644 --- a/cloudbaseinit/osutils/windows.py +++ b/cloudbaseinit/osutils/windows.py @@ -706,7 +706,8 @@ class WindowsUtils(base.BaseOSUtils): dhcp_hosts = [] for net_addr in network.get_adapter_addresses(): if net_addr["dhcp_enabled"] and net_addr["dhcp_server"]: - dhcp_hosts.append((net_addr["mac_address"], + dhcp_hosts.append((net_addr["friendly_name"], + net_addr["mac_address"], net_addr["dhcp_server"])) return dhcp_hosts diff --git a/cloudbaseinit/plugins/common/mtu.py b/cloudbaseinit/plugins/common/mtu.py index 2a6055ee..9a41ceeb 100644 --- a/cloudbaseinit/plugins/common/mtu.py +++ b/cloudbaseinit/plugins/common/mtu.py @@ -33,7 +33,7 @@ class MTUPlugin(base.BasePlugin): osutils = osutils_factory.get_os_utils() dhcp_hosts = osutils.get_dhcp_hosts_in_use() - for (mac_address, dhcp_host) in dhcp_hosts: + for (_, mac_address, dhcp_host) in dhcp_hosts: options_data = dhcp.get_dhcp_options(dhcp_host, [dhcp.OPTION_MTU]) if options_data: diff --git a/cloudbaseinit/plugins/common/ntpclient.py b/cloudbaseinit/plugins/common/ntpclient.py index 0bde3ffc..ab3356b0 100644 --- a/cloudbaseinit/plugins/common/ntpclient.py +++ b/cloudbaseinit/plugins/common/ntpclient.py @@ -59,7 +59,7 @@ class NTPClientPlugin(base.BasePlugin): ntp_option_data = None - for (_, dhcp_host) in dhcp_hosts: + for (_, _, dhcp_host) in dhcp_hosts: options_data = dhcp.get_dhcp_options(dhcp_host, [dhcp.OPTION_NTP_SERVERS]) if options_data: diff --git a/cloudbaseinit/tests/metadata/services/test_cloudstack.py b/cloudbaseinit/tests/metadata/services/test_cloudstack.py index 099b47e3..b2bd5098 100644 --- a/cloudbaseinit/tests/metadata/services/test_cloudstack.py +++ b/cloudbaseinit/tests/metadata/services/test_cloudstack.py @@ -66,9 +66,9 @@ class CloudStackTest(unittest.TestCase): def test_load(self, mock_test_api, mock_os_util): self._service._osutils.get_dhcp_hosts_in_use = mock.Mock() self._service._osutils.get_dhcp_hosts_in_use.side_effect = [ - [(mock.sentinel.mac_address, '10.10.0.1'), - (mock.sentinel.mac_address, '10.10.0.2'), - (mock.sentinel.mac_address, '10.10.0.3')] + [('eth0', mock.sentinel.mac_address, '10.10.0.1'), + ('eth1', mock.sentinel.mac_address, '10.10.0.2'), + ('eth2', mock.sentinel.mac_address, '10.10.0.3')] ] mock_test_api.side_effect = [False, False, False, True] @@ -102,7 +102,8 @@ class CloudStackTest(unittest.TestCase): def test_load_no_service(self, mock_test_api, mock_os_util): self._service._osutils.get_dhcp_hosts_in_use = mock.Mock() self._service._osutils.get_dhcp_hosts_in_use.side_effect = [ - [(mock.sentinel.mac_address, CONF.cloudstack.metadata_base_url)] + [('eth0', mock.sentinel.mac_address, + CONF.cloudstack.metadata_base_url)] ] mock_test_api.side_effect = [False, False] diff --git a/cloudbaseinit/tests/osutils/test_windows.py b/cloudbaseinit/tests/osutils/test_windows.py index c6795bf1..73f71e7a 100644 --- a/cloudbaseinit/tests/osutils/test_windows.py +++ b/cloudbaseinit/tests/osutils/test_windows.py @@ -1909,15 +1909,18 @@ class TestWindowsUtils(testutils.CloudbaseInitTestBase): @mock.patch('cloudbaseinit.utils.windows.network.get_adapter_addresses') def test_get_dhcp_hosts_in_use(self, mock_get_adapter_addresses): net_addr = {} - net_addr["mac_address"] = 'fake mac address' - net_addr["dhcp_server"] = 'fake dhcp server' + net_addr["friendly_name"] = mock.sentinel.friendly_name + net_addr["mac_address"] = mock.sentinel.mac_address + net_addr["dhcp_server"] = mock.sentinel.dhcp_server net_addr["dhcp_enabled"] = True mock_get_adapter_addresses.return_value = [net_addr] response = self._winutils.get_dhcp_hosts_in_use() mock_get_adapter_addresses.assert_called_once_with() - self.assertEqual([('fake mac address', 'fake dhcp server')], response) + self.assertEqual([(mock.sentinel.friendly_name, + mock.sentinel.mac_address, + mock.sentinel.dhcp_server)], response) @mock.patch('cloudbaseinit.osutils.windows.WindowsUtils' '.check_sysnative_dir_exists') diff --git a/cloudbaseinit/tests/plugins/common/test_mtu.py b/cloudbaseinit/tests/plugins/common/test_mtu.py index 16475f11..b7a650db 100644 --- a/cloudbaseinit/tests/plugins/common/test_mtu.py +++ b/cloudbaseinit/tests/plugins/common/test_mtu.py @@ -37,9 +37,12 @@ class MTUPluginTests(unittest.TestCase): dhcp_options=None): mock_osutils = mock_get_os_utils() mock_osutils.get_dhcp_hosts_in_use.return_value = [ - (mock.sentinel.mac_address1, mock.sentinel.dhcp_host1), - (mock.sentinel.mac_address2, mock.sentinel.dhcp_host2), + (mock.sentinel.adapter_name1, mock.sentinel.mac_address1, + mock.sentinel.dhcp_host1), + (mock.sentinel.adapter_name2, mock.sentinel.mac_address2, + mock.sentinel.dhcp_host2), ] + mock_get_dhcp_options.return_value = dhcp_options return_value = self._mtu.execute(mock.sentinel.service, diff --git a/cloudbaseinit/tests/plugins/common/test_ntpclient.py b/cloudbaseinit/tests/plugins/common/test_ntpclient.py index e1d700cc..c256f7a7 100644 --- a/cloudbaseinit/tests/plugins/common/test_ntpclient.py +++ b/cloudbaseinit/tests/plugins/common/test_ntpclient.py @@ -53,8 +53,8 @@ class NTPClientPluginTests(unittest.TestCase): mock_options_data = mock.MagicMock() mock_get_os_utils.return_value = mock_osutils - mock_osutils.get_dhcp_hosts_in_use.return_value = [('fake mac address', - 'fake dhcp host')] + mock_osutils.get_dhcp_hosts_in_use.return_value = [( + 'fake friendly name', 'fake mac address', 'fake dhcp host')] mock_get_dhcp_options.return_value = mock_options_data mock_options_data.get.return_value = ntp_data