From f25b4f17fbd56fb0b78943d0e828fac80a5aa7fe Mon Sep 17 00:00:00 2001 From: Gary Kotton Date: Wed, 5 Sep 2012 04:19:40 -0400 Subject: [PATCH] Allocation pool creation should check if gateway is in subnet 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 | 12 +++++---- quantum/tests/unit/test_db_plugin.py | 37 +++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/quantum/db/db_base_plugin_v2.py b/quantum/db/db_base_plugin_v2.py index 4cdfe9b4e..ed5bf238f 100644 --- a/quantum/db/db_base_plugin_v2.py +++ b/quantum/db/db_base_plugin_v2.py @@ -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 diff --git a/quantum/tests/unit/test_db_plugin.py b/quantum/tests/unit/test_db_plugin.py index 2fbb3bddf..5b10da4eb 100644 --- a/quantum/tests/unit/test_db_plugin.py +++ b/quantum/tests/unit/test_db_plugin.py @@ -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' -- 2.45.2