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())