root / snf-cyclades-app / synnefo / neutron / tests / api.py @ 83a99fbb
History | View | Annotate | Download (8.8 kB)
1 |
from snf_django.utils.testing import BaseAPITest |
---|---|
2 |
from django.utils import simplejson as json |
3 |
from synnefo.cyclades_settings import cyclades_services |
4 |
from synnefo.lib.services import get_service_path |
5 |
from synnefo.lib import join_urls |
6 |
import json |
7 |
import synnefo.neutron.models_factory as mf |
8 |
import synnefo.db.models_factory as dbmf |
9 |
|
10 |
NEUTRON_URL = get_service_path(cyclades_services, "neutron", "v2.0") |
11 |
NETWORKS_URL = join_urls(NEUTRON_URL, "networks/")
|
12 |
SUBNETS_URL = join_urls(NEUTRON_URL, "subnets/")
|
13 |
|
14 |
|
15 |
class NetworkTest(BaseAPITest): |
16 |
|
17 |
def test_list_networks(self): |
18 |
response = self.get(NETWORKS_URL)
|
19 |
self.assertSuccess(response)
|
20 |
networks = json.loads(response.content) |
21 |
self.assertEqual(networks, {'networks': []}) |
22 |
|
23 |
def test_create_network(self): |
24 |
request = { |
25 |
"network": {
|
26 |
"name": "sample_network" |
27 |
} |
28 |
} |
29 |
response = self.post(NETWORKS_URL, params=json.dumps(request))
|
30 |
code = response.status_code |
31 |
self.assertEqual(code, 201) |
32 |
|
33 |
def test_create_network_no_name(self): |
34 |
response = self.post(NETWORKS_URL, params="") |
35 |
code = response.status_code |
36 |
self.assertEqual(code, 201) |
37 |
|
38 |
def test_get_unfound_network(self): |
39 |
url = join_urls(NETWORKS_URL, "123")
|
40 |
response = self.get(url)
|
41 |
self.assertItemNotFound(response)
|
42 |
|
43 |
def test_get_network(self): |
44 |
test_net = mf.NetworkFactory.create() |
45 |
url = join_urls(NETWORKS_URL, str(test_net.id))
|
46 |
response = self.get(url, user=test_net.userid)
|
47 |
# validate response
|
48 |
res = json.loads(response.content) |
49 |
net = res['network']
|
50 |
keys = net.keys() |
51 |
must_send = set(["status", "subnets", "name", "admin_state_up", |
52 |
"tenant_id", "id"]) |
53 |
self.assertEqual(set(keys).issuperset(must_send), True) |
54 |
self.assertEqual(response.status_code, 200) |
55 |
|
56 |
def test_get_deleted_network(self): |
57 |
test_net = mf.NetworkFactory.create() |
58 |
url = join_urls(NETWORKS_URL, str(test_net.id))
|
59 |
self.delete(url, user=test_net.userid)
|
60 |
response = self.get(url, user=test_net.userid)
|
61 |
self.assertEqual(response.status_code, 400) |
62 |
|
63 |
def test_delete_unfound_network(self): |
64 |
url = join_urls(NETWORKS_URL, "123")
|
65 |
response = self.delete(url)
|
66 |
self.assertItemNotFound(response)
|
67 |
|
68 |
def test_delete_network(self): |
69 |
test_net = mf.NetworkFactory.create() |
70 |
url = join_urls(NETWORKS_URL, str(test_net.id))
|
71 |
response = self.delete(url, user=test_net.userid)
|
72 |
self.assertEqual(response.status_code, 204) |
73 |
|
74 |
def test_delete_network_in_use(self): |
75 |
test_net = mf.NetworkFactory.create() |
76 |
test_iface = mf.NetworkInterfaceFactory.create(network=test_net) |
77 |
url = join_urls(NETWORKS_URL, str(test_net.id))
|
78 |
response = self.delete(url, user=test_net.userid)
|
79 |
self.assertEqual(response.status_code, 409) |
80 |
|
81 |
def test_put_unfound_network(self): |
82 |
url = join_urls(NETWORKS_URL, "123")
|
83 |
response = self.delete(url)
|
84 |
self.assertItemNotFound(response)
|
85 |
|
86 |
def test_put_network(self): |
87 |
test_net = mf.NetworkFactory.create() |
88 |
url = join_urls(NETWORKS_URL, str(test_net.id))
|
89 |
request = { |
90 |
"network": {
|
91 |
"name": "new_name"} |
92 |
} |
93 |
response = self.put(url, params=json.dumps(request),
|
94 |
user=test_net.userid) |
95 |
self.assertEqual(response.status_code, 200) |
96 |
|
97 |
def test_put_network_wrong_data(self): |
98 |
test_net = mf.NetworkFactory.create() |
99 |
url = join_urls(NETWORKS_URL, str(test_net.id))
|
100 |
request = { |
101 |
"network": {
|
102 |
"wronng_field": "new_name"} |
103 |
} |
104 |
response = self.put(url, params=json.dumps(request),
|
105 |
user=test_net.userid) |
106 |
self.assertEqual(response.status_code, 400) |
107 |
|
108 |
def test_put_no_data(self): |
109 |
test_net = mf.NetworkFactory.create() |
110 |
url = join_urls(NETWORKS_URL, str(test_net.id))
|
111 |
response = self.put(url, params="", user=test_net.userid) |
112 |
self.assertEqual(response.status_code, 400) |
113 |
|
114 |
|
115 |
class SubnetTest(BaseAPITest): |
116 |
def test_list_subnets(self): |
117 |
'''Test list subnets without data'''
|
118 |
response = self.get(SUBNETS_URL)
|
119 |
self.assertSuccess(response)
|
120 |
subnets = json.loads(response.content) |
121 |
self.assertEqual(subnets, {'subnets': []}) |
122 |
|
123 |
def test_list_subnets_data(self): |
124 |
'''Test list subnets with data'''
|
125 |
test_net = mf.NetworkFactory() |
126 |
test_subnet_ipv4 = mf.SubnetFactory(network=test_net) |
127 |
test_subnet_ipv6 = mf.SubnetFactory(network=test_net, ipversion=6,
|
128 |
cidr='2620:0:2d0:200::7/32')
|
129 |
response = self.get(SUBNETS_URL, user=test_net.userid)
|
130 |
self.assertSuccess(response)
|
131 |
|
132 |
def test_get_subnet(self): |
133 |
'''Test get info of a single subnet'''
|
134 |
test_net = mf.NetworkFactory() |
135 |
test_subnet = mf.SubnetFactory(network=test_net) |
136 |
url = join_urls(SUBNETS_URL, str(test_subnet.id))
|
137 |
response = self.get(url, user=test_net.userid)
|
138 |
self.assertSuccess(response)
|
139 |
|
140 |
def test_get_subnet_404(self): |
141 |
'''Test get info of a subnet that doesn't exist'''
|
142 |
url = join_urls(SUBNETS_URL, '42')
|
143 |
response = self.get(url)
|
144 |
self.assertItemNotFound(response)
|
145 |
|
146 |
def test_subnet_delete(self): |
147 |
'''Test delete a subnet -- not supported'''
|
148 |
url = join_urls(SUBNETS_URL, '42')
|
149 |
response = self.delete(url)
|
150 |
self.assertBadRequest(response)
|
151 |
|
152 |
# def test_create_subnet_success(self):
|
153 |
# '''Test create a subnet successfully'''
|
154 |
# test_net = mf.NetworkFactory()
|
155 |
# request = {
|
156 |
# 'subnet': {
|
157 |
# 'network_id': test_net.id,
|
158 |
# 'cidr': '10.0.3.0/24',
|
159 |
# 'ip_version': 4}
|
160 |
# }
|
161 |
# response = self.post(SUBNETS_URL, test_net.userid,
|
162 |
# json.dumps(request), "json")
|
163 |
# self.assertSuccess(response)
|
164 |
|
165 |
def test_create_subnet_with_invalid_network_id(self): |
166 |
'''Test create a subnet with a network id that doesn't exist'''
|
167 |
test_net = mf.NetworkFactory() |
168 |
request = { |
169 |
'subnet': {
|
170 |
'network_id': '42', |
171 |
'cidr': '10.0.3.0/24', |
172 |
'ip_version': 4} |
173 |
} |
174 |
response = self.post(SUBNETS_URL, test_net.userid, json.dumps(request),
|
175 |
"json")
|
176 |
self.assertItemNotFound(response)
|
177 |
|
178 |
def test_create_subnet_with_malformed_ipversion(self): |
179 |
'''Create a subnet with a malformed ip_version type'''
|
180 |
test_net = mf.NetworkFactory() |
181 |
request = { |
182 |
'subnet': {
|
183 |
'network_id': test_net.id,
|
184 |
'cidr': '10.0.3.0/24', |
185 |
'ip_version': 8} |
186 |
} |
187 |
response = self.post(SUBNETS_URL, test_net.userid, json.dumps(request),
|
188 |
"json")
|
189 |
self.assertBadRequest(response)
|
190 |
|
191 |
def test_create_subnet_with_invalid_cidr(self): |
192 |
'''Create a subnet with an invalid cidr'''
|
193 |
test_net = mf.NetworkFactory() |
194 |
request = { |
195 |
'subnet': {
|
196 |
'network_id': test_net.id,
|
197 |
'cidr': '192.168.3.0/8'} |
198 |
} |
199 |
response = self.post(SUBNETS_URL, test_net.userid, json.dumps(request),
|
200 |
"json")
|
201 |
self.assertBadRequest(response)
|
202 |
|
203 |
def test_create_subnet_with_invalid_gateway(self): |
204 |
'''Create a subnet with a gateway outside of the subnet range'''
|
205 |
test_net = mf.NetworkFactory() |
206 |
request = { |
207 |
'subnet': {
|
208 |
'network_id': test_net.id,
|
209 |
'cidr': '192.168.3.0/24', |
210 |
'gateway_ip': '192.168.0.1'} |
211 |
} |
212 |
response = self.post(SUBNETS_URL, test_net.userid, json.dumps(request),
|
213 |
"json")
|
214 |
self.assertBadRequest(response)
|
215 |
|
216 |
def test_create_subnet_with_invalid_name(self): |
217 |
'''Create a subnet with an invalid subnet name'''
|
218 |
test_net = mf.NetworkFactory() |
219 |
request = { |
220 |
'subnet': {
|
221 |
'network_id': test_net.id,
|
222 |
'cidr': '192.168.3.0/24', |
223 |
'name': 'a' * 300} |
224 |
} |
225 |
response = self.post(SUBNETS_URL, test_net.userid, json.dumps(request),
|
226 |
"json")
|
227 |
self.assertBadRequest(response)
|
228 |
|
229 |
def test_create_subnet_with_invalid_dhcp(self): |
230 |
'''Create a subnet with an invalid dhcp value'''
|
231 |
test_net = mf.NetworkFactory() |
232 |
request = { |
233 |
'subnet': {
|
234 |
'network_id': test_net.id,
|
235 |
'cidr': '192.168.3.0/24', |
236 |
'enable_dhcp': 'None'} |
237 |
} |
238 |
response = self.post(SUBNETS_URL, test_net.userid, json.dumps(request),
|
239 |
"json")
|
240 |
self.assertBadRequest(response)
|