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
This commit is contained in:
Alessandro Pilotti 2018-09-04 13:50:06 +03:00
parent ae3d80fd63
commit 705daeb7d0
8 changed files with 23 additions and 15 deletions

View File

@ -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

View File

@ -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

View File

@ -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:

View File

@ -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:

View File

@ -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]

View File

@ -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')

View File

@ -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,

View File

@ -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