root / kamaki / clients / compute / test.py @ 99e4d887
History | View | Annotate | Download (27 kB)
1 | 304a92dc | Stavros Sachtouris | # Copyright 2013 GRNET S.A. All rights reserved.
|
---|---|---|---|
2 | 304a92dc | Stavros Sachtouris | #
|
3 | 304a92dc | Stavros Sachtouris | # Redistribution and use in source and binary forms, with or
|
4 | 304a92dc | Stavros Sachtouris | # without modification, are permitted provided that the following
|
5 | 304a92dc | Stavros Sachtouris | # conditions are met:
|
6 | 304a92dc | Stavros Sachtouris | #
|
7 | 304a92dc | Stavros Sachtouris | # 1. Redistributions of source code must retain the above
|
8 | 304a92dc | Stavros Sachtouris | # copyright notice, this list of conditions and the following
|
9 | 304a92dc | Stavros Sachtouris | # disclaimer.
|
10 | 304a92dc | Stavros Sachtouris | #
|
11 | 304a92dc | Stavros Sachtouris | # 2. Redistributions in binary form must reproduce the above
|
12 | 304a92dc | Stavros Sachtouris | # copyright notice, this list of conditions and the following
|
13 | 304a92dc | Stavros Sachtouris | # disclaimer in the documentation and/or other materials
|
14 | 304a92dc | Stavros Sachtouris | # provided with the distribution.
|
15 | 304a92dc | Stavros Sachtouris | #
|
16 | 304a92dc | Stavros Sachtouris | # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 | 304a92dc | Stavros Sachtouris | # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 | 304a92dc | Stavros Sachtouris | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 | 304a92dc | Stavros Sachtouris | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 | 304a92dc | Stavros Sachtouris | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 | 304a92dc | Stavros Sachtouris | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 | 304a92dc | Stavros Sachtouris | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 | 304a92dc | Stavros Sachtouris | # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 | 304a92dc | Stavros Sachtouris | # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 | 304a92dc | Stavros Sachtouris | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 | 304a92dc | Stavros Sachtouris | # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 | 304a92dc | Stavros Sachtouris | # POSSIBILITY OF SUCH DAMAGE.
|
28 | 304a92dc | Stavros Sachtouris | #
|
29 | 304a92dc | Stavros Sachtouris | # The views and conclusions contained in the software and
|
30 | 304a92dc | Stavros Sachtouris | # documentation are those of the authors and should not be
|
31 | 304a92dc | Stavros Sachtouris | # interpreted as representing official policies, either expressed
|
32 | 304a92dc | Stavros Sachtouris | # or implied, of GRNET S.A.
|
33 | 304a92dc | Stavros Sachtouris | |
34 | 0482817c | Stavros Sachtouris | from mock import patch, call |
35 | 304a92dc | Stavros Sachtouris | from unittest import TestCase |
36 | 630dea9b | Stavros Sachtouris | from itertools import product |
37 | 6695edc9 | Stavros Sachtouris | from json import dumps |
38 | 5ed87051 | Stavros Sachtouris | from sys import stdout |
39 | 304a92dc | Stavros Sachtouris | |
40 | 3c50df2e | Stavros Sachtouris | from kamaki.clients import ClientError, compute |
41 | 304a92dc | Stavros Sachtouris | |
42 | edf8ad52 | Stavros Sachtouris | |
43 | 55faa0bc | Stavros Sachtouris | rest_pkg = 'kamaki.clients.compute.rest_api.ComputeRestClient'
|
44 | 630dea9b | Stavros Sachtouris | compute_pkg = 'kamaki.clients.compute.ComputeClient'
|
45 | edf8ad52 | Stavros Sachtouris | |
46 | 304a92dc | Stavros Sachtouris | img_ref = "1m4g3-r3f3r3nc3"
|
47 | 304a92dc | Stavros Sachtouris | vm_name = "my new VM"
|
48 | 304a92dc | Stavros Sachtouris | fid = 42
|
49 | 304a92dc | Stavros Sachtouris | vm_send = dict(server=dict( |
50 | 304a92dc | Stavros Sachtouris | flavorRef=fid, |
51 | 304a92dc | Stavros Sachtouris | name=vm_name, |
52 | 304a92dc | Stavros Sachtouris | imageRef=img_ref, |
53 | 304a92dc | Stavros Sachtouris | metadata=dict(os="debian", users="root"))) |
54 | 304a92dc | Stavros Sachtouris | vm_recv = dict(server=dict( |
55 | 304a92dc | Stavros Sachtouris | status="BUILD",
|
56 | 304a92dc | Stavros Sachtouris | updated="2013-03-01T10:04:00.637152+00:00",
|
57 | 304a92dc | Stavros Sachtouris | hostId="",
|
58 | 304a92dc | Stavros Sachtouris | name=vm_name, |
59 | 304a92dc | Stavros Sachtouris | imageRef=img_ref, |
60 | 304a92dc | Stavros Sachtouris | created="2013-03-01T10:04:00.087324+00:00",
|
61 | 304a92dc | Stavros Sachtouris | flavorRef=fid, |
62 | 304a92dc | Stavros Sachtouris | adminPass="n0n3sh@11p@55",
|
63 | 304a92dc | Stavros Sachtouris | suspended=False,
|
64 | 304a92dc | Stavros Sachtouris | progress=0,
|
65 | 304a92dc | Stavros Sachtouris | id=31173,
|
66 | bcef3ac9 | Stavros Sachtouris | metadata=dict(os="debian", users="root"))) |
67 | 304a92dc | Stavros Sachtouris | img_recv = dict(image=dict( |
68 | 304a92dc | Stavros Sachtouris | status="ACTIVE",
|
69 | 304a92dc | Stavros Sachtouris | updated="2013-02-26T11:10:14+00:00",
|
70 | 304a92dc | Stavros Sachtouris | name="Debian Base",
|
71 | 304a92dc | Stavros Sachtouris | created="2013-02-26T11:03:29+00:00",
|
72 | 304a92dc | Stavros Sachtouris | progress=100,
|
73 | 304a92dc | Stavros Sachtouris | id=img_ref, |
74 | bcef3ac9 | Stavros Sachtouris | metadata=dict(
|
75 | 304a92dc | Stavros Sachtouris | partition_table="msdos",
|
76 | 304a92dc | Stavros Sachtouris | kernel="2.6.32",
|
77 | 304a92dc | Stavros Sachtouris | osfamily="linux",
|
78 | 304a92dc | Stavros Sachtouris | users="root",
|
79 | 304a92dc | Stavros Sachtouris | gui="No GUI",
|
80 | 304a92dc | Stavros Sachtouris | sortorder="1",
|
81 | 304a92dc | Stavros Sachtouris | os="debian",
|
82 | 304a92dc | Stavros Sachtouris | root_partition="1",
|
83 | bcef3ac9 | Stavros Sachtouris | description="Debian 6.0.7 (Squeeze) Base System")))
|
84 | bcef3ac9 | Stavros Sachtouris | vm_list = dict(servers=[
|
85 | 304a92dc | Stavros Sachtouris | dict(name='n1', id=1), |
86 | bcef3ac9 | Stavros Sachtouris | dict(name='n2', id=2)]) |
87 | bcef3ac9 | Stavros Sachtouris | flavor_list = dict(flavors=[
|
88 | 0482817c | Stavros Sachtouris | dict(id=41, name="C1R1024D20"), |
89 | 0482817c | Stavros Sachtouris | dict(id=42, name="C1R1024D40"), |
90 | bcef3ac9 | Stavros Sachtouris | dict(id=43, name="C1R1028D20")]) |
91 | bcef3ac9 | Stavros Sachtouris | img_list = dict(images=[
|
92 | 304a92dc | Stavros Sachtouris | dict(name="maelstrom", id="0fb03e45-7d5a-4515-bd4e-e6bbf6457f06"), |
93 | 304a92dc | Stavros Sachtouris | dict(name="edx_saas", id="1357163d-5fd8-488e-a117-48734c526206"), |
94 | 304a92dc | Stavros Sachtouris | dict(name="Debian_Wheezy_Base", id="1f8454f0-8e3e-4b6c-ab8e-5236b728dffe"), |
95 | 304a92dc | Stavros Sachtouris | dict(name="CentOS", id="21894b48-c805-4568-ac8b-7d4bb8eb533d"), |
96 | 304a92dc | Stavros Sachtouris | dict(name="Ubuntu Desktop", id="37bc522c-c479-4085-bfb9-464f9b9e2e31"), |
97 | 304a92dc | Stavros Sachtouris | dict(name="Ubuntu 12.10", id="3a24fef9-1a8c-47d1-8f11-e07bd5e544fd"), |
98 | 304a92dc | Stavros Sachtouris | dict(name="Debian Base", id="40ace203-6254-4e17-a5cb-518d55418a7d"), |
99 | bcef3ac9 | Stavros Sachtouris | dict(name="ubuntu_bundled", id="5336e265-5c7c-4127-95cb-2bf832a79903")]) |
100 | 304a92dc | Stavros Sachtouris | |
101 | 304a92dc | Stavros Sachtouris | |
102 | 304a92dc | Stavros Sachtouris | class FR(object): |
103 | 304a92dc | Stavros Sachtouris | """FR stands for Fake Response"""
|
104 | 304a92dc | Stavros Sachtouris | json = vm_recv |
105 | 304a92dc | Stavros Sachtouris | headers = {} |
106 | 304a92dc | Stavros Sachtouris | content = json |
107 | 304a92dc | Stavros Sachtouris | status = None
|
108 | 304a92dc | Stavros Sachtouris | status_code = 200
|
109 | 304a92dc | Stavros Sachtouris | |
110 | 304a92dc | Stavros Sachtouris | |
111 | 99e4d887 | Stavros Sachtouris | def print_iterations(old, new): |
112 | 99e4d887 | Stavros Sachtouris | if new:
|
113 | 99e4d887 | Stavros Sachtouris | if new % 1000: |
114 | 99e4d887 | Stavros Sachtouris | return old
|
115 | 99e4d887 | Stavros Sachtouris | stdout.write('\b' * len('%s' % old)) |
116 | 99e4d887 | Stavros Sachtouris | stdout.write('%s' % new)
|
117 | 99e4d887 | Stavros Sachtouris | else:
|
118 | 99e4d887 | Stavros Sachtouris | stdout.write('# of loops: ')
|
119 | 99e4d887 | Stavros Sachtouris | stdout.flush() |
120 | 99e4d887 | Stavros Sachtouris | return new
|
121 | 99e4d887 | Stavros Sachtouris | |
122 | 99e4d887 | Stavros Sachtouris | |
123 | 3c50df2e | Stavros Sachtouris | class ComputeRestClient(TestCase): |
124 | 630dea9b | Stavros Sachtouris | |
125 | 55faa0bc | Stavros Sachtouris | """Set up a ComputesRest thorough test"""
|
126 | 630dea9b | Stavros Sachtouris | def setUp(self): |
127 | 630dea9b | Stavros Sachtouris | self.url = 'http://cyclades.example.com' |
128 | 630dea9b | Stavros Sachtouris | self.token = 'cyc14d3s70k3n' |
129 | 3c50df2e | Stavros Sachtouris | self.client = compute.ComputeRestClient(self.url, self.token) |
130 | 630dea9b | Stavros Sachtouris | |
131 | 630dea9b | Stavros Sachtouris | def tearDown(self): |
132 | 630dea9b | Stavros Sachtouris | FR.json = vm_recv |
133 | 630dea9b | Stavros Sachtouris | |
134 | 630dea9b | Stavros Sachtouris | @patch('%s.get' % rest_pkg, return_value=FR()) |
135 | 5ed87051 | Stavros Sachtouris | def test_limits_get(self, get): |
136 | 5ed87051 | Stavros Sachtouris | self.client.limits_get(success='some_val') |
137 | 5ed87051 | Stavros Sachtouris | get.assert_called_once_with('/limits', success='some_val') |
138 | 5ed87051 | Stavros Sachtouris | |
139 | 5ed87051 | Stavros Sachtouris | @patch('%s.set_param' % rest_pkg) |
140 | 5ed87051 | Stavros Sachtouris | @patch('%s.get' % rest_pkg, return_value=FR()) |
141 | 5ed87051 | Stavros Sachtouris | def _test_get(self, service, params, get, set_param): |
142 | d08962f0 | Stavros Sachtouris | method = getattr(self.client, '%s_get' % service) |
143 | 5ed87051 | Stavros Sachtouris | param_args = [({}, {k: k}, {k: v[1]}) for k, v in params.items()] |
144 | 99e4d887 | Stavros Sachtouris | num_of_its = 0
|
145 | 5ed87051 | Stavros Sachtouris | for i, args in enumerate(product( |
146 | f3cb8dad | Stavros Sachtouris | ('', '%s_id' % service), |
147 | 5ed87051 | Stavros Sachtouris | (None, False, True), |
148 | 630dea9b | Stavros Sachtouris | (200, 204), |
149 | 5ed87051 | Stavros Sachtouris | ({}, {'k': 'v'}), |
150 | 5ed87051 | Stavros Sachtouris | *param_args)): |
151 | 5ed87051 | Stavros Sachtouris | (srv_id, detail, success, kwargs) = args[:4]
|
152 | 5ed87051 | Stavros Sachtouris | kwargs['success'] = success
|
153 | 5ed87051 | Stavros Sachtouris | srv_kwargs = dict()
|
154 | 5ed87051 | Stavros Sachtouris | for param in args[4:]: |
155 | 5ed87051 | Stavros Sachtouris | srv_kwargs.update(param) |
156 | 5ed87051 | Stavros Sachtouris | srv_kwargs.update(kwargs) |
157 | 5ed87051 | Stavros Sachtouris | method(*args[:2], **srv_kwargs)
|
158 | 5ed87051 | Stavros Sachtouris | srv_str = '/detail' if detail else ( |
159 | 5ed87051 | Stavros Sachtouris | '/%s' % srv_id) if srv_id else '' |
160 | 5ed87051 | Stavros Sachtouris | self.assertEqual(
|
161 | 5ed87051 | Stavros Sachtouris | get.mock_calls[-1],
|
162 | 5ed87051 | Stavros Sachtouris | call('/%s%s' % (service, srv_str), **kwargs))
|
163 | 5ed87051 | Stavros Sachtouris | param_calls = [] |
164 | 5ed87051 | Stavros Sachtouris | for k, v in params.items(): |
165 | 5ed87051 | Stavros Sachtouris | real_v = srv_kwargs.get(k, v[1]) if not srv_id else v[1] |
166 | 5ed87051 | Stavros Sachtouris | param_calls.append(call(v[0], real_v, iff=real_v))
|
167 | 5ed87051 | Stavros Sachtouris | actual = set_param.mock_calls[- len(param_calls):]
|
168 | 5ed87051 | Stavros Sachtouris | self.assertEqual(sorted(actual), sorted(param_calls)) |
169 | 5ed87051 | Stavros Sachtouris | |
170 | 99e4d887 | Stavros Sachtouris | num_of_its = print_iterations(num_of_its, i) |
171 | 99e4d887 | Stavros Sachtouris | print ('\b' * len('%s' % num_of_its)) + ('%s' % i) |
172 | 630dea9b | Stavros Sachtouris | |
173 | d08962f0 | Stavros Sachtouris | @patch('%s.set_param' % rest_pkg) |
174 | d08962f0 | Stavros Sachtouris | @patch('%s.get' % rest_pkg, return_value=FR()) |
175 | d08962f0 | Stavros Sachtouris | def _test_srv_cmd_get(self, srv, cmd, params, get, set_param): |
176 | d08962f0 | Stavros Sachtouris | method = getattr(self.client, '%s_%s_get' % (srv, cmd)) |
177 | d08962f0 | Stavros Sachtouris | param_args = [({}, {k: k}, {k: v[1]}) for k, v in params.items()] |
178 | 99e4d887 | Stavros Sachtouris | num_of_its = 0
|
179 | d08962f0 | Stavros Sachtouris | for i, args in enumerate(product( |
180 | d08962f0 | Stavros Sachtouris | ('some_server_id', 'other_server_id'), |
181 | d08962f0 | Stavros Sachtouris | (None, 'xtra_id'), |
182 | d08962f0 | Stavros Sachtouris | ((304, 200), (1000)), |
183 | d08962f0 | Stavros Sachtouris | ({}, {'k': 'v'}), |
184 | d08962f0 | Stavros Sachtouris | *param_args)): |
185 | d08962f0 | Stavros Sachtouris | srv_id, xtra_id, success, kwargs = args[:4]
|
186 | d08962f0 | Stavros Sachtouris | kwargs['success'] = success
|
187 | d08962f0 | Stavros Sachtouris | srv_kwargs = dict()
|
188 | d08962f0 | Stavros Sachtouris | for param in args[4:]: |
189 | d08962f0 | Stavros Sachtouris | srv_kwargs.update(param) |
190 | d08962f0 | Stavros Sachtouris | srv_kwargs.update(kwargs) |
191 | d08962f0 | Stavros Sachtouris | method(*args[:2], **srv_kwargs)
|
192 | d08962f0 | Stavros Sachtouris | srv_str = '/%s/%s/%s' % (srv, srv_id, cmd)
|
193 | d08962f0 | Stavros Sachtouris | srv_str += ('/%s' % xtra_id) if xtra_id else '' |
194 | d08962f0 | Stavros Sachtouris | self.assertEqual(get.mock_calls[-1], call(srv_str, **kwargs)) |
195 | d08962f0 | Stavros Sachtouris | param_calls = [] |
196 | d08962f0 | Stavros Sachtouris | for k, v in params.items(): |
197 | d08962f0 | Stavros Sachtouris | real_v = srv_kwargs.get(k, v[1])
|
198 | d08962f0 | Stavros Sachtouris | param_calls.append(call(v[0], real_v, iff=real_v))
|
199 | d08962f0 | Stavros Sachtouris | actual = set_param.mock_calls[- len(param_calls):]
|
200 | d08962f0 | Stavros Sachtouris | self.assertEqual(sorted(actual), sorted(param_calls)) |
201 | d08962f0 | Stavros Sachtouris | |
202 | 99e4d887 | Stavros Sachtouris | num_of_its = print_iterations(num_of_its, i) |
203 | 99e4d887 | Stavros Sachtouris | print ('\b' * len('%s' % num_of_its)) + ('%s' % i) |
204 | 99e4d887 | Stavros Sachtouris | |
205 | 99e4d887 | Stavros Sachtouris | @patch('%s.delete' % rest_pkg, return_value=FR()) |
206 | 99e4d887 | Stavros Sachtouris | def _test_delete(self, srv, cmd, delete): |
207 | 99e4d887 | Stavros Sachtouris | method = getattr(
|
208 | 99e4d887 | Stavros Sachtouris | self.client, '%s_%sdelete' % (srv, ('%s_' % cmd) if cmd else '')) |
209 | 99e4d887 | Stavros Sachtouris | cmd_params = ('some_cmd_value', 'some_other_value') if cmd else () |
210 | 99e4d887 | Stavros Sachtouris | num_of_its = 0
|
211 | 99e4d887 | Stavros Sachtouris | for i, args in enumerate(product( |
212 | 99e4d887 | Stavros Sachtouris | ('%s_id' % srv, 'some_value'), |
213 | 99e4d887 | Stavros Sachtouris | (204, 208), |
214 | 99e4d887 | Stavros Sachtouris | ({}, {'k': 'v'}), |
215 | 99e4d887 | Stavros Sachtouris | *cmd_params)): |
216 | 99e4d887 | Stavros Sachtouris | (srv_id, success, kwargs) = args[:3]
|
217 | 99e4d887 | Stavros Sachtouris | kwargs['success'] = success
|
218 | 99e4d887 | Stavros Sachtouris | cmd_value = args[-1] if cmd else '' |
219 | 99e4d887 | Stavros Sachtouris | method_args = (srv_id, cmd_value) if cmd else (srv_id, ) |
220 | 99e4d887 | Stavros Sachtouris | method(*method_args, **kwargs) |
221 | 99e4d887 | Stavros Sachtouris | srv_str = '/%s/%s' % (srv, srv_id)
|
222 | 99e4d887 | Stavros Sachtouris | cmd_str = ('/%s/%s' % (cmd, cmd_value)) if cmd else '' |
223 | 99e4d887 | Stavros Sachtouris | self.assertEqual(
|
224 | 99e4d887 | Stavros Sachtouris | delete.mock_calls[-1],
|
225 | 99e4d887 | Stavros Sachtouris | call('%s%s' % (srv_str, cmd_str), **kwargs))
|
226 | 99e4d887 | Stavros Sachtouris | num_of_its = print_iterations(num_of_its, i) |
227 | 99e4d887 | Stavros Sachtouris | print ('\b' * len('%s' % num_of_its)) + ('%s' % i) |
228 | d08962f0 | Stavros Sachtouris | |
229 | f3cb8dad | Stavros Sachtouris | def test_servers_get(self): |
230 | 5ed87051 | Stavros Sachtouris | params = dict(
|
231 | 5ed87051 | Stavros Sachtouris | changes_since=('changes-since', None), |
232 | 5ed87051 | Stavros Sachtouris | image=('image', None), |
233 | 5ed87051 | Stavros Sachtouris | flavor=('flavor', None), |
234 | 5ed87051 | Stavros Sachtouris | name=('name', None), |
235 | 5ed87051 | Stavros Sachtouris | marker=('marker', None), |
236 | 5ed87051 | Stavros Sachtouris | limit=('limit', None), |
237 | 5ed87051 | Stavros Sachtouris | status=('status', None), |
238 | 5ed87051 | Stavros Sachtouris | host=('host', None)) |
239 | 5ed87051 | Stavros Sachtouris | self._test_get('servers', params) |
240 | f3cb8dad | Stavros Sachtouris | |
241 | d08962f0 | Stavros Sachtouris | def test_servers_metadata_get(self): |
242 | d08962f0 | Stavros Sachtouris | self._test_srv_cmd_get('servers', 'metadata', {}) |
243 | d08962f0 | Stavros Sachtouris | |
244 | d08962f0 | Stavros Sachtouris | def test_servers_ips_get(self): |
245 | d08962f0 | Stavros Sachtouris | params = dict(changes_since=('changes-since', None)) |
246 | d08962f0 | Stavros Sachtouris | self._test_srv_cmd_get('servers', 'ips', params) |
247 | d08962f0 | Stavros Sachtouris | |
248 | f3cb8dad | Stavros Sachtouris | def test_flavors_get(self): |
249 | 5ed87051 | Stavros Sachtouris | params = dict(
|
250 | 5ed87051 | Stavros Sachtouris | changes_since=('changes-since', None), |
251 | 5ed87051 | Stavros Sachtouris | minDisk=('minDisk', None), |
252 | 5ed87051 | Stavros Sachtouris | minRam=('minRam', None), |
253 | 5ed87051 | Stavros Sachtouris | marker=('marker', None), |
254 | 5ed87051 | Stavros Sachtouris | limit=('limit', None)) |
255 | 5ed87051 | Stavros Sachtouris | self._test_get('flavors', params) |
256 | f3cb8dad | Stavros Sachtouris | |
257 | f3cb8dad | Stavros Sachtouris | def test_images_get(self): |
258 | 5ed87051 | Stavros Sachtouris | param = dict(
|
259 | 5ed87051 | Stavros Sachtouris | changes_since=('changes-since', None), |
260 | 5ed87051 | Stavros Sachtouris | server_name=('server', None), |
261 | 5ed87051 | Stavros Sachtouris | name=('name', None), |
262 | 5ed87051 | Stavros Sachtouris | status=('status', None), |
263 | 5ed87051 | Stavros Sachtouris | marker=('marker', None), |
264 | 5ed87051 | Stavros Sachtouris | limit=('limit', None), |
265 | 5ed87051 | Stavros Sachtouris | type=('type', None)) |
266 | 5ed87051 | Stavros Sachtouris | self._test_get('images', param) |
267 | f3cb8dad | Stavros Sachtouris | |
268 | d08962f0 | Stavros Sachtouris | def test_images_metadata_get(self): |
269 | d08962f0 | Stavros Sachtouris | self._test_srv_cmd_get('images', 'metadata', {}) |
270 | d08962f0 | Stavros Sachtouris | |
271 | f3cb8dad | Stavros Sachtouris | def test_servers_delete(self): |
272 | d08962f0 | Stavros Sachtouris | self._test_delete('servers', None) |
273 | d08962f0 | Stavros Sachtouris | |
274 | d08962f0 | Stavros Sachtouris | def test_servers_metadata_delete(self): |
275 | d08962f0 | Stavros Sachtouris | self._test_delete('servers', 'metadata') |
276 | f3cb8dad | Stavros Sachtouris | |
277 | f3cb8dad | Stavros Sachtouris | def test_images_delete(self): |
278 | d08962f0 | Stavros Sachtouris | self._test_delete('images', None) |
279 | d08962f0 | Stavros Sachtouris | |
280 | d08962f0 | Stavros Sachtouris | def test_images_metadata_delete(self): |
281 | d08962f0 | Stavros Sachtouris | self._test_delete('images', 'metadata') |
282 | f3cb8dad | Stavros Sachtouris | |
283 | 6a00d7a7 | Stavros Sachtouris | @patch('%s.set_header' % rest_pkg) |
284 | 6a00d7a7 | Stavros Sachtouris | @patch('%s.post' % rest_pkg, return_value=FR()) |
285 | a8fe029d | Stavros Sachtouris | def _test_post(self, service, post, SH): |
286 | 6a00d7a7 | Stavros Sachtouris | for args in product( |
287 | a8fe029d | Stavros Sachtouris | ('', '%s_id' % service), |
288 | 6a00d7a7 | Stavros Sachtouris | ('', 'cmd'), |
289 | 6a00d7a7 | Stavros Sachtouris | (None, [dict(json="data"), dict(data="json")]), |
290 | 6a00d7a7 | Stavros Sachtouris | (202, 204), |
291 | 6a00d7a7 | Stavros Sachtouris | ({}, {'k': 'v'})): |
292 | a8fe029d | Stavros Sachtouris | (srv_id, command, json_data, success, kwargs) = args |
293 | a8fe029d | Stavros Sachtouris | method = getattr(self.client, '%s_post' % service) |
294 | a8fe029d | Stavros Sachtouris | method(*args[:4], **kwargs)
|
295 | a8fe029d | Stavros Sachtouris | vm_str = '/%s' % srv_id if srv_id else '' |
296 | 6a00d7a7 | Stavros Sachtouris | cmd_str = '/%s' % command if command else '' |
297 | 6a00d7a7 | Stavros Sachtouris | if json_data:
|
298 | 6a00d7a7 | Stavros Sachtouris | json_data = dumps(json_data) |
299 | 6a00d7a7 | Stavros Sachtouris | self.assertEqual(SH.mock_calls[-2:], [ |
300 | 6a00d7a7 | Stavros Sachtouris | call('Content-Type', 'application/json'), |
301 | 6a00d7a7 | Stavros Sachtouris | call('Content-Length', len(json_data))]) |
302 | 6a00d7a7 | Stavros Sachtouris | self.assertEqual(post.mock_calls[-1], call( |
303 | a8fe029d | Stavros Sachtouris | '/%s%s%s' % (service, vm_str, cmd_str),
|
304 | 6a00d7a7 | Stavros Sachtouris | data=json_data, success=success, |
305 | 6a00d7a7 | Stavros Sachtouris | **kwargs)) |
306 | 882c7e33 | Stavros Sachtouris | |
307 | a8fe029d | Stavros Sachtouris | def test_servers_post(self): |
308 | a8fe029d | Stavros Sachtouris | self._test_post('servers') |
309 | a8fe029d | Stavros Sachtouris | |
310 | a8fe029d | Stavros Sachtouris | def test_images_post(self): |
311 | a8fe029d | Stavros Sachtouris | self._test_post('images') |
312 | a8fe029d | Stavros Sachtouris | |
313 | 6695edc9 | Stavros Sachtouris | @patch('%s.set_header' % rest_pkg) |
314 | 6695edc9 | Stavros Sachtouris | @patch('%s.put' % rest_pkg, return_value=FR()) |
315 | a8fe029d | Stavros Sachtouris | def _test_put(self, service, put, SH): |
316 | 6695edc9 | Stavros Sachtouris | for args in product( |
317 | a8fe029d | Stavros Sachtouris | ('', '%s_id' % service), |
318 | 6695edc9 | Stavros Sachtouris | ('', 'cmd'), |
319 | 6695edc9 | Stavros Sachtouris | (None, [dict(json="data"), dict(data="json")]), |
320 | 6695edc9 | Stavros Sachtouris | (204, 504), |
321 | 6695edc9 | Stavros Sachtouris | ({}, {'k': 'v'})): |
322 | 6695edc9 | Stavros Sachtouris | (server_id, command, json_data, success, kwargs) = args |
323 | a8fe029d | Stavros Sachtouris | method = getattr(self.client, '%s_put' % service) |
324 | a8fe029d | Stavros Sachtouris | method(*args[:4], **kwargs)
|
325 | 6695edc9 | Stavros Sachtouris | vm_str = '/%s' % server_id if server_id else '' |
326 | 6695edc9 | Stavros Sachtouris | cmd_str = '/%s' % command if command else '' |
327 | 6695edc9 | Stavros Sachtouris | if json_data:
|
328 | 6695edc9 | Stavros Sachtouris | json_data = dumps(json_data) |
329 | 6695edc9 | Stavros Sachtouris | self.assertEqual(SH.mock_calls[-2:], [ |
330 | 6695edc9 | Stavros Sachtouris | call('Content-Type', 'application/json'), |
331 | 6695edc9 | Stavros Sachtouris | call('Content-Length', len(json_data))]) |
332 | 6695edc9 | Stavros Sachtouris | self.assertEqual(put.mock_calls[-1], call( |
333 | a8fe029d | Stavros Sachtouris | '/%s%s%s' % (service, vm_str, cmd_str),
|
334 | 6695edc9 | Stavros Sachtouris | data=json_data, success=success, |
335 | 6695edc9 | Stavros Sachtouris | **kwargs)) |
336 | 6695edc9 | Stavros Sachtouris | |
337 | a8fe029d | Stavros Sachtouris | def test_servers_put(self): |
338 | a8fe029d | Stavros Sachtouris | self._test_put('servers') |
339 | a8fe029d | Stavros Sachtouris | |
340 | a8fe029d | Stavros Sachtouris | def test_images_put(self): |
341 | a8fe029d | Stavros Sachtouris | self._test_put('images') |
342 | a8fe029d | Stavros Sachtouris | |
343 | 0a62912d | Stavros Sachtouris | @patch('%s.get' % rest_pkg, return_value=FR()) |
344 | 0a62912d | Stavros Sachtouris | def test_floating_ip_pools_get(self, get): |
345 | 0a62912d | Stavros Sachtouris | for args in product( |
346 | 0a62912d | Stavros Sachtouris | ('tenant1', 'tenant2'), |
347 | 0a62912d | Stavros Sachtouris | (200, 204), |
348 | 0a62912d | Stavros Sachtouris | ({}, {'k': 'v'})): |
349 | 0a62912d | Stavros Sachtouris | tenant_id, success, kwargs = args |
350 | 0a62912d | Stavros Sachtouris | r = self.client.floating_ip_pools_get(tenant_id, success, **kwargs)
|
351 | 0a62912d | Stavros Sachtouris | self.assertTrue(isinstance(r, FR)) |
352 | 0a62912d | Stavros Sachtouris | self.assertEqual(get.mock_calls[-1], call( |
353 | 0a62912d | Stavros Sachtouris | '/%s/os-floating-ip-pools' % tenant_id,
|
354 | 0a62912d | Stavros Sachtouris | success=success, **kwargs)) |
355 | 0a62912d | Stavros Sachtouris | |
356 | 77d1b504 | Stavros Sachtouris | @patch('%s.get' % rest_pkg, return_value=FR()) |
357 | 77d1b504 | Stavros Sachtouris | def test_floating_ips_get(self, get): |
358 | 77d1b504 | Stavros Sachtouris | for args in product( |
359 | 77d1b504 | Stavros Sachtouris | ('tenant1', 'tenant2'), |
360 | 03033b54 | Stavros Sachtouris | ('', '192.193.194.195'), |
361 | 77d1b504 | Stavros Sachtouris | (200, 204), |
362 | 77d1b504 | Stavros Sachtouris | ({}, {'k': 'v'})): |
363 | 03033b54 | Stavros Sachtouris | tenant_id, ip, success, kwargs = args |
364 | 03033b54 | Stavros Sachtouris | r = self.client.floating_ips_get(*args[:3], **kwargs) |
365 | 77d1b504 | Stavros Sachtouris | self.assertTrue(isinstance(r, FR)) |
366 | 03033b54 | Stavros Sachtouris | expected = '' if not ip else '/%s' % ip |
367 | 77d1b504 | Stavros Sachtouris | self.assertEqual(get.mock_calls[-1], call( |
368 | 03033b54 | Stavros Sachtouris | '/%s/os-floating-ips%s' % (tenant_id, expected),
|
369 | 77d1b504 | Stavros Sachtouris | success=success, **kwargs)) |
370 | 77d1b504 | Stavros Sachtouris | |
371 | 77d1b504 | Stavros Sachtouris | @patch('%s.set_header' % rest_pkg) |
372 | 77d1b504 | Stavros Sachtouris | @patch('%s.post' % rest_pkg, return_value=FR()) |
373 | 77d1b504 | Stavros Sachtouris | def test_floating_ips_post(self, post, SH): |
374 | 77d1b504 | Stavros Sachtouris | for args in product( |
375 | 77d1b504 | Stavros Sachtouris | ('tenant1', 'tenant2'), |
376 | 77d1b504 | Stavros Sachtouris | (None, [dict(json="data"), dict(data="json")]), |
377 | 03033b54 | Stavros Sachtouris | ('', '192.193.194.195'), |
378 | 77d1b504 | Stavros Sachtouris | (202, 204), |
379 | 77d1b504 | Stavros Sachtouris | ({}, {'k': 'v'})): |
380 | 03033b54 | Stavros Sachtouris | (tenant_id, json_data, ip, success, kwargs) = args |
381 | 03033b54 | Stavros Sachtouris | self.client.floating_ips_post(*args[:4], **kwargs) |
382 | 77d1b504 | Stavros Sachtouris | if json_data:
|
383 | 77d1b504 | Stavros Sachtouris | json_data = dumps(json_data) |
384 | 77d1b504 | Stavros Sachtouris | self.assertEqual(SH.mock_calls[-2:], [ |
385 | 77d1b504 | Stavros Sachtouris | call('Content-Type', 'application/json'), |
386 | 77d1b504 | Stavros Sachtouris | call('Content-Length', len(json_data))]) |
387 | 03033b54 | Stavros Sachtouris | expected = '' if not ip else '/%s' % ip |
388 | 77d1b504 | Stavros Sachtouris | self.assertEqual(post.mock_calls[-1], call( |
389 | 03033b54 | Stavros Sachtouris | '/%s/os-floating-ips%s' % (tenant_id, expected),
|
390 | 77d1b504 | Stavros Sachtouris | data=json_data, success=success, |
391 | 77d1b504 | Stavros Sachtouris | **kwargs)) |
392 | 77d1b504 | Stavros Sachtouris | |
393 | 77d1b504 | Stavros Sachtouris | @patch('%s.delete' % rest_pkg, return_value=FR()) |
394 | 03033b54 | Stavros Sachtouris | def test_floating_ips_delete(self, delete): |
395 | 77d1b504 | Stavros Sachtouris | for args in product( |
396 | 77d1b504 | Stavros Sachtouris | ('tenant1', 'tenant2'), |
397 | 03033b54 | Stavros Sachtouris | ('', '192.193.194.195'), |
398 | 77d1b504 | Stavros Sachtouris | (204,),
|
399 | 77d1b504 | Stavros Sachtouris | ({}, {'k': 'v'})): |
400 | 03033b54 | Stavros Sachtouris | tenant_id, ip, success, kwargs = args |
401 | 03033b54 | Stavros Sachtouris | r = self.client.floating_ips_delete(*args[:3], **kwargs) |
402 | 77d1b504 | Stavros Sachtouris | self.assertTrue(isinstance(r, FR)) |
403 | 03033b54 | Stavros Sachtouris | expected = '' if not ip else '/%s' % ip |
404 | 77d1b504 | Stavros Sachtouris | self.assertEqual(delete.mock_calls[-1], call( |
405 | 03033b54 | Stavros Sachtouris | '/%s/os-floating-ips%s' % (tenant_id, expected),
|
406 | 77d1b504 | Stavros Sachtouris | success=success, **kwargs)) |
407 | 77d1b504 | Stavros Sachtouris | |
408 | 630dea9b | Stavros Sachtouris | |
409 | 3c50df2e | Stavros Sachtouris | class ComputeClient(TestCase): |
410 | 304a92dc | Stavros Sachtouris | |
411 | 304a92dc | Stavros Sachtouris | def assert_dicts_are_equal(self, d1, d2): |
412 | 304a92dc | Stavros Sachtouris | for k, v in d1.items(): |
413 | 304a92dc | Stavros Sachtouris | self.assertTrue(k in d2) |
414 | 304a92dc | Stavros Sachtouris | if isinstance(v, dict): |
415 | 304a92dc | Stavros Sachtouris | self.assert_dicts_are_equal(v, d2[k])
|
416 | 304a92dc | Stavros Sachtouris | else:
|
417 | 304a92dc | Stavros Sachtouris | self.assertEqual(unicode(v), unicode(d2[k])) |
418 | 304a92dc | Stavros Sachtouris | |
419 | 304a92dc | Stavros Sachtouris | """Set up a Cyclades thorough test"""
|
420 | 304a92dc | Stavros Sachtouris | def setUp(self): |
421 | 304a92dc | Stavros Sachtouris | self.url = 'http://cyclades.example.com' |
422 | 304a92dc | Stavros Sachtouris | self.token = 'cyc14d3s70k3n' |
423 | 3c50df2e | Stavros Sachtouris | self.client = compute.ComputeClient(self.url, self.token) |
424 | 304a92dc | Stavros Sachtouris | |
425 | 304a92dc | Stavros Sachtouris | def tearDown(self): |
426 | 304a92dc | Stavros Sachtouris | FR.status_code = 200
|
427 | 304a92dc | Stavros Sachtouris | FR.json = vm_recv |
428 | 304a92dc | Stavros Sachtouris | |
429 | edf8ad52 | Stavros Sachtouris | @patch(
|
430 | edf8ad52 | Stavros Sachtouris | '%s.get_image_details' % compute_pkg,
|
431 | edf8ad52 | Stavros Sachtouris | return_value=img_recv['image'])
|
432 | edf8ad52 | Stavros Sachtouris | def test_create_server(self, GID): |
433 | edf8ad52 | Stavros Sachtouris | with patch.object(
|
434 | 3c50df2e | Stavros Sachtouris | compute.ComputeClient, 'servers_post',
|
435 | edf8ad52 | Stavros Sachtouris | side_effect=ClientError( |
436 | edf8ad52 | Stavros Sachtouris | 'REQUEST ENTITY TOO LARGE',
|
437 | edf8ad52 | Stavros Sachtouris | status=403)):
|
438 | 304a92dc | Stavros Sachtouris | self.assertRaises(
|
439 | 304a92dc | Stavros Sachtouris | ClientError, |
440 | 304a92dc | Stavros Sachtouris | self.client.create_server,
|
441 | 304a92dc | Stavros Sachtouris | vm_name, fid, img_ref) |
442 | 304a92dc | Stavros Sachtouris | |
443 | 304a92dc | Stavros Sachtouris | with patch.object(
|
444 | 3c50df2e | Stavros Sachtouris | compute.ComputeClient, 'servers_post',
|
445 | edf8ad52 | Stavros Sachtouris | return_value=FR()) as post:
|
446 | 304a92dc | Stavros Sachtouris | r = self.client.create_server(vm_name, fid, img_ref)
|
447 | edf8ad52 | Stavros Sachtouris | self.assertEqual(r, FR.json['server']) |
448 | edf8ad52 | Stavros Sachtouris | self.assertEqual(GID.mock_calls[-1], call(img_ref)) |
449 | edf8ad52 | Stavros Sachtouris | self.assertEqual(post.mock_calls[-1], call(json_data=vm_send)) |
450 | 304a92dc | Stavros Sachtouris | prsn = 'Personality string (does not work with real servers)'
|
451 | 304a92dc | Stavros Sachtouris | self.client.create_server(vm_name, fid, img_ref, prsn)
|
452 | edf8ad52 | Stavros Sachtouris | expected = dict(server=dict(vm_send['server'])) |
453 | edf8ad52 | Stavros Sachtouris | expected['server']['personality'] = prsn |
454 | edf8ad52 | Stavros Sachtouris | self.assertEqual(post.mock_calls[-1], call(json_data=expected)) |
455 | 304a92dc | Stavros Sachtouris | |
456 | 0067c031 | Stavros Sachtouris | @patch('%s.servers_get' % compute_pkg, return_value=FR()) |
457 | 0067c031 | Stavros Sachtouris | def test_list_servers(self, SG): |
458 | 304a92dc | Stavros Sachtouris | FR.json = vm_list |
459 | 0067c031 | Stavros Sachtouris | for detail in (False, True): |
460 | 0067c031 | Stavros Sachtouris | r = self.client.list_servers(detail)
|
461 | 0067c031 | Stavros Sachtouris | self.assertEqual(SG.mock_calls[-1], call( |
462 | 0067c031 | Stavros Sachtouris | command='detail' if detail else '')) |
463 | bcef3ac9 | Stavros Sachtouris | for i, vm in enumerate(vm_list['servers']): |
464 | b6403d2d | Stavros Sachtouris | self.assert_dicts_are_equal(r[i], vm)
|
465 | b6403d2d | Stavros Sachtouris | self.assertEqual(i + 1, len(r)) |
466 | 304a92dc | Stavros Sachtouris | |
467 | 26ccd6fb | Stavros Sachtouris | @patch('%s.servers_get' % compute_pkg, return_value=FR()) |
468 | 26ccd6fb | Stavros Sachtouris | def test_get_server_details(self, SG): |
469 | 304a92dc | Stavros Sachtouris | vm_id = vm_recv['server']['id'] |
470 | 304a92dc | Stavros Sachtouris | r = self.client.get_server_details(vm_id)
|
471 | b6403d2d | Stavros Sachtouris | SG.assert_called_once_with(vm_id) |
472 | 304a92dc | Stavros Sachtouris | self.assert_dicts_are_equal(r, vm_recv['server']) |
473 | 304a92dc | Stavros Sachtouris | |
474 | 2ace03e5 | Stavros Sachtouris | @patch('%s.servers_put' % compute_pkg, return_value=FR()) |
475 | 2ace03e5 | Stavros Sachtouris | def test_update_server_name(self, SP): |
476 | 304a92dc | Stavros Sachtouris | vm_id = vm_recv['server']['id'] |
477 | 304a92dc | Stavros Sachtouris | new_name = vm_name + '_new'
|
478 | 304a92dc | Stavros Sachtouris | self.client.update_server_name(vm_id, new_name)
|
479 | b6403d2d | Stavros Sachtouris | SP.assert_called_once_with(vm_id, json_data=dict(
|
480 | b6403d2d | Stavros Sachtouris | server=dict(name=new_name)))
|
481 | 304a92dc | Stavros Sachtouris | |
482 | 876aae74 | Stavros Sachtouris | @patch('%s.servers_post' % compute_pkg, return_value=FR()) |
483 | 876aae74 | Stavros Sachtouris | def test_reboot_server(self, SP): |
484 | 304a92dc | Stavros Sachtouris | vm_id = vm_recv['server']['id'] |
485 | 876aae74 | Stavros Sachtouris | for hard in (None, True): |
486 | 876aae74 | Stavros Sachtouris | self.client.reboot_server(vm_id, hard=hard)
|
487 | 876aae74 | Stavros Sachtouris | self.assertEqual(SP.mock_calls[-1], call( |
488 | 876aae74 | Stavros Sachtouris | vm_id, 'action',
|
489 | 876aae74 | Stavros Sachtouris | json_data=dict(reboot=dict(type='HARD' if hard else 'SOFT')))) |
490 | 304a92dc | Stavros Sachtouris | |
491 | d07fddd9 | Stavros Sachtouris | @patch('%s.servers_post' % compute_pkg, return_value=FR()) |
492 | d07fddd9 | Stavros Sachtouris | def test_resize_server(self, SP): |
493 | d07fddd9 | Stavros Sachtouris | vm_id, flavor = vm_recv['server']['id'], flavor_list['flavors'][1] |
494 | d07fddd9 | Stavros Sachtouris | self.client.resize_server(vm_id, flavor['id']) |
495 | d07fddd9 | Stavros Sachtouris | exp = dict(resize=dict(flavorRef=flavor['id'])) |
496 | d07fddd9 | Stavros Sachtouris | SP.assert_called_once_with(vm_id, 'action', json_data=exp)
|
497 | d07fddd9 | Stavros Sachtouris | |
498 | d560c67b | Stavros Sachtouris | @patch('%s.servers_put' % compute_pkg, return_value=FR()) |
499 | d560c67b | Stavros Sachtouris | def test_create_server_metadata(self, SP): |
500 | 304a92dc | Stavros Sachtouris | vm_id = vm_recv['server']['id'] |
501 | 304a92dc | Stavros Sachtouris | metadata = dict(m1='v1', m2='v2', m3='v3') |
502 | c088076b | Stavros Sachtouris | FR.json = dict(meta=vm_recv['server']) |
503 | 304a92dc | Stavros Sachtouris | for k, v in metadata.items(): |
504 | 304a92dc | Stavros Sachtouris | r = self.client.create_server_metadata(vm_id, k, v)
|
505 | 304a92dc | Stavros Sachtouris | self.assert_dicts_are_equal(r, vm_recv['server']) |
506 | d560c67b | Stavros Sachtouris | self.assertEqual(SP.mock_calls[-1], call( |
507 | bcef3ac9 | Stavros Sachtouris | vm_id, 'metadata/%s' % k,
|
508 | c088076b | Stavros Sachtouris | json_data=dict(meta={k: v}), success=201)) |
509 | 304a92dc | Stavros Sachtouris | |
510 | d224bb37 | Stavros Sachtouris | @patch('%s.servers_get' % compute_pkg, return_value=FR()) |
511 | d224bb37 | Stavros Sachtouris | def test_get_server_metadata(self, SG): |
512 | 304a92dc | Stavros Sachtouris | vm_id = vm_recv['server']['id'] |
513 | 304a92dc | Stavros Sachtouris | metadata = dict(m1='v1', m2='v2', m3='v3') |
514 | bcef3ac9 | Stavros Sachtouris | FR.json = dict(metadata=metadata)
|
515 | 304a92dc | Stavros Sachtouris | r = self.client.get_server_metadata(vm_id)
|
516 | c088076b | Stavros Sachtouris | FR.json = dict(meta=metadata)
|
517 | bcef3ac9 | Stavros Sachtouris | SG.assert_called_once_with(vm_id, '/metadata')
|
518 | 304a92dc | Stavros Sachtouris | self.assert_dicts_are_equal(r, metadata)
|
519 | 304a92dc | Stavros Sachtouris | |
520 | 304a92dc | Stavros Sachtouris | for k, v in metadata.items(): |
521 | c088076b | Stavros Sachtouris | FR.json = dict(meta={k: v})
|
522 | 304a92dc | Stavros Sachtouris | r = self.client.get_server_metadata(vm_id, k)
|
523 | 304a92dc | Stavros Sachtouris | self.assert_dicts_are_equal(r, {k: v})
|
524 | bcef3ac9 | Stavros Sachtouris | self.assertEqual(
|
525 | bcef3ac9 | Stavros Sachtouris | SG.mock_calls[-1], call(vm_id, '/metadata/%s' % k)) |
526 | 304a92dc | Stavros Sachtouris | |
527 | d224bb37 | Stavros Sachtouris | @patch('%s.servers_post' % compute_pkg, return_value=FR()) |
528 | e750a969 | Stavros Sachtouris | def test_update_server_metadata(self, SP): |
529 | 304a92dc | Stavros Sachtouris | vm_id = vm_recv['server']['id'] |
530 | 304a92dc | Stavros Sachtouris | metadata = dict(m1='v1', m2='v2', m3='v3') |
531 | 304a92dc | Stavros Sachtouris | FR.json = dict(metadata=metadata)
|
532 | 304a92dc | Stavros Sachtouris | r = self.client.update_server_metadata(vm_id, **metadata)
|
533 | 304a92dc | Stavros Sachtouris | self.assert_dicts_are_equal(r, metadata)
|
534 | b6403d2d | Stavros Sachtouris | SP.assert_called_once_with( |
535 | bcef3ac9 | Stavros Sachtouris | vm_id, 'metadata',
|
536 | b6403d2d | Stavros Sachtouris | json_data=dict(metadata=metadata), success=201) |
537 | 304a92dc | Stavros Sachtouris | |
538 | d224bb37 | Stavros Sachtouris | @patch('%s.servers_delete' % compute_pkg, return_value=FR()) |
539 | dbd50059 | Stavros Sachtouris | def test_delete_server_metadata(self, SD): |
540 | 304a92dc | Stavros Sachtouris | vm_id = vm_recv['server']['id'] |
541 | 304a92dc | Stavros Sachtouris | key = 'metakey'
|
542 | 304a92dc | Stavros Sachtouris | self.client.delete_server_metadata(vm_id, key)
|
543 | bcef3ac9 | Stavros Sachtouris | SD.assert_called_once_with(vm_id, 'metadata/' + key)
|
544 | 304a92dc | Stavros Sachtouris | |
545 | 8c40c4a6 | Stavros Sachtouris | @patch('%s.flavors_get' % compute_pkg, return_value=FR()) |
546 | 8c40c4a6 | Stavros Sachtouris | def test_list_flavors(self, FG): |
547 | 304a92dc | Stavros Sachtouris | FR.json = flavor_list |
548 | 8c40c4a6 | Stavros Sachtouris | for cmd in ('', 'detail'): |
549 | 8c40c4a6 | Stavros Sachtouris | r = self.client.list_flavors(detail=(cmd == 'detail')) |
550 | 8c40c4a6 | Stavros Sachtouris | self.assertEqual(FG.mock_calls[-1], call(command=cmd)) |
551 | bcef3ac9 | Stavros Sachtouris | self.assertEqual(r, flavor_list['flavors']) |
552 | 304a92dc | Stavros Sachtouris | |
553 | ae9c3b5f | Stavros Sachtouris | @patch('%s.flavors_get' % compute_pkg, return_value=FR()) |
554 | ae9c3b5f | Stavros Sachtouris | def test_get_flavor_details(self, FG): |
555 | bcef3ac9 | Stavros Sachtouris | FR.json = dict(flavor=flavor_list['flavors'][0]) |
556 | 304a92dc | Stavros Sachtouris | r = self.client.get_flavor_details(fid)
|
557 | b6403d2d | Stavros Sachtouris | FG.assert_called_once_with(fid) |
558 | bcef3ac9 | Stavros Sachtouris | self.assert_dicts_are_equal(r, flavor_list['flavors'][0]) |
559 | 304a92dc | Stavros Sachtouris | |
560 | b2347a17 | Stavros Sachtouris | @patch('%s.images_get' % compute_pkg, return_value=FR()) |
561 | b2347a17 | Stavros Sachtouris | def test_list_images(self, IG): |
562 | 304a92dc | Stavros Sachtouris | FR.json = img_list |
563 | b2347a17 | Stavros Sachtouris | for cmd in ('', 'detail'): |
564 | b2347a17 | Stavros Sachtouris | r = self.client.list_images(detail=(cmd == 'detail')) |
565 | b2347a17 | Stavros Sachtouris | self.assertEqual(IG.mock_calls[-1], call(command=cmd)) |
566 | bcef3ac9 | Stavros Sachtouris | expected = img_list['images']
|
567 | b2347a17 | Stavros Sachtouris | for i in range(len(r)): |
568 | b2347a17 | Stavros Sachtouris | self.assert_dicts_are_equal(expected[i], r[i])
|
569 | 304a92dc | Stavros Sachtouris | |
570 | 937ef4dd | Stavros Sachtouris | @patch('%s.images_get' % compute_pkg, return_value=FR()) |
571 | 937ef4dd | Stavros Sachtouris | def test_get_image_details(self, IG): |
572 | 304a92dc | Stavros Sachtouris | FR.json = img_recv |
573 | 304a92dc | Stavros Sachtouris | r = self.client.get_image_details(img_ref)
|
574 | b6403d2d | Stavros Sachtouris | IG.assert_called_once_with(img_ref) |
575 | 304a92dc | Stavros Sachtouris | self.assert_dicts_are_equal(r, img_recv['image']) |
576 | 304a92dc | Stavros Sachtouris | |
577 | d224bb37 | Stavros Sachtouris | @patch('%s.images_get' % compute_pkg, return_value=FR()) |
578 | 304a92dc | Stavros Sachtouris | def test_get_image_metadata(self, IG): |
579 | b6403d2d | Stavros Sachtouris | for key in ('', '50m3k3y'): |
580 | c088076b | Stavros Sachtouris | FR.json = dict(meta=img_recv['image']) if ( |
581 | bcef3ac9 | Stavros Sachtouris | key) else dict(metadata=img_recv['image']) |
582 | b6403d2d | Stavros Sachtouris | r = self.client.get_image_metadata(img_ref, key)
|
583 | b6403d2d | Stavros Sachtouris | self.assertEqual(IG.mock_calls[-1], call( |
584 | b6403d2d | Stavros Sachtouris | '%s' % img_ref,
|
585 | bcef3ac9 | Stavros Sachtouris | '/metadata%s' % (('/%s' % key) if key else ''))) |
586 | b6403d2d | Stavros Sachtouris | self.assert_dicts_are_equal(img_recv['image'], r) |
587 | 304a92dc | Stavros Sachtouris | |
588 | 0651cda3 | Stavros Sachtouris | @patch('%s.servers_delete' % compute_pkg, return_value=FR()) |
589 | 0651cda3 | Stavros Sachtouris | def test_delete_server(self, SD): |
590 | 304a92dc | Stavros Sachtouris | vm_id = vm_recv['server']['id'] |
591 | 304a92dc | Stavros Sachtouris | self.client.delete_server(vm_id)
|
592 | 1df5305d | Stavros Sachtouris | SD.assert_called_once_with(vm_id) |
593 | 304a92dc | Stavros Sachtouris | |
594 | f711008c | Stavros Sachtouris | @patch('%s.images_delete' % compute_pkg, return_value=FR()) |
595 | f711008c | Stavros Sachtouris | def test_delete_image(self, ID): |
596 | 304a92dc | Stavros Sachtouris | self.client.delete_image(img_ref)
|
597 | 1df5305d | Stavros Sachtouris | ID.assert_called_once_with(img_ref) |
598 | 304a92dc | Stavros Sachtouris | |
599 | 71017d87 | Stavros Sachtouris | @patch('%s.images_put' % compute_pkg, return_value=FR()) |
600 | 71017d87 | Stavros Sachtouris | def test_create_image_metadata(self, IP): |
601 | 71017d87 | Stavros Sachtouris | (key, val) = ('k1', 'v1') |
602 | c088076b | Stavros Sachtouris | FR.json = dict(meta=img_recv['image']) |
603 | 71017d87 | Stavros Sachtouris | r = self.client.create_image_metadata(img_ref, key, val)
|
604 | 1df5305d | Stavros Sachtouris | IP.assert_called_once_with( |
605 | bcef3ac9 | Stavros Sachtouris | img_ref, 'metadata/%s' % key,
|
606 | c088076b | Stavros Sachtouris | json_data=dict(meta={key: val}))
|
607 | 71017d87 | Stavros Sachtouris | self.assert_dicts_are_equal(r, img_recv['image']) |
608 | 304a92dc | Stavros Sachtouris | |
609 | d224bb37 | Stavros Sachtouris | @patch('%s.images_post' % compute_pkg, return_value=FR()) |
610 | 428aa852 | Stavros Sachtouris | def test_update_image_metadata(self, IP): |
611 | 304a92dc | Stavros Sachtouris | metadata = dict(m1='v1', m2='v2', m3='v3') |
612 | 304a92dc | Stavros Sachtouris | FR.json = dict(metadata=metadata)
|
613 | 304a92dc | Stavros Sachtouris | r = self.client.update_image_metadata(img_ref, **metadata)
|
614 | 1df5305d | Stavros Sachtouris | IP.assert_called_once_with( |
615 | bcef3ac9 | Stavros Sachtouris | img_ref, 'metadata',
|
616 | 1df5305d | Stavros Sachtouris | json_data=dict(metadata=metadata))
|
617 | 304a92dc | Stavros Sachtouris | self.assert_dicts_are_equal(r, metadata)
|
618 | 304a92dc | Stavros Sachtouris | |
619 | d224bb37 | Stavros Sachtouris | @patch('%s.images_delete' % compute_pkg, return_value=FR()) |
620 | 0482817c | Stavros Sachtouris | def test_delete_image_metadata(self, ID): |
621 | 304a92dc | Stavros Sachtouris | key = 'metakey'
|
622 | 304a92dc | Stavros Sachtouris | self.client.delete_image_metadata(img_ref, key)
|
623 | bcef3ac9 | Stavros Sachtouris | ID.assert_called_once_with(img_ref, '/metadata/%s' % key)
|
624 | 304a92dc | Stavros Sachtouris | |
625 | 03033b54 | Stavros Sachtouris | @patch('%s.floating_ip_pools_get' % compute_pkg, return_value=FR()) |
626 | 03033b54 | Stavros Sachtouris | def test_get_floating_ip_pools(self, get): |
627 | 03033b54 | Stavros Sachtouris | tid = 't3n@nt_1d'
|
628 | 03033b54 | Stavros Sachtouris | r = self.client.get_floating_ip_pools(tid)
|
629 | 03033b54 | Stavros Sachtouris | self.assert_dicts_are_equal(r, FR.json)
|
630 | 03033b54 | Stavros Sachtouris | self.assertEqual(get.mock_calls[-1], call(tid)) |
631 | 03033b54 | Stavros Sachtouris | |
632 | 03033b54 | Stavros Sachtouris | @patch('%s.floating_ips_get' % compute_pkg, return_value=FR()) |
633 | 03033b54 | Stavros Sachtouris | def test_get_floating_ips(self, get): |
634 | 03033b54 | Stavros Sachtouris | tid = 't3n@nt_1d'
|
635 | 03033b54 | Stavros Sachtouris | r = self.client.get_floating_ips(tid)
|
636 | 03033b54 | Stavros Sachtouris | self.assert_dicts_are_equal(r, FR.json)
|
637 | 03033b54 | Stavros Sachtouris | self.assertEqual(get.mock_calls[-1], call(tid)) |
638 | 03033b54 | Stavros Sachtouris | |
639 | 03033b54 | Stavros Sachtouris | @patch('%s.floating_ips_post' % compute_pkg, return_value=FR()) |
640 | 03033b54 | Stavros Sachtouris | def test_alloc_floating_ip(self, post): |
641 | 03033b54 | Stavros Sachtouris | FR.json = dict(floating_ip=dict( |
642 | 03033b54 | Stavros Sachtouris | fixed_ip='fip',
|
643 | 03033b54 | Stavros Sachtouris | id=1,
|
644 | 03033b54 | Stavros Sachtouris | instance_id='lala',
|
645 | 03033b54 | Stavros Sachtouris | ip='102.0.0.1',
|
646 | 03033b54 | Stavros Sachtouris | pool='pisine'))
|
647 | 03033b54 | Stavros Sachtouris | for args in product( |
648 | 03033b54 | Stavros Sachtouris | ('t1', 't2'), |
649 | 03033b54 | Stavros Sachtouris | (None, 'pisine')): |
650 | 03033b54 | Stavros Sachtouris | r = self.client.alloc_floating_ip(*args)
|
651 | 03033b54 | Stavros Sachtouris | tenant_id, pool = args |
652 | 03033b54 | Stavros Sachtouris | self.assert_dicts_are_equal(r, FR.json['floating_ip']) |
653 | 03033b54 | Stavros Sachtouris | expected = dict(pool=pool) if pool else dict() |
654 | 03033b54 | Stavros Sachtouris | self.assertEqual(post.mock_calls[-1], call(tenant_id, expected)) |
655 | 03033b54 | Stavros Sachtouris | |
656 | 03033b54 | Stavros Sachtouris | @patch('%s.floating_ips_get' % compute_pkg, return_value=FR()) |
657 | 03033b54 | Stavros Sachtouris | def test_get_floating_ip(self, get): |
658 | 03033b54 | Stavros Sachtouris | FR.json = dict(floating_ips=[dict( |
659 | 03033b54 | Stavros Sachtouris | fixed_ip='fip',
|
660 | 03033b54 | Stavros Sachtouris | id=1,
|
661 | 03033b54 | Stavros Sachtouris | instance_id='lala',
|
662 | 03033b54 | Stavros Sachtouris | ip='102.0.0.1',
|
663 | 03033b54 | Stavros Sachtouris | pool='pisine'), ])
|
664 | 03033b54 | Stavros Sachtouris | for args in product( |
665 | 03033b54 | Stavros Sachtouris | ('t1', 't2'), |
666 | 03033b54 | Stavros Sachtouris | (None, 'fip')): |
667 | 03033b54 | Stavros Sachtouris | r = self.client.get_floating_ip(*args)
|
668 | 03033b54 | Stavros Sachtouris | tenant_id, fip = args |
669 | 03033b54 | Stavros Sachtouris | self.assertEqual(r, FR.json['floating_ips']) |
670 | 03033b54 | Stavros Sachtouris | self.assertEqual(get.mock_calls[-1], call(tenant_id, fip)) |
671 | 03033b54 | Stavros Sachtouris | |
672 | 03033b54 | Stavros Sachtouris | @patch('%s.floating_ips_delete' % compute_pkg, return_value=FR()) |
673 | 03033b54 | Stavros Sachtouris | def test_delete_floating_ip(self, delete): |
674 | 03033b54 | Stavros Sachtouris | for args in product( |
675 | 03033b54 | Stavros Sachtouris | ('t1', 't2'), |
676 | 03033b54 | Stavros Sachtouris | (None, 'fip')): |
677 | 03033b54 | Stavros Sachtouris | r = self.client.delete_floating_ip(*args)
|
678 | 03033b54 | Stavros Sachtouris | tenant_id, fip = args |
679 | 03033b54 | Stavros Sachtouris | self.assertEqual(r, FR.headers)
|
680 | 03033b54 | Stavros Sachtouris | self.assertEqual(delete.mock_calls[-1], call(tenant_id, fip)) |
681 | 03033b54 | Stavros Sachtouris | |
682 | 630dea9b | Stavros Sachtouris | |
683 | 304a92dc | Stavros Sachtouris | if __name__ == '__main__': |
684 | 304a92dc | Stavros Sachtouris | from sys import argv |
685 | 304a92dc | Stavros Sachtouris | from kamaki.clients.test import runTestCase |
686 | 630dea9b | Stavros Sachtouris | not_found = True
|
687 | 3c50df2e | Stavros Sachtouris | if not argv[1:] or argv[1] == 'ComputeClient': |
688 | 630dea9b | Stavros Sachtouris | not_found = False
|
689 | 3c50df2e | Stavros Sachtouris | runTestCase(ComputeClient, 'Compute Client', argv[2:]) |
690 | 55faa0bc | Stavros Sachtouris | if not argv[1:] or argv[1] == 'ComputeRest': |
691 | 630dea9b | Stavros Sachtouris | not_found = False
|
692 | 3c50df2e | Stavros Sachtouris | runTestCase(ComputeRestClient, 'ComputeRest Client', argv[2:]) |
693 | 630dea9b | Stavros Sachtouris | if not_found:
|
694 | 630dea9b | Stavros Sachtouris | print('TestCase %s not found' % argv[1]) |