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