root / snf-cyclades-app / synnefo / api / tests / ports.py @ 9c74cc19
History | View | Annotate | Download (12.7 kB)
1 |
# Copyright 2011-2013 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.i
|
33 |
|
34 |
from snf_django.utils.testing import (BaseAPITest, override_settings) |
35 |
from snf_django.utils.testing import BaseAPITest |
36 |
from django.utils import simplejson as json |
37 |
from synnefo.cyclades_settings import cyclades_services |
38 |
from synnefo.lib.services import get_service_path |
39 |
from synnefo.lib import join_urls |
40 |
import synnefo.db.models_factory as dbmf |
41 |
|
42 |
COMPUTE_URL = get_service_path(cyclades_services, 'compute',
|
43 |
version='v2.0')
|
44 |
PORTS_URL = join_urls(COMPUTE_URL, "ports/")
|
45 |
|
46 |
|
47 |
class PortTest(BaseAPITest): |
48 |
def test_get_ports(self): |
49 |
url = join_urls(PORTS_URL) |
50 |
response = self.get(url)
|
51 |
self.assertEqual(response.status_code, 200) |
52 |
ports = json.loads(response.content) |
53 |
self.assertEqual(ports, {"ports": []}) |
54 |
|
55 |
def test_get_port_unfound(self): |
56 |
url = join_urls(PORTS_URL, "123")
|
57 |
response = self.get(url)
|
58 |
self.assertEqual(response.status_code, 404) |
59 |
|
60 |
def test_get_port(self): |
61 |
nic = dbmf.NetworkInterfaceFactory.create() |
62 |
url = join_urls(PORTS_URL, str(nic.id))
|
63 |
response = self.get(url, user=nic.network.userid)
|
64 |
self.assertEqual(response.status_code, 200) |
65 |
|
66 |
def test_delete_port(self): |
67 |
nic = dbmf.NetworkInterfaceFactory.create(device_owner='vm')
|
68 |
url = join_urls(PORTS_URL, str(nic.id))
|
69 |
response = self.delete(url, user=nic.network.userid)
|
70 |
self.assertEqual(response.status_code, 204) |
71 |
|
72 |
def test_delete_port_from_nonvm(self): |
73 |
nic = dbmf.NetworkInterfaceFactory.create(device_owner='router')
|
74 |
url = join_urls(PORTS_URL, str(nic.id))
|
75 |
response = self.delete(url, user=nic.network.userid)
|
76 |
self.assertEqual(response.status_code, 400) |
77 |
|
78 |
def test_remove_nic_malformed(self): |
79 |
url = join_urls(PORTS_URL, "123")
|
80 |
response = self.delete(url)
|
81 |
self.assertItemNotFound(response)
|
82 |
|
83 |
def test_update_port_name(self): |
84 |
nic = dbmf.NetworkInterfaceFactory.create(device_owner='vm')
|
85 |
url = join_urls(PORTS_URL, str(nic.id))
|
86 |
request = {'port': {"name": "test-name"}} |
87 |
response = self.put(url, params=json.dumps(request),
|
88 |
user=nic.network.userid) |
89 |
self.assertEqual(response.status_code, 200) |
90 |
res = json.loads(response.content) |
91 |
self.assertEqual(res['port']['name'], "test-name") |
92 |
|
93 |
def test_update_port_sg_unfound(self): |
94 |
sg1 = dbmf.SecurityGroupFactory.create() |
95 |
nic = dbmf.NetworkInterfaceFactory.create(device_owner='vm')
|
96 |
nic.security_groups.add(sg1) |
97 |
nic.save() |
98 |
url = join_urls(PORTS_URL, str(nic.id))
|
99 |
request = {'port': {"security_groups": ["123"]}} |
100 |
response = self.put(url, params=json.dumps(request),
|
101 |
user=nic.network.userid) |
102 |
self.assertEqual(response.status_code, 404) |
103 |
|
104 |
def test_update_port_sg(self): |
105 |
sg1 = dbmf.SecurityGroupFactory.create() |
106 |
sg2 = dbmf.SecurityGroupFactory.create() |
107 |
sg3 = dbmf.SecurityGroupFactory.create() |
108 |
nic = dbmf.NetworkInterfaceFactory.create(device_owner='vm')
|
109 |
nic.security_groups.add(sg1) |
110 |
nic.save() |
111 |
url = join_urls(PORTS_URL, str(nic.id))
|
112 |
request = {'port': {"security_groups": [str(sg2.id), str(sg3.id)]}} |
113 |
response = self.put(url, params=json.dumps(request),
|
114 |
user=nic.network.userid) |
115 |
res = json.loads(response.content) |
116 |
self.assertEqual(res['port']['security_groups'], |
117 |
[str(sg2.id), str(sg3.id)]) |
118 |
|
119 |
def test_create_port_no_network(self): |
120 |
request = { |
121 |
"port": {
|
122 |
"device_id": "123", |
123 |
"name": "port1", |
124 |
"network_id": "123" |
125 |
} |
126 |
} |
127 |
response = self.post(PORTS_URL, params=json.dumps(request))
|
128 |
self.assertEqual(response.status_code, 404) |
129 |
|
130 |
def test_create_port_private_net(self): |
131 |
net = dbmf.NetworkFactory.create(public=False)
|
132 |
subnet1 = dbmf.IPv4SubnetFactory.create(network=net) |
133 |
subnet2 = dbmf.IPv6SubnetFactory.create(network=net) |
134 |
sg1 = dbmf.SecurityGroupFactory.create() |
135 |
sg2 = dbmf.SecurityGroupFactory.create() |
136 |
vm = dbmf.VirtualMachineFactory.create(userid=net.userid) |
137 |
request = { |
138 |
"port": {
|
139 |
"name": "port1", |
140 |
"network_id": str(net.id), |
141 |
"device_id": str(vm.id), |
142 |
"security_groups": [str(sg1.id), str(sg2.id)] |
143 |
} |
144 |
} |
145 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
146 |
user=net.userid) |
147 |
self.assertEqual(response.status_code, 201) |
148 |
|
149 |
def test_create_port_public_net_no_ip(self): |
150 |
net = dbmf.NetworkFactory.create(public=True)
|
151 |
vm = dbmf.VirtualMachineFactory.create(userid=net.userid) |
152 |
request = { |
153 |
"port": {
|
154 |
"name": "port1", |
155 |
"network_id": str(net.id), |
156 |
"device_id": str(vm.id), |
157 |
} |
158 |
} |
159 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
160 |
user=net.userid) |
161 |
self.assertEqual(response.status_code, 400) |
162 |
|
163 |
def test_create_port_public_net_wrong_ip(self): |
164 |
net = dbmf.NetworkFactory.create(public=True)
|
165 |
vm = dbmf.VirtualMachineFactory.create(userid=net.userid) |
166 |
request = { |
167 |
"port": {
|
168 |
"name": "port1", |
169 |
"network_id": str(net.id), |
170 |
"device_id": str(vm.id), |
171 |
"fixed_ips" : [{"ip_address":"8.8.8.8"}] |
172 |
} |
173 |
} |
174 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
175 |
user=net.userid) |
176 |
self.assertEqual(response.status_code, 404) |
177 |
|
178 |
def test_create_port_public_net_conflict(self): |
179 |
net = dbmf.NetworkFactory.create(public=True)
|
180 |
fip = dbmf.FloatingIPFactory(nic=None, userid=net.userid)
|
181 |
vm = dbmf.VirtualMachineFactory.create(userid=net.userid) |
182 |
request = { |
183 |
"port": {
|
184 |
"name": "port1", |
185 |
"network_id": str(net.id), |
186 |
"device_id": str(vm.id), |
187 |
"fixed_ips" : [{"ip_address":fip.address}] |
188 |
} |
189 |
} |
190 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
191 |
user=net.userid) |
192 |
self.assertEqual(response.status_code, 409) |
193 |
|
194 |
def test_create_port_public_net_taken_ip(self): |
195 |
net = dbmf.NetworkFactory.create(public=True)
|
196 |
fip = dbmf.FloatingIPFactory(network=net, userid=net.userid) |
197 |
vm = dbmf.VirtualMachineFactory.create(userid=net.userid) |
198 |
request = { |
199 |
"port": {
|
200 |
"name": "port1", |
201 |
"network_id": str(net.id), |
202 |
"device_id": str(vm.id), |
203 |
"fixed_ips" : [{"ip_address":fip.address}] |
204 |
} |
205 |
} |
206 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
207 |
user=net.userid) |
208 |
self.assertEqual(response.status_code, 400) |
209 |
|
210 |
def test_create_port_public_net(self): |
211 |
net = dbmf.NetworkFactory.create(public=True)
|
212 |
fip = dbmf.FloatingIPFactory(network=net, nic=None, userid=net.userid)
|
213 |
vm = dbmf.VirtualMachineFactory.create(userid=net.userid) |
214 |
request = { |
215 |
"port": {
|
216 |
"name": "port1", |
217 |
"network_id": str(net.id), |
218 |
"device_id": str(vm.id), |
219 |
"fixed_ips" : [{"ip_address":fip.address}] |
220 |
} |
221 |
} |
222 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
223 |
user=net.userid) |
224 |
print response.content
|
225 |
self.assertEqual(response.status_code, 200) |
226 |
|
227 |
def test_add_nic_to_deleted_network(self): |
228 |
user = 'userr'
|
229 |
vm = dbmf.VirtualMachineFactory(name='yo', userid=user,
|
230 |
operstate="ACTIVE")
|
231 |
net = dbmf.NetworkFactory(state='ACTIVE', userid=user,
|
232 |
deleted=True)
|
233 |
request = { |
234 |
"port": {
|
235 |
"device_id": str(vm.id), |
236 |
"name": "port1", |
237 |
"network_id": str(net.id) |
238 |
} |
239 |
} |
240 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
241 |
user=net.userid) |
242 |
self.assertBadRequest(response)
|
243 |
|
244 |
def test_add_nic_to_public_network(self): |
245 |
user = 'userr'
|
246 |
vm = dbmf.VirtualMachineFactory(name='yo', userid=user)
|
247 |
net = dbmf.NetworkFactory(state='ACTIVE', userid=user, public=True) |
248 |
request = { |
249 |
"port": {
|
250 |
"device_id": str(vm.id), |
251 |
"name": "port1", |
252 |
"network_id": str(net.id) |
253 |
} |
254 |
} |
255 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
256 |
user=net.userid) |
257 |
self.assertFault(response, 403, 'forbidden') |
258 |
|
259 |
def test_add_nic_malformed_1(self): |
260 |
user = 'userr'
|
261 |
vm = dbmf.VirtualMachineFactory(name='yo', userid=user)
|
262 |
net = dbmf.NetworkFactory(state='ACTIVE', userid=user)
|
263 |
request = { |
264 |
"port": {
|
265 |
"name": "port1", |
266 |
"network_id": str(net.id) |
267 |
} |
268 |
} |
269 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
270 |
user=net.userid) |
271 |
self.assertBadRequest(response)
|
272 |
|
273 |
def test_add_nic_malformed_2(self): |
274 |
user = 'userr'
|
275 |
vm = dbmf.VirtualMachineFactory(name='yo', userid=user)
|
276 |
net = dbmf.NetworkFactory(state='ACTIVE', userid=user)
|
277 |
request = { |
278 |
"port": {
|
279 |
"device_id": str(vm.id), |
280 |
"name": "port1" |
281 |
} |
282 |
} |
283 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
284 |
user=net.userid) |
285 |
self.assertBadRequest(response)
|
286 |
|
287 |
def test_add_nic_not_active(self): |
288 |
"""Test connecting VM to non-active network"""
|
289 |
user = 'dummy'
|
290 |
vm = dbmf.VirtualMachineFactory(name='yo', userid=user)
|
291 |
net = dbmf.NetworkFactory(state='PENDING', userid=user)
|
292 |
request = { |
293 |
"port": {
|
294 |
"device_id": str(vm.id), |
295 |
"name": "port1", |
296 |
"network_id": str(net.id) |
297 |
} |
298 |
} |
299 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
300 |
user=net.userid) |
301 |
# Test that returns BuildInProgress
|
302 |
self.assertEqual(response.status_code, 409) |
303 |
|
304 |
# def test_add_nic_full_network(self, mrapi):
|
305 |
# """Test connecting VM to a full network"""
|
306 |
# user = 'userr'
|
307 |
# vm = dbmf.VirtualMachineFactory(name='yo', userid=user,
|
308 |
# operstate="STARTED")
|
309 |
# net = dbmf.NetworkFactory(state='ACTIVE', subnet='10.0.0.0/30',
|
310 |
# userid=user, dhcp=True)
|
311 |
# pool = net.get_pool()
|
312 |
# while not pool.empty():
|
313 |
# pool.get()
|
314 |
# pool.save()
|
315 |
# pool = net.get_pool()
|
316 |
# self.assertTrue(pool.empty())
|
317 |
# request = {'add': {'serverRef': vm.id}}
|
318 |
# response = self.mypost('networks/%d/action' % net.id,
|
319 |
# net.userid, json.dumps(request), 'json')
|
320 |
# # Test that returns OverLimit
|
321 |
# self.assertEqual(response.status_code, 413)
|
322 |
# self.assertFalse(mrapi.called)
|