root / snf-cyclades-app / synnefo / api / test / floating_ips.py @ acda838e
History | View | Annotate | Download (5 kB)
1 | acda838e | Christos Stavrakakis | # Copyright 2012 GRNET S.A. All rights reserved.
|
---|---|---|---|
2 | acda838e | Christos Stavrakakis | #
|
3 | acda838e | Christos Stavrakakis | # Redistribution and use in source and binary forms, with or
|
4 | acda838e | Christos Stavrakakis | # without modification, are permitted provided that the following
|
5 | acda838e | Christos Stavrakakis | # conditions are met:
|
6 | acda838e | Christos Stavrakakis | #
|
7 | acda838e | Christos Stavrakakis | # 1. Redistributions of source code must retain the above
|
8 | acda838e | Christos Stavrakakis | # copyright notice, this list of conditions and the following
|
9 | acda838e | Christos Stavrakakis | # disclaimer.
|
10 | acda838e | Christos Stavrakakis | #
|
11 | acda838e | Christos Stavrakakis | # 2. Redistributions in binary form must reproduce the above
|
12 | acda838e | Christos Stavrakakis | # copyright notice, this list of conditions and the following
|
13 | acda838e | Christos Stavrakakis | # disclaimer in the documentation and/or other materials
|
14 | acda838e | Christos Stavrakakis | # provided with the distribution.
|
15 | acda838e | Christos Stavrakakis | #
|
16 | acda838e | Christos Stavrakakis | # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 | acda838e | Christos Stavrakakis | # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 | acda838e | Christos Stavrakakis | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 | acda838e | Christos Stavrakakis | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 | acda838e | Christos Stavrakakis | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 | acda838e | Christos Stavrakakis | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 | acda838e | Christos Stavrakakis | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 | acda838e | Christos Stavrakakis | # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 | acda838e | Christos Stavrakakis | # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 | acda838e | Christos Stavrakakis | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 | acda838e | Christos Stavrakakis | # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 | acda838e | Christos Stavrakakis | # POSSIBILITY OF SUCH DAMAGE.
|
28 | acda838e | Christos Stavrakakis | #
|
29 | acda838e | Christos Stavrakakis | # The views and conclusions contained in the software and
|
30 | acda838e | Christos Stavrakakis | # documentation are those of the authors and should not be
|
31 | acda838e | Christos Stavrakakis | # interpreted as representing official policies, either expressed
|
32 | acda838e | Christos Stavrakakis | # or implied, of GRNET S.A.
|
33 | acda838e | Christos Stavrakakis | |
34 | acda838e | Christos Stavrakakis | import json |
35 | acda838e | Christos Stavrakakis | |
36 | acda838e | Christos Stavrakakis | from snf_django.utils.testing import BaseAPITest, mocked_quotaholder |
37 | acda838e | Christos Stavrakakis | from synnefo.db.models import FloatingIP |
38 | acda838e | Christos Stavrakakis | from synnefo.db.models_factory import FloatingIPFactory, NetworkFactory |
39 | acda838e | Christos Stavrakakis | |
40 | acda838e | Christos Stavrakakis | |
41 | acda838e | Christos Stavrakakis | URL = "/api/v1.1/os-floating-ips"
|
42 | acda838e | Christos Stavrakakis | |
43 | acda838e | Christos Stavrakakis | |
44 | acda838e | Christos Stavrakakis | class FloatingIPAPITest(BaseAPITest): |
45 | acda838e | Christos Stavrakakis | def test_no_floating_ip(self): |
46 | acda838e | Christos Stavrakakis | response = self.get(URL)
|
47 | acda838e | Christos Stavrakakis | self.assertSuccess(response)
|
48 | acda838e | Christos Stavrakakis | self.assertEqual(json.loads(response.content)["floating_ips"], []) |
49 | acda838e | Christos Stavrakakis | |
50 | acda838e | Christos Stavrakakis | def test_list_ips(self): |
51 | acda838e | Christos Stavrakakis | ip = FloatingIPFactory(userid="user1")
|
52 | acda838e | Christos Stavrakakis | FloatingIPFactory(userid="user1", deleted=True) |
53 | acda838e | Christos Stavrakakis | with mocked_quotaholder():
|
54 | acda838e | Christos Stavrakakis | response = self.get(URL, "user1") |
55 | acda838e | Christos Stavrakakis | self.assertSuccess(response)
|
56 | acda838e | Christos Stavrakakis | api_ip = json.loads(response.content)["floating_ips"][0] |
57 | acda838e | Christos Stavrakakis | self.assertEqual(api_ip,
|
58 | acda838e | Christos Stavrakakis | {"instance_id": str(ip.machine.id), "ip": ip.ipv4, |
59 | acda838e | Christos Stavrakakis | "fixed_ip": None, "id": str(ip.id), "pool": |
60 | acda838e | Christos Stavrakakis | str(ip.network.id)})
|
61 | acda838e | Christos Stavrakakis | |
62 | acda838e | Christos Stavrakakis | def test_get_ip(self): |
63 | acda838e | Christos Stavrakakis | ip = FloatingIPFactory(userid="user1")
|
64 | acda838e | Christos Stavrakakis | with mocked_quotaholder():
|
65 | acda838e | Christos Stavrakakis | response = self.get(URL + "/%s" % ip.id, "user1") |
66 | acda838e | Christos Stavrakakis | self.assertSuccess(response)
|
67 | acda838e | Christos Stavrakakis | api_ip = json.loads(response.content)["floating_ip"]
|
68 | acda838e | Christos Stavrakakis | self.assertEqual(api_ip,
|
69 | acda838e | Christos Stavrakakis | {"instance_id": str(ip.machine.id), "ip": ip.ipv4, |
70 | acda838e | Christos Stavrakakis | "fixed_ip": None, "id": str(ip.id), "pool": |
71 | acda838e | Christos Stavrakakis | str(ip.network.id)})
|
72 | acda838e | Christos Stavrakakis | |
73 | acda838e | Christos Stavrakakis | def test_wrong_user(self): |
74 | acda838e | Christos Stavrakakis | ip = FloatingIPFactory(userid="user1")
|
75 | acda838e | Christos Stavrakakis | with mocked_quotaholder():
|
76 | acda838e | Christos Stavrakakis | response = self.delete(URL + "/%s" % ip.id, "user2") |
77 | acda838e | Christos Stavrakakis | self.assertItemNotFound(response)
|
78 | acda838e | Christos Stavrakakis | |
79 | acda838e | Christos Stavrakakis | def test_deleted_ip(self): |
80 | acda838e | Christos Stavrakakis | ip = FloatingIPFactory(userid="user1", deleted=True) |
81 | acda838e | Christos Stavrakakis | with mocked_quotaholder():
|
82 | acda838e | Christos Stavrakakis | response = self.delete(URL + "/%s" % ip.id, "user1") |
83 | acda838e | Christos Stavrakakis | self.assertItemNotFound(response)
|
84 | acda838e | Christos Stavrakakis | |
85 | acda838e | Christos Stavrakakis | def test_reserve(self): |
86 | acda838e | Christos Stavrakakis | net = NetworkFactory(userid="test_user", subnet="192.168.2.0/24", |
87 | acda838e | Christos Stavrakakis | gateway=None, public=True) |
88 | acda838e | Christos Stavrakakis | request = {'pool': net.id}
|
89 | acda838e | Christos Stavrakakis | with mocked_quotaholder():
|
90 | acda838e | Christos Stavrakakis | response = self.post(URL, "test_user", json.dumps(request), "json") |
91 | acda838e | Christos Stavrakakis | self.assertSuccess(response)
|
92 | acda838e | Christos Stavrakakis | ip = FloatingIP.objects.get() |
93 | acda838e | Christos Stavrakakis | self.assertEqual(ip.ipv4, "192.168.2.1") |
94 | acda838e | Christos Stavrakakis | self.assertEqual(ip.machine, None) |
95 | acda838e | Christos Stavrakakis | self.assertEqual(ip.network, net)
|
96 | acda838e | Christos Stavrakakis | self.assertEqual(json.loads(response.content)["floating_ip"], |
97 | acda838e | Christos Stavrakakis | {"instance_id": None, "ip": "192.168.2.1", |
98 | acda838e | Christos Stavrakakis | "fixed_ip": None, "id": "1", "pool": "1"}) |
99 | acda838e | Christos Stavrakakis | |
100 | acda838e | Christos Stavrakakis | def test_reserve_full(self): |
101 | acda838e | Christos Stavrakakis | net = NetworkFactory(userid="test_user", subnet="192.168.2.0/32", |
102 | acda838e | Christos Stavrakakis | gateway=None, public=True) |
103 | acda838e | Christos Stavrakakis | request = {'pool': net.id}
|
104 | acda838e | Christos Stavrakakis | with mocked_quotaholder():
|
105 | acda838e | Christos Stavrakakis | response = self.post(URL, "test_user", json.dumps(request), "json") |
106 | acda838e | Christos Stavrakakis | self.assertEqual(response.status_code, 413) |
107 | acda838e | Christos Stavrakakis | |
108 | acda838e | Christos Stavrakakis | def test_release_in_use(self): |
109 | acda838e | Christos Stavrakakis | ip = FloatingIPFactory() |
110 | acda838e | Christos Stavrakakis | with mocked_quotaholder():
|
111 | acda838e | Christos Stavrakakis | response = self.delete(URL + "/%s" % ip.id, ip.userid) |
112 | acda838e | Christos Stavrakakis | self.assertFault(response, 409, "conflict") |
113 | acda838e | Christos Stavrakakis | |
114 | acda838e | Christos Stavrakakis | def test_release(self): |
115 | acda838e | Christos Stavrakakis | ip = FloatingIPFactory(machine=None)
|
116 | acda838e | Christos Stavrakakis | with mocked_quotaholder():
|
117 | acda838e | Christos Stavrakakis | response = self.delete(URL + "/%s" % ip.id, ip.userid) |
118 | acda838e | Christos Stavrakakis | self.assertSuccess(response)
|
119 | acda838e | Christos Stavrakakis | ips_after = FloatingIP.objects.filter(id=ip.id) |
120 | acda838e | Christos Stavrakakis | self.assertEqual(len(ips_after), 0) |