root / snf-cyclades-app / synnefo / api / tests / floating_ips.py @ b9364b15
History | View | Annotate | Download (18.1 kB)
1 |
# Copyright 2012 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.
|
33 |
|
34 |
from django.utils import simplejson as json |
35 |
from snf_django.utils.testing import BaseAPITest, mocked_quotaholder |
36 |
from synnefo.db.models import IPAddress |
37 |
from synnefo.db import models_factory as mf |
38 |
from synnefo.db.models_factory import (NetworkFactory, |
39 |
VirtualMachineFactory) |
40 |
|
41 |
from mock import patch, Mock |
42 |
from functools import partial |
43 |
|
44 |
from synnefo.cyclades_settings import cyclades_services |
45 |
from synnefo.lib.services import get_service_path |
46 |
from synnefo.lib import join_urls |
47 |
|
48 |
|
49 |
compute_path = get_service_path(cyclades_services, "compute", version="v2.0") |
50 |
URL = join_urls(compute_path, "floatingips")
|
51 |
NETWORKS_URL = join_urls(compute_path, "networks")
|
52 |
SERVERS_URL = join_urls(compute_path, "servers")
|
53 |
|
54 |
|
55 |
floating_ips = IPAddress.objects.filter(floating_ip=True)
|
56 |
FloatingIPPoolFactory = partial(NetworkFactory, public=True, deleted=False, |
57 |
floating_ip_pool=True)
|
58 |
|
59 |
|
60 |
class FloatingIPAPITest(BaseAPITest): |
61 |
def setUp(self): |
62 |
self.pool = mf.NetworkWithSubnetFactory(floating_ip_pool=True, |
63 |
public=True,
|
64 |
subnet__cidr="192.168.2.0/24",
|
65 |
subnet__gateway="192.168.2.1")
|
66 |
|
67 |
def test_no_floating_ip(self): |
68 |
response = self.get(URL)
|
69 |
self.assertSuccess(response)
|
70 |
self.assertEqual(json.loads(response.content)["floating_ips"], []) |
71 |
|
72 |
def test_list_ips(self): |
73 |
ip = mf.IPv4AddressFactory(userid="user1", floating_ip=True) |
74 |
with mocked_quotaholder():
|
75 |
response = self.get(URL, "user1") |
76 |
self.assertSuccess(response)
|
77 |
api_ip = json.loads(response.content)["floating_ips"][0] |
78 |
self.assertEqual(api_ip,
|
79 |
{"instance_id": str(ip.nic.machine_id), |
80 |
"floating_ip_address": ip.address,
|
81 |
"fixed_ip_address": None, |
82 |
"id": str(ip.id), |
83 |
"port_id": str(ip.nic.id), |
84 |
"floating_network_id": str(ip.network_id)}) |
85 |
|
86 |
def test_get_ip(self): |
87 |
ip = mf.IPv4AddressFactory(userid="user1", floating_ip=True) |
88 |
with mocked_quotaholder():
|
89 |
response = self.get(URL + "/%s" % ip.id, "user1") |
90 |
self.assertSuccess(response)
|
91 |
api_ip = json.loads(response.content)["floating_ip"]
|
92 |
self.assertEqual(api_ip,
|
93 |
{"instance_id": str(ip.nic.machine_id), |
94 |
"floating_ip_address": ip.address,
|
95 |
"fixed_ip_address": None, |
96 |
"id": str(ip.id), |
97 |
"port_id": str(ip.nic.id), |
98 |
"floating_network_id": str(ip.network_id)}) |
99 |
|
100 |
def test_wrong_user(self): |
101 |
ip = mf.IPv4AddressFactory(userid="user1", floating_ip=True) |
102 |
response = self.delete(URL + "/%s" % ip.id, "user2") |
103 |
self.assertItemNotFound(response)
|
104 |
|
105 |
def test_deleted_ip(self): |
106 |
ip = mf.IPv4AddressFactory(userid="user1", floating_ip=True, |
107 |
deleted=True)
|
108 |
response = self.delete(URL + "/%s" % ip.id, "user1") |
109 |
self.assertItemNotFound(response)
|
110 |
|
111 |
def test_reserve(self): |
112 |
request = {"floatingip": {
|
113 |
"floating_network_id": self.pool.id} |
114 |
} |
115 |
with mocked_quotaholder():
|
116 |
response = self.post(URL, "test_user", json.dumps(request), "json") |
117 |
self.assertSuccess(response)
|
118 |
api_ip = json.loads(response.content, encoding="utf-8")["floating_ip"] |
119 |
ip = floating_ips.get() |
120 |
self.assertEqual(ip.address, "192.168.2.2") |
121 |
self.assertEqual(ip.nic, None) |
122 |
self.assertEqual(ip.network, self.pool) |
123 |
self.assertEqual(api_ip,
|
124 |
{"instance_id": None, |
125 |
"floating_ip_address": "192.168.2.2", |
126 |
"fixed_ip_address": None, |
127 |
"id": str(ip.id), |
128 |
"port_id": None, |
129 |
"floating_network_id": str(self.pool.id)}) |
130 |
|
131 |
def test_reserve_no_pool(self): |
132 |
# Network is not a floating IP pool
|
133 |
pool2 = mf.NetworkWithSubnetFactory(floating_ip_pool=False,
|
134 |
public=True,
|
135 |
subnet__cidr="192.168.2.0/24",
|
136 |
subnet__gateway="192.168.2.1")
|
137 |
request = {"floatingip": {
|
138 |
'floating_network_id': pool2.id}
|
139 |
} |
140 |
response = self.post(URL, "test_user", json.dumps(request), "json") |
141 |
self.assertEqual(response.status_code, 404) |
142 |
|
143 |
# Full network
|
144 |
net = mf.NetworkWithSubnetFactory(floating_ip_pool=True,
|
145 |
public=True,
|
146 |
subnet__cidr="192.168.2.0/31",
|
147 |
subnet__gateway="192.168.2.1",
|
148 |
subnet__pool__size=0)
|
149 |
request = {"floatingip": {
|
150 |
'floating_network_id': net.id}
|
151 |
} |
152 |
response = self.post(URL, "test_user", json.dumps(request), "json") |
153 |
self.assertConflict(response)
|
154 |
|
155 |
def test_reserve_with_address(self): |
156 |
request = {"floatingip": {
|
157 |
"floating_network_id": self.pool.id, |
158 |
"floating_ip_address": "192.168.2.10"} |
159 |
} |
160 |
with mocked_quotaholder():
|
161 |
response = self.post(URL, "test_user", json.dumps(request), "json") |
162 |
self.assertSuccess(response)
|
163 |
ip = floating_ips.get() |
164 |
self.assertEqual(json.loads(response.content)["floating_ip"], |
165 |
{"instance_id": None, |
166 |
"floating_ip_address": "192.168.2.10", |
167 |
"fixed_ip_address": None, |
168 |
"id": str(ip.id), |
169 |
"port_id": None, |
170 |
"floating_network_id": str(self.pool.id)}) |
171 |
|
172 |
# Already reserved
|
173 |
with mocked_quotaholder():
|
174 |
response = self.post(URL, "test_user", json.dumps(request), "json") |
175 |
self.assertFault(response, 409, "conflict") |
176 |
|
177 |
# Used by instance
|
178 |
self.pool.reserve_address("192.168.2.20") |
179 |
request = {"floatingip": {
|
180 |
"floating_network_id": self.pool.id, |
181 |
"floating_ip_address": "192.168.2.20"} |
182 |
} |
183 |
with mocked_quotaholder():
|
184 |
response = self.post(URL, "test_user", json.dumps(request), "json") |
185 |
self.assertFault(response, 409, "conflict") |
186 |
|
187 |
# Address out of pool
|
188 |
request = {"floatingip": {
|
189 |
"floating_network_id": self.pool.id, |
190 |
"floating_ip_address": "192.168.3.5"} |
191 |
} |
192 |
with mocked_quotaholder():
|
193 |
response = self.post(URL, "test_user", json.dumps(request), "json") |
194 |
self.assertBadRequest(response)
|
195 |
|
196 |
@patch("synnefo.db.models.get_rapi_client") |
197 |
def test_reserve_and_connect(self, mrapi): |
198 |
vm = mf.VirtualMachineFactory(userid="test_user")
|
199 |
request = {"floatingip": {
|
200 |
"floating_network_id": self.pool.id, |
201 |
"floating_ip_address": "192.168.2.12", |
202 |
"device_id": vm.id}
|
203 |
} |
204 |
response = self.post(URL, "test_user", json.dumps(request), "json") |
205 |
ip = floating_ips.get() |
206 |
api_ip = json.loads(response.content, "utf-8")["floating_ip"] |
207 |
self.assertEqual(api_ip,
|
208 |
{"instance_id": str(vm.id), |
209 |
"floating_ip_address": "192.168.2.12", |
210 |
"fixed_ip_address": None, |
211 |
"id": str(ip.id), |
212 |
"port_id": str(vm.nics.all()[0].id), |
213 |
"floating_network_id": str(self.pool.id)}) |
214 |
|
215 |
@patch("synnefo.db.models.get_rapi_client") |
216 |
def test_update_attach(self, mrapi): |
217 |
ip = mf.IPv4AddressFactory(userid="user1", floating_ip=True, nic=None) |
218 |
vm = mf.VirtualMachineFactory(userid="user1")
|
219 |
request = {"floatingip": {
|
220 |
"device_id": vm.id}
|
221 |
} |
222 |
with mocked_quotaholder():
|
223 |
response = self.put(URL + "/%s" % ip.id, "user1", |
224 |
json.dumps(request), "json")
|
225 |
self.assertEqual(response.status_code, 202) |
226 |
|
227 |
def test_update_attach_conflict(self): |
228 |
ip = mf.IPv4AddressFactory(userid="user1", floating_ip=True) |
229 |
vm = mf.VirtualMachineFactory(userid="user1")
|
230 |
request = {"floatingip": {
|
231 |
"device_id": vm.id}
|
232 |
} |
233 |
with mocked_quotaholder():
|
234 |
response = self.put(URL + "/%s" % ip.id, "user1", |
235 |
json.dumps(request), "json")
|
236 |
self.assertEqual(response.status_code, 409) |
237 |
|
238 |
@patch("synnefo.db.models.get_rapi_client") |
239 |
def test_update_dettach(self, mrapi): |
240 |
ip = mf.IPv4AddressFactory(userid="user1", floating_ip=True) |
241 |
request = {"floatingip": {
|
242 |
"device_id": None} |
243 |
} |
244 |
mrapi().ModifyInstance.return_value = 42
|
245 |
with mocked_quotaholder():
|
246 |
response = self.put(URL + "/%s" % ip.id, "user1", |
247 |
json.dumps(request), "json")
|
248 |
self.assertEqual(response.status_code, 202) |
249 |
|
250 |
def test_update_dettach_unassociated(self): |
251 |
ip = mf.IPv4AddressFactory(userid="user1", floating_ip=True, nic=None) |
252 |
request = {"floatingip": {}}
|
253 |
with mocked_quotaholder():
|
254 |
response = self.put(URL + "/%s" % ip.id, "user1", |
255 |
json.dumps(request), "json")
|
256 |
self.assertEqual(response.status_code, 400) |
257 |
|
258 |
def test_release_in_use(self): |
259 |
ip = mf.IPv4AddressFactory(userid="user1", floating_ip=True) |
260 |
vm = mf.VirtualMachineFactory(userid="user1")
|
261 |
request = {"floatingip": {
|
262 |
"device_id": vm.id}
|
263 |
} |
264 |
with mocked_quotaholder():
|
265 |
response = self.put(URL + "/%s" % ip.id, "user1", |
266 |
json.dumps(request), "json")
|
267 |
self.assertEqual(response.status_code, 409) |
268 |
|
269 |
@patch("synnefo.db.models.get_rapi_client") |
270 |
def test_update_dettach(self, mrapi): |
271 |
ip = mf.IPv4AddressFactory(userid="user1", floating_ip=True) |
272 |
request = {"floatingip": {
|
273 |
"device_id": None} |
274 |
} |
275 |
mrapi().ModifyInstance.return_value = 42
|
276 |
with mocked_quotaholder():
|
277 |
response = self.put(URL + "/%s" % ip.id, "user1", |
278 |
json.dumps(request), "json")
|
279 |
print response.content
|
280 |
self.assertEqual(response.status_code, 202) |
281 |
|
282 |
def test_update_dettach_unassociated(self): |
283 |
ip = mf.IPv4AddressFactory(userid="user1", floating_ip=True, nic=None) |
284 |
request = {"floatingip": {}}
|
285 |
with mocked_quotaholder():
|
286 |
response = self.put(URL + "/%s" % ip.id, "user1", |
287 |
json.dumps(request), "json")
|
288 |
self.assertEqual(response.status_code, 400) |
289 |
|
290 |
def test_release_in_use(self): |
291 |
ip = mf.IPv4AddressFactory(userid="user1", floating_ip=True) |
292 |
vm = ip.nic.machine |
293 |
with mocked_quotaholder():
|
294 |
response = self.delete(URL + "/%s" % ip.id, ip.userid) |
295 |
self.assertFault(response, 409, "conflict") |
296 |
|
297 |
def test_release(self): |
298 |
ip = mf.IPv4AddressFactory(userid="user1", floating_ip=True, nic=None) |
299 |
with mocked_quotaholder():
|
300 |
response = self.delete(URL + "/%s" % ip.id, ip.userid) |
301 |
self.assertSuccess(response)
|
302 |
ips_after = floating_ips.filter(id=ip.id) |
303 |
self.assertEqual(len(ips_after), 0) |
304 |
|
305 |
@patch("synnefo.logic.backend", Mock()) |
306 |
def test_delete_network_with_floating_ips(self): |
307 |
ip = mf.IPv4AddressFactory(userid="user1", floating_ip=True, |
308 |
network=self.pool, nic=None) |
309 |
# Mark the network as non-pubic to not get 403
|
310 |
network = ip.network |
311 |
network.public = False
|
312 |
network.save() |
313 |
# Can not remove network with floating IPs
|
314 |
with mocked_quotaholder():
|
315 |
response = self.delete(NETWORKS_URL + "/%s" % self.pool.id, |
316 |
self.pool.userid)
|
317 |
self.assertConflict(response)
|
318 |
# But we can with only deleted Floating Ips
|
319 |
ip.deleted = True
|
320 |
ip.save() |
321 |
with mocked_quotaholder():
|
322 |
response = self.delete(NETWORKS_URL + "/%s" % self.pool.id, |
323 |
self.pool.userid)
|
324 |
self.assertSuccess(response)
|
325 |
|
326 |
'''
|
327 |
POOLS_URL = join_urls(compute_path, "os-floating-ip-pools")
|
328 |
|
329 |
|
330 |
class FloatingIPPoolsAPITest(BaseAPITest):
|
331 |
def test_no_pool(self):
|
332 |
response = self.get(POOLS_URL)
|
333 |
self.assertSuccess(response)
|
334 |
self.assertEqual(json.loads(response.content)["floating_ip_pools"], [])
|
335 |
|
336 |
def test_list_pools(self):
|
337 |
net = mf.NetworkWithSubnetFactory(floating_ip_pool=True,
|
338 |
public=True,
|
339 |
subnet__cidr="192.168.2.0/30",
|
340 |
subnet__gateway="192.168.2.1",
|
341 |
subnet__pool__size=1,
|
342 |
subnet__pool__offset=1)
|
343 |
mf.NetworkWithSubnetFactory(public=True, deleted=True)
|
344 |
mf.NetworkWithSubnetFactory(public=False, deleted=False)
|
345 |
mf.NetworkWithSubnetFactory(public=True, floating_ip_pool=False)
|
346 |
response = self.get(POOLS_URL)
|
347 |
self.assertSuccess(response)
|
348 |
self.assertEqual(json.loads(response.content)["floating_ip_pools"],
|
349 |
[{"name": str(net.id), "size": 1, "free": 1}])
|
350 |
|
351 |
|
352 |
class FloatingIPActionsTest(BaseAPITest):
|
353 |
def setUp(self):
|
354 |
self.vm = VirtualMachineFactory()
|
355 |
self.vm.operstate = "ACTIVE"
|
356 |
self.vm.save()
|
357 |
|
358 |
def test_bad_request(self):
|
359 |
url = SERVERS_URL + "/%s/action" % self.vm.id
|
360 |
response = self.post(url, self.vm.userid, json.dumps({}), "json")
|
361 |
self.assertBadRequest(response)
|
362 |
response = self.post(url, self.vm.userid,
|
363 |
json.dumps({"addFloatingIp": {}}),
|
364 |
"json")
|
365 |
self.assertBadRequest(response)
|
366 |
|
367 |
@patch('synnefo.logic.rapi_pool.GanetiRapiClient')
|
368 |
def test_add_floating_ip(self, mock):
|
369 |
# Not exists
|
370 |
url = SERVERS_URL + "/%s/action" % self.vm.id
|
371 |
request = {"addFloatingIp": {"address": "10.0.0.1"}}
|
372 |
response = self.post(url, self.vm.userid, json.dumps(request), "json")
|
373 |
self.assertItemNotFound(response)
|
374 |
# In use
|
375 |
ip = mf.IPv4AddressFactory(floating_ip=True, userid=self.vm.userid)
|
376 |
request = {"addFloatingIp": {"address": ip.address}}
|
377 |
response = self.post(url, self.vm.userid, json.dumps(request), "json")
|
378 |
self.assertConflict(response)
|
379 |
# Success
|
380 |
ip = mf.IPv4AddressFactory(floating_ip=True, nic=None,
|
381 |
userid=self.vm.userid)
|
382 |
request = {"addFloatingIp": {"address": ip.address}}
|
383 |
mock().ModifyInstance.return_value = 1
|
384 |
response = self.post(url, self.vm.userid, json.dumps(request), "json")
|
385 |
self.assertEqual(response.status_code, 202)
|
386 |
ip_after = floating_ips.get(id=ip.id)
|
387 |
self.assertEqual(ip_after.nic.machine, self.vm)
|
388 |
nic = self.vm.nics.get()
|
389 |
nic.state = "ACTIVE"
|
390 |
nic.save()
|
391 |
response = self.get(SERVERS_URL + "/%s" % self.vm.id,
|
392 |
self.vm.userid)
|
393 |
self.assertSuccess(response)
|
394 |
nic = json.loads(response.content)["server"]["attachments"][0]
|
395 |
self.assertEqual(nic["OS-EXT-IPS:type"], "floating")
|
396 |
|
397 |
@patch('synnefo.logic.rapi_pool.GanetiRapiClient')
|
398 |
def test_remove_floating_ip(self, mock):
|
399 |
# Not exists
|
400 |
url = SERVERS_URL + "/%s/action" % self.vm.id
|
401 |
request = {"removeFloatingIp": {"address": "10.0.0.1"}}
|
402 |
response = self.post(url, self.vm.userid, json.dumps(request), "json")
|
403 |
self.assertBadRequest(response)
|
404 |
# Not In Use
|
405 |
ip = mf.IPv4AddressFactory(floating_ip=True, nic=None,
|
406 |
userid=self.vm.userid)
|
407 |
request = {"removeFloatingIp": {"address": ip.address}}
|
408 |
response = self.post(url, self.vm.userid, json.dumps(request), "json")
|
409 |
self.assertBadRequest(response)
|
410 |
# Success
|
411 |
ip = mf.IPv4AddressFactory(floating_ip=True,
|
412 |
userid=self.vm.userid, nic__machine=self.vm)
|
413 |
request = {"removeFloatingIp": {"address": ip.address}}
|
414 |
mock().ModifyInstance.return_value = 2
|
415 |
response = self.post(url, self.vm.userid, json.dumps(request), "json")
|
416 |
self.assertEqual(response.status_code, 202)
|
417 |
# Yet used. Wait for the callbacks
|
418 |
ip_after = floating_ips.get(id=ip.id)
|
419 |
self.assertEqual(ip_after.nic.machine, self.vm)
|
420 |
'''
|