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
This commit is contained in:
Gary Kotton 2012-09-05 04:19:40 -04:00
parent 3b0bf59136
commit f2471d04ce
2 changed files with 43 additions and 6 deletions

View 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

View 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'