blob: ad1066dbcf82ff3d89ba09c1267da877d97ea5b9 [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
163
164class CreateUpdateResConditionTest(functional_base.FunctionalTestsBase):
165
166 def setUp(self):
167 super(CreateUpdateResConditionTest, self).setUp()
168
Zane Bitteree582d92016-09-08 11:33:46 -0400169 def res_assert_for_prod(self, resources, bj_prod=True, fj_zone=False):
huangtianhua99a25de2016-07-26 10:58:33 +0800170 res_names = [res.resource_name for res in resources]
huangtianhua7bdc7512016-06-17 09:25:38 +0800171 if bj_prod:
Zane Bitteree582d92016-09-08 11:33:46 -0400172 self.assertEqual(3, len(resources))
huangtianhua7bdc7512016-06-17 09:25:38 +0800173 self.assertIn('beijing_prod_res', res_names)
huangtianhua26da8982016-07-27 18:11:58 +0800174 elif fj_zone:
Zane Bitteree582d92016-09-08 11:33:46 -0400175 self.assertEqual(3, len(resources))
huangtianhua26da8982016-07-27 18:11:58 +0800176 self.assertIn('fujian_res', res_names)
177 self.assertNotIn('beijing_prod_res', res_names)
huangtianhua7bdc7512016-06-17 09:25:38 +0800178 else:
Zane Bitteree582d92016-09-08 11:33:46 -0400179 self.assertEqual(2, len(resources))
huangtianhua99a25de2016-07-26 10:58:33 +0800180 self.assertIn('prod_res', res_names)
181 self.assertIn('test_res', res_names)
182
Zane Bitteree582d92016-09-08 11:33:46 -0400183 def res_assert_for_test(self, resources, fj_zone=False):
huangtianhua99a25de2016-07-26 10:58:33 +0800184 res_names = [res.resource_name for res in resources]
huangtianhua26da8982016-07-27 18:11:58 +0800185
186 if fj_zone:
Zane Bitteree582d92016-09-08 11:33:46 -0400187 self.assertEqual(3, len(resources))
huangtianhua26da8982016-07-27 18:11:58 +0800188 self.assertIn('fujian_res', res_names)
huangtianhuae4365742016-08-30 10:27:21 +0800189 else:
Zane Bitteree582d92016-09-08 11:33:46 -0400190 self.assertEqual(2, len(resources))
191 self.assertNotIn('fujian_res', res_names)
192
huangtianhua99a25de2016-07-26 10:58:33 +0800193 self.assertIn('test_res', res_names)
huangtianhuabf63fcb2016-07-27 13:02:39 +0800194 self.assertIn('test_res1', res_names)
huangtianhua99a25de2016-07-26 10:58:33 +0800195 self.assertNotIn('prod_res', res_names)
196
huangtianhua7bdc7512016-06-17 09:25:38 +0800197 def output_assert_for_prod(self, stack_id, bj_prod=True):
huangtianhua27075a92016-07-26 14:32:40 +0800198 output = self.client.stacks.output_show(stack_id,
199 'res_value')['output']
200 self.assertEqual('prod_res', output['output_value'])
201
huangtianhuafb8b51f2016-02-23 17:03:10 +0800202 test_res_value = self.client.stacks.output_show(
203 stack_id, 'test_res_value')['output']
204 self.assertEqual('env_is_prod', test_res_value['output_value'])
205
206 prod_resource = self.client.stacks.output_show(
207 stack_id, 'prod_resource')['output']
208 self.assertNotEqual('no_prod_res', prod_resource['output_value'])
209
huangtianhuabf63fcb2016-07-27 13:02:39 +0800210 test_res_output = self.client.stacks.output_show(
211 stack_id, 'test_res1_value')['output']
212 self.assertEqual('no_test_res1', test_res_output['output_value'])
213
huangtianhua7bdc7512016-06-17 09:25:38 +0800214 beijing_prod_res = self.client.stacks.output_show(
215 stack_id, 'beijing_prod_res')['output']
216 if bj_prod:
217 self.assertNotEqual('no_prod_res',
218 beijing_prod_res['output_value'])
219 else:
220 self.assertEqual('no_prod_res', beijing_prod_res['output_value'])
221
huangtianhua27075a92016-07-26 14:32:40 +0800222 def output_assert_for_test(self, stack_id):
223 output = self.client.stacks.output_show(stack_id,
224 'res_value')['output']
225 self.assertIsNone(output['output_value'])
226
huangtianhuafb8b51f2016-02-23 17:03:10 +0800227 test_res_value = self.client.stacks.output_show(
228 stack_id, 'test_res_value')['output']
229 self.assertEqual('env_is_test', test_res_value['output_value'])
230
231 prod_resource = self.client.stacks.output_show(
232 stack_id, 'prod_resource')['output']
233 self.assertEqual('no_prod_res', prod_resource['output_value'])
234
huangtianhuabf63fcb2016-07-27 13:02:39 +0800235 test_res_output = self.client.stacks.output_show(
236 stack_id, 'test_res1_value')['output']
237 self.assertEqual('just in test env',
238 test_res_output['output_value'])
239
huangtianhua7bdc7512016-06-17 09:25:38 +0800240 beijing_prod_res = self.client.stacks.output_show(
241 stack_id, 'beijing_prod_res')['output']
242 self.assertEqual('no_prod_res', beijing_prod_res['output_value'])
243
huangtianhua99a25de2016-07-26 10:58:33 +0800244 def test_stack_create_update_cfn_template_test_to_prod(self):
245 stack_identifier = self.stack_create(template=cfn_template)
246 resources = self.client.resources.list(stack_identifier)
247 self.res_assert_for_test(resources)
huangtianhua27075a92016-07-26 14:32:40 +0800248 self.output_assert_for_test(stack_identifier)
huangtianhua99a25de2016-07-26 10:58:33 +0800249
huangtianhua26da8982016-07-27 18:11:58 +0800250 parms = {'zone': 'fuzhou'}
251 self.update_stack(stack_identifier,
252 template=cfn_template,
253 parameters=parms)
254
255 resources = self.client.resources.list(stack_identifier)
256 self.res_assert_for_test(resources, fj_zone=True)
257 self.output_assert_for_test(stack_identifier)
258
huangtianhua99a25de2016-07-26 10:58:33 +0800259 parms = {'env_type': 'prod'}
260 self.update_stack(stack_identifier,
261 template=cfn_template,
262 parameters=parms)
263
264 resources = self.client.resources.list(stack_identifier)
265 self.res_assert_for_prod(resources)
huangtianhua27075a92016-07-26 14:32:40 +0800266 self.output_assert_for_prod(stack_identifier)
huangtianhua99a25de2016-07-26 10:58:33 +0800267
huangtianhua7bdc7512016-06-17 09:25:38 +0800268 parms = {'env_type': 'prod',
269 'zone': 'shanghai'}
270 self.update_stack(stack_identifier,
271 template=cfn_template,
272 parameters=parms)
273
274 resources = self.client.resources.list(stack_identifier)
275 self.res_assert_for_prod(resources, False)
276 self.output_assert_for_prod(stack_identifier, False)
277
huangtianhua26da8982016-07-27 18:11:58 +0800278 parms = {'env_type': 'prod',
279 'zone': 'xiamen'}
280 self.update_stack(stack_identifier,
281 template=cfn_template,
282 parameters=parms)
283
284 resources = self.client.resources.list(stack_identifier)
285 self.res_assert_for_prod(resources, bj_prod=False, fj_zone=True)
286 self.output_assert_for_prod(stack_identifier, False)
287
huangtianhua99a25de2016-07-26 10:58:33 +0800288 def test_stack_create_update_cfn_template_prod_to_test(self):
289 parms = {'env_type': 'prod'}
290 stack_identifier = self.stack_create(template=cfn_template,
291 parameters=parms)
292 resources = self.client.resources.list(stack_identifier)
293 self.res_assert_for_prod(resources)
huangtianhua27075a92016-07-26 14:32:40 +0800294 self.output_assert_for_prod(stack_identifier)
huangtianhua99a25de2016-07-26 10:58:33 +0800295
huangtianhua26da8982016-07-27 18:11:58 +0800296 parms = {'zone': 'xiamen',
297 'env_type': 'prod'}
298 self.update_stack(stack_identifier,
299 template=cfn_template,
300 parameters=parms)
301
302 resources = self.client.resources.list(stack_identifier)
303 self.res_assert_for_prod(resources, bj_prod=False, fj_zone=True)
304 self.output_assert_for_prod(stack_identifier, bj_prod=False)
305
huangtianhua99a25de2016-07-26 10:58:33 +0800306 parms = {'env_type': 'test'}
307 self.update_stack(stack_identifier,
308 template=cfn_template,
309 parameters=parms)
310
311 resources = self.client.resources.list(stack_identifier)
312 self.res_assert_for_test(resources)
huangtianhua27075a92016-07-26 14:32:40 +0800313 self.output_assert_for_test(stack_identifier)
huangtianhua99a25de2016-07-26 10:58:33 +0800314
huangtianhua26da8982016-07-27 18:11:58 +0800315 parms = {'env_type': 'test',
316 'zone': 'fuzhou'}
317 self.update_stack(stack_identifier,
318 template=cfn_template,
319 parameters=parms)
320
321 resources = self.client.resources.list(stack_identifier)
322 self.res_assert_for_test(resources, fj_zone=True)
323 self.output_assert_for_test(stack_identifier)
324
huangtianhua99a25de2016-07-26 10:58:33 +0800325 def test_stack_create_update_hot_template_test_to_prod(self):
326 stack_identifier = self.stack_create(template=hot_template)
327 resources = self.client.resources.list(stack_identifier)
328 self.res_assert_for_test(resources)
huangtianhua27075a92016-07-26 14:32:40 +0800329 self.output_assert_for_test(stack_identifier)
huangtianhua99a25de2016-07-26 10:58:33 +0800330
331 parms = {'env_type': 'prod'}
332 self.update_stack(stack_identifier,
333 template=hot_template,
334 parameters=parms)
335
336 resources = self.client.resources.list(stack_identifier)
337 self.res_assert_for_prod(resources)
huangtianhua27075a92016-07-26 14:32:40 +0800338 self.output_assert_for_prod(stack_identifier)
huangtianhua99a25de2016-07-26 10:58:33 +0800339
huangtianhua7bdc7512016-06-17 09:25:38 +0800340 parms = {'env_type': 'prod',
341 'zone': 'shanghai'}
342 self.update_stack(stack_identifier,
343 template=hot_template,
344 parameters=parms)
345
346 resources = self.client.resources.list(stack_identifier)
347 self.res_assert_for_prod(resources, False)
348 self.output_assert_for_prod(stack_identifier, False)
349
huangtianhua99a25de2016-07-26 10:58:33 +0800350 def test_stack_create_update_hot_template_prod_to_test(self):
351 parms = {'env_type': 'prod'}
352 stack_identifier = self.stack_create(template=hot_template,
353 parameters=parms)
354 resources = self.client.resources.list(stack_identifier)
355 self.res_assert_for_prod(resources)
huangtianhua27075a92016-07-26 14:32:40 +0800356 self.output_assert_for_prod(stack_identifier)
huangtianhua99a25de2016-07-26 10:58:33 +0800357
358 parms = {'env_type': 'test'}
359 self.update_stack(stack_identifier,
360 template=hot_template,
361 parameters=parms)
362
363 resources = self.client.resources.list(stack_identifier)
364 self.res_assert_for_test(resources)
huangtianhua27075a92016-07-26 14:32:40 +0800365 self.output_assert_for_test(stack_identifier)