]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
supress dhcp router opt for subnets with null gw
authorMark McClain <mark.mcclain@dreamhost.com>
Wed, 5 Sep 2012 04:27:27 +0000 (00:27 -0400)
committerMark McClain <mark.mcclain@dreamhost.com>
Wed, 5 Sep 2012 04:53:57 +0000 (00:53 -0400)
fixes bug 1045617

This patch supresses the default gateway for subnets without gateway
defined.

Change-Id: I29d84863e51f613d8529153870eb7542d992be0e

quantum/agent/linux/dhcp.py
quantum/tests/unit/test_linux_dhcp.py

index add556b0a4279d92f0a5a7c6e22468598452d0d0..e6dcc25cecb99ca944e9c7f55f648fe34895f331 100644 (file)
@@ -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)
index 93f1ae1f30ef4a6e43f2cde1f5c9990476ad439a..6804bbf48d70ae8cf9f5d62d1b1aec6e1ad7e282 100644 (file)
@@ -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 = """