root / kamaki / clients / cyclades / test.py @ ccdd1b82
History | View | Annotate | Download (23.7 kB)
1 | 24851aa5 | Stavros Sachtouris | # Copyright 2013 GRNET S.A. All rights reserved.
|
---|---|---|---|
2 | c1004a00 | Stavros Sachtouris | #
|
3 | c1004a00 | Stavros Sachtouris | # Redistribution and use in source and binary forms, with or
|
4 | c1004a00 | Stavros Sachtouris | # without modification, are permitted provided that the following
|
5 | c1004a00 | Stavros Sachtouris | # conditions are met:
|
6 | c1004a00 | Stavros Sachtouris | #
|
7 | c1004a00 | Stavros Sachtouris | # 1. Redistributions of source code must retain the above
|
8 | c1004a00 | Stavros Sachtouris | # copyright notice, this list of conditions and the following
|
9 | c1004a00 | Stavros Sachtouris | # disclaimer.
|
10 | c1004a00 | Stavros Sachtouris | #
|
11 | c1004a00 | Stavros Sachtouris | # 2. Redistributions in binary form must reproduce the above
|
12 | c1004a00 | Stavros Sachtouris | # copyright notice, this list of conditions and the following
|
13 | c1004a00 | Stavros Sachtouris | # disclaimer in the documentation and/or other materials
|
14 | c1004a00 | Stavros Sachtouris | # provided with the distribution.
|
15 | c1004a00 | Stavros Sachtouris | #
|
16 | c1004a00 | Stavros Sachtouris | # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 | c1004a00 | Stavros Sachtouris | # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 | c1004a00 | Stavros Sachtouris | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 | c1004a00 | Stavros Sachtouris | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 | c1004a00 | Stavros Sachtouris | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 | c1004a00 | Stavros Sachtouris | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 | c1004a00 | Stavros Sachtouris | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 | c1004a00 | Stavros Sachtouris | # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 | c1004a00 | Stavros Sachtouris | # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 | c1004a00 | Stavros Sachtouris | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 | c1004a00 | Stavros Sachtouris | # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 | c1004a00 | Stavros Sachtouris | # POSSIBILITY OF SUCH DAMAGE.
|
28 | c1004a00 | Stavros Sachtouris | #
|
29 | c1004a00 | Stavros Sachtouris | # The views and conclusions contained in the software and
|
30 | c1004a00 | Stavros Sachtouris | # documentation are those of the authors and should not be
|
31 | c1004a00 | Stavros Sachtouris | # interpreted as representing official policies, either expressed
|
32 | c1004a00 | Stavros Sachtouris | # or implied, of GRNET S.A.
|
33 | 3c50df2e | Stavros Sachtouris | |
34 | 0482817c | Stavros Sachtouris | from mock import patch, call |
35 | 03493855 | Stavros Sachtouris | from unittest import TestCase |
36 | 68fd1bda | Stavros Sachtouris | from itertools import product |
37 | 77d1b504 | Stavros Sachtouris | from json import dumps |
38 | 03493855 | Stavros Sachtouris | |
39 | 3c50df2e | Stavros Sachtouris | from kamaki.clients import ClientError, cyclades |
40 | 03493855 | Stavros Sachtouris | |
41 | 03493855 | Stavros Sachtouris | img_ref = "1m4g3-r3f3r3nc3"
|
42 | 03493855 | Stavros Sachtouris | vm_name = "my new VM"
|
43 | 03493855 | Stavros Sachtouris | fid = 42
|
44 | 03493855 | Stavros Sachtouris | vm_recv = dict(server=dict( |
45 | 03493855 | Stavros Sachtouris | status="BUILD",
|
46 | 03493855 | Stavros Sachtouris | updated="2013-03-01T10:04:00.637152+00:00",
|
47 | 03493855 | Stavros Sachtouris | hostId="",
|
48 | 5e194c99 | Stavros Sachtouris | name=vm_name, |
49 | 5e194c99 | Stavros Sachtouris | imageRef=img_ref, |
50 | 03493855 | Stavros Sachtouris | created="2013-03-01T10:04:00.087324+00:00",
|
51 | 03493855 | Stavros Sachtouris | flavorRef=fid, |
52 | 03493855 | Stavros Sachtouris | adminPass="n0n3sh@11p@55",
|
53 | 03493855 | Stavros Sachtouris | suspended=False,
|
54 | 03493855 | Stavros Sachtouris | progress=0,
|
55 | 03493855 | Stavros Sachtouris | id=31173,
|
56 | bcef3ac9 | Stavros Sachtouris | metadata=dict(os="debian", users="root"))) |
57 | bcef3ac9 | Stavros Sachtouris | vm_list = dict(servers=[
|
58 | 257b931a | Stavros Sachtouris | dict(name='n1', id=1), |
59 | bcef3ac9 | Stavros Sachtouris | dict(name='n2', id=2)]) |
60 | e6ec9ea6 | Stavros Sachtouris | net_send = dict(network=dict(dhcp=False, name='someNet')) |
61 | e6ec9ea6 | Stavros Sachtouris | net_recv = dict(network=dict( |
62 | e6ec9ea6 | Stavros Sachtouris | status="PENDING",
|
63 | e6ec9ea6 | Stavros Sachtouris | updated="2013-03-05T15:04:51.758780+00:00",
|
64 | e6ec9ea6 | Stavros Sachtouris | name="someNet",
|
65 | e6ec9ea6 | Stavros Sachtouris | created="2013-03-05T15:04:51.758728+00:00",
|
66 | e6ec9ea6 | Stavros Sachtouris | cidr6=None,
|
67 | e6ec9ea6 | Stavros Sachtouris | id="2130",
|
68 | e6ec9ea6 | Stavros Sachtouris | gateway6=None,
|
69 | e6ec9ea6 | Stavros Sachtouris | public=False,
|
70 | e6ec9ea6 | Stavros Sachtouris | dhcp=False,
|
71 | e6ec9ea6 | Stavros Sachtouris | cidr="192.168.1.0/24",
|
72 | e6ec9ea6 | Stavros Sachtouris | type="MAC_FILTERED",
|
73 | e6ec9ea6 | Stavros Sachtouris | gateway=None,
|
74 | bcef3ac9 | Stavros Sachtouris | attachments=[dict(name='att1'), dict(name='att2')])) |
75 | bcef3ac9 | Stavros Sachtouris | net_list = dict(networks=[
|
76 | 0da4f84b | Stavros Sachtouris | dict(id=1, name='n1'), |
77 | 0da4f84b | Stavros Sachtouris | dict(id=2, name='n2'), |
78 | bcef3ac9 | Stavros Sachtouris | dict(id=3, name='n3')]) |
79 | bcef3ac9 | Stavros Sachtouris | firewalls = dict(attachments=[
|
80 | bcef3ac9 | Stavros Sachtouris | dict(firewallProfile='50m3_pr0f1L3', otherStuff='57uff')]) |
81 | 03493855 | Stavros Sachtouris | |
82 | 03493855 | Stavros Sachtouris | |
83 | 975270f2 | Stavros Sachtouris | class FR(object): |
84 | 975270f2 | Stavros Sachtouris | """FR stands for Fake Response"""
|
85 | 975270f2 | Stavros Sachtouris | json = vm_recv |
86 | 975270f2 | Stavros Sachtouris | headers = {} |
87 | 975270f2 | Stavros Sachtouris | content = json |
88 | 975270f2 | Stavros Sachtouris | status = None
|
89 | 975270f2 | Stavros Sachtouris | status_code = 200
|
90 | 975270f2 | Stavros Sachtouris | |
91 | 55faa0bc | Stavros Sachtouris | rest_pkg = 'kamaki.clients.cyclades.CycladesRestClient'
|
92 | 975270f2 | Stavros Sachtouris | cyclades_pkg = 'kamaki.clients.cyclades.CycladesClient'
|
93 | 975270f2 | Stavros Sachtouris | |
94 | 975270f2 | Stavros Sachtouris | |
95 | 3c50df2e | Stavros Sachtouris | class CycladesRestClient(TestCase): |
96 | 68fd1bda | Stavros Sachtouris | |
97 | 68fd1bda | Stavros Sachtouris | """Set up a Cyclades thorough test"""
|
98 | 68fd1bda | Stavros Sachtouris | def setUp(self): |
99 | 68fd1bda | Stavros Sachtouris | self.url = 'http://cyclades.example.com' |
100 | 68fd1bda | Stavros Sachtouris | self.token = 'cyc14d3s70k3n' |
101 | 3c50df2e | Stavros Sachtouris | self.client = cyclades.CycladesRestClient(self.url, self.token) |
102 | 68fd1bda | Stavros Sachtouris | |
103 | 68fd1bda | Stavros Sachtouris | def tearDown(self): |
104 | 68fd1bda | Stavros Sachtouris | FR.json = vm_recv |
105 | 68fd1bda | Stavros Sachtouris | |
106 | fc835c42 | Stavros Sachtouris | @patch('%s.get' % rest_pkg, return_value=FR()) |
107 | fc835c42 | Stavros Sachtouris | def test_networks_get(self, get): |
108 | fc835c42 | Stavros Sachtouris | for args in product( |
109 | fc835c42 | Stavros Sachtouris | ('', 'net_id'), |
110 | fc835c42 | Stavros Sachtouris | ('', 'cmd'), |
111 | fc835c42 | Stavros Sachtouris | (200, 204), |
112 | fc835c42 | Stavros Sachtouris | ({}, {'k': 'v'})): |
113 | fc835c42 | Stavros Sachtouris | (srv_id, command, success, kwargs) = args |
114 | fc835c42 | Stavros Sachtouris | self.client.networks_get(*args[:3], **kwargs) |
115 | fc835c42 | Stavros Sachtouris | srv_str = '/%s' % srv_id if srv_id else '' |
116 | fc835c42 | Stavros Sachtouris | cmd_str = '/%s' % command if command else '' |
117 | fc835c42 | Stavros Sachtouris | self.assertEqual(get.mock_calls[-1], call( |
118 | fc835c42 | Stavros Sachtouris | '/networks%s%s' % (srv_str, cmd_str),
|
119 | fc835c42 | Stavros Sachtouris | success=success, |
120 | fc835c42 | Stavros Sachtouris | **kwargs)) |
121 | fc835c42 | Stavros Sachtouris | |
122 | fc835c42 | Stavros Sachtouris | @patch('%s.delete' % rest_pkg, return_value=FR()) |
123 | fc835c42 | Stavros Sachtouris | def test_networks_delete(self, delete): |
124 | fc835c42 | Stavros Sachtouris | for args in product( |
125 | fc835c42 | Stavros Sachtouris | ('', 'net_id'), |
126 | fc835c42 | Stavros Sachtouris | ('', 'cmd'), |
127 | fc835c42 | Stavros Sachtouris | (202, 204), |
128 | fc835c42 | Stavros Sachtouris | ({}, {'k': 'v'})): |
129 | fc835c42 | Stavros Sachtouris | (srv_id, command, success, kwargs) = args |
130 | fc835c42 | Stavros Sachtouris | self.client.networks_delete(*args[:3], **kwargs) |
131 | fc835c42 | Stavros Sachtouris | srv_str = '/%s' % srv_id if srv_id else '' |
132 | fc835c42 | Stavros Sachtouris | cmd_str = '/%s' % command if command else '' |
133 | fc835c42 | Stavros Sachtouris | self.assertEqual(delete.mock_calls[-1], call( |
134 | fc835c42 | Stavros Sachtouris | '/networks%s%s' % (srv_str, cmd_str),
|
135 | fc835c42 | Stavros Sachtouris | success=success, |
136 | fc835c42 | Stavros Sachtouris | **kwargs)) |
137 | 68fd1bda | Stavros Sachtouris | |
138 | 50edcde7 | Stavros Sachtouris | @patch('%s.set_header' % rest_pkg) |
139 | 50edcde7 | Stavros Sachtouris | @patch('%s.post' % rest_pkg, return_value=FR()) |
140 | 50edcde7 | Stavros Sachtouris | def test_networks_post(self, post, SH): |
141 | 50edcde7 | Stavros Sachtouris | for args in product( |
142 | 50edcde7 | Stavros Sachtouris | ('', 'net_id'), |
143 | 50edcde7 | Stavros Sachtouris | ('', 'cmd'), |
144 | 50edcde7 | Stavros Sachtouris | (None, [dict(json="data"), dict(data="json")]), |
145 | 50edcde7 | Stavros Sachtouris | (202, 204), |
146 | 50edcde7 | Stavros Sachtouris | ({}, {'k': 'v'})): |
147 | 50edcde7 | Stavros Sachtouris | (srv_id, command, json_data, success, kwargs) = args |
148 | 50edcde7 | Stavros Sachtouris | self.client.networks_post(*args[:4], **kwargs) |
149 | 50edcde7 | Stavros Sachtouris | vm_str = '/%s' % srv_id if srv_id else '' |
150 | 50edcde7 | Stavros Sachtouris | cmd_str = '/%s' % command if command else '' |
151 | 50edcde7 | Stavros Sachtouris | if json_data:
|
152 | 50edcde7 | Stavros Sachtouris | json_data = dumps(json_data) |
153 | 50edcde7 | Stavros Sachtouris | self.assertEqual(SH.mock_calls[-2:], [ |
154 | 50edcde7 | Stavros Sachtouris | call('Content-Type', 'application/json'), |
155 | 50edcde7 | Stavros Sachtouris | call('Content-Length', len(json_data))]) |
156 | 50edcde7 | Stavros Sachtouris | self.assertEqual(post.mock_calls[-1], call( |
157 | 50edcde7 | Stavros Sachtouris | '/networks%s%s' % (vm_str, cmd_str),
|
158 | 50edcde7 | Stavros Sachtouris | data=json_data, success=success, |
159 | 50edcde7 | Stavros Sachtouris | **kwargs)) |
160 | 50edcde7 | Stavros Sachtouris | |
161 | cd5c8cc9 | Stavros Sachtouris | @patch('%s.set_header' % rest_pkg) |
162 | cd5c8cc9 | Stavros Sachtouris | @patch('%s.put' % rest_pkg, return_value=FR()) |
163 | cd5c8cc9 | Stavros Sachtouris | def test_networks_put(self, put, SH): |
164 | cd5c8cc9 | Stavros Sachtouris | for args in product( |
165 | cd5c8cc9 | Stavros Sachtouris | ('', 'net_id'), |
166 | cd5c8cc9 | Stavros Sachtouris | ('', 'cmd'), |
167 | cd5c8cc9 | Stavros Sachtouris | (None, [dict(json="data"), dict(data="json")]), |
168 | cd5c8cc9 | Stavros Sachtouris | (202, 204), |
169 | cd5c8cc9 | Stavros Sachtouris | ({}, {'k': 'v'})): |
170 | cd5c8cc9 | Stavros Sachtouris | (srv_id, command, json_data, success, kwargs) = args |
171 | cd5c8cc9 | Stavros Sachtouris | self.client.networks_put(*args[:4], **kwargs) |
172 | cd5c8cc9 | Stavros Sachtouris | vm_str = '/%s' % srv_id if srv_id else '' |
173 | cd5c8cc9 | Stavros Sachtouris | cmd_str = '/%s' % command if command else '' |
174 | cd5c8cc9 | Stavros Sachtouris | if json_data:
|
175 | cd5c8cc9 | Stavros Sachtouris | json_data = dumps(json_data) |
176 | cd5c8cc9 | Stavros Sachtouris | self.assertEqual(SH.mock_calls[-2:], [ |
177 | cd5c8cc9 | Stavros Sachtouris | call('Content-Type', 'application/json'), |
178 | cd5c8cc9 | Stavros Sachtouris | call('Content-Length', len(json_data))]) |
179 | cd5c8cc9 | Stavros Sachtouris | self.assertEqual(put.mock_calls[-1], call( |
180 | cd5c8cc9 | Stavros Sachtouris | '/networks%s%s' % (vm_str, cmd_str),
|
181 | cd5c8cc9 | Stavros Sachtouris | data=json_data, success=success, |
182 | cd5c8cc9 | Stavros Sachtouris | **kwargs)) |
183 | cd5c8cc9 | Stavros Sachtouris | |
184 | 0a62912d | Stavros Sachtouris | @patch('%s.get' % rest_pkg, return_value=FR()) |
185 | 0a62912d | Stavros Sachtouris | def test_floating_ip_pools_get(self, get): |
186 | 0a62912d | Stavros Sachtouris | for args in product( |
187 | 0a62912d | Stavros Sachtouris | (200, 204), |
188 | 0a62912d | Stavros Sachtouris | ({}, {'k': 'v'})): |
189 | 0a62912d | Stavros Sachtouris | success, kwargs = args |
190 | 0a62912d | Stavros Sachtouris | r = self.client.floating_ip_pools_get(success, **kwargs)
|
191 | 0a62912d | Stavros Sachtouris | self.assertTrue(isinstance(r, FR)) |
192 | 0a62912d | Stavros Sachtouris | self.assertEqual(get.mock_calls[-1], call( |
193 | 0a62912d | Stavros Sachtouris | '/os-floating-ip-pools', success=success, **kwargs))
|
194 | 0a62912d | Stavros Sachtouris | |
195 | 77d1b504 | Stavros Sachtouris | @patch('%s.get' % rest_pkg, return_value=FR()) |
196 | 77d1b504 | Stavros Sachtouris | def test_floating_ips_get(self, get): |
197 | 77d1b504 | Stavros Sachtouris | for args in product( |
198 | 03033b54 | Stavros Sachtouris | ('fip', ''), |
199 | 77d1b504 | Stavros Sachtouris | (200, 204), |
200 | 77d1b504 | Stavros Sachtouris | ({}, {'k': 'v'})): |
201 | 03033b54 | Stavros Sachtouris | fip, success, kwargs = args |
202 | 03033b54 | Stavros Sachtouris | r = self.client.floating_ips_get(fip, success, **kwargs)
|
203 | 77d1b504 | Stavros Sachtouris | self.assertTrue(isinstance(r, FR)) |
204 | 03033b54 | Stavros Sachtouris | expected = '' if not fip else '/%s' % fip |
205 | 77d1b504 | Stavros Sachtouris | self.assertEqual(get.mock_calls[-1], call( |
206 | 03033b54 | Stavros Sachtouris | '/os-floating-ips%s' % expected, success=success, **kwargs))
|
207 | 77d1b504 | Stavros Sachtouris | |
208 | 77d1b504 | Stavros Sachtouris | @patch('%s.set_header' % rest_pkg) |
209 | 77d1b504 | Stavros Sachtouris | @patch('%s.post' % rest_pkg, return_value=FR()) |
210 | 77d1b504 | Stavros Sachtouris | def test_floating_ips_post(self, post, SH): |
211 | 77d1b504 | Stavros Sachtouris | for args in product( |
212 | 77d1b504 | Stavros Sachtouris | (None, [dict(json="data"), dict(data="json")]), |
213 | 03033b54 | Stavros Sachtouris | ('fip', ''), |
214 | 77d1b504 | Stavros Sachtouris | (202, 204), |
215 | 77d1b504 | Stavros Sachtouris | ({}, {'k': 'v'})): |
216 | 03033b54 | Stavros Sachtouris | json_data, fip, success, kwargs = args |
217 | 03033b54 | Stavros Sachtouris | self.client.floating_ips_post(*args[:3], **kwargs) |
218 | 77d1b504 | Stavros Sachtouris | if json_data:
|
219 | 77d1b504 | Stavros Sachtouris | json_data = dumps(json_data) |
220 | 77d1b504 | Stavros Sachtouris | self.assertEqual(SH.mock_calls[-2:], [ |
221 | 77d1b504 | Stavros Sachtouris | call('Content-Type', 'application/json'), |
222 | 77d1b504 | Stavros Sachtouris | call('Content-Length', len(json_data))]) |
223 | 03033b54 | Stavros Sachtouris | expected = '' if not fip else '/%s' % fip |
224 | 77d1b504 | Stavros Sachtouris | self.assertEqual(post.mock_calls[-1], call( |
225 | 03033b54 | Stavros Sachtouris | '/os-floating-ips%s' % expected,
|
226 | 77d1b504 | Stavros Sachtouris | data=json_data, success=success, |
227 | 77d1b504 | Stavros Sachtouris | **kwargs)) |
228 | 77d1b504 | Stavros Sachtouris | |
229 | 77d1b504 | Stavros Sachtouris | @patch('%s.delete' % rest_pkg, return_value=FR()) |
230 | 03033b54 | Stavros Sachtouris | def test_floating_ips_delete(self, delete): |
231 | 77d1b504 | Stavros Sachtouris | for args in product( |
232 | 77d1b504 | Stavros Sachtouris | ('fip1', 'fip2'), |
233 | 77d1b504 | Stavros Sachtouris | (200, 204), |
234 | 77d1b504 | Stavros Sachtouris | ({}, {'k': 'v'})): |
235 | 77d1b504 | Stavros Sachtouris | fip, success, kwargs = args |
236 | 03033b54 | Stavros Sachtouris | r = self.client.floating_ips_delete(fip, success, **kwargs)
|
237 | 77d1b504 | Stavros Sachtouris | self.assertTrue(isinstance(r, FR)) |
238 | 77d1b504 | Stavros Sachtouris | self.assertEqual(delete.mock_calls[-1], call( |
239 | 03033b54 | Stavros Sachtouris | '/os-floating-ips/%s' % fip, success=success, **kwargs))
|
240 | 77d1b504 | Stavros Sachtouris | |
241 | 68fd1bda | Stavros Sachtouris | |
242 | e864cd9e | Stavros Sachtouris | class CycladesNetworkClient(TestCase): |
243 | e864cd9e | Stavros Sachtouris | |
244 | e864cd9e | Stavros Sachtouris | """Set up a ComputesRest thorough test"""
|
245 | e864cd9e | Stavros Sachtouris | def setUp(self): |
246 | e864cd9e | Stavros Sachtouris | self.url = 'http://network.example.com' |
247 | e864cd9e | Stavros Sachtouris | self.token = 'n2tw0rk70k3n' |
248 | e864cd9e | Stavros Sachtouris | self.client = cyclades.CycladesNetworkClient(self.url, self.token) |
249 | e864cd9e | Stavros Sachtouris | |
250 | e864cd9e | Stavros Sachtouris | def tearDown(self): |
251 | e864cd9e | Stavros Sachtouris | FR.json = vm_recv |
252 | e864cd9e | Stavros Sachtouris | del self.client |
253 | e864cd9e | Stavros Sachtouris | |
254 | 0e27687b | Stavros Sachtouris | @patch('kamaki.clients.Client.get', return_value=FR) |
255 | 0e27687b | Stavros Sachtouris | def test_list_networks(self, get): |
256 | 0e27687b | Stavros Sachtouris | FR.json = dict(networks='ret val') |
257 | 0e27687b | Stavros Sachtouris | for detail in (True, None): |
258 | 0e27687b | Stavros Sachtouris | self.assertEqual(self.client.list_networks(detail), 'ret val') |
259 | 0e27687b | Stavros Sachtouris | path = '/networks/detail' if detail else '/networks' |
260 | 0e27687b | Stavros Sachtouris | self.assertEqual(get.mock_calls[-1], call(path, success=200)) |
261 | 0e27687b | Stavros Sachtouris | |
262 | e864cd9e | Stavros Sachtouris | @patch(
|
263 | 0e27687b | Stavros Sachtouris | 'kamaki.clients.network.rest_api.NetworkRestClient.networks_post',
|
264 | e864cd9e | Stavros Sachtouris | return_value=FR()) |
265 | e864cd9e | Stavros Sachtouris | def test_create_network(self, networks_post): |
266 | e864cd9e | Stavros Sachtouris | for name, shared in product((None, 'net name'), (None, True)): |
267 | e864cd9e | Stavros Sachtouris | FR.json = dict(network='ret val') |
268 | e864cd9e | Stavros Sachtouris | type = 'net type'
|
269 | e864cd9e | Stavros Sachtouris | self.assertEqual(
|
270 | e864cd9e | Stavros Sachtouris | self.client.create_network(type, name=name, shared=shared), |
271 | e864cd9e | Stavros Sachtouris | 'ret val')
|
272 | e864cd9e | Stavros Sachtouris | req = dict(type=type, admin_state_up=True) |
273 | e864cd9e | Stavros Sachtouris | if name:
|
274 | e864cd9e | Stavros Sachtouris | req['name'] = name
|
275 | e864cd9e | Stavros Sachtouris | if shared:
|
276 | e864cd9e | Stavros Sachtouris | req['shared'] = shared
|
277 | e864cd9e | Stavros Sachtouris | expargs = dict(json_data=dict(network=req), success=201) |
278 | e864cd9e | Stavros Sachtouris | self.assertEqual(networks_post.mock_calls[-1], call(**expargs)) |
279 | e864cd9e | Stavros Sachtouris | |
280 | ccdd1b82 | Stavros Sachtouris | @patch(
|
281 | ccdd1b82 | Stavros Sachtouris | 'kamaki.clients.network.rest_api.NetworkRestClient.ports_post',
|
282 | ccdd1b82 | Stavros Sachtouris | return_value=FR) |
283 | ccdd1b82 | Stavros Sachtouris | def test_create_port(self, ports_post): |
284 | ccdd1b82 | Stavros Sachtouris | network_id, device_id, FR.json = 'netid', 'devid', dict(port='ret v') |
285 | ccdd1b82 | Stavros Sachtouris | for security_groups in ([1, 2, 3], None): |
286 | ccdd1b82 | Stavros Sachtouris | self.assertEqual(
|
287 | ccdd1b82 | Stavros Sachtouris | self.client.create_port(
|
288 | ccdd1b82 | Stavros Sachtouris | network_id, device_id, security_groups=security_groups), |
289 | ccdd1b82 | Stavros Sachtouris | 'ret v')
|
290 | ccdd1b82 | Stavros Sachtouris | req = dict(network_id=network_id, device_id=device_id)
|
291 | ccdd1b82 | Stavros Sachtouris | if security_groups:
|
292 | ccdd1b82 | Stavros Sachtouris | req['security_groups'] = security_groups
|
293 | ccdd1b82 | Stavros Sachtouris | expargs = dict(json_data=dict(port=req), success=201) |
294 | ccdd1b82 | Stavros Sachtouris | self.assertEqual(ports_post.mock_calls[-1], call(**expargs)) |
295 | ccdd1b82 | Stavros Sachtouris | |
296 | e864cd9e | Stavros Sachtouris | |
297 | 3c50df2e | Stavros Sachtouris | class CycladesClient(TestCase): |
298 | 03493855 | Stavros Sachtouris | |
299 | 257b931a | Stavros Sachtouris | def assert_dicts_are_equal(self, d1, d2): |
300 | 03493855 | Stavros Sachtouris | for k, v in d1.items(): |
301 | 03493855 | Stavros Sachtouris | self.assertTrue(k in d2) |
302 | 03493855 | Stavros Sachtouris | if isinstance(v, dict): |
303 | 257b931a | Stavros Sachtouris | self.assert_dicts_are_equal(v, d2[k])
|
304 | 03493855 | Stavros Sachtouris | else:
|
305 | 03493855 | Stavros Sachtouris | self.assertEqual(unicode(v), unicode(d2[k])) |
306 | 03493855 | Stavros Sachtouris | |
307 | c1004a00 | Stavros Sachtouris | """Set up a Cyclades thorough test"""
|
308 | c1004a00 | Stavros Sachtouris | def setUp(self): |
309 | 03493855 | Stavros Sachtouris | self.url = 'http://cyclades.example.com' |
310 | 03493855 | Stavros Sachtouris | self.token = 'cyc14d3s70k3n' |
311 | 3c50df2e | Stavros Sachtouris | self.client = cyclades.CycladesClient(self.url, self.token) |
312 | c1004a00 | Stavros Sachtouris | |
313 | c1004a00 | Stavros Sachtouris | def tearDown(self): |
314 | 975270f2 | Stavros Sachtouris | FR.status_code = 200
|
315 | 975270f2 | Stavros Sachtouris | FR.json = vm_recv |
316 | c1004a00 | Stavros Sachtouris | |
317 | 6290b789 | Stavros Sachtouris | @patch('%s.servers_action_post' % cyclades_pkg, return_value=FR()) |
318 | d841b964 | Stavros Sachtouris | def test_shutdown_server(self, SP): |
319 | 386d82a1 | Stavros Sachtouris | vm_id = vm_recv['server']['id'] |
320 | 975270f2 | Stavros Sachtouris | self.client.shutdown_server(vm_id)
|
321 | fb392083 | Stavros Sachtouris | SP.assert_called_once_with( |
322 | 6290b789 | Stavros Sachtouris | vm_id, json_data=dict(shutdown=dict()), success=202) |
323 | 975270f2 | Stavros Sachtouris | |
324 | 6290b789 | Stavros Sachtouris | @patch('%s.servers_action_post' % cyclades_pkg, return_value=FR()) |
325 | cecad288 | Stavros Sachtouris | def test_start_server(self, SP): |
326 | 2c987e80 | Stavros Sachtouris | vm_id = vm_recv['server']['id'] |
327 | 975270f2 | Stavros Sachtouris | self.client.start_server(vm_id)
|
328 | fb392083 | Stavros Sachtouris | SP.assert_called_once_with( |
329 | 6290b789 | Stavros Sachtouris | vm_id, json_data=dict(start=dict()), success=202) |
330 | 975270f2 | Stavros Sachtouris | |
331 | 6290b789 | Stavros Sachtouris | @patch('%s.servers_action_post' % cyclades_pkg, return_value=FR()) |
332 | c390cb98 | Stavros Sachtouris | def test_get_server_console(self, SP): |
333 | 33ebcd22 | Stavros Sachtouris | cnsl = dict(console=dict(info1='i1', info2='i2', info3='i3')) |
334 | 975270f2 | Stavros Sachtouris | FR.json = cnsl |
335 | 33ebcd22 | Stavros Sachtouris | vm_id = vm_recv['server']['id'] |
336 | 975270f2 | Stavros Sachtouris | r = self.client.get_server_console(vm_id)
|
337 | fb392083 | Stavros Sachtouris | SP.assert_called_once_with( |
338 | 6290b789 | Stavros Sachtouris | vm_id, json_data=dict(console=dict(type='vnc')), success=200) |
339 | c390cb98 | Stavros Sachtouris | self.assert_dicts_are_equal(r, cnsl['console']) |
340 | c1004a00 | Stavros Sachtouris | |
341 | c1004a00 | Stavros Sachtouris | def test_get_firewall_profile(self): |
342 | 7d768bc6 | Stavros Sachtouris | vm_id = vm_recv['server']['id'] |
343 | bcef3ac9 | Stavros Sachtouris | v = firewalls['attachments'][0]['firewallProfile'] |
344 | 7d768bc6 | Stavros Sachtouris | with patch.object(
|
345 | 3c50df2e | Stavros Sachtouris | cyclades.CycladesClient, 'get_server_details',
|
346 | 852d2713 | Stavros Sachtouris | return_value=firewalls) as GSD:
|
347 | 7d768bc6 | Stavros Sachtouris | r = self.client.get_firewall_profile(vm_id)
|
348 | fb392083 | Stavros Sachtouris | GSD.assert_called_once_with(vm_id) |
349 | 7d768bc6 | Stavros Sachtouris | self.assertEqual(r, v)
|
350 | 852d2713 | Stavros Sachtouris | with patch.object(
|
351 | 3c50df2e | Stavros Sachtouris | cyclades.CycladesClient, 'get_server_details',
|
352 | 852d2713 | Stavros Sachtouris | return_value=dict()):
|
353 | 7d768bc6 | Stavros Sachtouris | self.assertRaises(
|
354 | 975270f2 | Stavros Sachtouris | ClientError, |
355 | 975270f2 | Stavros Sachtouris | self.client.get_firewall_profile,
|
356 | 7d768bc6 | Stavros Sachtouris | vm_id) |
357 | c1004a00 | Stavros Sachtouris | |
358 | 6290b789 | Stavros Sachtouris | @patch('%s.servers_action_post' % cyclades_pkg, return_value=FR()) |
359 | 153910c8 | Stavros Sachtouris | def test_set_firewall_profile(self, SP): |
360 | 278f2eb3 | Stavros Sachtouris | vm_id = vm_recv['server']['id'] |
361 | bcef3ac9 | Stavros Sachtouris | v = firewalls['attachments'][0]['firewallProfile'] |
362 | 975270f2 | Stavros Sachtouris | self.client.set_firewall_profile(vm_id, v)
|
363 | 6290b789 | Stavros Sachtouris | SP.assert_called_once_with(vm_id, json_data=dict(
|
364 | 6290b789 | Stavros Sachtouris | firewallProfile=dict(profile=v)), success=202) |
365 | 975270f2 | Stavros Sachtouris | |
366 | 18a27c41 | Stavros Sachtouris | @patch('%s.networks_post' % cyclades_pkg, return_value=FR()) |
367 | 18a27c41 | Stavros Sachtouris | def test_create_network(self, NP): |
368 | 975270f2 | Stavros Sachtouris | net_name = net_send['network']['name'] |
369 | 975270f2 | Stavros Sachtouris | FR.json = net_recv |
370 | 975270f2 | Stavros Sachtouris | full_args = dict(
|
371 | 975270f2 | Stavros Sachtouris | cidr='192.168.0.0/24',
|
372 | 975270f2 | Stavros Sachtouris | gateway='192.168.0.1',
|
373 | 975270f2 | Stavros Sachtouris | type='MAC_FILTERED',
|
374 | 975270f2 | Stavros Sachtouris | dhcp=True)
|
375 | 975270f2 | Stavros Sachtouris | test_args = dict(full_args)
|
376 | 975270f2 | Stavros Sachtouris | test_args.update(dict(empty=None, full=None)) |
377 | f6f81cd3 | Stavros Sachtouris | net_exp = dict(dhcp=False, name=net_name, type='MAC_FILTERED') |
378 | 975270f2 | Stavros Sachtouris | for arg, val in test_args.items(): |
379 | 975270f2 | Stavros Sachtouris | kwargs = {} if arg == 'empty' else full_args if ( |
380 | 975270f2 | Stavros Sachtouris | arg == 'full') else {arg: val} |
381 | 18a27c41 | Stavros Sachtouris | expected = dict(network=dict(net_exp)) |
382 | 18a27c41 | Stavros Sachtouris | expected['network'].update(kwargs)
|
383 | 975270f2 | Stavros Sachtouris | r = self.client.create_network(net_name, **kwargs)
|
384 | 27f04e4f | Stavros Sachtouris | self.assertEqual(
|
385 | 18a27c41 | Stavros Sachtouris | NP.mock_calls[-1],
|
386 | 18a27c41 | Stavros Sachtouris | call(json_data=expected, success=202))
|
387 | 975270f2 | Stavros Sachtouris | self.assert_dicts_are_equal(r, net_recv['network']) |
388 | c1004a00 | Stavros Sachtouris | |
389 | 850d58c7 | Stavros Sachtouris | @patch('%s.networks_post' % cyclades_pkg, return_value=FR()) |
390 | 850d58c7 | Stavros Sachtouris | def test_connect_server(self, NP): |
391 | 0e8a5f03 | Stavros Sachtouris | vm_id = vm_recv['server']['id'] |
392 | 0e8a5f03 | Stavros Sachtouris | net_id = net_recv['network']['id'] |
393 | 975270f2 | Stavros Sachtouris | self.client.connect_server(vm_id, net_id)
|
394 | fb392083 | Stavros Sachtouris | NP.assert_called_once_with( |
395 | 850d58c7 | Stavros Sachtouris | net_id, 'action',
|
396 | fb392083 | Stavros Sachtouris | json_data=dict(add=dict(serverRef=vm_id))) |
397 | 975270f2 | Stavros Sachtouris | |
398 | 975270f2 | Stavros Sachtouris | @patch('%s.networks_post' % cyclades_pkg, return_value=FR()) |
399 | 975270f2 | Stavros Sachtouris | def test_disconnect_server(self, NP): |
400 | b87ed277 | Stavros Sachtouris | net_id, vm_id = net_recv['network']['id'], vm_recv['server']['id'] |
401 | 9ced0bd8 | Stavros Sachtouris | nic_id = 'nic-%s-%s' % (net_id, vm_id)
|
402 | 9ced0bd8 | Stavros Sachtouris | vm_nics = [ |
403 | 9ced0bd8 | Stavros Sachtouris | dict(id=nic_id, network_id=net_id),
|
404 | 9ced0bd8 | Stavros Sachtouris | dict(id='another-nic-id', network_id='another-net-id'), |
405 | 9ced0bd8 | Stavros Sachtouris | dict(id=nic_id * 2, network_id=net_id * 2)] |
406 | 9ced0bd8 | Stavros Sachtouris | with patch.object(
|
407 | 3c50df2e | Stavros Sachtouris | cyclades.CycladesClient, |
408 | 9ced0bd8 | Stavros Sachtouris | 'list_server_nics',
|
409 | 975270f2 | Stavros Sachtouris | return_value=vm_nics) as LSN:
|
410 | 975270f2 | Stavros Sachtouris | r = self.client.disconnect_server(vm_id, nic_id)
|
411 | fb392083 | Stavros Sachtouris | LSN.assert_called_once_with(vm_id) |
412 | fb392083 | Stavros Sachtouris | NP.assert_called_once_with( |
413 | b87ed277 | Stavros Sachtouris | net_id, 'action',
|
414 | fb392083 | Stavros Sachtouris | json_data=dict(remove=dict(attachment=nic_id))) |
415 | fb392083 | Stavros Sachtouris | self.assertEqual(r, 1) |
416 | 0da4f84b | Stavros Sachtouris | |
417 | 6290b789 | Stavros Sachtouris | @patch('%s.servers_ips_get' % cyclades_pkg, return_value=FR()) |
418 | a5c8ec5d | Stavros Sachtouris | def test_list_server_nics(self, SG): |
419 | 975270f2 | Stavros Sachtouris | vm_id = vm_recv['server']['id'] |
420 | c088076b | Stavros Sachtouris | nics = dict(attachments=[dict(id='nic1'), dict(id='nic2')]) |
421 | 975270f2 | Stavros Sachtouris | FR.json = nics |
422 | 975270f2 | Stavros Sachtouris | r = self.client.list_server_nics(vm_id)
|
423 | 6290b789 | Stavros Sachtouris | SG.assert_called_once_with(vm_id) |
424 | c088076b | Stavros Sachtouris | expected = nics['attachments']
|
425 | 975270f2 | Stavros Sachtouris | for i in range(len(r)): |
426 | 975270f2 | Stavros Sachtouris | self.assert_dicts_are_equal(r[i], expected[i])
|
427 | a5c8ec5d | Stavros Sachtouris | self.assertEqual(i + 1, len(r)) |
428 | 975270f2 | Stavros Sachtouris | |
429 | dce00f54 | Stavros Sachtouris | @patch('%s.networks_get' % cyclades_pkg, return_value=FR()) |
430 | dce00f54 | Stavros Sachtouris | def test_list_networks(self, NG): |
431 | 975270f2 | Stavros Sachtouris | FR.json = net_list |
432 | bcef3ac9 | Stavros Sachtouris | expected = net_list['networks']
|
433 | dce00f54 | Stavros Sachtouris | for detail in ('', 'detail'): |
434 | dce00f54 | Stavros Sachtouris | r = self.client.list_networks(detail=True if detail else False) |
435 | dce00f54 | Stavros Sachtouris | self.assertEqual(NG.mock_calls[-1], call(command=detail)) |
436 | dce00f54 | Stavros Sachtouris | for i, net in enumerate(expected): |
437 | dce00f54 | Stavros Sachtouris | self.assert_dicts_are_equal(r[i], net)
|
438 | dce00f54 | Stavros Sachtouris | self.assertEqual(i + 1, len(r)) |
439 | 975270f2 | Stavros Sachtouris | |
440 | 99d462c6 | Stavros Sachtouris | @patch('%s.networks_get' % cyclades_pkg, return_value=FR()) |
441 | 99d462c6 | Stavros Sachtouris | def test_list_network_nics(self, NG): |
442 | c6f8aeb4 | Stavros Sachtouris | net_id = net_recv['network']['id'] |
443 | 975270f2 | Stavros Sachtouris | FR.json = net_recv |
444 | 975270f2 | Stavros Sachtouris | r = self.client.list_network_nics(net_id)
|
445 | be7599cf | Stavros Sachtouris | NG.assert_called_once_with(network_id=net_id) |
446 | bcef3ac9 | Stavros Sachtouris | expected = net_recv['network']['attachments'] |
447 | 975270f2 | Stavros Sachtouris | for i in range(len(r)): |
448 | 975270f2 | Stavros Sachtouris | self.assert_dicts_are_equal(r[i], expected[i])
|
449 | 975270f2 | Stavros Sachtouris | |
450 | 975270f2 | Stavros Sachtouris | @patch('%s.networks_post' % cyclades_pkg, return_value=FR()) |
451 | 975270f2 | Stavros Sachtouris | def test_disconnect_network_nics(self, NP): |
452 | 9794e66a | Stavros Sachtouris | net_id = net_recv['network']['id'] |
453 | 9794e66a | Stavros Sachtouris | nics = ['nic1', 'nic2', 'nic3'] |
454 | 9794e66a | Stavros Sachtouris | with patch.object(
|
455 | 3c50df2e | Stavros Sachtouris | cyclades.CycladesClient, |
456 | 9794e66a | Stavros Sachtouris | 'list_network_nics',
|
457 | be7599cf | Stavros Sachtouris | return_value=nics) as LNN:
|
458 | 975270f2 | Stavros Sachtouris | self.client.disconnect_network_nics(net_id)
|
459 | be7599cf | Stavros Sachtouris | LNN.assert_called_once_with(net_id) |
460 | 975270f2 | Stavros Sachtouris | for i in range(len(nics)): |
461 | 975270f2 | Stavros Sachtouris | expected = call(net_id, 'action', json_data=dict( |
462 | 975270f2 | Stavros Sachtouris | remove=dict(attachment=nics[i])))
|
463 | 975270f2 | Stavros Sachtouris | self.assertEqual(expected, NP.mock_calls[i])
|
464 | 975270f2 | Stavros Sachtouris | |
465 | be7599cf | Stavros Sachtouris | @patch('%s.networks_get' % cyclades_pkg, return_value=FR()) |
466 | 5f4fcc1b | Stavros Sachtouris | def test_get_network_details(self, NG): |
467 | 975270f2 | Stavros Sachtouris | FR.json = net_recv |
468 | 65cc246b | Stavros Sachtouris | net_id = net_recv['network']['id'] |
469 | 975270f2 | Stavros Sachtouris | r = self.client.get_network_details(net_id)
|
470 | 5f4fcc1b | Stavros Sachtouris | NG.assert_called_once_with(network_id=net_id) |
471 | 975270f2 | Stavros Sachtouris | self.assert_dicts_are_equal(r, net_recv['network']) |
472 | 975270f2 | Stavros Sachtouris | |
473 | fb392083 | Stavros Sachtouris | @patch('%s.networks_put' % cyclades_pkg, return_value=FR()) |
474 | fb392083 | Stavros Sachtouris | def test_update_network_name(self, NP): |
475 | 06030a1b | Stavros Sachtouris | net_id = net_recv['network']['id'] |
476 | 06030a1b | Stavros Sachtouris | new_name = '%s_new' % net_id
|
477 | 975270f2 | Stavros Sachtouris | self.client.update_network_name(net_id, new_name)
|
478 | fb392083 | Stavros Sachtouris | NP.assert_called_once_with( |
479 | fb392083 | Stavros Sachtouris | network_id=net_id, |
480 | fb392083 | Stavros Sachtouris | json_data=dict(network=dict(name=new_name))) |
481 | 975270f2 | Stavros Sachtouris | |
482 | f15a4776 | Stavros Sachtouris | def test_delete_network(self): |
483 | c23f1dec | Stavros Sachtouris | net_id = net_recv['network']['id'] |
484 | f15a4776 | Stavros Sachtouris | with patch.object(
|
485 | 3c50df2e | Stavros Sachtouris | cyclades.CycladesClient, 'networks_delete',
|
486 | f15a4776 | Stavros Sachtouris | return_value=FR()) as ND:
|
487 | f15a4776 | Stavros Sachtouris | self.client.delete_network(net_id)
|
488 | f15a4776 | Stavros Sachtouris | ND.assert_called_once_with(net_id) |
489 | f15a4776 | Stavros Sachtouris | with patch.object(
|
490 | 3c50df2e | Stavros Sachtouris | cyclades.CycladesClient, 'networks_delete',
|
491 | f15a4776 | Stavros Sachtouris | side_effect=ClientError('A 421 Error', 421)): |
492 | f15a4776 | Stavros Sachtouris | try:
|
493 | f15a4776 | Stavros Sachtouris | self.client.delete_network(421) |
494 | f15a4776 | Stavros Sachtouris | except ClientError as err: |
495 | f15a4776 | Stavros Sachtouris | self.assertEqual(err.status, 421) |
496 | f15a4776 | Stavros Sachtouris | self.assertEqual(err.details, [
|
497 | f15a4776 | Stavros Sachtouris | 'Network may be still connected to at least one server'])
|
498 | 975270f2 | Stavros Sachtouris | |
499 | 03033b54 | Stavros Sachtouris | @patch('%s.floating_ip_pools_get' % cyclades_pkg, return_value=FR()) |
500 | 03033b54 | Stavros Sachtouris | def test_get_floating_ip_pools(self, get): |
501 | 03033b54 | Stavros Sachtouris | r = self.client.get_floating_ip_pools()
|
502 | 03033b54 | Stavros Sachtouris | self.assert_dicts_are_equal(r, FR.json)
|
503 | 03033b54 | Stavros Sachtouris | self.assertEqual(get.mock_calls[-1], call()) |
504 | 03033b54 | Stavros Sachtouris | |
505 | 03033b54 | Stavros Sachtouris | @patch('%s.floating_ips_get' % cyclades_pkg, return_value=FR()) |
506 | 03033b54 | Stavros Sachtouris | def test_get_floating_ips(self, get): |
507 | 03033b54 | Stavros Sachtouris | r = self.client.get_floating_ips()
|
508 | 03033b54 | Stavros Sachtouris | self.assert_dicts_are_equal(r, FR.json)
|
509 | 03033b54 | Stavros Sachtouris | self.assertEqual(get.mock_calls[-1], call()) |
510 | 03033b54 | Stavros Sachtouris | |
511 | 03033b54 | Stavros Sachtouris | @patch('%s.floating_ips_post' % cyclades_pkg, return_value=FR()) |
512 | 03033b54 | Stavros Sachtouris | def test_alloc_floating_ip(self, post): |
513 | 03033b54 | Stavros Sachtouris | FR.json = dict(floating_ip=dict( |
514 | 03033b54 | Stavros Sachtouris | fixed_ip='fip',
|
515 | 03033b54 | Stavros Sachtouris | id=1,
|
516 | 03033b54 | Stavros Sachtouris | instance_id='lala',
|
517 | 03033b54 | Stavros Sachtouris | ip='102.0.0.1',
|
518 | 03033b54 | Stavros Sachtouris | pool='pisine'))
|
519 | 03033b54 | Stavros Sachtouris | for args in product( |
520 | 03033b54 | Stavros Sachtouris | (None, 'pisine'), |
521 | 03033b54 | Stavros Sachtouris | (None, 'Iwannanip')): |
522 | 03033b54 | Stavros Sachtouris | r = self.client.alloc_floating_ip(*args)
|
523 | 03033b54 | Stavros Sachtouris | pool, address = args |
524 | 03033b54 | Stavros Sachtouris | self.assert_dicts_are_equal(r, FR.json['floating_ip']) |
525 | 03033b54 | Stavros Sachtouris | json_data = dict()
|
526 | 03033b54 | Stavros Sachtouris | if pool:
|
527 | 03033b54 | Stavros Sachtouris | json_data['pool'] = pool
|
528 | 7d4ebdab | Stavros Sachtouris | if address:
|
529 | 7d4ebdab | Stavros Sachtouris | json_data['address'] = address
|
530 | 03033b54 | Stavros Sachtouris | self.assertEqual(post.mock_calls[-1], call(json_data)) |
531 | 03033b54 | Stavros Sachtouris | |
532 | 03033b54 | Stavros Sachtouris | @patch('%s.floating_ips_get' % cyclades_pkg, return_value=FR()) |
533 | 03033b54 | Stavros Sachtouris | def test_get_floating_ip(self, get): |
534 | 03033b54 | Stavros Sachtouris | FR.json = dict(floating_ip=dict( |
535 | 03033b54 | Stavros Sachtouris | fixed_ip='fip',
|
536 | 03033b54 | Stavros Sachtouris | id=1,
|
537 | 03033b54 | Stavros Sachtouris | instance_id='lala',
|
538 | 03033b54 | Stavros Sachtouris | ip='102.0.0.1',
|
539 | 03033b54 | Stavros Sachtouris | pool='pisine'))
|
540 | 03033b54 | Stavros Sachtouris | self.assertRaises(AssertionError, self.client.get_floating_ip, None) |
541 | 03033b54 | Stavros Sachtouris | fip = 'fip'
|
542 | 03033b54 | Stavros Sachtouris | r = self.client.get_floating_ip(fip)
|
543 | 03033b54 | Stavros Sachtouris | self.assert_dicts_are_equal(r, FR.json['floating_ip']) |
544 | 03033b54 | Stavros Sachtouris | self.assertEqual(get.mock_calls[-1], call(fip)) |
545 | 03033b54 | Stavros Sachtouris | |
546 | 03033b54 | Stavros Sachtouris | @patch('%s.floating_ips_delete' % cyclades_pkg, return_value=FR()) |
547 | 03033b54 | Stavros Sachtouris | def test_delete_floating_ip(self, delete): |
548 | 03033b54 | Stavros Sachtouris | self.assertRaises(AssertionError, self.client.delete_floating_ip, None) |
549 | 03033b54 | Stavros Sachtouris | fip = 'fip'
|
550 | 03033b54 | Stavros Sachtouris | r = self.client.delete_floating_ip(fip)
|
551 | 03033b54 | Stavros Sachtouris | self.assert_dicts_are_equal(r, FR.headers)
|
552 | 03033b54 | Stavros Sachtouris | self.assertEqual(delete.mock_calls[-1], call(fip)) |
553 | 03033b54 | Stavros Sachtouris | |
554 | 6290b789 | Stavros Sachtouris | @patch('%s.servers_action_post' % cyclades_pkg, return_value=FR()) |
555 | 8b2d63a7 | Stavros Sachtouris | def test_attach_floating_ip(self, spost): |
556 | 03033b54 | Stavros Sachtouris | vmid, addr = 42, 'anIpAddress' |
557 | 03033b54 | Stavros Sachtouris | for err, args in { |
558 | 03033b54 | Stavros Sachtouris | ValueError: ['not a server id', addr], |
559 | 03033b54 | Stavros Sachtouris | TypeError: [None, addr], |
560 | 03033b54 | Stavros Sachtouris | AssertionError: [vmid, None], |
561 | 03033b54 | Stavros Sachtouris | AssertionError: [vmid, '']}.items(): |
562 | 03033b54 | Stavros Sachtouris | self.assertRaises(
|
563 | 8b2d63a7 | Stavros Sachtouris | err, self.client.attach_floating_ip, *args)
|
564 | 8b2d63a7 | Stavros Sachtouris | r = self.client.attach_floating_ip(vmid, addr)
|
565 | 03033b54 | Stavros Sachtouris | self.assert_dicts_are_equal(r, FR.headers)
|
566 | 03033b54 | Stavros Sachtouris | expected = dict(addFloatingIp=dict(address=addr)) |
567 | 6290b789 | Stavros Sachtouris | self.assertEqual(spost.mock_calls[-1], call(vmid, json_data=expected)) |
568 | 03033b54 | Stavros Sachtouris | |
569 | 6290b789 | Stavros Sachtouris | @patch('%s.servers_action_post' % cyclades_pkg, return_value=FR()) |
570 | 8b2d63a7 | Stavros Sachtouris | def test_detach_floating_ip(self, spost): |
571 | 03033b54 | Stavros Sachtouris | vmid, addr = 42, 'anIpAddress' |
572 | 03033b54 | Stavros Sachtouris | for err, args in { |
573 | 03033b54 | Stavros Sachtouris | ValueError: ['not a server id', addr], |
574 | 03033b54 | Stavros Sachtouris | TypeError: [None, addr], |
575 | 03033b54 | Stavros Sachtouris | AssertionError: [vmid, None], |
576 | 03033b54 | Stavros Sachtouris | AssertionError: [vmid, '']}.items(): |
577 | 03033b54 | Stavros Sachtouris | self.assertRaises(
|
578 | 8b2d63a7 | Stavros Sachtouris | err, self.client.detach_floating_ip, *args)
|
579 | 8b2d63a7 | Stavros Sachtouris | r = self.client.detach_floating_ip(vmid, addr)
|
580 | 03033b54 | Stavros Sachtouris | self.assert_dicts_are_equal(r, FR.headers)
|
581 | 03033b54 | Stavros Sachtouris | expected = dict(removeFloatingIp=dict(address=addr)) |
582 | 6290b789 | Stavros Sachtouris | self.assertEqual(spost.mock_calls[-1], call(vmid, json_data=expected)) |
583 | 03033b54 | Stavros Sachtouris | |
584 | 68fd1bda | Stavros Sachtouris | |
585 | 85898ca4 | Stavros Sachtouris | if __name__ == '__main__': |
586 | 85898ca4 | Stavros Sachtouris | from sys import argv |
587 | 85898ca4 | Stavros Sachtouris | from kamaki.clients.test import runTestCase |
588 | 68fd1bda | Stavros Sachtouris | not_found = True
|
589 | 3c50df2e | Stavros Sachtouris | if not argv[1:] or argv[1] == 'CycladesClient': |
590 | 68fd1bda | Stavros Sachtouris | not_found = False
|
591 | e864cd9e | Stavros Sachtouris | runTestCase(CycladesNetworkClient, 'Cyclades Client', argv[2:]) |
592 | e864cd9e | Stavros Sachtouris | if not argv[1:] or argv[1] == 'CycladesNetworkClient': |
593 | e864cd9e | Stavros Sachtouris | not_found = False
|
594 | e864cd9e | Stavros Sachtouris | runTestCase(CycladesNetworkClient, 'CycladesNetwork Client', argv[2:]) |
595 | 3c50df2e | Stavros Sachtouris | if not argv[1:] or argv[1] == 'CycladesRestClient': |
596 | 68fd1bda | Stavros Sachtouris | not_found = False
|
597 | 3c50df2e | Stavros Sachtouris | runTestCase(CycladesRestClient, 'CycladesRest Client', argv[2:]) |
598 | 68fd1bda | Stavros Sachtouris | if not_found:
|
599 | 68fd1bda | Stavros Sachtouris | print('TestCase %s not found' % argv[1]) |