From dc3ab47b21d444774bcc70fde6f6763c22dc8ae1 Mon Sep 17 00:00:00 2001 From: Itsuro Oda Date: Tue, 6 Jan 2015 09:35:25 +0900 Subject: [PATCH] Skip adding ips from non dhcp enabled subnets to hosts file If a network had a mix of dhcp and non dhcp enabled subnets, then ips from all subnets were added to the hosts file. This fix ensures only ips from dhcp enabled subnets are added. Closes-Bug: #1316444 Change-Id: I673088f7be0fb16a27b09e07f8ac41d21d1ea4d4 --- neutron/agent/linux/dhcp.py | 6 ++++++ neutron/tests/unit/test_linux_dhcp.py | 20 +++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/neutron/agent/linux/dhcp.py b/neutron/agent/linux/dhcp.py index d6ee68c1c..b59392df1 100644 --- a/neutron/agent/linux/dhcp.py +++ b/neutron/agent/linux/dhcp.py @@ -502,7 +502,13 @@ class Dnsmasq(DhcpLocalProcess): filename = self.get_conf_file_name('host') LOG.debug('Building host file: %s', filename) + dhcp_enabled_subnet_ids = [s.id for s in self.network.subnets + if s.enable_dhcp] for (port, alloc, hostname, name) in self._iter_hosts(): + # don't write ip address which belongs to a dhcp disabled subnet. + if alloc.subnet_id not in dhcp_enabled_subnet_ids: + continue + # (dzyu) Check if it is legal ipv6 address, if so, need wrap # it with '[]' to let dnsmasq to distinguish MAC address from # IPv6 address. diff --git a/neutron/tests/unit/test_linux_dhcp.py b/neutron/tests/unit/test_linux_dhcp.py index 27b6f3d3e..d326154ba 100644 --- a/neutron/tests/unit/test_linux_dhcp.py +++ b/neutron/tests/unit/test_linux_dhcp.py @@ -89,7 +89,9 @@ class FakePort4: admin_state_up = False device_owner = 'foo3' fixed_ips = [FakeIPAllocation('192.168.0.4', - 'ffda:3ba5:a17a:4ba3:0216:3eff:fec2:771d')] + 'dddddddd-dddd-dddd-dddd-dddddddddddd'), + FakeIPAllocation('ffda:3ba5:a17a:4ba3:0216:3eff:fec2:771d', + 'eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee')] mac_address = '00:16:3E:C2:77:1D' def __init__(self): @@ -1338,6 +1340,22 @@ class TestDnsmasq(TestBase): self.safe.assert_has_calls([mock.call(exp_host_name, exp_host_data)]) + def test_only_populates_dhcp_enabled_subnet_on_a_network(self): + exp_host_name = '/dhcp/cccccccc-cccc-cccc-cccc-cccccccccccc/host' + exp_host_data = ('00:00:80:aa:bb:cc,host-192-168-0-2.openstacklocal,' + '192.168.0.2\n' + '00:00:f3:aa:bb:cc,host-192-168-0-3.openstacklocal,' + '192.168.0.3\n' + '00:00:0f:aa:bb:cc,host-192-168-0-4.openstacklocal,' + '192.168.0.4\n' + '00:00:0f:rr:rr:rr,host-192-168-0-1.openstacklocal,' + '192.168.0.1\n').lstrip() + dm = dhcp.Dnsmasq(self.conf, FakeDualNetworkSingleDHCP(), + version=dhcp.Dnsmasq.MINIMUM_VERSION) + dm._output_hosts_file() + self.safe.assert_has_calls([mock.call(exp_host_name, + exp_host_data)]) + def test_should_enable_metadata_namespaces_disabled_returns_false(self): self.conf.set_override('use_namespaces', False) self.assertFalse(dhcp.Dnsmasq.should_enable_metadata(self.conf, -- 2.45.2