root / kamaki / clients / cyclades / test.py @ 3c50df2e
History | View | Annotate | Download (17.5 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 | 03493855 | Stavros Sachtouris | |
38 | 3c50df2e | Stavros Sachtouris | from kamaki.clients import ClientError, cyclades |
39 | 03493855 | Stavros Sachtouris | |
40 | 03493855 | Stavros Sachtouris | img_ref = "1m4g3-r3f3r3nc3"
|
41 | 03493855 | Stavros Sachtouris | vm_name = "my new VM"
|
42 | 03493855 | Stavros Sachtouris | fid = 42
|
43 | 03493855 | Stavros Sachtouris | vm_recv = dict(server=dict( |
44 | 03493855 | Stavros Sachtouris | status="BUILD",
|
45 | 03493855 | Stavros Sachtouris | updated="2013-03-01T10:04:00.637152+00:00",
|
46 | 03493855 | Stavros Sachtouris | hostId="",
|
47 | 5e194c99 | Stavros Sachtouris | name=vm_name, |
48 | 5e194c99 | Stavros Sachtouris | imageRef=img_ref, |
49 | 03493855 | Stavros Sachtouris | created="2013-03-01T10:04:00.087324+00:00",
|
50 | 03493855 | Stavros Sachtouris | flavorRef=fid, |
51 | 03493855 | Stavros Sachtouris | adminPass="n0n3sh@11p@55",
|
52 | 03493855 | Stavros Sachtouris | suspended=False,
|
53 | 03493855 | Stavros Sachtouris | progress=0,
|
54 | 03493855 | Stavros Sachtouris | id=31173,
|
55 | 03493855 | Stavros Sachtouris | metadata=dict(values=dict(os="debian", users="root")))) |
56 | 257b931a | Stavros Sachtouris | vm_list = dict(servers=dict(values=[ |
57 | 257b931a | Stavros Sachtouris | dict(name='n1', id=1), |
58 | 257b931a | Stavros Sachtouris | dict(name='n2', id=2)])) |
59 | e6ec9ea6 | Stavros Sachtouris | net_send = dict(network=dict(dhcp=False, name='someNet')) |
60 | e6ec9ea6 | Stavros Sachtouris | net_recv = dict(network=dict( |
61 | e6ec9ea6 | Stavros Sachtouris | status="PENDING",
|
62 | e6ec9ea6 | Stavros Sachtouris | updated="2013-03-05T15:04:51.758780+00:00",
|
63 | e6ec9ea6 | Stavros Sachtouris | name="someNet",
|
64 | e6ec9ea6 | Stavros Sachtouris | created="2013-03-05T15:04:51.758728+00:00",
|
65 | e6ec9ea6 | Stavros Sachtouris | cidr6=None,
|
66 | e6ec9ea6 | Stavros Sachtouris | id="2130",
|
67 | e6ec9ea6 | Stavros Sachtouris | gateway6=None,
|
68 | e6ec9ea6 | Stavros Sachtouris | public=False,
|
69 | e6ec9ea6 | Stavros Sachtouris | dhcp=False,
|
70 | e6ec9ea6 | Stavros Sachtouris | cidr="192.168.1.0/24",
|
71 | e6ec9ea6 | Stavros Sachtouris | type="MAC_FILTERED",
|
72 | e6ec9ea6 | Stavros Sachtouris | gateway=None,
|
73 | e6ec9ea6 | Stavros Sachtouris | attachments=dict(values=[dict(name='att1'), dict(name='att2')]))) |
74 | 0da4f84b | Stavros Sachtouris | net_list = dict(networks=dict(values=[ |
75 | 0da4f84b | Stavros Sachtouris | dict(id=1, name='n1'), |
76 | 0da4f84b | Stavros Sachtouris | dict(id=2, name='n2'), |
77 | 0da4f84b | Stavros Sachtouris | dict(id=3, name='n3')])) |
78 | 852d2713 | Stavros Sachtouris | firewalls = dict(attachments=dict(values=[ |
79 | 852d2713 | Stavros Sachtouris | dict(firewallProfile='50m3_pr0f1L3', otherStuff='57uff')])) |
80 | 03493855 | Stavros Sachtouris | |
81 | 03493855 | Stavros Sachtouris | |
82 | 975270f2 | Stavros Sachtouris | class FR(object): |
83 | 975270f2 | Stavros Sachtouris | """FR stands for Fake Response"""
|
84 | 975270f2 | Stavros Sachtouris | json = vm_recv |
85 | 975270f2 | Stavros Sachtouris | headers = {} |
86 | 975270f2 | Stavros Sachtouris | content = json |
87 | 975270f2 | Stavros Sachtouris | status = None
|
88 | 975270f2 | Stavros Sachtouris | status_code = 200
|
89 | 975270f2 | Stavros Sachtouris | |
90 | 975270f2 | Stavros Sachtouris | def release(self): |
91 | 975270f2 | Stavros Sachtouris | pass
|
92 | 975270f2 | Stavros Sachtouris | |
93 | 55faa0bc | Stavros Sachtouris | rest_pkg = 'kamaki.clients.cyclades.CycladesRestClient'
|
94 | 975270f2 | Stavros Sachtouris | cyclades_pkg = 'kamaki.clients.cyclades.CycladesClient'
|
95 | 975270f2 | Stavros Sachtouris | |
96 | 975270f2 | Stavros Sachtouris | |
97 | 3c50df2e | Stavros Sachtouris | class CycladesRestClient(TestCase): |
98 | 68fd1bda | Stavros Sachtouris | |
99 | 68fd1bda | Stavros Sachtouris | """Set up a Cyclades thorough test"""
|
100 | 68fd1bda | Stavros Sachtouris | def setUp(self): |
101 | 68fd1bda | Stavros Sachtouris | self.url = 'http://cyclades.example.com' |
102 | 68fd1bda | Stavros Sachtouris | self.token = 'cyc14d3s70k3n' |
103 | 3c50df2e | Stavros Sachtouris | self.client = cyclades.CycladesRestClient(self.url, self.token) |
104 | 68fd1bda | Stavros Sachtouris | |
105 | 68fd1bda | Stavros Sachtouris | def tearDown(self): |
106 | 68fd1bda | Stavros Sachtouris | FR.json = vm_recv |
107 | 68fd1bda | Stavros Sachtouris | |
108 | 68fd1bda | Stavros Sachtouris | @patch('%s.set_param' % rest_pkg) |
109 | 68fd1bda | Stavros Sachtouris | @patch('%s.get' % rest_pkg, return_value=FR()) |
110 | fc835c42 | Stavros Sachtouris | def test_servers_get(self, get, SP): |
111 | 68fd1bda | Stavros Sachtouris | for args in product( |
112 | fc835c42 | Stavros Sachtouris | ('', 'vm_id'), |
113 | 68fd1bda | Stavros Sachtouris | ('', 'cmd'), |
114 | 68fd1bda | Stavros Sachtouris | (200, 204), |
115 | 68fd1bda | Stavros Sachtouris | (None, '50m3-d473'), |
116 | 68fd1bda | Stavros Sachtouris | ({}, {'k': 'v'})): |
117 | 68fd1bda | Stavros Sachtouris | (srv_id, command, success, changes_since, kwargs) = args |
118 | fc835c42 | Stavros Sachtouris | self.client.servers_get(*args[:4], **kwargs) |
119 | 68fd1bda | Stavros Sachtouris | srv_str = '/%s' % srv_id if srv_id else '' |
120 | 68fd1bda | Stavros Sachtouris | cmd_str = '/%s' % command if command else '' |
121 | 68fd1bda | Stavros Sachtouris | self.assertEqual(get.mock_calls[-1], call( |
122 | fc835c42 | Stavros Sachtouris | '/servers%s%s' % (srv_str, cmd_str),
|
123 | 68fd1bda | Stavros Sachtouris | success=success, |
124 | 68fd1bda | Stavros Sachtouris | **kwargs)) |
125 | 68fd1bda | Stavros Sachtouris | if changes_since:
|
126 | 68fd1bda | Stavros Sachtouris | self.assertEqual(
|
127 | 68fd1bda | Stavros Sachtouris | SP.mock_calls[-1],
|
128 | 68fd1bda | Stavros Sachtouris | call('changes-since', changes_since, changes_since))
|
129 | 68fd1bda | Stavros Sachtouris | |
130 | fc835c42 | Stavros Sachtouris | @patch('%s.get' % rest_pkg, return_value=FR()) |
131 | fc835c42 | Stavros Sachtouris | def test_networks_get(self, get): |
132 | fc835c42 | Stavros Sachtouris | for args in product( |
133 | fc835c42 | Stavros Sachtouris | ('', 'net_id'), |
134 | fc835c42 | Stavros Sachtouris | ('', 'cmd'), |
135 | fc835c42 | Stavros Sachtouris | (200, 204), |
136 | fc835c42 | Stavros Sachtouris | ({}, {'k': 'v'})): |
137 | fc835c42 | Stavros Sachtouris | (srv_id, command, success, kwargs) = args |
138 | fc835c42 | Stavros Sachtouris | self.client.networks_get(*args[:3], **kwargs) |
139 | fc835c42 | Stavros Sachtouris | srv_str = '/%s' % srv_id if srv_id else '' |
140 | fc835c42 | Stavros Sachtouris | cmd_str = '/%s' % command if command else '' |
141 | fc835c42 | Stavros Sachtouris | self.assertEqual(get.mock_calls[-1], call( |
142 | fc835c42 | Stavros Sachtouris | '/networks%s%s' % (srv_str, cmd_str),
|
143 | fc835c42 | Stavros Sachtouris | success=success, |
144 | fc835c42 | Stavros Sachtouris | **kwargs)) |
145 | fc835c42 | Stavros Sachtouris | |
146 | fc835c42 | Stavros Sachtouris | @patch('%s.delete' % rest_pkg, return_value=FR()) |
147 | fc835c42 | Stavros Sachtouris | def test_networks_delete(self, delete): |
148 | fc835c42 | Stavros Sachtouris | for args in product( |
149 | fc835c42 | Stavros Sachtouris | ('', 'net_id'), |
150 | fc835c42 | Stavros Sachtouris | ('', 'cmd'), |
151 | fc835c42 | Stavros Sachtouris | (202, 204), |
152 | fc835c42 | Stavros Sachtouris | ({}, {'k': 'v'})): |
153 | fc835c42 | Stavros Sachtouris | (srv_id, command, success, kwargs) = args |
154 | fc835c42 | Stavros Sachtouris | self.client.networks_delete(*args[:3], **kwargs) |
155 | fc835c42 | Stavros Sachtouris | srv_str = '/%s' % srv_id if srv_id else '' |
156 | fc835c42 | Stavros Sachtouris | cmd_str = '/%s' % command if command else '' |
157 | fc835c42 | Stavros Sachtouris | self.assertEqual(delete.mock_calls[-1], call( |
158 | fc835c42 | Stavros Sachtouris | '/networks%s%s' % (srv_str, cmd_str),
|
159 | fc835c42 | Stavros Sachtouris | success=success, |
160 | fc835c42 | Stavros Sachtouris | **kwargs)) |
161 | 68fd1bda | Stavros Sachtouris | |
162 | 50edcde7 | Stavros Sachtouris | @patch('%s.set_header' % rest_pkg) |
163 | 50edcde7 | Stavros Sachtouris | @patch('%s.post' % rest_pkg, return_value=FR()) |
164 | 50edcde7 | Stavros Sachtouris | def test_networks_post(self, post, SH): |
165 | 50edcde7 | Stavros Sachtouris | from json import dumps |
166 | 50edcde7 | Stavros Sachtouris | for args in product( |
167 | 50edcde7 | Stavros Sachtouris | ('', 'net_id'), |
168 | 50edcde7 | Stavros Sachtouris | ('', 'cmd'), |
169 | 50edcde7 | Stavros Sachtouris | (None, [dict(json="data"), dict(data="json")]), |
170 | 50edcde7 | Stavros Sachtouris | (202, 204), |
171 | 50edcde7 | Stavros Sachtouris | ({}, {'k': 'v'})): |
172 | 50edcde7 | Stavros Sachtouris | (srv_id, command, json_data, success, kwargs) = args |
173 | 50edcde7 | Stavros Sachtouris | self.client.networks_post(*args[:4], **kwargs) |
174 | 50edcde7 | Stavros Sachtouris | vm_str = '/%s' % srv_id if srv_id else '' |
175 | 50edcde7 | Stavros Sachtouris | cmd_str = '/%s' % command if command else '' |
176 | 50edcde7 | Stavros Sachtouris | if json_data:
|
177 | 50edcde7 | Stavros Sachtouris | json_data = dumps(json_data) |
178 | 50edcde7 | Stavros Sachtouris | self.assertEqual(SH.mock_calls[-2:], [ |
179 | 50edcde7 | Stavros Sachtouris | call('Content-Type', 'application/json'), |
180 | 50edcde7 | Stavros Sachtouris | call('Content-Length', len(json_data))]) |
181 | 50edcde7 | Stavros Sachtouris | self.assertEqual(post.mock_calls[-1], call( |
182 | 50edcde7 | Stavros Sachtouris | '/networks%s%s' % (vm_str, cmd_str),
|
183 | 50edcde7 | Stavros Sachtouris | data=json_data, success=success, |
184 | 50edcde7 | Stavros Sachtouris | **kwargs)) |
185 | 50edcde7 | Stavros Sachtouris | |
186 | cd5c8cc9 | Stavros Sachtouris | @patch('%s.set_header' % rest_pkg) |
187 | cd5c8cc9 | Stavros Sachtouris | @patch('%s.put' % rest_pkg, return_value=FR()) |
188 | cd5c8cc9 | Stavros Sachtouris | def test_networks_put(self, put, SH): |
189 | cd5c8cc9 | Stavros Sachtouris | from json import dumps |
190 | cd5c8cc9 | Stavros Sachtouris | for args in product( |
191 | cd5c8cc9 | Stavros Sachtouris | ('', 'net_id'), |
192 | cd5c8cc9 | Stavros Sachtouris | ('', 'cmd'), |
193 | cd5c8cc9 | Stavros Sachtouris | (None, [dict(json="data"), dict(data="json")]), |
194 | cd5c8cc9 | Stavros Sachtouris | (202, 204), |
195 | cd5c8cc9 | Stavros Sachtouris | ({}, {'k': 'v'})): |
196 | cd5c8cc9 | Stavros Sachtouris | (srv_id, command, json_data, success, kwargs) = args |
197 | cd5c8cc9 | Stavros Sachtouris | self.client.networks_put(*args[:4], **kwargs) |
198 | cd5c8cc9 | Stavros Sachtouris | vm_str = '/%s' % srv_id if srv_id else '' |
199 | cd5c8cc9 | Stavros Sachtouris | cmd_str = '/%s' % command if command else '' |
200 | cd5c8cc9 | Stavros Sachtouris | if json_data:
|
201 | cd5c8cc9 | Stavros Sachtouris | json_data = dumps(json_data) |
202 | cd5c8cc9 | Stavros Sachtouris | self.assertEqual(SH.mock_calls[-2:], [ |
203 | cd5c8cc9 | Stavros Sachtouris | call('Content-Type', 'application/json'), |
204 | cd5c8cc9 | Stavros Sachtouris | call('Content-Length', len(json_data))]) |
205 | cd5c8cc9 | Stavros Sachtouris | self.assertEqual(put.mock_calls[-1], call( |
206 | cd5c8cc9 | Stavros Sachtouris | '/networks%s%s' % (vm_str, cmd_str),
|
207 | cd5c8cc9 | Stavros Sachtouris | data=json_data, success=success, |
208 | cd5c8cc9 | Stavros Sachtouris | **kwargs)) |
209 | cd5c8cc9 | Stavros Sachtouris | |
210 | 68fd1bda | Stavros Sachtouris | |
211 | 3c50df2e | Stavros Sachtouris | class CycladesClient(TestCase): |
212 | 03493855 | Stavros Sachtouris | |
213 | 257b931a | Stavros Sachtouris | def assert_dicts_are_equal(self, d1, d2): |
214 | 03493855 | Stavros Sachtouris | for k, v in d1.items(): |
215 | 03493855 | Stavros Sachtouris | self.assertTrue(k in d2) |
216 | 03493855 | Stavros Sachtouris | if isinstance(v, dict): |
217 | 257b931a | Stavros Sachtouris | self.assert_dicts_are_equal(v, d2[k])
|
218 | 03493855 | Stavros Sachtouris | else:
|
219 | 03493855 | Stavros Sachtouris | self.assertEqual(unicode(v), unicode(d2[k])) |
220 | 03493855 | Stavros Sachtouris | |
221 | c1004a00 | Stavros Sachtouris | """Set up a Cyclades thorough test"""
|
222 | c1004a00 | Stavros Sachtouris | def setUp(self): |
223 | 03493855 | Stavros Sachtouris | self.url = 'http://cyclades.example.com' |
224 | 03493855 | Stavros Sachtouris | self.token = 'cyc14d3s70k3n' |
225 | 3c50df2e | Stavros Sachtouris | self.client = cyclades.CycladesClient(self.url, self.token) |
226 | c1004a00 | Stavros Sachtouris | |
227 | c1004a00 | Stavros Sachtouris | def tearDown(self): |
228 | 975270f2 | Stavros Sachtouris | FR.status_code = 200
|
229 | 975270f2 | Stavros Sachtouris | FR.json = vm_recv |
230 | c1004a00 | Stavros Sachtouris | |
231 | 545be6b7 | Stavros Sachtouris | @patch('%s.servers_get' % cyclades_pkg, return_value=FR()) |
232 | 545be6b7 | Stavros Sachtouris | def test_list_servers(self, SG): |
233 | 975270f2 | Stavros Sachtouris | FR.json = vm_list |
234 | 545be6b7 | Stavros Sachtouris | for detail, since in ((0, 0), (True, 0), (0, 'd473'), (True, 'd473')): |
235 | 545be6b7 | Stavros Sachtouris | r = self.client.list_servers(detail=detail, changes_since=since)
|
236 | 545be6b7 | Stavros Sachtouris | self.assertEqual(SG.mock_calls[-1], call( |
237 | 545be6b7 | Stavros Sachtouris | command='detail' if detail else '', |
238 | 545be6b7 | Stavros Sachtouris | changes_since=since)) |
239 | 545be6b7 | Stavros Sachtouris | expected = vm_list['servers']['values'] |
240 | 545be6b7 | Stavros Sachtouris | for i, vm in enumerate(r): |
241 | 545be6b7 | Stavros Sachtouris | self.assert_dicts_are_equal(vm, expected[i])
|
242 | 545be6b7 | Stavros Sachtouris | self.assertEqual(i + 1, len(expected)) |
243 | c1004a00 | Stavros Sachtouris | |
244 | d841b964 | Stavros Sachtouris | @patch('%s.servers_post' % cyclades_pkg, return_value=FR()) |
245 | d841b964 | Stavros Sachtouris | def test_shutdown_server(self, SP): |
246 | 386d82a1 | Stavros Sachtouris | vm_id = vm_recv['server']['id'] |
247 | 975270f2 | Stavros Sachtouris | self.client.shutdown_server(vm_id)
|
248 | fb392083 | Stavros Sachtouris | SP.assert_called_once_with( |
249 | d841b964 | Stavros Sachtouris | vm_id, 'action',
|
250 | fb392083 | Stavros Sachtouris | json_data=dict(shutdown=dict()), success=202) |
251 | 975270f2 | Stavros Sachtouris | |
252 | cecad288 | Stavros Sachtouris | @patch('%s.servers_post' % cyclades_pkg, return_value=FR()) |
253 | cecad288 | Stavros Sachtouris | def test_start_server(self, SP): |
254 | 2c987e80 | Stavros Sachtouris | vm_id = vm_recv['server']['id'] |
255 | 975270f2 | Stavros Sachtouris | self.client.start_server(vm_id)
|
256 | fb392083 | Stavros Sachtouris | SP.assert_called_once_with( |
257 | cecad288 | Stavros Sachtouris | vm_id, 'action',
|
258 | fb392083 | Stavros Sachtouris | json_data=dict(start=dict()), success=202) |
259 | 975270f2 | Stavros Sachtouris | |
260 | c390cb98 | Stavros Sachtouris | @patch('%s.servers_post' % cyclades_pkg, return_value=FR()) |
261 | c390cb98 | Stavros Sachtouris | def test_get_server_console(self, SP): |
262 | 33ebcd22 | Stavros Sachtouris | cnsl = dict(console=dict(info1='i1', info2='i2', info3='i3')) |
263 | 975270f2 | Stavros Sachtouris | FR.json = cnsl |
264 | 33ebcd22 | Stavros Sachtouris | vm_id = vm_recv['server']['id'] |
265 | 975270f2 | Stavros Sachtouris | r = self.client.get_server_console(vm_id)
|
266 | fb392083 | Stavros Sachtouris | SP.assert_called_once_with( |
267 | c390cb98 | Stavros Sachtouris | vm_id, 'action',
|
268 | fb392083 | Stavros Sachtouris | json_data=dict(console=dict(type='vnc')), success=200) |
269 | c390cb98 | Stavros Sachtouris | self.assert_dicts_are_equal(r, cnsl['console']) |
270 | c1004a00 | Stavros Sachtouris | |
271 | c1004a00 | Stavros Sachtouris | def test_get_firewall_profile(self): |
272 | 7d768bc6 | Stavros Sachtouris | vm_id = vm_recv['server']['id'] |
273 | 852d2713 | Stavros Sachtouris | v = firewalls['attachments']['values'][0]['firewallProfile'] |
274 | 7d768bc6 | Stavros Sachtouris | with patch.object(
|
275 | 3c50df2e | Stavros Sachtouris | cyclades.CycladesClient, 'get_server_details',
|
276 | 852d2713 | Stavros Sachtouris | return_value=firewalls) as GSD:
|
277 | 7d768bc6 | Stavros Sachtouris | r = self.client.get_firewall_profile(vm_id)
|
278 | fb392083 | Stavros Sachtouris | GSD.assert_called_once_with(vm_id) |
279 | 7d768bc6 | Stavros Sachtouris | self.assertEqual(r, v)
|
280 | 852d2713 | Stavros Sachtouris | with patch.object(
|
281 | 3c50df2e | Stavros Sachtouris | cyclades.CycladesClient, 'get_server_details',
|
282 | 852d2713 | Stavros Sachtouris | return_value=dict()):
|
283 | 7d768bc6 | Stavros Sachtouris | self.assertRaises(
|
284 | 975270f2 | Stavros Sachtouris | ClientError, |
285 | 975270f2 | Stavros Sachtouris | self.client.get_firewall_profile,
|
286 | 7d768bc6 | Stavros Sachtouris | vm_id) |
287 | c1004a00 | Stavros Sachtouris | |
288 | 153910c8 | Stavros Sachtouris | @patch('%s.servers_post' % cyclades_pkg, return_value=FR()) |
289 | 153910c8 | Stavros Sachtouris | def test_set_firewall_profile(self, SP): |
290 | 278f2eb3 | Stavros Sachtouris | vm_id = vm_recv['server']['id'] |
291 | 153910c8 | Stavros Sachtouris | v = firewalls['attachments']['values'][0]['firewallProfile'] |
292 | 975270f2 | Stavros Sachtouris | self.client.set_firewall_profile(vm_id, v)
|
293 | fb392083 | Stavros Sachtouris | SP.assert_called_once_with( |
294 | 153910c8 | Stavros Sachtouris | vm_id, 'action',
|
295 | fb392083 | Stavros Sachtouris | json_data=dict(firewallProfile=dict(profile=v)), success=202) |
296 | 975270f2 | Stavros Sachtouris | |
297 | f6c1319e | Stavros Sachtouris | @patch('%s.servers_get' % cyclades_pkg, return_value=FR()) |
298 | f6c1319e | Stavros Sachtouris | def test_get_server_stats(self, SG): |
299 | 27f04e4f | Stavros Sachtouris | vm_id = vm_recv['server']['id'] |
300 | 27f04e4f | Stavros Sachtouris | stats = dict(stat1='v1', stat2='v2', stat3='v3', stat4='v4') |
301 | 975270f2 | Stavros Sachtouris | FR.json = dict(stats=stats)
|
302 | 975270f2 | Stavros Sachtouris | r = self.client.get_server_stats(vm_id)
|
303 | fb392083 | Stavros Sachtouris | SG.assert_called_once_with(vm_id, 'stats')
|
304 | 975270f2 | Stavros Sachtouris | self.assert_dicts_are_equal(stats, r)
|
305 | 975270f2 | Stavros Sachtouris | |
306 | 18a27c41 | Stavros Sachtouris | @patch('%s.networks_post' % cyclades_pkg, return_value=FR()) |
307 | 18a27c41 | Stavros Sachtouris | def test_create_network(self, NP): |
308 | 975270f2 | Stavros Sachtouris | net_name = net_send['network']['name'] |
309 | 975270f2 | Stavros Sachtouris | FR.json = net_recv |
310 | 975270f2 | Stavros Sachtouris | full_args = dict(
|
311 | 975270f2 | Stavros Sachtouris | cidr='192.168.0.0/24',
|
312 | 975270f2 | Stavros Sachtouris | gateway='192.168.0.1',
|
313 | 975270f2 | Stavros Sachtouris | type='MAC_FILTERED',
|
314 | 975270f2 | Stavros Sachtouris | dhcp=True)
|
315 | 975270f2 | Stavros Sachtouris | test_args = dict(full_args)
|
316 | 975270f2 | Stavros Sachtouris | test_args.update(dict(empty=None, full=None)) |
317 | 18a27c41 | Stavros Sachtouris | net_exp = dict(dhcp=False, name=net_name) |
318 | 975270f2 | Stavros Sachtouris | for arg, val in test_args.items(): |
319 | 975270f2 | Stavros Sachtouris | kwargs = {} if arg == 'empty' else full_args if ( |
320 | 975270f2 | Stavros Sachtouris | arg == 'full') else {arg: val} |
321 | 18a27c41 | Stavros Sachtouris | expected = dict(network=dict(net_exp)) |
322 | 18a27c41 | Stavros Sachtouris | expected['network'].update(kwargs)
|
323 | 975270f2 | Stavros Sachtouris | r = self.client.create_network(net_name, **kwargs)
|
324 | 27f04e4f | Stavros Sachtouris | self.assertEqual(
|
325 | 18a27c41 | Stavros Sachtouris | NP.mock_calls[-1],
|
326 | 18a27c41 | Stavros Sachtouris | call(json_data=expected, success=202))
|
327 | 975270f2 | Stavros Sachtouris | self.assert_dicts_are_equal(r, net_recv['network']) |
328 | c1004a00 | Stavros Sachtouris | |
329 | 850d58c7 | Stavros Sachtouris | @patch('%s.networks_post' % cyclades_pkg, return_value=FR()) |
330 | 850d58c7 | Stavros Sachtouris | def test_connect_server(self, NP): |
331 | 0e8a5f03 | Stavros Sachtouris | vm_id = vm_recv['server']['id'] |
332 | 0e8a5f03 | Stavros Sachtouris | net_id = net_recv['network']['id'] |
333 | 975270f2 | Stavros Sachtouris | self.client.connect_server(vm_id, net_id)
|
334 | fb392083 | Stavros Sachtouris | NP.assert_called_once_with( |
335 | 850d58c7 | Stavros Sachtouris | net_id, 'action',
|
336 | fb392083 | Stavros Sachtouris | json_data=dict(add=dict(serverRef=vm_id))) |
337 | 975270f2 | Stavros Sachtouris | |
338 | 975270f2 | Stavros Sachtouris | @patch('%s.networks_post' % cyclades_pkg, return_value=FR()) |
339 | 975270f2 | Stavros Sachtouris | def test_disconnect_server(self, NP): |
340 | b87ed277 | Stavros Sachtouris | net_id, vm_id = net_recv['network']['id'], vm_recv['server']['id'] |
341 | 9ced0bd8 | Stavros Sachtouris | nic_id = 'nic-%s-%s' % (net_id, vm_id)
|
342 | 9ced0bd8 | Stavros Sachtouris | vm_nics = [ |
343 | 9ced0bd8 | Stavros Sachtouris | dict(id=nic_id, network_id=net_id),
|
344 | 9ced0bd8 | Stavros Sachtouris | dict(id='another-nic-id', network_id='another-net-id'), |
345 | 9ced0bd8 | Stavros Sachtouris | dict(id=nic_id * 2, network_id=net_id * 2)] |
346 | 9ced0bd8 | Stavros Sachtouris | with patch.object(
|
347 | 3c50df2e | Stavros Sachtouris | cyclades.CycladesClient, |
348 | 9ced0bd8 | Stavros Sachtouris | 'list_server_nics',
|
349 | 975270f2 | Stavros Sachtouris | return_value=vm_nics) as LSN:
|
350 | 975270f2 | Stavros Sachtouris | r = self.client.disconnect_server(vm_id, nic_id)
|
351 | fb392083 | Stavros Sachtouris | LSN.assert_called_once_with(vm_id) |
352 | fb392083 | Stavros Sachtouris | NP.assert_called_once_with( |
353 | b87ed277 | Stavros Sachtouris | net_id, 'action',
|
354 | fb392083 | Stavros Sachtouris | json_data=dict(remove=dict(attachment=nic_id))) |
355 | fb392083 | Stavros Sachtouris | self.assertEqual(r, 1) |
356 | 0da4f84b | Stavros Sachtouris | |
357 | a5c8ec5d | Stavros Sachtouris | @patch('%s.servers_get' % cyclades_pkg, return_value=FR()) |
358 | a5c8ec5d | Stavros Sachtouris | def test_list_server_nics(self, SG): |
359 | 975270f2 | Stavros Sachtouris | vm_id = vm_recv['server']['id'] |
360 | 975270f2 | Stavros Sachtouris | nics = dict(addresses=dict(values=[dict(id='nic1'), dict(id='nic2')])) |
361 | 975270f2 | Stavros Sachtouris | FR.json = nics |
362 | 975270f2 | Stavros Sachtouris | r = self.client.list_server_nics(vm_id)
|
363 | fb392083 | Stavros Sachtouris | SG.assert_called_once_with(vm_id, 'ips')
|
364 | 975270f2 | Stavros Sachtouris | expected = nics['addresses']['values'] |
365 | 975270f2 | Stavros Sachtouris | for i in range(len(r)): |
366 | 975270f2 | Stavros Sachtouris | self.assert_dicts_are_equal(r[i], expected[i])
|
367 | a5c8ec5d | Stavros Sachtouris | self.assertEqual(i + 1, len(r)) |
368 | 975270f2 | Stavros Sachtouris | |
369 | dce00f54 | Stavros Sachtouris | @patch('%s.networks_get' % cyclades_pkg, return_value=FR()) |
370 | dce00f54 | Stavros Sachtouris | def test_list_networks(self, NG): |
371 | 975270f2 | Stavros Sachtouris | FR.json = net_list |
372 | 975270f2 | Stavros Sachtouris | expected = net_list['networks']['values'] |
373 | dce00f54 | Stavros Sachtouris | for detail in ('', 'detail'): |
374 | dce00f54 | Stavros Sachtouris | r = self.client.list_networks(detail=True if detail else False) |
375 | dce00f54 | Stavros Sachtouris | self.assertEqual(NG.mock_calls[-1], call(command=detail)) |
376 | dce00f54 | Stavros Sachtouris | for i, net in enumerate(expected): |
377 | dce00f54 | Stavros Sachtouris | self.assert_dicts_are_equal(r[i], net)
|
378 | dce00f54 | Stavros Sachtouris | self.assertEqual(i + 1, len(r)) |
379 | 975270f2 | Stavros Sachtouris | |
380 | 99d462c6 | Stavros Sachtouris | @patch('%s.networks_get' % cyclades_pkg, return_value=FR()) |
381 | 99d462c6 | Stavros Sachtouris | def test_list_network_nics(self, NG): |
382 | c6f8aeb4 | Stavros Sachtouris | net_id = net_recv['network']['id'] |
383 | 975270f2 | Stavros Sachtouris | FR.json = net_recv |
384 | 975270f2 | Stavros Sachtouris | r = self.client.list_network_nics(net_id)
|
385 | be7599cf | Stavros Sachtouris | NG.assert_called_once_with(network_id=net_id) |
386 | 975270f2 | Stavros Sachtouris | expected = net_recv['network']['attachments']['values'] |
387 | 975270f2 | Stavros Sachtouris | for i in range(len(r)): |
388 | 975270f2 | Stavros Sachtouris | self.assert_dicts_are_equal(r[i], expected[i])
|
389 | 975270f2 | Stavros Sachtouris | |
390 | 975270f2 | Stavros Sachtouris | @patch('%s.networks_post' % cyclades_pkg, return_value=FR()) |
391 | 975270f2 | Stavros Sachtouris | def test_disconnect_network_nics(self, NP): |
392 | 9794e66a | Stavros Sachtouris | net_id = net_recv['network']['id'] |
393 | 9794e66a | Stavros Sachtouris | nics = ['nic1', 'nic2', 'nic3'] |
394 | 9794e66a | Stavros Sachtouris | with patch.object(
|
395 | 3c50df2e | Stavros Sachtouris | cyclades.CycladesClient, |
396 | 9794e66a | Stavros Sachtouris | 'list_network_nics',
|
397 | be7599cf | Stavros Sachtouris | return_value=nics) as LNN:
|
398 | 975270f2 | Stavros Sachtouris | self.client.disconnect_network_nics(net_id)
|
399 | be7599cf | Stavros Sachtouris | LNN.assert_called_once_with(net_id) |
400 | 975270f2 | Stavros Sachtouris | for i in range(len(nics)): |
401 | 975270f2 | Stavros Sachtouris | expected = call(net_id, 'action', json_data=dict( |
402 | 975270f2 | Stavros Sachtouris | remove=dict(attachment=nics[i])))
|
403 | 975270f2 | Stavros Sachtouris | self.assertEqual(expected, NP.mock_calls[i])
|
404 | 975270f2 | Stavros Sachtouris | |
405 | be7599cf | Stavros Sachtouris | @patch('%s.networks_get' % cyclades_pkg, return_value=FR()) |
406 | 5f4fcc1b | Stavros Sachtouris | def test_get_network_details(self, NG): |
407 | 975270f2 | Stavros Sachtouris | FR.json = net_recv |
408 | 65cc246b | Stavros Sachtouris | net_id = net_recv['network']['id'] |
409 | 975270f2 | Stavros Sachtouris | r = self.client.get_network_details(net_id)
|
410 | 5f4fcc1b | Stavros Sachtouris | NG.assert_called_once_with(network_id=net_id) |
411 | 975270f2 | Stavros Sachtouris | self.assert_dicts_are_equal(r, net_recv['network']) |
412 | 975270f2 | Stavros Sachtouris | |
413 | fb392083 | Stavros Sachtouris | @patch('%s.networks_put' % cyclades_pkg, return_value=FR()) |
414 | fb392083 | Stavros Sachtouris | def test_update_network_name(self, NP): |
415 | 06030a1b | Stavros Sachtouris | net_id = net_recv['network']['id'] |
416 | 06030a1b | Stavros Sachtouris | new_name = '%s_new' % net_id
|
417 | 975270f2 | Stavros Sachtouris | self.client.update_network_name(net_id, new_name)
|
418 | fb392083 | Stavros Sachtouris | NP.assert_called_once_with( |
419 | fb392083 | Stavros Sachtouris | network_id=net_id, |
420 | fb392083 | Stavros Sachtouris | json_data=dict(network=dict(name=new_name))) |
421 | 975270f2 | Stavros Sachtouris | |
422 | f15a4776 | Stavros Sachtouris | def test_delete_network(self): |
423 | c23f1dec | Stavros Sachtouris | net_id = net_recv['network']['id'] |
424 | f15a4776 | Stavros Sachtouris | with patch.object(
|
425 | 3c50df2e | Stavros Sachtouris | cyclades.CycladesClient, 'networks_delete',
|
426 | f15a4776 | Stavros Sachtouris | return_value=FR()) as ND:
|
427 | f15a4776 | Stavros Sachtouris | self.client.delete_network(net_id)
|
428 | f15a4776 | Stavros Sachtouris | ND.assert_called_once_with(net_id) |
429 | f15a4776 | Stavros Sachtouris | with patch.object(
|
430 | 3c50df2e | Stavros Sachtouris | cyclades.CycladesClient, 'networks_delete',
|
431 | f15a4776 | Stavros Sachtouris | side_effect=ClientError('A 421 Error', 421)): |
432 | f15a4776 | Stavros Sachtouris | try:
|
433 | f15a4776 | Stavros Sachtouris | self.client.delete_network(421) |
434 | f15a4776 | Stavros Sachtouris | except ClientError as err: |
435 | f15a4776 | Stavros Sachtouris | self.assertEqual(err.status, 421) |
436 | f15a4776 | Stavros Sachtouris | self.assertEqual(err.details, [
|
437 | f15a4776 | Stavros Sachtouris | 'Network may be still connected to at least one server'])
|
438 | 975270f2 | Stavros Sachtouris | |
439 | 68fd1bda | Stavros Sachtouris | |
440 | 85898ca4 | Stavros Sachtouris | if __name__ == '__main__': |
441 | 85898ca4 | Stavros Sachtouris | from sys import argv |
442 | 85898ca4 | Stavros Sachtouris | from kamaki.clients.test import runTestCase |
443 | 68fd1bda | Stavros Sachtouris | not_found = True
|
444 | 3c50df2e | Stavros Sachtouris | if not argv[1:] or argv[1] == 'CycladesClient': |
445 | 68fd1bda | Stavros Sachtouris | not_found = False
|
446 | 3c50df2e | Stavros Sachtouris | runTestCase(CycladesClient, 'Cyclades Client', argv[2:]) |
447 | 3c50df2e | Stavros Sachtouris | if not argv[1:] or argv[1] == 'CycladesRestClient': |
448 | 68fd1bda | Stavros Sachtouris | not_found = False
|
449 | 3c50df2e | Stavros Sachtouris | runTestCase(CycladesRestClient, 'CycladesRest Client', argv[2:]) |
450 | 68fd1bda | Stavros Sachtouris | if not_found:
|
451 | 68fd1bda | Stavros Sachtouris | print('TestCase %s not found' % argv[1]) |