From c2a768bab719922ad770a29f607ed7fa3a3cf824 Mon Sep 17 00:00:00 2001 From: Darragh O'Reilly Date: Sun, 8 Mar 2015 11:10:34 +0000 Subject: [PATCH] Fix dhcp config dir removed too soon The config dir is now removed after unplugging. This patch also mocks out shutil.rmtree in all dhcp tests. Change-Id: I68faacbb820957fa0bece7003676bd981c56de58 Closes-Bug: 1429559 --- neutron/agent/linux/dhcp.py | 3 +-- neutron/tests/unit/test_linux_dhcp.py | 24 +++++++++++++++++------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/neutron/agent/linux/dhcp.py b/neutron/agent/linux/dhcp.py index c558167c0..744ac1d53 100644 --- a/neutron/agent/linux/dhcp.py +++ b/neutron/agent/linux/dhcp.py @@ -219,10 +219,9 @@ class DhcpLocalProcess(DhcpBase): """Disable DHCP for this network by killing the local process.""" self.process_monitor.unregister(self.network.id, DNSMASQ_SERVICE_NAME) self._get_process_manager().disable() - - self._remove_config_files() if not retain_port: self._destroy_namespace_and_port() + self._remove_config_files() def _destroy_namespace_and_port(self): try: diff --git a/neutron/tests/unit/test_linux_dhcp.py b/neutron/tests/unit/test_linux_dhcp.py index 6533c9d28..779bd7108 100644 --- a/neutron/tests/unit/test_linux_dhcp.py +++ b/neutron/tests/unit/test_linux_dhcp.py @@ -622,6 +622,7 @@ class TestBase(base.BaseTestCase): self.makedirs = mock.patch('os.makedirs').start() self.isdir = mock.patch('os.path.isdir').start() self.isdir.return_value = False + self.rmtree = mock.patch('shutil.rmtree').start() self.external_process = mock.patch( 'neutron.agent.linux.external_process.ProcessManager').start() @@ -775,6 +776,18 @@ class TestDhcpLocalProcess(TestBase): ip.return_value.netns.delete.assert_called_with('qdhcp-ns') + def test_disable_config_dir_removed_after_destroy(self): + parent = mock.MagicMock() + parent.attach_mock(self.rmtree, 'rmtree') + parent.attach_mock(self.mock_mgr, 'DeviceManager') + + lp = LocalChild(self.conf, FakeDualNetwork()) + lp.disable(retain_port=False) + + expected = [mock.call.DeviceManager().destroy(mock.ANY, mock.ANY), + mock.call.rmtree(mock.ANY, ignore_errors=True)] + parent.assert_has_calls(expected) + def test_get_interface_name(self): with mock.patch('__builtin__.open') as mock_open: mock_open.return_value.__enter__ = lambda s: s @@ -1352,13 +1365,10 @@ class TestDnsmasq(TestBase): net = FakeV4Network() path = '/opt/data/neutron/dhcp' self.conf.dhcp_confs = path - - with mock.patch('shutil.rmtree') as rmtree: - lp = LocalChild(self.conf, net) - lp._remove_config_files() - - rmtree.assert_called_once_with(os.path.join(path, net.id), - ignore_errors=True) + lp = LocalChild(self.conf, net) + lp._remove_config_files() + self.rmtree.assert_called_once_with(os.path.join(path, net.id), + ignore_errors=True) def test_existing_dhcp_networks(self): path = '/opt/data/neutron/dhcp' -- 2.45.2