blob: 4597d4ccb7125e26dbb11d312cc43045eebbd831 [file] [log] [blame]
Rakesh H Sa3325d62015-04-04 19:42:29 +05301# 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
14import copy
Rakesh H Sa3325d62015-04-04 19:42:29 +053015import json
16
Rabi Mishra477efc92015-07-31 13:01:45 +053017from heat_integrationtests.functional import functional_base
18
Rakesh H Sa3325d62015-04-04 19:42:29 +053019test_template_one_resource = {
20 'heat_template_version': '2013-05-23',
21 'description': 'Test template to create one instance.',
22 'resources': {
23 'test1': {
24 'type': 'OS::Heat::TestResource',
25 'properties': {
26 'value': 'Test1',
27 'fail': False,
28 'update_replace': False,
Angus Salkeld73dcbc62015-08-31 09:02:58 +100029 'wait_secs': 0,
30 'action_wait_secs': {'create': 1},
31 'client_name': 'nova',
32 'entity_name': 'servers',
Rakesh H Sa3325d62015-04-04 19:42:29 +053033 }
34 }
35 }
36}
37
38test_template_two_resource = {
39 'heat_template_version': '2013-05-23',
40 'description': 'Test template to create two instance.',
41 'resources': {
42 'test1': {
43 'type': 'OS::Heat::TestResource',
44 'properties': {
45 'value': 'Test1',
46 'fail': False,
47 'update_replace': False,
Angus Salkeld73dcbc62015-08-31 09:02:58 +100048 'wait_secs': 0,
49 'action_wait_secs': {'update': 1}
Rakesh H Sa3325d62015-04-04 19:42:29 +053050 }
51 },
52 'test2': {
53 'type': 'OS::Heat::TestResource',
54 'properties': {
55 'value': 'Test1',
56 'fail': False,
57 'update_replace': False,
58 'wait_secs': 0
59 }
60 }
61 }
62}
63
64
65def _change_rsrc_properties(template, rsrcs, values):
66 modified_template = copy.deepcopy(template)
67 for rsrc_name in rsrcs:
68 rsrc_prop = modified_template['resources'][
69 rsrc_name]['properties']
70 for prop in rsrc_prop:
71 if prop in values:
72 rsrc_prop[prop] = values[prop]
73 return modified_template
74
75
Rabi Mishra477efc92015-07-31 13:01:45 +053076class CreateStackTest(functional_base.FunctionalTestsBase):
Rakesh H Sa3325d62015-04-04 19:42:29 +053077 def setUp(self):
78 super(CreateStackTest, self).setUp()
Rakesh H Sa3325d62015-04-04 19:42:29 +053079
80 def test_create_rollback(self):
81 values = {'fail': True, 'value': 'test_create_rollback'}
82 template = _change_rsrc_properties(test_template_one_resource,
83 ['test1'], values)
84
85 self.stack_create(
86 template=template,
87 expected_status='ROLLBACK_COMPLETE',
88 disable_rollback=False)
89
90
Rabi Mishra477efc92015-07-31 13:01:45 +053091class UpdateStackTest(functional_base.FunctionalTestsBase):
Rakesh H Sa3325d62015-04-04 19:42:29 +053092
93 provider_template = {
94 'heat_template_version': '2013-05-23',
95 'description': 'foo',
96 'resources': {
97 'test1': {
98 'type': 'My::TestResource'
99 }
100 }
101 }
102
103 provider_group_template = '''
104heat_template_version: 2013-05-23
Steven Hardy23284b62015-10-01 19:03:42 +0100105parameters:
106 count:
107 type: number
108 default: 2
Rakesh H Sa3325d62015-04-04 19:42:29 +0530109resources:
110 test_group:
111 type: OS::Heat::ResourceGroup
112 properties:
Steven Hardy23284b62015-10-01 19:03:42 +0100113 count: {get_param: count}
Rakesh H Sa3325d62015-04-04 19:42:29 +0530114 resource_def:
115 type: My::TestResource
116'''
117
118 update_userdata_template = '''
119heat_template_version: 2014-10-16
120parameters:
121 flavor:
122 type: string
123 user_data:
124 type: string
125 image:
126 type: string
Rabi Mishraec4b03b2015-05-23 02:20:47 +0530127 network:
128 type: string
Rakesh H Sa3325d62015-04-04 19:42:29 +0530129
130resources:
131 server:
132 type: OS::Nova::Server
133 properties:
134 image: {get_param: image}
135 flavor: {get_param: flavor}
Rabi Mishraec4b03b2015-05-23 02:20:47 +0530136 networks: [{network: {get_param: network} }]
Rakesh H Sa3325d62015-04-04 19:42:29 +0530137 user_data_format: SOFTWARE_CONFIG
138 user_data: {get_param: user_data}
139'''
140
Steven Hardy23284b62015-10-01 19:03:42 +0100141 fail_param_template = '''
142heat_template_version: 2014-10-16
143parameters:
144 do_fail:
145 type: boolean
146 default: False
147resources:
148 aresource:
149 type: OS::Heat::TestResource
150 properties:
151 value: Test
152 fail: {get_param: do_fail}
153'''
154
Rakesh H Sa3325d62015-04-04 19:42:29 +0530155 def setUp(self):
156 super(UpdateStackTest, self).setUp()
Rakesh H Sa3325d62015-04-04 19:42:29 +0530157
158 def test_stack_update_nochange(self):
159 template = _change_rsrc_properties(test_template_one_resource,
160 ['test1'],
161 {'value': 'test_no_change'})
162 stack_identifier = self.stack_create(
163 template=template)
164 expected_resources = {'test1': 'OS::Heat::TestResource'}
165 self.assertEqual(expected_resources,
166 self.list_resources(stack_identifier))
167
168 # Update with no changes, resources should be unchanged
169 self.update_stack(stack_identifier, template)
170 self.assertEqual(expected_resources,
171 self.list_resources(stack_identifier))
172
173 def test_stack_in_place_update(self):
174 template = _change_rsrc_properties(test_template_one_resource,
175 ['test1'],
176 {'value': 'test_in_place'})
177 stack_identifier = self.stack_create(
178 template=template)
179 expected_resources = {'test1': 'OS::Heat::TestResource'}
180 self.assertEqual(expected_resources,
181 self.list_resources(stack_identifier))
182 resource = self.client.resources.list(stack_identifier)
183 initial_phy_id = resource[0].physical_resource_id
184
185 tmpl_update = _change_rsrc_properties(
186 test_template_one_resource, ['test1'],
187 {'value': 'test_in_place_update'})
188 # Update the Value
189 self.update_stack(stack_identifier, tmpl_update)
190 resource = self.client.resources.list(stack_identifier)
191 # By default update_in_place
192 self.assertEqual(initial_phy_id,
193 resource[0].physical_resource_id)
194
195 def test_stack_update_replace(self):
196 template = _change_rsrc_properties(test_template_one_resource,
197 ['test1'],
198 {'value': 'test_replace'})
199 stack_identifier = self.stack_create(
200 template=template)
201 expected_resources = {'test1': 'OS::Heat::TestResource'}
202 self.assertEqual(expected_resources,
203 self.list_resources(stack_identifier))
204 resource = self.client.resources.list(stack_identifier)
205 initial_phy_id = resource[0].physical_resource_id
206
207 # Update the value and also set update_replace prop
208 tmpl_update = _change_rsrc_properties(
209 test_template_one_resource, ['test1'],
210 {'value': 'test_in_place_update', 'update_replace': True})
211 self.update_stack(stack_identifier, tmpl_update)
212 resource = self.client.resources.list(stack_identifier)
213 # update Replace
214 self.assertNotEqual(initial_phy_id,
215 resource[0].physical_resource_id)
216
217 def test_stack_update_add_remove(self):
218 template = _change_rsrc_properties(test_template_one_resource,
219 ['test1'],
220 {'value': 'test_add_remove'})
221 stack_identifier = self.stack_create(
222 template=template)
223 initial_resources = {'test1': 'OS::Heat::TestResource'}
224 self.assertEqual(initial_resources,
225 self.list_resources(stack_identifier))
226
227 tmpl_update = _change_rsrc_properties(
228 test_template_two_resource, ['test1', 'test2'],
229 {'value': 'test_add_remove_update'})
230 # Add one resource via a stack update
231 self.update_stack(stack_identifier, tmpl_update)
232 updated_resources = {'test1': 'OS::Heat::TestResource',
233 'test2': 'OS::Heat::TestResource'}
234 self.assertEqual(updated_resources,
235 self.list_resources(stack_identifier))
236
237 # Then remove it by updating with the original template
238 self.update_stack(stack_identifier, template)
239 self.assertEqual(initial_resources,
240 self.list_resources(stack_identifier))
241
242 def test_stack_update_rollback(self):
243 template = _change_rsrc_properties(test_template_one_resource,
244 ['test1'],
245 {'value': 'test_update_rollback'})
246 stack_identifier = self.stack_create(
247 template=template)
248 initial_resources = {'test1': 'OS::Heat::TestResource'}
249 self.assertEqual(initial_resources,
250 self.list_resources(stack_identifier))
251
252 tmpl_update = _change_rsrc_properties(
253 test_template_two_resource, ['test1', 'test2'],
254 {'value': 'test_update_rollback', 'fail': True})
255 # stack update, also set failure
256 self.update_stack(stack_identifier, tmpl_update,
257 expected_status='ROLLBACK_COMPLETE',
258 disable_rollback=False)
259 # since stack update failed only the original resource is present
260 updated_resources = {'test1': 'OS::Heat::TestResource'}
261 self.assertEqual(updated_resources,
262 self.list_resources(stack_identifier))
263
Sergey Kraynev89082a32015-09-04 04:42:33 -0400264 def test_stack_update_from_failed(self):
265 # Prove it's possible to update from an UPDATE_FAILED state
266 template = _change_rsrc_properties(test_template_one_resource,
267 ['test1'],
268 {'value': 'test_update_failed'})
269 stack_identifier = self.stack_create(
270 template=template)
271 initial_resources = {'test1': 'OS::Heat::TestResource'}
272 self.assertEqual(initial_resources,
273 self.list_resources(stack_identifier))
274
275 tmpl_update = _change_rsrc_properties(
276 test_template_one_resource, ['test1'], {'fail': True})
277 # Update with bad template, we should fail
278 self.update_stack(stack_identifier, tmpl_update,
279 expected_status='UPDATE_FAILED')
280 # but then passing a good template should succeed
281 self.update_stack(stack_identifier, test_template_two_resource)
282 updated_resources = {'test1': 'OS::Heat::TestResource',
283 'test2': 'OS::Heat::TestResource'}
284 self.assertEqual(updated_resources,
285 self.list_resources(stack_identifier))
286
Rakesh H Sa3325d62015-04-04 19:42:29 +0530287 def test_stack_update_provider(self):
288 template = _change_rsrc_properties(
289 test_template_one_resource, ['test1'],
290 {'value': 'test_provider_template'})
291 files = {'provider.template': json.dumps(template)}
292 env = {'resource_registry':
293 {'My::TestResource': 'provider.template'}}
294 stack_identifier = self.stack_create(
295 template=self.provider_template,
296 files=files,
297 environment=env
298 )
299
300 initial_resources = {'test1': 'My::TestResource'}
301 self.assertEqual(initial_resources,
302 self.list_resources(stack_identifier))
303
304 # Prove the resource is backed by a nested stack, save the ID
305 nested_identifier = self.assert_resource_is_a_stack(stack_identifier,
306 'test1')
307 nested_id = nested_identifier.split('/')[-1]
308
309 # Then check the expected resources are in the nested stack
310 nested_resources = {'test1': 'OS::Heat::TestResource'}
311 self.assertEqual(nested_resources,
312 self.list_resources(nested_identifier))
313 tmpl_update = _change_rsrc_properties(
314 test_template_two_resource, ['test1', 'test2'],
315 {'value': 'test_provider_template'})
316 # Add one resource via a stack update by changing the nested stack
317 files['provider.template'] = json.dumps(tmpl_update)
318 self.update_stack(stack_identifier, self.provider_template,
319 environment=env, files=files)
320
321 # Parent resources should be unchanged and the nested stack
322 # should have been updated in-place without replacement
323 self.assertEqual(initial_resources,
324 self.list_resources(stack_identifier))
325 rsrc = self.client.resources.get(stack_identifier, 'test1')
326 self.assertEqual(rsrc.physical_resource_id, nested_id)
327
328 # Then check the expected resources are in the nested stack
329 nested_resources = {'test1': 'OS::Heat::TestResource',
330 'test2': 'OS::Heat::TestResource'}
331 self.assertEqual(nested_resources,
332 self.list_resources(nested_identifier))
333
334 def test_stack_update_provider_group(self):
335 '''Test two-level nested update.'''
336 # Create a ResourceGroup (which creates a nested stack),
337 # containing provider resources (which create a nested
Steven Hardy23284b62015-10-01 19:03:42 +0100338 # stack), thus exercising an update which traverses
Rakesh H Sa3325d62015-04-04 19:42:29 +0530339 # two levels of nesting.
340 template = _change_rsrc_properties(
341 test_template_one_resource, ['test1'],
342 {'value': 'test_provider_group_template'})
343 files = {'provider.template': json.dumps(template)}
344 env = {'resource_registry':
345 {'My::TestResource': 'provider.template'}}
346
347 stack_identifier = self.stack_create(
348 template=self.provider_group_template,
349 files=files,
350 environment=env
351 )
352
353 initial_resources = {'test_group': 'OS::Heat::ResourceGroup'}
354 self.assertEqual(initial_resources,
355 self.list_resources(stack_identifier))
356
357 # Prove the resource is backed by a nested stack, save the ID
358 nested_identifier = self.assert_resource_is_a_stack(stack_identifier,
359 'test_group')
360
361 # Then check the expected resources are in the nested stack
362 nested_resources = {'0': 'My::TestResource',
363 '1': 'My::TestResource'}
364 self.assertEqual(nested_resources,
365 self.list_resources(nested_identifier))
366
367 for n_rsrc in nested_resources:
368 rsrc = self.client.resources.get(nested_identifier, n_rsrc)
369 provider_stack = self.client.stacks.get(rsrc.physical_resource_id)
370 provider_identifier = '%s/%s' % (provider_stack.stack_name,
371 provider_stack.id)
372 provider_resources = {u'test1': u'OS::Heat::TestResource'}
373 self.assertEqual(provider_resources,
374 self.list_resources(provider_identifier))
375
376 tmpl_update = _change_rsrc_properties(
377 test_template_two_resource, ['test1', 'test2'],
378 {'value': 'test_provider_group_template'})
379 # Add one resource via a stack update by changing the nested stack
380 files['provider.template'] = json.dumps(tmpl_update)
381 self.update_stack(stack_identifier, self.provider_group_template,
382 environment=env, files=files)
383
384 # Parent resources should be unchanged and the nested stack
385 # should have been updated in-place without replacement
386 self.assertEqual(initial_resources,
387 self.list_resources(stack_identifier))
388
389 # Resource group stack should also be unchanged (but updated)
390 nested_stack = self.client.stacks.get(nested_identifier)
391 self.assertEqual('UPDATE_COMPLETE', nested_stack.stack_status)
392 self.assertEqual(nested_resources,
393 self.list_resources(nested_identifier))
394
395 for n_rsrc in nested_resources:
396 rsrc = self.client.resources.get(nested_identifier, n_rsrc)
397 provider_stack = self.client.stacks.get(rsrc.physical_resource_id)
398 provider_identifier = '%s/%s' % (provider_stack.stack_name,
399 provider_stack.id)
400 provider_resources = {'test1': 'OS::Heat::TestResource',
401 'test2': 'OS::Heat::TestResource'}
402 self.assertEqual(provider_resources,
403 self.list_resources(provider_identifier))
404
405 def test_stack_update_with_replacing_userdata(self):
406 """Confirm that we can update userdata of instance during updating
407 stack by the user of member role.
408
409 Make sure that a resource that inherites from StackUser can be deleted
410 during updating stack.
411 """
412 if not self.conf.minimal_image_ref:
413 raise self.skipException("No minimal image configured to test")
414 if not self.conf.minimal_instance_type:
415 raise self.skipException("No flavor configured to test")
416
417 parms = {'flavor': self.conf.minimal_instance_type,
418 'image': self.conf.minimal_image_ref,
Rabi Mishraec4b03b2015-05-23 02:20:47 +0530419 'network': self.conf.fixed_network_name,
Rakesh H Sa3325d62015-04-04 19:42:29 +0530420 'user_data': ''}
421 name = self._stack_rand_name()
422
423 stack_identifier = self.stack_create(
424 stack_name=name,
425 template=self.update_userdata_template,
426 parameters=parms
427 )
428
429 parms_updated = parms
430 parms_updated['user_data'] = 'two'
431 self.update_stack(
432 stack_identifier,
433 template=self.update_userdata_template,
434 parameters=parms_updated)
Steven Hardy23284b62015-10-01 19:03:42 +0100435
436 def test_stack_update_provider_group_patch(self):
437 '''Test two-level nested update with PATCH'''
438 template = _change_rsrc_properties(
439 test_template_one_resource, ['test1'],
440 {'value': 'test_provider_group_template'})
441 files = {'provider.template': json.dumps(template)}
442 env = {'resource_registry':
443 {'My::TestResource': 'provider.template'}}
444
445 stack_identifier = self.stack_create(
446 template=self.provider_group_template,
447 files=files,
448 environment=env
449 )
450
451 initial_resources = {'test_group': 'OS::Heat::ResourceGroup'}
452 self.assertEqual(initial_resources,
453 self.list_resources(stack_identifier))
454
455 # Prove the resource is backed by a nested stack, save the ID
456 nested_identifier = self.assert_resource_is_a_stack(stack_identifier,
457 'test_group')
458
459 # Then check the expected resources are in the nested stack
460 nested_resources = {'0': 'My::TestResource',
461 '1': 'My::TestResource'}
462 self.assertEqual(nested_resources,
463 self.list_resources(nested_identifier))
464
465 # increase the count, pass only the paramter, no env or template
466 params = {'count': 3}
467 self.update_stack(stack_identifier, parameters=params, existing=True)
468
469 # Parent resources should be unchanged and the nested stack
470 # should have been updated in-place without replacement
471 self.assertEqual(initial_resources,
472 self.list_resources(stack_identifier))
473
474 # Resource group stack should also be unchanged (but updated)
475 nested_stack = self.client.stacks.get(nested_identifier)
476 self.assertEqual('UPDATE_COMPLETE', nested_stack.stack_status)
477 # Add a resource, as we should have added one
478 nested_resources['2'] = 'My::TestResource'
479 self.assertEqual(nested_resources,
480 self.list_resources(nested_identifier))
481
482 def test_stack_update_from_failed_patch(self):
483 '''Test PATCH update from a failed state.'''
484
485 # Start with empty template
486 stack_identifier = self.stack_create(
487 template='heat_template_version: 2014-10-16')
488
489 # Update with a good template, but bad parameter
490 self.update_stack(stack_identifier,
491 template=self.fail_param_template,
492 parameters={'do_fail': True},
493 expected_status='UPDATE_FAILED')
494
495 # PATCH update, only providing the parameter
496 self.update_stack(stack_identifier,
497 parameters={'do_fail': False},
498 existing=True)
499 self.assertEqual({u'aresource': u'OS::Heat::TestResource'},
500 self.list_resources(stack_identifier))