From a58a527494aa40b11b23eca47a8afb60fb24583a Mon Sep 17 00:00:00 2001 From: Brian Haley Date: Thu, 1 Nov 2018 15:48:18 -0400 Subject: [PATCH] Scan for MAC through all devices in macvtap agent The first device the agent happens to pick could be something without a MAC address like a 6in6 interface. This was causing the agent to fail to start if it was unlucky enough to pick that address. This patch just adjusts the logic to keep iterating through the list until we find a MAC. Change-Id: Iba9c7c3cb200e74a78ea885e8d9323de64c2c663 Closes-Bug: #1801030 --- .../drivers/macvtap/agent/macvtap_neutron_agent.py | 14 +++++++------- .../macvtap/agent/test_macvtap_neutron_agent.py | 11 +++++++++++ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/neutron/plugins/ml2/drivers/macvtap/agent/macvtap_neutron_agent.py b/neutron/plugins/ml2/drivers/macvtap/agent/macvtap_neutron_agent.py index 3a771fbba7f..0dabf8afd8a 100644 --- a/neutron/plugins/ml2/drivers/macvtap/agent/macvtap_neutron_agent.py +++ b/neutron/plugins/ml2/drivers/macvtap/agent/macvtap_neutron_agent.py @@ -112,13 +112,13 @@ class MacvtapManager(amb.CommonAgentManagerBase): def get_agent_id(self): devices = ip_lib.IPWrapper().get_devices(True) - if devices: - mac = ip_lib.get_device_mac(devices[0].name) - return 'macvtap%s' % mac.replace(":", "") - else: - LOG.error("Unable to obtain MAC address for unique ID. " - "Agent terminated!") - sys.exit(1) + for device in devices: + mac = ip_lib.get_device_mac(device.name) + if mac: + return 'macvtap%s' % mac.replace(":", "") + LOG.error("Unable to obtain MAC address for unique ID. " + "Agent terminated!") + sys.exit(1) def get_devices_modified_timestamps(self, devices): # TODO(kevinbenton): this should be implemented to detect diff --git a/neutron/tests/unit/plugins/ml2/drivers/macvtap/agent/test_macvtap_neutron_agent.py b/neutron/tests/unit/plugins/ml2/drivers/macvtap/agent/test_macvtap_neutron_agent.py index 026cfb0ae4d..f650e9bb938 100644 --- a/neutron/tests/unit/plugins/ml2/drivers/macvtap/agent/test_macvtap_neutron_agent.py +++ b/neutron/tests/unit/plugins/ml2/drivers/macvtap/agent/test_macvtap_neutron_agent.py @@ -146,6 +146,17 @@ class TestMacvtapManager(base.BaseTestCase): self.mgr.get_agent_id() mock_exit.assert_called_once_with(1) + def test_get_agent_id_no_mac(self): + mock_devices = [ip_lib.IPDevice('macvtap0'), + ip_lib.IPDevice('macvtap1')] + with mock.patch.object(ip_lib.IPWrapper, 'get_devices', + return_value=mock_devices),\ + mock.patch.object(ip_lib, 'get_device_mac', + side_effect=[None, 'foo:bar:1']) as mock_gdm: + self.assertEqual('macvtapfoobar1', self.mgr.get_agent_id()) + mock_gdm.assert_has_calls([mock.call('macvtap0'), + mock.call('macvtap1')]) + def test_get_extension_driver_type(self): self.assertEqual('macvtap', self.mgr.get_extension_driver_type())