From 9274095b4af63de7224b524e482872a78e027a7b Mon Sep 17 00:00:00 2001 From: Gary Kotton Date: Thu, 2 May 2013 11:35:58 +0000 Subject: [PATCH] Do not attempt to kill already-dead dnsmasq Fixes bug 1080846 The fix is following comments by Thiery Carrez (ttx) on the bug. Change-Id: If4f6baad4212c23845c46703140e15f1ffcfe558 --- quantum/agent/linux/dhcp.py | 7 +++-- quantum/tests/unit/test_linux_dhcp.py | 43 +++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/quantum/agent/linux/dhcp.py b/quantum/agent/linux/dhcp.py index dd397bcb8e0..4f834a1ea04 100644 --- a/quantum/agent/linux/dhcp.py +++ b/quantum/agent/linux/dhcp.py @@ -301,8 +301,11 @@ def reload_allocations(self): self._output_hosts_file() self._output_opts_file() - cmd = ['kill', '-HUP', self.pid] - utils.execute(cmd, self.root_helper) + if self.active: + cmd = ['kill', '-HUP', self.pid] + utils.execute(cmd, self.root_helper) + else: + LOG.debug(_('Pid %d is stale, relaunching dnsmasq'), self.pid) LOG.debug(_('Reloading allocations for network: %s'), self.network.id) def _output_hosts_file(self): diff --git a/quantum/tests/unit/test_linux_dhcp.py b/quantum/tests/unit/test_linux_dhcp.py index c19b9633453..414b197b190 100644 --- a/quantum/tests/unit/test_linux_dhcp.py +++ b/quantum/tests/unit/test_linux_dhcp.py @@ -538,6 +538,49 @@ def test_reload_allocations(self): exp_args = ['kill', '-HUP', 5] + with mock.patch('os.path.isdir') as isdir: + isdir.return_value = True + with mock.patch.object(dhcp.Dnsmasq, 'active') as active: + active.__get__ = mock.Mock(return_value=True) + with mock.patch.object(dhcp.Dnsmasq, 'pid') as pid: + pid.__get__ = mock.Mock(return_value=5) + dm = dhcp.Dnsmasq(self.conf, FakeDualNetwork(), + namespace='qdhcp-ns') + + method_name = '_make_subnet_interface_ip_map' + with mock.patch.object(dhcp.Dnsmasq, + method_name) as ip_map: + ip_map.return_value = {} + dm.reload_allocations() + self.assertTrue(ip_map.called) + + self.safe.assert_has_calls([mock.call(exp_host_name, exp_host_data), + mock.call(exp_opt_name, exp_opt_data)]) + self.execute.assert_called_once_with(exp_args, 'sudo') + + def test_reload_allocations_stale_pid(self): + exp_host_name = '/dhcp/cccccccc-cccc-cccc-cccc-cccccccccccc/host' + exp_host_data = """ +00:00:80:aa:bb:cc,192-168-0-2.openstacklocal,192.168.0.2 +00:00:f3:aa:bb:cc,fdca-3ba5-a17a-4ba3--2.openstacklocal,fdca:3ba5:a17a:4ba3::2 +00:00:0f:aa:bb:cc,192-168-0-3.openstacklocal,192.168.0.3 +00:00:0f:aa:bb:cc,fdca-3ba5-a17a-4ba3--3.openstacklocal,fdca:3ba5:a17a:4ba3::3 +""".lstrip() + exp_opt_name = '/dhcp/cccccccc-cccc-cccc-cccc-cccccccccccc/opts' + exp_opt_data = "tag:tag0,option:router,192.168.0.1" + fake_v6 = 'gdca:3ba5:a17a:4ba3::1' + fake_v6_cidr = 'gdca:3ba5:a17a:4ba3::/64' + exp_opt_data = """ +tag:tag0,option:dns-server,8.8.8.8 +tag:tag0,option:classless-static-route,20.0.0.1/24,20.0.0.1 +tag:tag0,option:router,192.168.0.1 +tag:tag1,option:dns-server,%s +tag:tag1,option:classless-static-route,%s,%s""".lstrip() % (fake_v6, + fake_v6_cidr, + fake_v6) + + exp_args = ['cat', '/proc/5/cmdline'] + with mock.patch('os.path.isdir') as isdir: isdir.return_value = True with mock.patch.object(dhcp.Dnsmasq, 'pid') as pid: