Debian Codegraph @

heatclient/tests/unit/test_stacks.py

raw symbols SCIP
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 from unittest import mock
14
15 import testscenarios
16 from testscenarios import scenarios as scnrs
17 import testtools
18
19 from heatclient.v1 import stacks
20
21 load_tests = testscenarios.load_tests_apply_scenarios
22
23
24 def mock_stack(manager, stack_name, stack_id):
25 return stacks.Stack(manager, {
26 "id": stack_id,
27 "stack_name": stack_name,
28 "links": [{
29 "href": "http://192.0.2.1:8004/v1/1234/stacks/{}/{}".format(
30 stack_name, stack_id),
31 "rel": "self"}],
32 "description": "No description",
33 "stack_status_reason": "Stack create completed successfully",
34 "creation_time": "2013-08-04T20:57:55Z",
35 "updated_time": "2013-08-04T20:57:55Z",
36 "stack_status": "CREATE_COMPLETE"
37 })
38
39
40 class StackStatusActionTest(testtools.TestCase):
41
42 scenarios = scnrs.multiply_scenarios([
43 ('CREATE', dict(action='CREATE')),
44 ('DELETE', dict(action='DELETE')),
45 ('UPDATE', dict(action='UPDATE')),
46 ('ROLLBACK', dict(action='ROLLBACK')),
47 ('SUSPEND', dict(action='SUSPEND')),
48 ('RESUME', dict(action='RESUME')),
49 ('CHECK', dict(action='CHECK'))
50 ], [
51 ('IN_PROGRESS', dict(status='IN_PROGRESS')),
52 ('FAILED', dict(status='FAILED')),
53 ('COMPLETE', dict(status='COMPLETE'))
54 ])
55
56 def test_status_action(self):
57 stack_status = f'{self.action}_{self.status}'
58 stack = mock_stack(None, 'stack_1', 'abcd1234')
59 stack.stack_status = stack_status
60 self.assertEqual(self.action, stack.action)
61 self.assertEqual(self.status, stack.status)
62
63
64 class StackIdentifierTest(testtools.TestCase):
65
66 def test_stack_identifier(self):
67 stack = mock_stack(None, 'the_stack', 'abcd1234')
68 self.assertEqual('the_stack/abcd1234', stack.identifier)
69
70
71 class StackOperationsTest(testtools.TestCase):
72
73 def test_delete_stack(self):
74 manager = mock.MagicMock()
75 stack = mock_stack(manager, 'the_stack', 'abcd1234')
76 stack.delete()
77 manager.delete.assert_called_once_with('the_stack/abcd1234')
78
79 def test_abandon_stack(self):
80 manager = mock.MagicMock()
81 stack = mock_stack(manager, 'the_stack', 'abcd1234')
82 stack.abandon()
83 manager.abandon.assert_called_once_with('the_stack/abcd1234')
84
85 def test_get_stack(self):
86 manager = mock.MagicMock()
87 stack = mock_stack(manager, 'the_stack', 'abcd1234')
88 stack.get()
89 manager.get.assert_called_once_with('the_stack/abcd1234')
90
91 def test_update_stack(self):
92 manager = mock.MagicMock()
93 stack = mock_stack(manager, 'the_stack', 'abcd1234')
94 stack.update()
95 manager.update.assert_called_once_with('the_stack/abcd1234')
96
97 def test_create_stack(self):
98 manager = mock.MagicMock()
99 stack = mock_stack(manager, 'the_stack', 'abcd1234')
100 stack = stack.create()
101 manager.create.assert_called_once_with('the_stack/abcd1234')
102
103 def test_preview_stack(self):
104 manager = mock.MagicMock()
105 stack = mock_stack(manager, 'the_stack', 'abcd1234')
106 stack = stack.preview()
107 manager.preview.assert_called_once_with()
108
109 def test_snapshot(self):
110 manager = mock.MagicMock()
111 stack = mock_stack(manager, 'the_stack', 'abcd1234')
112 stack.snapshot('foo')
113 manager.snapshot.assert_called_once_with('the_stack/abcd1234', 'foo')
114
115 def test_snapshot_show(self):
116 manager = mock.MagicMock()
117 stack = mock_stack(manager, 'the_stack', 'abcd1234')
118 stack.snapshot_show('snap1234')
119 manager.snapshot_show.assert_called_once_with(
120 'the_stack/abcd1234', 'snap1234')
121
122 def test_snapshot_delete(self):
123 manager = mock.MagicMock()
124 stack = mock_stack(manager, 'the_stack', 'abcd1234')
125 stack.snapshot_delete('snap1234')
126 manager.snapshot_delete.assert_called_once_with(
127 'the_stack/abcd1234', 'snap1234')
128
129 def test_restore(self):
130 manager = mock.MagicMock()
131 stack = mock_stack(manager, 'the_stack', 'abcd1234')
132 stack.restore('snap1234')
133 manager.restore.assert_called_once_with(
134 'the_stack/abcd1234', 'snap1234')
135
136 def test_snapshot_list(self):
137 manager = mock.MagicMock()
138 stack = mock_stack(manager, 'the_stack', 'abcd1234')
139 stack.snapshot_list()
140 manager.snapshot_list.assert_called_once_with('the_stack/abcd1234')
141
142 def test_output_list(self):
143 manager = mock.MagicMock()
144 stack = mock_stack(manager, 'the_stack', 'abcd1234')
145 stack.output_list()
146 manager.output_list.assert_called_once_with('the_stack/abcd1234')
147
148 def test_output_show(self):
149 manager = mock.MagicMock()
150 stack = mock_stack(manager, 'the_stack', 'abcd1234')
151 stack.output_show('out123')
152 manager.output_show.assert_called_once_with('the_stack/abcd1234',
153 'out123')
154
155 def test_environment_show(self):
156 manager = mock.MagicMock()
157 stack = mock_stack(manager, 'env_stack', 'env1')
158 stack.environment()
159 manager.environment.assert_called_once_with('env_stack/env1')
160
161 def test_files_show(self):
162 manager = mock.MagicMock()
163 stack = mock_stack(manager, 'files_stack', 'files1')
164 stack.files()
165 manager.files.assert_called_once_with('files_stack/files1')
166
167
168 class StackManagerNoPaginationTest(testtools.TestCase):
169
170 scenarios = [
171 ('total_0', dict(total=0)),
172 ('total_1', dict(total=1)),
173 ('total_9', dict(total=9)),
174 ('total_10', dict(total=10)),
175 ('total_11', dict(total=11)),
176 ('total_19', dict(total=19)),
177 ('total_20', dict(total=20)),
178 ('total_21', dict(total=21)),
179 ('total_49', dict(total=49)),
180 ('total_50', dict(total=50)),
181 ('total_51', dict(total=51)),
182 ('total_95', dict(total=95)),
183 ]
184
185 # absolute limit for results returned
186 limit = 50
187
188 def mock_manager(self):
189 manager = stacks.StackManager(None)
190 manager._list = mock.MagicMock()
191
192 def mock_list(*args, **kwargs):
193 def results():
194 for i in range(0, self.total):
195 stack_name = 'stack_%s' % (i + 1)
196 stack_id = 'abcd1234-%s' % (i + 1)
197 yield mock_stack(manager, stack_name, stack_id)
198
199 return list(results())
200
201 manager._list.side_effect = mock_list
202 return manager
203
204 def test_stack_list_no_pagination(self):
205 manager = self.mock_manager()
206
207 results = list(manager.list())
208 manager._list.assert_called_once_with(
209 '/stacks?', 'stacks')
210
211 # paginate is not specified, so the total
212 # results is always returned
213 self.assertEqual(self.total, len(results))
214
215 if self.total > 0:
216 self.assertEqual('stack_1', results[0].stack_name)
217 self.assertEqual('stack_%s' % self.total, results[-1].stack_name)
218
219
220 class StackManagerPaginationTest(testtools.TestCase):
221
222 scenarios = [
223 ('0_offset_0', dict(
224 offset=0,
225 total=0,
226 results=((0, 0),)
227 )),
228 ('1_offset_0', dict(
229 offset=0,
230 total=1,
231 results=((0, 1),)
232 )),
233 ('9_offset_0', dict(
234 offset=0,
235 total=9,
236 results=((0, 9),)
237 )),
238 ('10_offset_0', dict(
239 offset=0,
240 total=10,
241 results=((0, 10), (10, 10))
242 )),
243 ('11_offset_0', dict(
244 offset=0,
245 total=11,
246 results=((0, 10), (10, 11))
247 )),
248 ('11_offset_10', dict(
249 offset=10,
250 total=11,
251 results=((10, 11),)
252 )),
253 ('19_offset_10', dict(
254 offset=10,
255 total=19,
256 results=((10, 19),)
257 )),
258 ('20_offset_10', dict(
259 offset=10,
260 total=20,
261 results=((10, 20), (20, 20))
262 )),
263 ('21_offset_10', dict(
264 offset=10,
265 total=21,
266 results=((10, 20), (20, 21))
267 )),
268 ('21_offset_0', dict(
269 offset=0,
270 total=21,
271 results=((0, 10), (10, 20), (20, 21))
272 )),
273 ('21_offset_20', dict(
274 offset=20,
275 total=21,
276 results=((20, 21),)
277 )),
278 ('95_offset_90', dict(
279 offset=90,
280 total=95,
281 results=((90, 95),)
282 )),
283 ]
284
285 # absolute limit for results returned
286 limit = 50
287
288 def mock_manager(self):
289 manager = stacks.StackManager(None)
290 manager._list = mock.MagicMock()
291
292 def mock_list(arg_url, arg_response_key):
293 try:
294 result = self.results[self.result_index]
295 except IndexError:
296 return []
297 self.result_index = self.result_index + 1
298
299 limit_string = 'limit=%s' % self.limit
300 self.assertIn(limit_string, arg_url)
301
302 offset = result[0]
303 if offset > 0:
304 offset_string = 'marker=abcd1234-%s' % offset
305 self.assertIn(offset_string, arg_url)
306
307 def results():
308
309 for i in range(*result):
310 self.limit -= 1
311 stack_name = 'stack_%s' % (i + 1)
312 stack_id = 'abcd1234-%s' % (i + 1)
313 yield mock_stack(manager, stack_name, stack_id)
314
315 return list(results())
316
317 manager._list.side_effect = mock_list
318 return manager
319
320 def test_stack_list_pagination(self):
321 manager = self.mock_manager()
322
323 list_params = {'limit': self.limit}
324
325 if self.offset > 0:
326 marker = 'abcd1234-%s' % self.offset
327 list_params['marker'] = marker
328
329 self.result_index = 0
330 results = list(manager.list(**list_params))
331
332 # assert that the list method has been called enough times
333 self.assertEqual(len(self.results), self.result_index)
334
335 last_result = min(self.limit, self.total - self.offset)
336 # one or more list calls have been recomposed into a single list
337 self.assertEqual(last_result, len(results))
338
339 if last_result > 0:
340 self.assertEqual('stack_%s' % (self.offset + 1),
341 results[0].stack_name)
342 self.assertEqual('stack_%s' % (self.offset + last_result),
343 results[-1].stack_name)
344
345
346 class StackManagerValidateTest(testtools.TestCase):
347
348 def setUp(self):
349 super().setUp()
350
351 self.mock_response = mock.MagicMock()
352 self.mock_response.json.return_value = {'result': 'fake_response'}
353 self.mock_response.headers = {'content-type': 'application/json'}
354
355 self.mock_client = mock.MagicMock()
356 self.mock_client.post.return_value = self.mock_response
357
358 self.manager = stacks.StackManager(self.mock_client)
359
360 def test_validate_show_nested(self):
361 # Test
362 result = self.manager.validate(**{'show_nested': True})
363
364 # Verify
365 self.assertEqual(self.mock_response.json.return_value, result)
366 self.mock_client.post.assert_called_once_with(
367 '/validate', params={'show_nested': True})
368
369 def test_validate_show_nested_false(self):
370 # Test
371 result = self.manager.validate(**{'show_nested': False})
372
373 # Verify
374 self.assertEqual(self.mock_response.json.return_value, result)
375 self.mock_client.post.assert_called_once_with(
376 '/validate')
377
378 def test_validate_show_nested_default(self):
379 # Test
380 result = self.manager.validate()
381
382 # Verify
383 self.assertEqual(self.mock_response.json.return_value, result)
384 self.mock_client.post.assert_called_once_with(
385 '/validate')
386
387 def test_validate_ignore_errors(self):
388 self.manager.validate(ignore_errors='99001,99002')
389 self.mock_client.post.assert_called_once_with(
390 '/validate', params={'ignore_errors': '99001,99002'})