]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Allocation pool creation should check if gateway is in subnet
authorGary Kotton <gkotton@redhat.com>
Wed, 5 Sep 2012 08:19:40 +0000 (04:19 -0400)
committerGary Kotton <gkotton@redhat.com>
Thu, 6 Sep 2012 12:16:42 +0000 (08:16 -0400)
Fixes bug 1046192

If the configured gateway is not in the subnet then it should not be
used for the allocation pool creation.

Change-Id: Ifba766a13d860b58d21034db278c6b24d4c126e7

quantum/db/db_base_plugin_v2.py
quantum/tests/unit/test_db_plugin.py

index 4cdfe9b4e4a3e3c17b873d95b388fad99f42600c..ed5bf238ffce78501117a79f326853f108f3b4cd 100644 (file)
@@ -693,12 +693,14 @@ class QuantumDbPluginV2(quantum_plugin_base_v2.QuantumPluginBaseV2):
             first_ip = net.first + 1
             last_ip = net.last - 1
             gw_ip = int(netaddr.IPAddress(subnet['gateway_ip'] or net.last))
-
-            if gw_ip > first_ip:
+            # Use the gw_ip to find a point for splitting allocation pools
+            # for this subnet
+            split_ip = min(max(gw_ip, net.first), net.last)
+            if split_ip > first_ip:
                 pools.append({'start': str(netaddr.IPAddress(first_ip)),
-                              'end': str(netaddr.IPAddress(gw_ip - 1))})
-            if gw_ip < last_ip:
-                pools.append({'start': str(netaddr.IPAddress(gw_ip + 1)),
+                              'end': str(netaddr.IPAddress(split_ip - 1))})
+            if split_ip < last_ip:
+                pools.append({'start': str(netaddr.IPAddress(split_ip + 1)),
                               'end': str(netaddr.IPAddress(last_ip))})
             # return auto-generated pools
             # no need to check for their validity
index 2fbb3bddff7aec0b7109575a65b4d830ea311edc..5b10da4eb2e853dd6290578a3c890a9126610475 100644 (file)
@@ -1663,7 +1663,7 @@ class TestNetworksV2(QuantumDbPluginV2TestCase):
 
 class TestSubnetsV2(QuantumDbPluginV2TestCase):
 
-    def _test_create_subnet(self, network=None, **kwargs):
+    def _test_create_subnet(self, network=None, expected=None, **kwargs):
         keys = kwargs.copy()
         keys.setdefault('cidr', '10.0.0.0/24')
         keys.setdefault('ip_version', 4)
@@ -1673,6 +1673,11 @@ class TestSubnetsV2(QuantumDbPluginV2TestCase):
             for k in keys:
                 self.assertIn(k, subnet['subnet'])
                 self.assertEquals(subnet['subnet'][k], keys[k])
+            # verify the configured validations are correct
+            if expected:
+                for k in expected:
+                    self.assertIn(k, subnet['subnet'])
+                    self.assertEquals(subnet['subnet'][k], expected[k])
             return subnet
 
     def test_create_subnet(self):
@@ -1871,6 +1876,36 @@ class TestSubnetsV2(QuantumDbPluginV2TestCase):
         self.assertEquals(subnet['subnet']['allocation_pools'],
                           allocation_pools)
 
+    def test_create_subnet_gw_values(self):
+        # Gateway not in subnet
+        gateway = '100.0.0.1'
+        cidr = '10.0.0.0/24'
+        allocation_pools = [{'start': '10.0.0.1',
+                             'end': '10.0.0.254'}]
+        expected = {'gateway_ip': gateway,
+                    'cidr': cidr,
+                    'allocation_pools': allocation_pools}
+        subnet = self._test_create_subnet(expected=expected,
+                                          gateway_ip=gateway)
+        # Gateway is last IP in range
+        gateway = '10.0.0.254'
+        allocation_pools = [{'start': '10.0.0.1',
+                             'end': '10.0.0.253'}]
+        expected = {'gateway_ip': gateway,
+                    'cidr': cidr,
+                    'allocation_pools': allocation_pools}
+        subnet = self._test_create_subnet(expected=expected,
+                                          gateway_ip=gateway)
+        # Gateway is first in subnet
+        gateway = '10.0.0.1'
+        allocation_pools = [{'start': '10.0.0.2',
+                             'end': '10.0.0.254'}]
+        expected = {'gateway_ip': gateway,
+                    'cidr': cidr,
+                    'allocation_pools': allocation_pools}
+        subnet = self._test_create_subnet(expected=expected,
+                                          gateway_ip=gateway)
+
     def test_create_subnet_with_allocation_pool(self):
         gateway_ip = '10.0.0.1'
         cidr = '10.0.0.0/24'