1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2011 OpenStack LLC.
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may
7
# not use this file except in compliance with the License. You may obtain
8
# a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
# License for the specific language governing permissions and limitations
21
from webtest import TestApp
22
from webob import Request, Response
23
from webob.exc import HTTPUnprocessableEntity
25
from melange.ipam.models import IpBlock
26
from melange.ipam.models import IpAddress
27
from melange.common import config
28
from melange.db import session
30
class TestIpBlockController(unittest.TestCase):
32
conf, melange_app = config.load_paste_app('melange',
33
{"config_file":os.path.abspath("../../etc/melange.conf.test")}, None)
34
self.app = TestApp(melange_app)
37
def test_create(self):
38
response = self.app.post("/ipam/ip_blocks",
39
{'network_id':"300",'cidr':"10.1.1.0/2"})
41
self.assertEqual(response.status,"200 OK")
42
saved_block = IpBlock.find_by_network_id("300")
43
self.assertEqual(saved_block.cidr, "10.1.1.0/2")
44
self.assertEqual(response.json, {'id':saved_block.id,'network_id':"300",
47
def test_cannot_create_duplicate_public_cidr(self):
48
self.app.post("/ipam/ip_blocks",
49
{"network_id":"300", 'cidr':"192.1.1.1/2", 'type':'public'})
50
response = self.app.post("/ipam/ip_blocks",
51
{"network_id":"22200", 'cidr':"192.1.1.1/2", 'type':'public'},
54
self.assertEqual(response.status, "400 Bad Request")
55
self.assertTrue("[{'cidr': 'cidr for public ip is not unique'}]" in response.body)
57
def test_create_with_bad_cidr(self):
58
response = self.app.post("/ipam/ip_blocks",
59
{'network_id':"300",'cidr':"10..."}, status="*")
61
self.assertEqual(response.status, "400 Bad Request")
62
self.assertTrue("[{'cidr': 'cidr is invalid'}]" in response.body)
66
block = IpBlock.create({'network_id':"301",'cidr':"10.1.1.0/2"})
67
response = self.app.get("/ipam/ip_blocks/%s" %block.id)
69
self.assertEqual(response.status,"200 OK")
70
self.assertEqual(response.json, {'id': block.id,'network_id':"301",
73
class TestIpAddressController(unittest.TestCase):
75
conf, melange_app = config.load_paste_app('melange',
76
{"config_file":os.path.abspath("../../etc/melange.conf.test")}, None)
77
self.app = TestApp(melange_app)
79
def test_create(self):
80
block = IpBlock.create({'network_id':"301",'cidr':"10.1.1.0/28"})
81
response = self.app.post("/ipam/ip_blocks/%s/ip_addresses" % block.id)
83
self.assertEqual(response.status,"200 OK")
84
allocated_address = IpAddress.find_all_by_ip_block(block.id).first()
85
self.assertEqual(allocated_address.address, "10.1.1.0")
86
self.assertEqual(response.json, {'id':allocated_address.id,
87
'address':allocated_address.address,
88
'port_id':allocated_address.port_id})
90
def test_create_when_no_more_addresses(self):
91
block = IpBlock.create({'network_id':"301",'cidr':"10.1.1.0/32"})
94
response = self.app.post("/ipam/ip_blocks/%s/ip_addresses" % block.id,
96
self.assertEqual(response.status,"422 Unprocessable Entity")
97
self.assertTrue("ip block is full" in response.body)
99
def test_create_with_port(self):
100
block = IpBlock.create({'network_id':"301",'cidr':"10.1.1.0/28"})
101
response = self.app.post("/ipam/ip_blocks/%s/ip_addresses" % block.id,
104
allocated_address = IpAddress.find_all_by_ip_block(block.id).first()
105
self.assertEqual(allocated_address.port_id, "1111")
108
block_1 = IpBlock.create({'network_id':"301",'cidr':"10.1.1.0/28"})
109
block_2 = IpBlock.create({'network_id':"301",'cidr':"10.1.1.0/28"})
110
ip = block_1.allocate_ip(port_id="3333")
111
block_2.allocate_ip(port_id="9999")
113
response = self.app.get("/ipam/ip_blocks/%s/ip_addresses/%s" %
114
(block_1.id, ip.address))
116
self.assertEqual(response.status,"200 OK")
117
self.assertEqual(response.json, {'id': ip.id,
118
'address':ip.address,
121
def test_delete_ip(self):
122
block_1 = IpBlock.create({'network_id':"301",'cidr':"10.1.1.0/28"})
123
block_2 = IpBlock.create({'network_id':"301",'cidr':"10.1.1.0/28"})
124
ip = block_1.allocate_ip()
125
block_2.allocate_ip()
127
response = self.app.delete("/ipam/ip_blocks/%s/ip_addresses/%s" %
128
(block_1.id, ip.address))
130
self.assertEqual(response.status, "200 OK")
131
self.assertEqual(IpAddress.find(ip.id), None)
133
def test_index(self):
134
block = IpBlock.create({'network_id':"301",'cidr':"10.1.1.0/28"})
135
address_1 = block.allocate_ip()
136
address_2 = block.allocate_ip()
138
response = self.app.get("/ipam/ip_blocks/%s/ip_addresses" % block.id)
140
ip_addresses = response.json["ip_addresses"]
141
self.assertEqual(response.status, "200 OK")
142
self.assertEqual(len(ip_addresses), 2)
143
self.assertEqual(ip_addresses[0]['address'], address_1.address)
144
self.assertEqual(ip_addresses[1]['address'], address_2.address)