Commit 3e0d4da8 authored by Dionysis Grigoropoulos's avatar Dionysis Grigoropoulos Committed by Christos Stavrakakis
Browse files

cyclades: Fix a bug in logic/subnets.py

* Fix a potential crash if the gateway IP was the last available IP of
the subnet
* Create the corresponding test
parent b6d3e87b
......@@ -250,6 +250,24 @@ class SubnetTest(BaseAPITest):
json.dumps(request), "json")
self.assertConflict(response)
def test_create_subnet_with_gateway_as_the_last_ip_of_subnet(self):
"""Test create a subnet with a gateway, as the last IP of the subnet"""
test_net = mf.NetworkFactory()
request = {
'subnet': {
'network_id': test_net.id,
'cidr': '10.0.3.0/24',
'ip_version': 4,
'gateway_ip': 10.0.3.254}
}
response = self.post(SUBNETS_URL, test_net.userid,
json.dumps(request), "json")
self.assertSuccess(response)
resp = json.loads(response.content)['subnet']
self.assertEqual("10.0.3.254", resp['gateway_ip'])
self.assertEqual([{"start": "10.0.3.1", "end": "10.0.3.253"}],
resp['allocation_pools'])
def test_create_subnet_with_ip_pool_end_lower_than_start(self):
"""Test create a subnet with a pool where end is lower than start"""
test_net = mf.NetworkFactory()
......
......@@ -115,11 +115,13 @@ def _create_subnet(network_id, user_id, cidr, name, ipversion=4, gateway=None,
if ipversion == 6:
raise api.faults.Conflict("Can't allocate an IP Pool in IPv6")
elif ipversion == 4:
# Check if the gateway is the first IP of the subnet, in this case
# create a single ip pool
# Check if the gateway is the first IP of the subnet, or the last. In
# that case create a single ip pool.
if gateway_ip:
if int(gateway_ip) - int(cidr_ip) == 1:
allocation_pools = [(gateway_ip + 1, cidr_ip.broadcast - 1)]
elif int(cidr_ip.broadcast) - int(gateway_ip) == 1:
allocation_pools = [(cidr_ip.network + 1, gateway_ip - 1)]
else:
# If the gateway isn't the first available ip, create two
# different ip pools adjacent to said ip
......@@ -232,7 +234,7 @@ def validate_pools(pool_list, cidr, gateway):
if not (gateway < start or gateway > end):
raise api.faults.Conflict("Gateway cannot be in pool range")
# Check if there is a conflict between the IP Poll ranges
# Check if there is a conflict between the IP Pool ranges
end = cidr.network
for pool in pool_list:
if end >= pool[0]:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment