root / snf-cyclades-app / synnefo / api / test / floating_ips.py @ acda838e
History | View | Annotate | Download (5 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 |
import json |
35 |
|
36 |
from snf_django.utils.testing import BaseAPITest, mocked_quotaholder |
37 |
from synnefo.db.models import FloatingIP |
38 |
from synnefo.db.models_factory import FloatingIPFactory, NetworkFactory |
39 |
|
40 |
|
41 |
URL = "/api/v1.1/os-floating-ips"
|
42 |
|
43 |
|
44 |
class FloatingIPAPITest(BaseAPITest): |
45 |
def test_no_floating_ip(self): |
46 |
response = self.get(URL)
|
47 |
self.assertSuccess(response)
|
48 |
self.assertEqual(json.loads(response.content)["floating_ips"], []) |
49 |
|
50 |
def test_list_ips(self): |
51 |
ip = FloatingIPFactory(userid="user1")
|
52 |
FloatingIPFactory(userid="user1", deleted=True) |
53 |
with mocked_quotaholder():
|
54 |
response = self.get(URL, "user1") |
55 |
self.assertSuccess(response)
|
56 |
api_ip = json.loads(response.content)["floating_ips"][0] |
57 |
self.assertEqual(api_ip,
|
58 |
{"instance_id": str(ip.machine.id), "ip": ip.ipv4, |
59 |
"fixed_ip": None, "id": str(ip.id), "pool": |
60 |
str(ip.network.id)})
|
61 |
|
62 |
def test_get_ip(self): |
63 |
ip = FloatingIPFactory(userid="user1")
|
64 |
with mocked_quotaholder():
|
65 |
response = self.get(URL + "/%s" % ip.id, "user1") |
66 |
self.assertSuccess(response)
|
67 |
api_ip = json.loads(response.content)["floating_ip"]
|
68 |
self.assertEqual(api_ip,
|
69 |
{"instance_id": str(ip.machine.id), "ip": ip.ipv4, |
70 |
"fixed_ip": None, "id": str(ip.id), "pool": |
71 |
str(ip.network.id)})
|
72 |
|
73 |
def test_wrong_user(self): |
74 |
ip = FloatingIPFactory(userid="user1")
|
75 |
with mocked_quotaholder():
|
76 |
response = self.delete(URL + "/%s" % ip.id, "user2") |
77 |
self.assertItemNotFound(response)
|
78 |
|
79 |
def test_deleted_ip(self): |
80 |
ip = FloatingIPFactory(userid="user1", deleted=True) |
81 |
with mocked_quotaholder():
|
82 |
response = self.delete(URL + "/%s" % ip.id, "user1") |
83 |
self.assertItemNotFound(response)
|
84 |
|
85 |
def test_reserve(self): |
86 |
net = NetworkFactory(userid="test_user", subnet="192.168.2.0/24", |
87 |
gateway=None, public=True) |
88 |
request = {'pool': net.id}
|
89 |
with mocked_quotaholder():
|
90 |
response = self.post(URL, "test_user", json.dumps(request), "json") |
91 |
self.assertSuccess(response)
|
92 |
ip = FloatingIP.objects.get() |
93 |
self.assertEqual(ip.ipv4, "192.168.2.1") |
94 |
self.assertEqual(ip.machine, None) |
95 |
self.assertEqual(ip.network, net)
|
96 |
self.assertEqual(json.loads(response.content)["floating_ip"], |
97 |
{"instance_id": None, "ip": "192.168.2.1", |
98 |
"fixed_ip": None, "id": "1", "pool": "1"}) |
99 |
|
100 |
def test_reserve_full(self): |
101 |
net = NetworkFactory(userid="test_user", subnet="192.168.2.0/32", |
102 |
gateway=None, public=True) |
103 |
request = {'pool': net.id}
|
104 |
with mocked_quotaholder():
|
105 |
response = self.post(URL, "test_user", json.dumps(request), "json") |
106 |
self.assertEqual(response.status_code, 413) |
107 |
|
108 |
def test_release_in_use(self): |
109 |
ip = FloatingIPFactory() |
110 |
with mocked_quotaholder():
|
111 |
response = self.delete(URL + "/%s" % ip.id, ip.userid) |
112 |
self.assertFault(response, 409, "conflict") |
113 |
|
114 |
def test_release(self): |
115 |
ip = FloatingIPFactory(machine=None)
|
116 |
with mocked_quotaholder():
|
117 |
response = self.delete(URL + "/%s" % ip.id, ip.userid) |
118 |
self.assertSuccess(response)
|
119 |
ips_after = FloatingIP.objects.filter(id=ip.id) |
120 |
self.assertEqual(len(ips_after), 0) |