root / snf-cyclades-app / synnefo / api / tests / ports.py @ b9364b15
History | View | Annotate | Download (12.8 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 |
35 |
from django.utils import simplejson as json |
36 |
from synnefo.cyclades_settings import cyclades_services |
37 |
from synnefo.lib.services import get_service_path |
38 |
from synnefo.lib import join_urls |
39 |
from mock import patch |
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 |
@patch("synnefo.db.models.get_rapi_client") |
67 |
def test_delete_port(self, mrapi): |
68 |
nic = dbmf.NetworkInterfaceFactory.create(device_owner='vm')
|
69 |
url = join_urls(PORTS_URL, str(nic.id))
|
70 |
response = self.delete(url, user=nic.network.userid)
|
71 |
self.assertEqual(response.status_code, 204) |
72 |
|
73 |
def test_delete_port_from_nonvm(self): |
74 |
nic = dbmf.NetworkInterfaceFactory.create(device_owner='router')
|
75 |
url = join_urls(PORTS_URL, str(nic.id))
|
76 |
response = self.delete(url, user=nic.network.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.create(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.network.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.create() |
96 |
nic = dbmf.NetworkInterfaceFactory.create(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.network.userid) |
103 |
self.assertEqual(response.status_code, 404) |
104 |
|
105 |
def test_update_port_sg(self): |
106 |
sg1 = dbmf.SecurityGroupFactory.create() |
107 |
sg2 = dbmf.SecurityGroupFactory.create() |
108 |
sg3 = dbmf.SecurityGroupFactory.create() |
109 |
nic = dbmf.NetworkInterfaceFactory.create(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.network.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.create(public=False)
|
134 |
dbmf.IPv4SubnetFactory.create(network=net) |
135 |
dbmf.IPv6SubnetFactory.create(network=net) |
136 |
sg1 = dbmf.SecurityGroupFactory.create() |
137 |
sg2 = dbmf.SecurityGroupFactory.create() |
138 |
vm = dbmf.VirtualMachineFactory.create(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 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
148 |
user=net.userid) |
149 |
self.assertEqual(response.status_code, 201) |
150 |
|
151 |
def test_create_port_public_net_no_ip(self): |
152 |
net = dbmf.NetworkFactory.create(public=True)
|
153 |
vm = dbmf.VirtualMachineFactory.create(userid=net.userid) |
154 |
request = { |
155 |
"port": {
|
156 |
"name": "port1", |
157 |
"network_id": str(net.id), |
158 |
"device_id": str(vm.id), |
159 |
} |
160 |
} |
161 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
162 |
user=net.userid) |
163 |
self.assertEqual(response.status_code, 400) |
164 |
|
165 |
def test_create_port_public_net_wrong_ip(self): |
166 |
net = dbmf.NetworkFactory.create(public=True)
|
167 |
vm = dbmf.VirtualMachineFactory.create(userid=net.userid) |
168 |
request = { |
169 |
"port": {
|
170 |
"name": "port1", |
171 |
"network_id": str(net.id), |
172 |
"device_id": str(vm.id), |
173 |
"fixed_ips": [{"ip_address": "8.8.8.8"}] |
174 |
} |
175 |
} |
176 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
177 |
user=net.userid) |
178 |
self.assertEqual(response.status_code, 404) |
179 |
|
180 |
def test_create_port_public_net_conflict(self): |
181 |
net = dbmf.NetworkFactory.create(public=True)
|
182 |
fip = dbmf.FloatingIPFactory(nic=None, userid=net.userid)
|
183 |
vm = dbmf.VirtualMachineFactory.create(userid=net.userid) |
184 |
request = { |
185 |
"port": {
|
186 |
"name": "port1", |
187 |
"network_id": str(net.id), |
188 |
"device_id": str(vm.id), |
189 |
"fixed_ips": [{"ip_address": fip.address}] |
190 |
} |
191 |
} |
192 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
193 |
user=net.userid) |
194 |
self.assertEqual(response.status_code, 409) |
195 |
|
196 |
def test_create_port_public_net_taken_ip(self): |
197 |
net = dbmf.NetworkFactory.create(public=True)
|
198 |
fip = dbmf.FloatingIPFactory(network=net, userid=net.userid) |
199 |
vm = dbmf.VirtualMachineFactory.create(userid=net.userid) |
200 |
request = { |
201 |
"port": {
|
202 |
"name": "port1", |
203 |
"network_id": str(net.id), |
204 |
"device_id": str(vm.id), |
205 |
"fixed_ips": [{"ip_address": fip.address}] |
206 |
} |
207 |
} |
208 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
209 |
user=net.userid) |
210 |
self.assertEqual(response.status_code, 400) |
211 |
|
212 |
@patch("synnefo.db.models.get_rapi_client") |
213 |
def test_create_port_public_net(self, mrapi): |
214 |
net = dbmf.NetworkFactory.create(public=True)
|
215 |
fip = dbmf.FloatingIPFactory(network=net, nic=None, userid=net.userid)
|
216 |
vm = dbmf.VirtualMachineFactory.create(userid=net.userid) |
217 |
request = { |
218 |
"port": {
|
219 |
"name": "port1", |
220 |
"network_id": str(net.id), |
221 |
"device_id": str(vm.id), |
222 |
"fixed_ips": [{"ip_address": fip.address}] |
223 |
} |
224 |
} |
225 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
226 |
user=net.userid) |
227 |
self.assertEqual(response.status_code, 201) |
228 |
|
229 |
def test_add_nic_to_deleted_network(self): |
230 |
user = 'userr'
|
231 |
vm = dbmf.VirtualMachineFactory(name='yo', userid=user,
|
232 |
operstate="ACTIVE")
|
233 |
net = dbmf.NetworkFactory(state='ACTIVE', userid=user,
|
234 |
deleted=True)
|
235 |
request = { |
236 |
"port": {
|
237 |
"device_id": str(vm.id), |
238 |
"name": "port1", |
239 |
"network_id": str(net.id) |
240 |
} |
241 |
} |
242 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
243 |
user=net.userid) |
244 |
self.assertBadRequest(response)
|
245 |
|
246 |
def test_add_nic_to_public_network(self): |
247 |
user = 'userr'
|
248 |
vm = dbmf.VirtualMachineFactory(name='yo', userid=user)
|
249 |
net = dbmf.NetworkFactory(state='ACTIVE', userid=user, public=True) |
250 |
request = { |
251 |
"port": {
|
252 |
"device_id": str(vm.id), |
253 |
"name": "port1", |
254 |
"network_id": str(net.id) |
255 |
} |
256 |
} |
257 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
258 |
user=net.userid) |
259 |
self.assertBadRequest(response)
|
260 |
#self.assertFault(response, 403, 'forbidden')
|
261 |
|
262 |
def test_add_nic_malformed_1(self): |
263 |
user = 'userr'
|
264 |
dbmf.VirtualMachineFactory(name='yo', userid=user)
|
265 |
net = dbmf.NetworkFactory(state='ACTIVE', userid=user)
|
266 |
request = { |
267 |
"port": {
|
268 |
"name": "port1", |
269 |
"network_id": str(net.id) |
270 |
} |
271 |
} |
272 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
273 |
user=net.userid) |
274 |
self.assertBadRequest(response)
|
275 |
|
276 |
def test_add_nic_malformed_2(self): |
277 |
user = 'userr'
|
278 |
vm = dbmf.VirtualMachineFactory(name='yo', userid=user)
|
279 |
net = dbmf.NetworkFactory(state='ACTIVE', userid=user)
|
280 |
request = { |
281 |
"port": {
|
282 |
"device_id": str(vm.id), |
283 |
"name": "port1" |
284 |
} |
285 |
} |
286 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
287 |
user=net.userid) |
288 |
self.assertBadRequest(response)
|
289 |
|
290 |
def test_add_nic_not_active(self): |
291 |
"""Test connecting VM to non-active network"""
|
292 |
user = 'dummy'
|
293 |
vm = dbmf.VirtualMachineFactory(name='yo', userid=user)
|
294 |
net = dbmf.NetworkFactory(state='PENDING', userid=user)
|
295 |
request = { |
296 |
"port": {
|
297 |
"device_id": str(vm.id), |
298 |
"name": "port1", |
299 |
"network_id": str(net.id) |
300 |
} |
301 |
} |
302 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
303 |
user=net.userid) |
304 |
# Test that returns BuildInProgress
|
305 |
self.assertEqual(response.status_code, 409) |
306 |
|
307 |
# def test_add_nic_full_network(self, mrapi):
|
308 |
# """Test connecting VM to a full network"""
|
309 |
# user = 'userr'
|
310 |
# vm = dbmf.VirtualMachineFactory(name='yo', userid=user,
|
311 |
# operstate="STARTED")
|
312 |
# net = dbmf.NetworkFactory(state='ACTIVE', subnet='10.0.0.0/30',
|
313 |
# userid=user, dhcp=True)
|
314 |
# pool = net.get_pool()
|
315 |
# while not pool.empty():
|
316 |
# pool.get()
|
317 |
# pool.save()
|
318 |
# pool = net.get_pool()
|
319 |
# self.assertTrue(pool.empty())
|
320 |
# request = {'add': {'serverRef': vm.id}}
|
321 |
# response = self.mypost('networks/%d/action' % net.id,
|
322 |
# net.userid, json.dumps(request), 'json')
|
323 |
# # Test that returns OverLimit
|
324 |
# self.assertEqual(response.status_code, 413)
|
325 |
# self.assertFalse(mrapi.called)
|