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