root / snf-cyclades-app / synnefo / api / test / floating_ips.py @ f8675683
History | View | Annotate | Download (13.8 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 |
VirtualMachineFactory, |
40 |
NetworkInterfaceFactory, |
41 |
BackendNetworkFactory) |
42 |
from mock import patch, Mock |
43 |
from functools import partial |
44 |
|
45 |
|
46 |
URL = "/api/v1.1/os-floating-ips"
|
47 |
|
48 |
FloatingIPPoolFactory = partial(NetworkFactory, public=True, deleted=False, |
49 |
floating_ip_pool=True)
|
50 |
|
51 |
|
52 |
class FloatingIPAPITest(BaseAPITest): |
53 |
def test_no_floating_ip(self): |
54 |
response = self.get(URL)
|
55 |
self.assertSuccess(response)
|
56 |
self.assertEqual(json.loads(response.content)["floating_ips"], []) |
57 |
|
58 |
def test_list_ips(self): |
59 |
ip = FloatingIPFactory(userid="user1")
|
60 |
FloatingIPFactory(userid="user1", deleted=True) |
61 |
with mocked_quotaholder():
|
62 |
response = self.get(URL, "user1") |
63 |
self.assertSuccess(response)
|
64 |
api_ip = json.loads(response.content)["floating_ips"][0] |
65 |
self.assertEqual(api_ip,
|
66 |
{"instance_id": str(ip.machine.id), "ip": ip.ipv4, |
67 |
"fixed_ip": None, "id": str(ip.id), "pool": |
68 |
str(ip.network.id)})
|
69 |
|
70 |
def test_get_ip(self): |
71 |
ip = FloatingIPFactory(userid="user1")
|
72 |
with mocked_quotaholder():
|
73 |
response = self.get(URL + "/%s" % ip.id, "user1") |
74 |
self.assertSuccess(response)
|
75 |
api_ip = json.loads(response.content)["floating_ip"]
|
76 |
self.assertEqual(api_ip,
|
77 |
{"instance_id": str(ip.machine.id), "ip": ip.ipv4, |
78 |
"fixed_ip": None, "id": str(ip.id), "pool": |
79 |
str(ip.network.id)})
|
80 |
|
81 |
def test_wrong_user(self): |
82 |
ip = FloatingIPFactory(userid="user1")
|
83 |
with mocked_quotaholder():
|
84 |
response = self.delete(URL + "/%s" % ip.id, "user2") |
85 |
self.assertItemNotFound(response)
|
86 |
|
87 |
def test_deleted_ip(self): |
88 |
ip = FloatingIPFactory(userid="user1", deleted=True) |
89 |
with mocked_quotaholder():
|
90 |
response = self.delete(URL + "/%s" % ip.id, "user1") |
91 |
self.assertItemNotFound(response)
|
92 |
|
93 |
def test_reserve(self): |
94 |
net = FloatingIPPoolFactory(userid="test_user",
|
95 |
subnet="192.168.2.0/24",
|
96 |
gateway=None)
|
97 |
request = {'pool': net.id}
|
98 |
with mocked_quotaholder():
|
99 |
response = self.post(URL, "test_user", json.dumps(request), "json") |
100 |
self.assertSuccess(response)
|
101 |
ip = FloatingIP.objects.get() |
102 |
self.assertEqual(ip.ipv4, "192.168.2.1") |
103 |
self.assertEqual(ip.machine, None) |
104 |
self.assertEqual(ip.network, net)
|
105 |
self.assertEqual(json.loads(response.content)["floating_ip"], |
106 |
{"instance_id": None, "ip": "192.168.2.1", |
107 |
"fixed_ip": None, "id": "1", "pool": "1"}) |
108 |
|
109 |
def test_reserve_no_pool(self): |
110 |
# No networks
|
111 |
with mocked_quotaholder():
|
112 |
response = self.post(URL, "test_user", json.dumps({}), "json") |
113 |
self.assertFault(response, 413, "overLimit") |
114 |
# Full network
|
115 |
FloatingIPPoolFactory(userid="test_user",
|
116 |
subnet="192.168.2.0/32",
|
117 |
gateway=None)
|
118 |
with mocked_quotaholder():
|
119 |
response = self.post(URL, "test_user", json.dumps({}), "json") |
120 |
self.assertFault(response, 413, "overLimit") |
121 |
# Success
|
122 |
net2 = FloatingIPPoolFactory(userid="test_user",
|
123 |
subnet="192.168.2.0/24",
|
124 |
gateway=None)
|
125 |
with mocked_quotaholder():
|
126 |
response = self.post(URL, "test_user", json.dumps({}), "json") |
127 |
self.assertSuccess(response)
|
128 |
self.assertEqual(json.loads(response.content)["floating_ip"], |
129 |
{"instance_id": None, "ip": "192.168.2.1", |
130 |
"fixed_ip": None, "id": "1", "pool": str(net2.id)}) |
131 |
|
132 |
def test_reserve_full(self): |
133 |
net = FloatingIPPoolFactory(userid="test_user",
|
134 |
subnet="192.168.2.0/32")
|
135 |
request = {'pool': net.id}
|
136 |
with mocked_quotaholder():
|
137 |
response = self.post(URL, "test_user", json.dumps(request), "json") |
138 |
self.assertEqual(response.status_code, 413) |
139 |
|
140 |
def test_reserve_with_address(self): |
141 |
net = FloatingIPPoolFactory(userid="test_user",
|
142 |
subnet="192.168.2.0/24")
|
143 |
request = {'pool': net.id, "address": "192.168.2.10"} |
144 |
with mocked_quotaholder():
|
145 |
response = self.post(URL, "test_user", json.dumps(request), "json") |
146 |
self.assertSuccess(response)
|
147 |
self.assertEqual(json.loads(response.content)["floating_ip"], |
148 |
{"instance_id": None, "ip": "192.168.2.10", |
149 |
"fixed_ip": None, "id": "1", "pool": "1"}) |
150 |
|
151 |
# Already reserved
|
152 |
FloatingIPFactory(network=net, ipv4="192.168.2.3")
|
153 |
request = {'pool': net.id, "address": "192.168.2.3"} |
154 |
with mocked_quotaholder():
|
155 |
response = self.post(URL, "test_user", json.dumps(request), "json") |
156 |
self.assertFault(response, 409, "conflict") |
157 |
|
158 |
# Already used
|
159 |
pool = net.get_pool() |
160 |
pool.reserve("192.168.2.5")
|
161 |
pool.save() |
162 |
# ..by another_user
|
163 |
nic = NetworkInterfaceFactory(network=net, ipv4="192.168.2.5",
|
164 |
machine__userid="test2")
|
165 |
request = {'pool': net.id, "address": "192.168.2.5"} |
166 |
with mocked_quotaholder():
|
167 |
response = self.post(URL, "test_user", json.dumps(request), "json") |
168 |
self.assertFault(response, 409, "conflict") |
169 |
# ..and by him
|
170 |
nic.delete() |
171 |
NetworkInterfaceFactory(network=net, ipv4="192.168.2.5",
|
172 |
machine__userid="test_user")
|
173 |
request = {'pool': net.id, "address": "192.168.2.5"} |
174 |
with mocked_quotaholder():
|
175 |
response = self.post(URL, "test_user", json.dumps(request), "json") |
176 |
self.assertSuccess(response)
|
177 |
|
178 |
# Address out of pool
|
179 |
request = {'pool': net.id, "address": "192.168.3.5"} |
180 |
with mocked_quotaholder():
|
181 |
response = self.post(URL, "test_user", json.dumps(request), "json") |
182 |
self.assertBadRequest(response)
|
183 |
|
184 |
def test_release_in_use(self): |
185 |
ip = FloatingIPFactory() |
186 |
vm = ip.machine |
187 |
vm.operstate = "ACTIVE"
|
188 |
vm.userid = ip.userid |
189 |
vm.save() |
190 |
vm.nics.create(index=0, ipv4=ip.ipv4, network=ip.network,
|
191 |
state="ACTIVE")
|
192 |
with mocked_quotaholder():
|
193 |
response = self.delete(URL + "/%s" % ip.id, ip.userid) |
194 |
self.assertFault(response, 409, "conflict") |
195 |
# Also send a notification to remove the NIC and assert that FIP is in
|
196 |
# use until notification from ganeti arrives
|
197 |
request = {"removeFloatingIp": {"address": ip.ipv4}} |
198 |
url = "/api/v1.1/servers/%s/action" % vm.id
|
199 |
with patch('synnefo.logic.rapi_pool.GanetiRapiClient') as c: |
200 |
c().ModifyInstance.return_value = 10
|
201 |
response = self.post(url, vm.userid, json.dumps(request),
|
202 |
"json")
|
203 |
self.assertEqual(response.status_code, 202) |
204 |
with mocked_quotaholder():
|
205 |
response = self.delete(URL + "/%s" % ip.id, ip.userid) |
206 |
self.assertFault(response, 409, "conflict") |
207 |
|
208 |
def test_release(self): |
209 |
ip = FloatingIPFactory(machine=None)
|
210 |
with mocked_quotaholder():
|
211 |
response = self.delete(URL + "/%s" % ip.id, ip.userid) |
212 |
self.assertSuccess(response)
|
213 |
ips_after = FloatingIP.objects.filter(id=ip.id) |
214 |
self.assertEqual(len(ips_after), 0) |
215 |
|
216 |
@patch("synnefo.logic.backend", Mock()) |
217 |
def test_delete_network_with_floating_ips(self): |
218 |
ip = FloatingIPFactory(machine=None, network__flavor="IP_LESS_ROUTED") |
219 |
net = ip.network |
220 |
# Can not remove network with floating IPs
|
221 |
with mocked_quotaholder():
|
222 |
response = self.delete("/api/v1.1/networks/%s" % net.id, |
223 |
net.userid) |
224 |
self.assertFault(response, 421, "networkInUse") |
225 |
# But we can with only deleted Floating Ips
|
226 |
ip.deleted = True
|
227 |
ip.save() |
228 |
with mocked_quotaholder():
|
229 |
response = self.delete("/api/v1.1/networks/%s" % net.id, |
230 |
net.userid) |
231 |
self.assertSuccess(response)
|
232 |
|
233 |
|
234 |
POOLS_URL = "/api/v1.1/os-floating-ip-pools"
|
235 |
|
236 |
|
237 |
class FloatingIPPoolsAPITest(BaseAPITest): |
238 |
def test_no_pool(self): |
239 |
response = self.get(POOLS_URL)
|
240 |
self.assertSuccess(response)
|
241 |
self.assertEqual(json.loads(response.content)["floating_ip_pools"], []) |
242 |
|
243 |
def test_list_pools(self): |
244 |
net = FloatingIPPoolFactory() |
245 |
NetworkFactory(public=True, deleted=True) |
246 |
NetworkFactory(public=False, deleted=False) |
247 |
NetworkFactory(public=True, deleted=False) |
248 |
response = self.get(POOLS_URL)
|
249 |
self.assertSuccess(response)
|
250 |
self.assertEqual(json.loads(response.content)["floating_ip_pools"], |
251 |
[{"name": str(net.id)}]) |
252 |
|
253 |
|
254 |
class FloatingIPActionsTest(BaseAPITest): |
255 |
def setUp(self): |
256 |
vm = VirtualMachineFactory() |
257 |
vm.operstate = "ACTIVE"
|
258 |
vm.save() |
259 |
self.vm = vm
|
260 |
|
261 |
def test_bad_request(self): |
262 |
url = "/api/v1.1/servers/%s/action" % self.vm.id |
263 |
response = self.post(url, self.vm.userid, json.dumps({}), "json") |
264 |
self.assertBadRequest(response)
|
265 |
response = self.post(url, self.vm.userid, |
266 |
json.dumps({"addFloatingIp": {}}),
|
267 |
"json")
|
268 |
self.assertBadRequest(response)
|
269 |
|
270 |
@patch('synnefo.logic.rapi_pool.GanetiRapiClient') |
271 |
def test_add_floating_ip(self, mock): |
272 |
# Not exists
|
273 |
url = "/api/v1.1/servers/%s/action" % self.vm.id |
274 |
request = {"addFloatingIp": {"address": "10.0.0.1"}} |
275 |
response = self.post(url, self.vm.userid, json.dumps(request), "json") |
276 |
self.assertItemNotFound(response)
|
277 |
# In use
|
278 |
vm1 = VirtualMachineFactory() |
279 |
ip1 = FloatingIPFactory(userid=self.vm.userid, machine=vm1)
|
280 |
BackendNetworkFactory(network=ip1.network, backend=vm1.backend, |
281 |
operstate='ACTIVE')
|
282 |
request = {"addFloatingIp": {"address": ip1.ipv4}} |
283 |
response = self.post(url, self.vm.userid, json.dumps(request), "json") |
284 |
self.assertFault(response, 409, "conflict") |
285 |
# Success
|
286 |
ip1 = FloatingIPFactory(userid=self.vm.userid, machine=None) |
287 |
BackendNetworkFactory(network=ip1.network, backend=self.vm.backend,
|
288 |
operstate='ACTIVE')
|
289 |
request = {"addFloatingIp": {"address": ip1.ipv4}} |
290 |
mock().ModifyInstance.return_value = 1
|
291 |
response = self.post(url, self.vm.userid, json.dumps(request), "json") |
292 |
self.assertEqual(response.status_code, 202) |
293 |
ip1_after = FloatingIP.objects.get(id=ip1.id) |
294 |
self.assertEqual(ip1_after.machine, self.vm) |
295 |
self.assertTrue(ip1_after.in_use())
|
296 |
|
297 |
@patch('synnefo.logic.rapi_pool.GanetiRapiClient') |
298 |
def test_remove_floating_ip(self, mock): |
299 |
# Not exists
|
300 |
url = "/api/v1.1/servers/%s/action" % self.vm.id |
301 |
request = {"removeFloatingIp": {"address": "10.0.0.1"}} |
302 |
response = self.post(url, self.vm.userid, json.dumps(request), "json") |
303 |
self.assertItemNotFound(response)
|
304 |
# Not In Use
|
305 |
ip1 = FloatingIPFactory(userid=self.vm.userid, machine=None) |
306 |
request = {"removeFloatingIp": {"address": ip1.ipv4}} |
307 |
response = self.post(url, self.vm.userid, json.dumps(request), "json") |
308 |
self.assertItemNotFound(response)
|
309 |
# Success
|
310 |
ip1 = FloatingIPFactory(userid=self.vm.userid, machine=self.vm) |
311 |
NetworkInterfaceFactory(machine=self.vm, ipv4=ip1.ipv4)
|
312 |
request = {"removeFloatingIp": {"address": ip1.ipv4}} |
313 |
mock().ModifyInstance.return_value = 2
|
314 |
response = self.post(url, self.vm.userid, json.dumps(request), "json") |
315 |
self.assertEqual(response.status_code, 202) |
316 |
# Yet used. Wait for the callbacks
|
317 |
ip1_after = FloatingIP.objects.get(id=ip1.id) |
318 |
self.assertEqual(ip1_after.machine, self.vm) |
319 |
self.assertTrue(ip1_after.in_use())
|