root / snf-cyclades-app / synnefo / api / tests / ports.py @ cdd6fc06
History | View | Annotate | Download (15.9 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 django.conf import settings |
35 |
from snf_django.utils.testing import BaseAPITest, override_settings |
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 |
from mock import patch |
41 |
import synnefo.db.models_factory as dbmf |
42 |
|
43 |
NETWORK_URL = get_service_path(cyclades_services, 'network',
|
44 |
version='v2.0')
|
45 |
PORTS_URL = join_urls(NETWORK_URL, "ports/")
|
46 |
|
47 |
|
48 |
class PortTest(BaseAPITest): |
49 |
def test_get_ports(self): |
50 |
url = join_urls(PORTS_URL) |
51 |
response = self.get(url)
|
52 |
self.assertEqual(response.status_code, 200) |
53 |
ports = json.loads(response.content) |
54 |
self.assertEqual(ports, {"ports": []}) |
55 |
|
56 |
def test_get_port_unfound(self): |
57 |
url = join_urls(PORTS_URL, "123")
|
58 |
response = self.get(url)
|
59 |
self.assertEqual(response.status_code, 404) |
60 |
|
61 |
def test_get_port(self): |
62 |
nic = dbmf.NetworkInterfaceFactory() |
63 |
url = join_urls(PORTS_URL, str(nic.id))
|
64 |
response = self.get(url, user=nic.userid)
|
65 |
self.assertEqual(response.status_code, 200) |
66 |
|
67 |
@patch("synnefo.db.models.get_rapi_client") |
68 |
def test_delete_port(self, mrapi): |
69 |
nic = dbmf.NetworkInterfaceFactory(device_owner='vm')
|
70 |
url = join_urls(PORTS_URL, str(nic.id))
|
71 |
mrapi().ModifyInstance.return_value = 42
|
72 |
with override_settings(settings, GANETI_USE_HOTPLUG=True): |
73 |
response = self.delete(url, user=nic.userid)
|
74 |
self.assertEqual(response.status_code, 204) |
75 |
with override_settings(settings, GANETI_USE_HOTPLUG=False): |
76 |
response = self.delete(url, user=nic.userid)
|
77 |
self.assertEqual(response.status_code, 400) |
78 |
|
79 |
def test_remove_nic_malformed(self): |
80 |
url = join_urls(PORTS_URL, "123")
|
81 |
response = self.delete(url)
|
82 |
self.assertItemNotFound(response)
|
83 |
|
84 |
def test_update_port_name(self): |
85 |
nic = dbmf.NetworkInterfaceFactory(device_owner='vm')
|
86 |
url = join_urls(PORTS_URL, str(nic.id))
|
87 |
request = {'port': {"name": "test-name"}} |
88 |
response = self.put(url, params=json.dumps(request),
|
89 |
user=nic.userid) |
90 |
self.assertEqual(response.status_code, 200) |
91 |
res = json.loads(response.content) |
92 |
self.assertEqual(res['port']['name'], "test-name") |
93 |
|
94 |
def test_update_port_sg_unfound(self): |
95 |
sg1 = dbmf.SecurityGroupFactory() |
96 |
nic = dbmf.NetworkInterfaceFactory(device_owner='vm')
|
97 |
nic.security_groups.add(sg1) |
98 |
nic.save() |
99 |
url = join_urls(PORTS_URL, str(nic.id))
|
100 |
request = {'port': {"security_groups": ["123"]}} |
101 |
response = self.put(url, params=json.dumps(request),
|
102 |
user=nic.userid) |
103 |
self.assertEqual(response.status_code, 404) |
104 |
|
105 |
def test_update_port_sg(self): |
106 |
sg1 = dbmf.SecurityGroupFactory() |
107 |
sg2 = dbmf.SecurityGroupFactory() |
108 |
sg3 = dbmf.SecurityGroupFactory() |
109 |
nic = dbmf.NetworkInterfaceFactory(device_owner='vm')
|
110 |
nic.security_groups.add(sg1) |
111 |
nic.save() |
112 |
url = join_urls(PORTS_URL, str(nic.id))
|
113 |
request = {'port': {"security_groups": [str(sg2.id), str(sg3.id)]}} |
114 |
response = self.put(url, params=json.dumps(request),
|
115 |
user=nic.userid) |
116 |
res = json.loads(response.content) |
117 |
self.assertEqual(res['port']['security_groups'], |
118 |
[str(sg2.id), str(sg3.id)]) |
119 |
|
120 |
def test_create_port_no_network(self): |
121 |
request = { |
122 |
"port": {
|
123 |
"device_id": "123", |
124 |
"name": "port1", |
125 |
"network_id": "123" |
126 |
} |
127 |
} |
128 |
response = self.post(PORTS_URL, params=json.dumps(request))
|
129 |
self.assertEqual(response.status_code, 404) |
130 |
|
131 |
@patch("synnefo.db.models.get_rapi_client") |
132 |
def test_create_port_private_net(self, mrapi): |
133 |
net = dbmf.NetworkFactory(public=False)
|
134 |
dbmf.IPv4SubnetFactory(network=net) |
135 |
dbmf.IPv6SubnetFactory(network=net) |
136 |
sg1 = dbmf.SecurityGroupFactory() |
137 |
sg2 = dbmf.SecurityGroupFactory() |
138 |
vm = dbmf.VirtualMachineFactory(userid=net.userid) |
139 |
request = { |
140 |
"port": {
|
141 |
"name": "port1", |
142 |
"network_id": str(net.id), |
143 |
"device_id": str(vm.id), |
144 |
"security_groups": [str(sg1.id), str(sg2.id)] |
145 |
} |
146 |
} |
147 |
mrapi().ModifyInstance.return_value = 42
|
148 |
with override_settings(settings, GANETI_USE_HOTPLUG=False): |
149 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
150 |
user=net.userid) |
151 |
self.assertEqual(response.status_code, 400) |
152 |
with override_settings(settings, GANETI_USE_HOTPLUG=True): |
153 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
154 |
user=net.userid) |
155 |
self.assertEqual(response.status_code, 201) |
156 |
|
157 |
def test_create_port_public_net_no_ip(self): |
158 |
net = dbmf.NetworkFactory(public=True)
|
159 |
vm = dbmf.VirtualMachineFactory(userid=net.userid) |
160 |
request = { |
161 |
"port": {
|
162 |
"name": "port1", |
163 |
"network_id": str(net.id), |
164 |
"device_id": str(vm.id), |
165 |
} |
166 |
} |
167 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
168 |
user=net.userid) |
169 |
self.assertEqual(response.status_code, 400) |
170 |
|
171 |
def test_create_port_public_net_wrong_ip(self): |
172 |
net = dbmf.NetworkFactory(public=True)
|
173 |
vm = dbmf.VirtualMachineFactory(userid=net.userid) |
174 |
request = { |
175 |
"port": {
|
176 |
"name": "port1", |
177 |
"network_id": str(net.id), |
178 |
"device_id": str(vm.id), |
179 |
"fixed_ips": [{"ip_address": "8.8.8.8"}] |
180 |
} |
181 |
} |
182 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
183 |
user=net.userid) |
184 |
self.assertEqual(response.status_code, 404) |
185 |
|
186 |
def test_create_port_public_net_conflict(self): |
187 |
net = dbmf.NetworkFactory(public=True)
|
188 |
fip = dbmf.FloatingIPFactory(nic=None, userid=net.userid)
|
189 |
vm = dbmf.VirtualMachineFactory(userid=net.userid) |
190 |
request = { |
191 |
"port": {
|
192 |
"name": "port1", |
193 |
"network_id": str(net.id), |
194 |
"device_id": str(vm.id), |
195 |
"fixed_ips": [{"ip_address": fip.address}] |
196 |
} |
197 |
} |
198 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
199 |
user=net.userid) |
200 |
self.assertEqual(response.status_code, 409) |
201 |
|
202 |
def test_create_port_public_net_taken_ip(self): |
203 |
net = dbmf.NetworkFactory(public=True)
|
204 |
fip = dbmf.FloatingIPFactory(network=net, userid=net.userid) |
205 |
vm = dbmf.VirtualMachineFactory(userid=net.userid) |
206 |
request = { |
207 |
"port": {
|
208 |
"name": "port1", |
209 |
"network_id": str(net.id), |
210 |
"device_id": str(vm.id), |
211 |
"fixed_ips": [{"ip_address": fip.address}] |
212 |
} |
213 |
} |
214 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
215 |
user=net.userid) |
216 |
self.assertEqual(response.status_code, 409) |
217 |
|
218 |
@patch("synnefo.db.models.get_rapi_client") |
219 |
def test_create_port_with_floating_ip(self, mrapi): |
220 |
vm = dbmf.VirtualMachineFactory() |
221 |
fip = dbmf.FloatingIPFactory(network__public=True, nic=None, |
222 |
userid=vm.userid) |
223 |
request = { |
224 |
"port": {
|
225 |
"name": "port1", |
226 |
"network_id": str(fip.network_id), |
227 |
"device_id": str(vm.id), |
228 |
"fixed_ips": [{"ip_address": fip.address}] |
229 |
} |
230 |
} |
231 |
mrapi().ModifyInstance.return_value = 42
|
232 |
with override_settings(settings, GANETI_USE_HOTPLUG=True): |
233 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
234 |
user=vm.userid) |
235 |
self.assertEqual(response.status_code, 201) |
236 |
|
237 |
@patch("synnefo.db.models.get_rapi_client") |
238 |
def test_create_port_with_address(self, mrapi): |
239 |
"""Test creation if IP address is specified."""
|
240 |
mrapi().ModifyInstance.return_value = 42
|
241 |
vm = dbmf.VirtualMachineFactory() |
242 |
net = dbmf.NetworkWithSubnetFactory(userid=vm.userid, |
243 |
public=False,
|
244 |
subnet__cidr="192.168.2.0/24",
|
245 |
subnet__gateway=None,
|
246 |
subnet__pool__size=1,
|
247 |
subnet__pool__offset=1)
|
248 |
request = { |
249 |
"port": {
|
250 |
"name": "port_with_address", |
251 |
"network_id": str(net.id), |
252 |
"device_id": str(vm.id), |
253 |
"fixed_ips": [{"ip_address": "192.168.2.1"}] |
254 |
} |
255 |
} |
256 |
with override_settings(settings, GANETI_USE_HOTPLUG=True): |
257 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
258 |
user=vm.userid) |
259 |
self.assertEqual(response.status_code, 201) |
260 |
new_port_ip = json.loads(response.content)["port"]["fixed_ips"][0] |
261 |
self.assertEqual(new_port_ip["ip_address"], "192.168.2.1") |
262 |
|
263 |
# But 409 if address is already used
|
264 |
with override_settings(settings, GANETI_USE_HOTPLUG=True): |
265 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
266 |
user=vm.userid) |
267 |
self.assertConflict(response)
|
268 |
|
269 |
# And bad request if IPv6 address is specified
|
270 |
request["port"]["fixed_ips"][0]["ip_address"] = "babe::" |
271 |
with override_settings(settings, GANETI_USE_HOTPLUG=True): |
272 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
273 |
user=vm.userid) |
274 |
self.assertBadRequest(response)
|
275 |
|
276 |
def test_create_port_without_device(self): |
277 |
net = dbmf.NetworkWithSubnetFactory(userid="test_user",
|
278 |
public=False,
|
279 |
subnet__cidr="192.168.2.0/24",
|
280 |
subnet__gateway=None,
|
281 |
subnet__pool__size=3,
|
282 |
subnet__pool__offset=1)
|
283 |
request = { |
284 |
"port": {
|
285 |
"name": "port_with_address", |
286 |
"network_id": str(net.id), |
287 |
} |
288 |
} |
289 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
290 |
user="test_user")
|
291 |
self.assertEqual(response.status_code, 201) |
292 |
new_port = json.loads(response.content)["port"]
|
293 |
self.assertEqual(new_port["device_id"], None) |
294 |
# And with address
|
295 |
request["port"]["fixed_ips"] = [{"ip_address": "192.168.2.2"}] |
296 |
with override_settings(settings, GANETI_USE_HOTPLUG=True): |
297 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
298 |
user="test_user")
|
299 |
self.assertEqual(response.status_code, 201) |
300 |
new_port = json.loads(response.content)["port"]
|
301 |
self.assertEqual(new_port["device_id"], None) |
302 |
self.assertEqual(new_port["fixed_ips"][0]["ip_address"], "192.168.2.2") |
303 |
|
304 |
def test_add_nic_to_deleted_network(self): |
305 |
user = 'userr'
|
306 |
vm = dbmf.VirtualMachineFactory(name='yo', userid=user,
|
307 |
operstate="ACTIVE")
|
308 |
net = dbmf.NetworkFactory(state='ACTIVE', userid=user,
|
309 |
deleted=True)
|
310 |
request = { |
311 |
"port": {
|
312 |
"device_id": str(vm.id), |
313 |
"name": "port1", |
314 |
"network_id": str(net.id) |
315 |
} |
316 |
} |
317 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
318 |
user=net.userid) |
319 |
self.assertBadRequest(response)
|
320 |
|
321 |
def test_add_nic_to_public_network(self): |
322 |
user = 'userr'
|
323 |
vm = dbmf.VirtualMachineFactory(name='yo', userid=user)
|
324 |
net = dbmf.NetworkFactory(state='ACTIVE', userid=user, public=True) |
325 |
request = { |
326 |
"port": {
|
327 |
"device_id": str(vm.id), |
328 |
"name": "port1", |
329 |
"network_id": str(net.id) |
330 |
} |
331 |
} |
332 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
333 |
user=net.userid) |
334 |
self.assertBadRequest(response)
|
335 |
#self.assertFault(response, 403, 'forbidden')
|
336 |
|
337 |
def test_add_nic_malformed_2(self): |
338 |
user = 'userr'
|
339 |
vm = dbmf.VirtualMachineFactory(name='yo', userid=user)
|
340 |
net = dbmf.NetworkFactory(state='ACTIVE', userid=user)
|
341 |
request = { |
342 |
"port": {
|
343 |
"device_id": str(vm.id), |
344 |
"name": "port1" |
345 |
} |
346 |
} |
347 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
348 |
user=net.userid) |
349 |
self.assertBadRequest(response)
|
350 |
|
351 |
def test_add_nic_not_active(self): |
352 |
"""Test connecting VM to non-active network"""
|
353 |
user = 'dummy'
|
354 |
vm = dbmf.VirtualMachineFactory(name='yo', userid=user)
|
355 |
net = dbmf.NetworkFactory(state='PENDING', userid=user)
|
356 |
request = { |
357 |
"port": {
|
358 |
"device_id": str(vm.id), |
359 |
"name": "port1", |
360 |
"network_id": str(net.id) |
361 |
} |
362 |
} |
363 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
364 |
user=net.userid) |
365 |
# Test that returns BuildInProgress
|
366 |
self.assertEqual(response.status_code, 409) |
367 |
|
368 |
# def test_add_nic_full_network(self, mrapi):
|
369 |
# """Test connecting VM to a full network"""
|
370 |
# user = 'userr'
|
371 |
# vm = dbmf.VirtualMachineFactory(name='yo', userid=user,
|
372 |
# operstate="STARTED")
|
373 |
# net = dbmf.NetworkFactory(state='ACTIVE', subnet='10.0.0.0/30',
|
374 |
# userid=user, dhcp=True)
|
375 |
# pool = net.get_pool()
|
376 |
# while not pool.empty():
|
377 |
# pool.get()
|
378 |
# pool.save()
|
379 |
# pool = net.get_pool()
|
380 |
# self.assertTrue(pool.empty())
|
381 |
# request = {'add': {'serverRef': vm.id}}
|
382 |
# response = self.mypost('networks/%d/action' % net.id,
|
383 |
# net.userid, json.dumps(request), 'json')
|
384 |
# # Test that returns OverLimit
|
385 |
# self.assertEqual(response.status_code, 413)
|
386 |
# self.assertFalse(mrapi.called)
|