From: Gary Kotton Date: Sun, 27 Sep 2015 07:24:31 +0000 (-0700) Subject: DHCP: protect against case when device name is None X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=caebc8fb8e8d9782746c3cc3ddc86f786342c819;p=openstack-build%2Fneutron-build.git 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. Change-Id: I6917ec94f685f3dd3bff6aa1d43dc56aab76274a Closes-bug: #1498370 --- diff --git a/neutron/agent/linux/dhcp.py b/neutron/agent/linux/dhcp.py index 9e8b53f20..279d2ca7b 100644 --- a/neutron/agent/linux/dhcp.py +++ b/neutron/agent/linux/dhcp.py @@ -1225,7 +1225,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)) diff --git a/neutron/tests/unit/agent/dhcp/test_agent.py b/neutron/tests/unit/agent/dhcp/test_agent.py index ae5594a8e..89ff045f5 100644 --- a/neutron/tests/unit/agent/dhcp/test_agent.py +++ b/neutron/tests/unit/agent/dhcp/test_agent.py @@ -1393,6 +1393,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',