DHCP: protect against case when device name is None

There are edge cases when the agent attempts to unplug an interface and
the device does not exist.

Closes-bug: #1498370
(cherry picked from commit caebc8fb8e)
(cherry picked from commit 0b07910f33)
Change-Id: I6917ec94f685f3dd3bff6aa1d43dc56aab76274a
This commit is contained in:
Gary Kotton 2015-09-27 00:24:31 -07:00 committed by Edward Hope-Morley
parent 1578dcbb8d
commit a27b30d726
2 changed files with 24 additions and 1 deletions

View File

@ -1072,7 +1072,10 @@ class DeviceManager(object):
def destroy(self, network, device_name):
"""Destroy the device used for the network's DHCP on this host."""
self.driver.unplug(device_name, namespace=network.namespace)
if device_name:
self.driver.unplug(device_name, namespace=network.namespace)
else:
LOG.debug('No interface exists for network %s', network.id)
self.plugin.release_dhcp_port(network.id,
self.get_device_id(network))

View File

@ -1370,6 +1370,26 @@ class TestDeviceManager(base.BaseTestCase):
plugin.assert_has_calls(
[mock.call.release_dhcp_port(fake_net.id, mock.ANY)])
def test_destroy_with_none(self):
fake_net = dhcp.NetModel(
True, dict(id=FAKE_NETWORK_UUID,
tenant_id='aaaaaaaa-aaaa-aaaa-aaaaaaaaaaaa'))
with mock.patch('neutron.agent.linux.interface.NullDriver') as dvr_cls:
mock_driver = mock.MagicMock()
mock_driver.get_device_name.return_value = 'tap12345678-12'
dvr_cls.return_value = mock_driver
plugin = mock.Mock()
dh = dhcp.DeviceManager(cfg.CONF, plugin)
dh.destroy(fake_net, None)
dvr_cls.assert_called_once_with(cfg.CONF)
plugin.assert_has_calls(
[mock.call.release_dhcp_port(fake_net.id, mock.ANY)])
self.assertFalse(mock_driver.called)
def test_get_interface_name(self):
fake_net = dhcp.NetModel(
True, dict(id='12345678-1234-5678-1234567890ab',