blob: 95b43fe23c62abf436d1875ce709f19605fc3cec [file] [log] [blame]
huangtianhua99a25de2016-07-26 10:58:33 +08001# 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
13from heat_integrationtests.functional import functional_base
14
15
16cfn_template = '''
17AWSTemplateFormatVersion: 2010-09-09
18Parameters:
19 env_type:
20 Default: test
21 Type: String
22 AllowedValues: [prod, test]
huangtianhua7bdc7512016-06-17 09:25:38 +080023 zone:
24 Type: String
25 Default: beijing
huangtianhua99a25de2016-07-26 10:58:33 +080026Conditions:
27 Prod: {"Fn::Equals" : [{Ref: env_type}, "prod"]}
huangtianhuabf63fcb2016-07-27 13:02:39 +080028 Test:
29 Fn::Not:
30 - Fn::Equals:
31 - Ref: env_type
32 - prod
huangtianhua7bdc7512016-06-17 09:25:38 +080033 Beijing_Prod:
34 Fn::And:
35 - Fn::Equals:
36 - Ref: env_type
37 - prod
38 - Fn::Equals:
39 - Ref: zone
40 - beijing
huangtianhua26da8982016-07-27 18:11:58 +080041 Fujian_Zone:
42 Fn::Or:
43 - Fn::Equals:
44 - Ref: zone
45 - fuzhou
46 - Fn::Equals:
47 - Ref: zone
48 - xiamen
huangtianhua99a25de2016-07-26 10:58:33 +080049Resources:
50 test_res:
51 Type: OS::Heat::TestResource
52 Properties:
huangtianhuafb8b51f2016-02-23 17:03:10 +080053 value: {"Fn::If": ["Prod", "env_is_prod", "env_is_test"]}
huangtianhua99a25de2016-07-26 10:58:33 +080054 prod_res:
55 Type: OS::Heat::TestResource
56 Properties:
57 value: prod_res
58 Condition: Prod
huangtianhuabf63fcb2016-07-27 13:02:39 +080059 test_res1:
60 Type: OS::Heat::TestResource
61 Properties:
62 value: just in test env
63 Condition: Test
huangtianhua7bdc7512016-06-17 09:25:38 +080064 beijing_prod_res:
65 Type: OS::Heat::TestResource
66 Properties:
67 value: beijing_prod_res
68 Condition: Beijing_Prod
huangtianhua26da8982016-07-27 18:11:58 +080069 fujian_res:
70 Type: OS::Heat::TestResource
71 Condition: Fujian_Zone
72 Properties:
73 value: fujian_res
huangtianhua27075a92016-07-26 14:32:40 +080074Outputs:
75 res_value:
76 Value: {"Fn::GetAtt": [prod_res, output]}
77 Condition: Prod
huangtianhuafb8b51f2016-02-23 17:03:10 +080078 test_res_value:
79 Value: {"Fn::GetAtt": [test_res, output]}
80 prod_resource:
81 Value: {"Fn::If": [Prod, {Ref: prod_res}, 'no_prod_res']}
huangtianhuabf63fcb2016-07-27 13:02:39 +080082 test_res1_value:
83 Value: {"Fn::If": [Test, {"Fn::GetAtt": [test_res1, output]},
84 'no_test_res1']}
huangtianhua7bdc7512016-06-17 09:25:38 +080085 beijing_prod_res:
86 Value: {"Fn::If": [Beijing_Prod, {Ref: beijing_prod_res}, 'no_prod_res']}
huangtianhua99a25de2016-07-26 10:58:33 +080087'''
88
89hot_template = '''
90heat_template_version: 2016-10-14
91parameters:
92 env_type:
93 default: test
94 type: string
95 constraints:
96 - allowed_values: [prod, test]
huangtianhua7bdc7512016-06-17 09:25:38 +080097 zone:
98 type: string
99 default: beijing
huangtianhua99a25de2016-07-26 10:58:33 +0800100conditions:
101 prod: {equals : [{get_param: env_type}, "prod"]}
huangtianhuabf63fcb2016-07-27 13:02:39 +0800102 test:
103 not:
104 equals:
105 - get_param: env_type
106 - prod
huangtianhua7bdc7512016-06-17 09:25:38 +0800107 beijing_prod:
108 and:
109 - equals:
110 - get_param: zone
111 - beijing
112 - equals:
113 - get_param: env_type
114 - prod
huangtianhua26da8982016-07-27 18:11:58 +0800115 fujian_zone:
116 or:
117 - equals:
118 - get_param: zone
119 - fuzhou
120 - equals:
121 - get_param: zone
122 - xiamen
huangtianhua99a25de2016-07-26 10:58:33 +0800123resources:
124 test_res:
125 type: OS::Heat::TestResource
126 properties:
huangtianhuafb8b51f2016-02-23 17:03:10 +0800127 value: {if: ["prod", "env_is_prod", "env_is_test"]}
huangtianhua99a25de2016-07-26 10:58:33 +0800128 prod_res:
129 type: OS::Heat::TestResource
130 properties:
131 value: prod_res
132 condition: prod
huangtianhuabf63fcb2016-07-27 13:02:39 +0800133 test_res1:
134 type: OS::Heat::TestResource
135 properties:
136 value: just in test env
137 condition: test
huangtianhua7bdc7512016-06-17 09:25:38 +0800138 beijing_prod_res:
139 type: OS::Heat::TestResource
140 properties:
141 value: beijing_prod_res
142 condition: beijing_prod
huangtianhua26da8982016-07-27 18:11:58 +0800143 fujian_res:
144 type: OS::Heat::TestResource
145 condition: fujian_zone
146 properties:
147 value: fujian_res
huangtianhua27075a92016-07-26 14:32:40 +0800148outputs:
149 res_value:
150 value: {get_attr: [prod_res, output]}
151 condition: prod
huangtianhuafb8b51f2016-02-23 17:03:10 +0800152 test_res_value:
153 value: {get_attr: [test_res, output]}
154 prod_resource:
155 value: {if: [prod, {get_resource: prod_res}, 'no_prod_res']}
huangtianhuabf63fcb2016-07-27 13:02:39 +0800156 test_res1_value:
157 value: {if: [test, {get_attr: [test_res1, output]}, 'no_test_res1']}
huangtianhua7bdc7512016-06-17 09:25:38 +0800158 beijing_prod_res:
159 value: {if: [beijing_prod, {get_resource: beijing_prod_res},
160 'no_prod_res']}
huangtianhua99a25de2016-07-26 10:58:33 +0800161'''
162
Zane Bittera9b5aaf2016-09-12 20:51:52 -0400163before_rename_tmpl = '''
164heat_template_version: 2016-10-14
165parameters:
166 env_type:
167 default: test
168 type: string
169conditions:
170 cd1: {equals : [{get_param: env_type}, "prod"]}
171resources:
172 test:
173 type: OS::Heat::TestResource
174 properties:
175 value: {if: [cd1, 'prod', 'test']}
176'''
177
178after_rename_tmpl = '''
179heat_template_version: 2016-10-14
180parameters:
181 env_type:
182 default: prod
183 type: string
184conditions:
185 cd2: {equals : [{get_param: env_type}, "prod"]}
186resources:
187 test:
188 type: OS::Heat::TestResource
189 properties:
190 value: {if: [cd2, 'prod', 'test']}
191 test2:
192 type: OS::Heat::TestResource
193 properties:
194 value: {if: [cd2, 'prod', 'test']}
195'''
196
197fail_rename_tmpl = '''
198heat_template_version: 2016-10-14
199parameters:
200 env_type:
201 default: prod
202 type: string
203conditions:
204 cd3: {equals : [{get_param: env_type}, "prod"]}
205resources:
206 test:
207 type: OS::Heat::TestResource
208 properties:
209 value: {if: [cd3, 'prod', 'test']}
210 test2:
211 type: OS::Heat::TestResource
212 properties:
213 value: {if: [cd3, 'prod', 'test']}
214 test_fail:
215 type: OS::Heat::TestResource
216 properties:
217 fail: True
218 depends_on: [test, test2]
219'''
220
221recover_rename_tmpl = '''
222heat_template_version: 2016-10-14
223parameters:
224 env_type:
225 default: prod
226 type: string
227conditions:
228 cd3: {equals : [{get_param: env_type}, "prod"]}
229resources:
230 test2:
231 type: OS::Heat::TestResource
232 properties:
233 value: {if: [cd3, 'prod', 'test']}
234 test_fail:
235 type: OS::Heat::TestResource
236 properties:
237 fail: False
238 depends_on: [test2]
239'''
240
huangtianhua99a25de2016-07-26 10:58:33 +0800241
242class CreateUpdateResConditionTest(functional_base.FunctionalTestsBase):
243
244 def setUp(self):
245 super(CreateUpdateResConditionTest, self).setUp()
246
Zane Bitteree582d92016-09-08 11:33:46 -0400247 def res_assert_for_prod(self, resources, bj_prod=True, fj_zone=False):
huangtianhua99a25de2016-07-26 10:58:33 +0800248 res_names = [res.resource_name for res in resources]
huangtianhua7bdc7512016-06-17 09:25:38 +0800249 if bj_prod:
Zane Bitteree582d92016-09-08 11:33:46 -0400250 self.assertEqual(3, len(resources))
huangtianhua7bdc7512016-06-17 09:25:38 +0800251 self.assertIn('beijing_prod_res', res_names)
huangtianhua26da8982016-07-27 18:11:58 +0800252 elif fj_zone:
Zane Bitteree582d92016-09-08 11:33:46 -0400253 self.assertEqual(3, len(resources))
huangtianhua26da8982016-07-27 18:11:58 +0800254 self.assertIn('fujian_res', res_names)
255 self.assertNotIn('beijing_prod_res', res_names)
huangtianhua7bdc7512016-06-17 09:25:38 +0800256 else:
Zane Bitteree582d92016-09-08 11:33:46 -0400257 self.assertEqual(2, len(resources))
huangtianhua99a25de2016-07-26 10:58:33 +0800258 self.assertIn('prod_res', res_names)
259 self.assertIn('test_res', res_names)
260
Zane Bitteree582d92016-09-08 11:33:46 -0400261 def res_assert_for_test(self, resources, fj_zone=False):
huangtianhua99a25de2016-07-26 10:58:33 +0800262 res_names = [res.resource_name for res in resources]
huangtianhua26da8982016-07-27 18:11:58 +0800263
264 if fj_zone:
Zane Bitteree582d92016-09-08 11:33:46 -0400265 self.assertEqual(3, len(resources))
huangtianhua26da8982016-07-27 18:11:58 +0800266 self.assertIn('fujian_res', res_names)
huangtianhuae4365742016-08-30 10:27:21 +0800267 else:
Zane Bitteree582d92016-09-08 11:33:46 -0400268 self.assertEqual(2, len(resources))
269 self.assertNotIn('fujian_res', res_names)
270
huangtianhua99a25de2016-07-26 10:58:33 +0800271 self.assertIn('test_res', res_names)
huangtianhuabf63fcb2016-07-27 13:02:39 +0800272 self.assertIn('test_res1', res_names)
huangtianhua99a25de2016-07-26 10:58:33 +0800273 self.assertNotIn('prod_res', res_names)
274
huangtianhua7bdc7512016-06-17 09:25:38 +0800275 def output_assert_for_prod(self, stack_id, bj_prod=True):
huangtianhua27075a92016-07-26 14:32:40 +0800276 output = self.client.stacks.output_show(stack_id,
277 'res_value')['output']
278 self.assertEqual('prod_res', output['output_value'])
279
huangtianhuafb8b51f2016-02-23 17:03:10 +0800280 test_res_value = self.client.stacks.output_show(
281 stack_id, 'test_res_value')['output']
282 self.assertEqual('env_is_prod', test_res_value['output_value'])
283
284 prod_resource = self.client.stacks.output_show(
285 stack_id, 'prod_resource')['output']
286 self.assertNotEqual('no_prod_res', prod_resource['output_value'])
287
huangtianhuabf63fcb2016-07-27 13:02:39 +0800288 test_res_output = self.client.stacks.output_show(
289 stack_id, 'test_res1_value')['output']
290 self.assertEqual('no_test_res1', test_res_output['output_value'])
291
huangtianhua7bdc7512016-06-17 09:25:38 +0800292 beijing_prod_res = self.client.stacks.output_show(
293 stack_id, 'beijing_prod_res')['output']
294 if bj_prod:
295 self.assertNotEqual('no_prod_res',
296 beijing_prod_res['output_value'])
297 else:
298 self.assertEqual('no_prod_res', beijing_prod_res['output_value'])
299
huangtianhua27075a92016-07-26 14:32:40 +0800300 def output_assert_for_test(self, stack_id):
301 output = self.client.stacks.output_show(stack_id,
302 'res_value')['output']
303 self.assertIsNone(output['output_value'])
304
huangtianhuafb8b51f2016-02-23 17:03:10 +0800305 test_res_value = self.client.stacks.output_show(
306 stack_id, 'test_res_value')['output']
307 self.assertEqual('env_is_test', test_res_value['output_value'])
308
309 prod_resource = self.client.stacks.output_show(
310 stack_id, 'prod_resource')['output']
311 self.assertEqual('no_prod_res', prod_resource['output_value'])
312
huangtianhuabf63fcb2016-07-27 13:02:39 +0800313 test_res_output = self.client.stacks.output_show(
314 stack_id, 'test_res1_value')['output']
315 self.assertEqual('just in test env',
316 test_res_output['output_value'])
317
huangtianhua7bdc7512016-06-17 09:25:38 +0800318 beijing_prod_res = self.client.stacks.output_show(
319 stack_id, 'beijing_prod_res')['output']
320 self.assertEqual('no_prod_res', beijing_prod_res['output_value'])
321
huangtianhua99a25de2016-07-26 10:58:33 +0800322 def test_stack_create_update_cfn_template_test_to_prod(self):
323 stack_identifier = self.stack_create(template=cfn_template)
324 resources = self.client.resources.list(stack_identifier)
325 self.res_assert_for_test(resources)
huangtianhua27075a92016-07-26 14:32:40 +0800326 self.output_assert_for_test(stack_identifier)
huangtianhua99a25de2016-07-26 10:58:33 +0800327
huangtianhua26da8982016-07-27 18:11:58 +0800328 parms = {'zone': 'fuzhou'}
329 self.update_stack(stack_identifier,
330 template=cfn_template,
331 parameters=parms)
332
333 resources = self.client.resources.list(stack_identifier)
334 self.res_assert_for_test(resources, fj_zone=True)
335 self.output_assert_for_test(stack_identifier)
336
huangtianhua99a25de2016-07-26 10:58:33 +0800337 parms = {'env_type': 'prod'}
338 self.update_stack(stack_identifier,
339 template=cfn_template,
340 parameters=parms)
341
342 resources = self.client.resources.list(stack_identifier)
343 self.res_assert_for_prod(resources)
huangtianhua27075a92016-07-26 14:32:40 +0800344 self.output_assert_for_prod(stack_identifier)
huangtianhua99a25de2016-07-26 10:58:33 +0800345
huangtianhua7bdc7512016-06-17 09:25:38 +0800346 parms = {'env_type': 'prod',
347 'zone': 'shanghai'}
348 self.update_stack(stack_identifier,
349 template=cfn_template,
350 parameters=parms)
351
352 resources = self.client.resources.list(stack_identifier)
353 self.res_assert_for_prod(resources, False)
354 self.output_assert_for_prod(stack_identifier, False)
355
huangtianhua26da8982016-07-27 18:11:58 +0800356 parms = {'env_type': 'prod',
357 'zone': 'xiamen'}
358 self.update_stack(stack_identifier,
359 template=cfn_template,
360 parameters=parms)
361
362 resources = self.client.resources.list(stack_identifier)
363 self.res_assert_for_prod(resources, bj_prod=False, fj_zone=True)
364 self.output_assert_for_prod(stack_identifier, False)
365
huangtianhua99a25de2016-07-26 10:58:33 +0800366 def test_stack_create_update_cfn_template_prod_to_test(self):
367 parms = {'env_type': 'prod'}
368 stack_identifier = self.stack_create(template=cfn_template,
369 parameters=parms)
370 resources = self.client.resources.list(stack_identifier)
371 self.res_assert_for_prod(resources)
huangtianhua27075a92016-07-26 14:32:40 +0800372 self.output_assert_for_prod(stack_identifier)
huangtianhua99a25de2016-07-26 10:58:33 +0800373
huangtianhua26da8982016-07-27 18:11:58 +0800374 parms = {'zone': 'xiamen',
375 'env_type': 'prod'}
376 self.update_stack(stack_identifier,
377 template=cfn_template,
378 parameters=parms)
379
380 resources = self.client.resources.list(stack_identifier)
381 self.res_assert_for_prod(resources, bj_prod=False, fj_zone=True)
382 self.output_assert_for_prod(stack_identifier, bj_prod=False)
383
huangtianhua99a25de2016-07-26 10:58:33 +0800384 parms = {'env_type': 'test'}
385 self.update_stack(stack_identifier,
386 template=cfn_template,
387 parameters=parms)
388
389 resources = self.client.resources.list(stack_identifier)
390 self.res_assert_for_test(resources)
huangtianhua27075a92016-07-26 14:32:40 +0800391 self.output_assert_for_test(stack_identifier)
huangtianhua99a25de2016-07-26 10:58:33 +0800392
huangtianhua26da8982016-07-27 18:11:58 +0800393 parms = {'env_type': 'test',
394 'zone': 'fuzhou'}
395 self.update_stack(stack_identifier,
396 template=cfn_template,
397 parameters=parms)
398
399 resources = self.client.resources.list(stack_identifier)
400 self.res_assert_for_test(resources, fj_zone=True)
401 self.output_assert_for_test(stack_identifier)
402
huangtianhua99a25de2016-07-26 10:58:33 +0800403 def test_stack_create_update_hot_template_test_to_prod(self):
404 stack_identifier = self.stack_create(template=hot_template)
405 resources = self.client.resources.list(stack_identifier)
406 self.res_assert_for_test(resources)
huangtianhua27075a92016-07-26 14:32:40 +0800407 self.output_assert_for_test(stack_identifier)
huangtianhua99a25de2016-07-26 10:58:33 +0800408
409 parms = {'env_type': 'prod'}
410 self.update_stack(stack_identifier,
411 template=hot_template,
412 parameters=parms)
413
414 resources = self.client.resources.list(stack_identifier)
415 self.res_assert_for_prod(resources)
huangtianhua27075a92016-07-26 14:32:40 +0800416 self.output_assert_for_prod(stack_identifier)
huangtianhua99a25de2016-07-26 10:58:33 +0800417
huangtianhua7bdc7512016-06-17 09:25:38 +0800418 parms = {'env_type': 'prod',
419 'zone': 'shanghai'}
420 self.update_stack(stack_identifier,
421 template=hot_template,
422 parameters=parms)
423
424 resources = self.client.resources.list(stack_identifier)
425 self.res_assert_for_prod(resources, False)
426 self.output_assert_for_prod(stack_identifier, False)
427
huangtianhua99a25de2016-07-26 10:58:33 +0800428 def test_stack_create_update_hot_template_prod_to_test(self):
429 parms = {'env_type': 'prod'}
430 stack_identifier = self.stack_create(template=hot_template,
431 parameters=parms)
432 resources = self.client.resources.list(stack_identifier)
433 self.res_assert_for_prod(resources)
huangtianhua27075a92016-07-26 14:32:40 +0800434 self.output_assert_for_prod(stack_identifier)
huangtianhua99a25de2016-07-26 10:58:33 +0800435
436 parms = {'env_type': 'test'}
437 self.update_stack(stack_identifier,
438 template=hot_template,
439 parameters=parms)
440
441 resources = self.client.resources.list(stack_identifier)
442 self.res_assert_for_test(resources)
huangtianhua27075a92016-07-26 14:32:40 +0800443 self.output_assert_for_test(stack_identifier)
Zane Bittera9b5aaf2016-09-12 20:51:52 -0400444
445 def test_condition_rename(self):
446 stack_identifier = self.stack_create(template=before_rename_tmpl)
447 self.update_stack(stack_identifier, template=after_rename_tmpl)
448 self.update_stack(stack_identifier, template=fail_rename_tmpl,
449 expected_status='UPDATE_FAILED')
450 self.update_stack(stack_identifier, template=recover_rename_tmpl)