From: Mark McClain Date: Wed, 5 Sep 2012 04:27:27 +0000 (-0400) Subject: supress dhcp router opt for subnets with null gw X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=bb68d9c93ef391e68e3752a4b7af1f06dffc4418;p=openstack-build%2Fneutron-build.git supress dhcp router opt for subnets with null gw fixes bug 1045617 This patch supresses the default gateway for subnets without gateway defined. Change-Id: I29d84863e51f613d8529153870eb7542d992be0e --- diff --git a/quantum/agent/linux/dhcp.py b/quantum/agent/linux/dhcp.py index add556b0a..e6dcc25ce 100644 --- a/quantum/agent/linux/dhcp.py +++ b/quantum/agent/linux/dhcp.py @@ -301,33 +301,36 @@ class Dnsmasq(DhcpLocalProcess): if not subnet.enable_dhcp: continue if subnet.dns_nameservers: - options.append((self._TAG_PREFIX % i, - 'option', - 'dns-server', - ','.join(subnet.dns_nameservers))) + options.append( + self._format_option(i, 'dns-server', + ','.join(subnet.dns_nameservers))) host_routes = ["%s,%s" % (hr.destination, hr.nexthop) for hr in subnet.host_routes] if host_routes: - options.append((self._TAG_PREFIX % i, - 'option', - 'classless-static-route', - ','.join(host_routes))) + options.append( + self._format_option(i, 'classless-static-route', + ','.join(host_routes))) - if subnet.ip_version != 6 and subnet.gateway_ip: - options.append((self._TAG_PREFIX % i, - 'option', - 'router', - subnet.gateway_ip)) + if subnet.ip_version == 4: + if subnet.gateway_ip: + options.append(self._format_option(i, 'router', + subnet.gateway_ip)) + else: + options.append(self._format_option(i, 'router')) name = self.get_conf_file_name('opts') - replace_file(name, '\n'.join(['tag:%s,%s:%s,%s' % o for o in options])) + replace_file(name, '\n'.join(options)) return name def _lease_relay_script_path(self): return os.path.join(os.path.dirname(sys.argv[0]), 'quantum-dhcp-agent-dnsmasq-lease-update') + def _format_option(self, index, option_name, *args): + return ','.join(('tag:' + self._TAG_PREFIX % index, + 'option:%s' % option_name) + args) + @classmethod def lease_update(cls): network_id = os.environ.get(cls.QUANTUM_NETWORK_ID_KEY) diff --git a/quantum/tests/unit/test_linux_dhcp.py b/quantum/tests/unit/test_linux_dhcp.py index 93f1ae1f3..6804bbf48 100644 --- a/quantum/tests/unit/test_linux_dhcp.py +++ b/quantum/tests/unit/test_linux_dhcp.py @@ -95,6 +95,16 @@ class FakeV4SubnetNoDHCP: dns_nameservers = [] +class FakeV4SubnetNoGateway: + id = 'eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee' + ip_version = 4 + cidr = '192.168.1.0/24' + gateway_ip = None + enable_dhcp = True + host_routes = [] + dns_nameservers = [] + + class FakeV4Network: id = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa' subnets = [FakeV4Subnet()] @@ -119,6 +129,12 @@ class FakeDualNetworkSingleDHCP: ports = [FakePort1(), FakePort2(), FakePort3()] +class FakeV4NoGatewayNetwork: + id = 'cccccccc-cccc-cccc-cccc-cccccccccccc' + subnets = [FakeV4SubnetNoGateway()] + ports = [FakePort1()] + + class TestDhcpBase(unittest.TestCase): def test_base_abc_error(self): self.assertRaises(TypeError, dhcp.DhcpBase, None) @@ -460,6 +476,16 @@ tag:tag0,option:router,192.168.0.1""".lstrip() self.safe.assert_called_once_with('/foo/opts', expected) + def test_output_opts_file_no_gateway(self): + expected = "tag:tag0,option:router" + + with mock.patch.object(dhcp.Dnsmasq, 'get_conf_file_name') as conf_fn: + conf_fn.return_value = '/foo/opts' + dm = dhcp.Dnsmasq(self.conf, FakeV4NoGatewayNetwork()) + dm._output_opts_file() + + self.safe.assert_called_once_with('/foo/opts', expected) + def test_reload_allocations(self): exp_host_name = '/dhcp/cccccccc-cccc-cccc-cccc-cccccccccccc/host' exp_host_data = """