blob: 577a645188ff11f0720ee8689b1c349df1554562 [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]
23Conditions:
24 Prod: {"Fn::Equals" : [{Ref: env_type}, "prod"]}
25Resources:
26 test_res:
27 Type: OS::Heat::TestResource
28 Properties:
huangtianhuafb8b51f2016-02-23 17:03:10 +080029 value: {"Fn::If": ["Prod", "env_is_prod", "env_is_test"]}
huangtianhua99a25de2016-07-26 10:58:33 +080030 prod_res:
31 Type: OS::Heat::TestResource
32 Properties:
33 value: prod_res
34 Condition: Prod
huangtianhua27075a92016-07-26 14:32:40 +080035Outputs:
36 res_value:
37 Value: {"Fn::GetAtt": [prod_res, output]}
38 Condition: Prod
huangtianhuafb8b51f2016-02-23 17:03:10 +080039 test_res_value:
40 Value: {"Fn::GetAtt": [test_res, output]}
41 prod_resource:
42 Value: {"Fn::If": [Prod, {Ref: prod_res}, 'no_prod_res']}
huangtianhua99a25de2016-07-26 10:58:33 +080043'''
44
45hot_template = '''
46heat_template_version: 2016-10-14
47parameters:
48 env_type:
49 default: test
50 type: string
51 constraints:
52 - allowed_values: [prod, test]
53conditions:
54 prod: {equals : [{get_param: env_type}, "prod"]}
55resources:
56 test_res:
57 type: OS::Heat::TestResource
58 properties:
huangtianhuafb8b51f2016-02-23 17:03:10 +080059 value: {if: ["prod", "env_is_prod", "env_is_test"]}
huangtianhua99a25de2016-07-26 10:58:33 +080060 prod_res:
61 type: OS::Heat::TestResource
62 properties:
63 value: prod_res
64 condition: prod
huangtianhua27075a92016-07-26 14:32:40 +080065outputs:
66 res_value:
67 value: {get_attr: [prod_res, output]}
68 condition: prod
huangtianhuafb8b51f2016-02-23 17:03:10 +080069 test_res_value:
70 value: {get_attr: [test_res, output]}
71 prod_resource:
72 value: {if: [prod, {get_resource: prod_res}, 'no_prod_res']}
huangtianhua99a25de2016-07-26 10:58:33 +080073'''
74
75
76class CreateUpdateResConditionTest(functional_base.FunctionalTestsBase):
77
78 def setUp(self):
79 super(CreateUpdateResConditionTest, self).setUp()
80
81 def res_assert_for_prod(self, resources):
82 self.assertEqual(2, len(resources))
83 res_names = [res.resource_name for res in resources]
84 self.assertIn('prod_res', res_names)
85 self.assertIn('test_res', res_names)
86
87 def res_assert_for_test(self, resources):
88 self.assertEqual(1, len(resources))
89 res_names = [res.resource_name for res in resources]
90 self.assertIn('test_res', res_names)
91 self.assertNotIn('prod_res', res_names)
92
huangtianhua27075a92016-07-26 14:32:40 +080093 def output_assert_for_prod(self, stack_id):
94 output = self.client.stacks.output_show(stack_id,
95 'res_value')['output']
96 self.assertEqual('prod_res', output['output_value'])
97
huangtianhuafb8b51f2016-02-23 17:03:10 +080098 test_res_value = self.client.stacks.output_show(
99 stack_id, 'test_res_value')['output']
100 self.assertEqual('env_is_prod', test_res_value['output_value'])
101
102 prod_resource = self.client.stacks.output_show(
103 stack_id, 'prod_resource')['output']
104 self.assertNotEqual('no_prod_res', prod_resource['output_value'])
105
huangtianhua27075a92016-07-26 14:32:40 +0800106 def output_assert_for_test(self, stack_id):
107 output = self.client.stacks.output_show(stack_id,
108 'res_value')['output']
109 self.assertIsNone(output['output_value'])
110
huangtianhuafb8b51f2016-02-23 17:03:10 +0800111 test_res_value = self.client.stacks.output_show(
112 stack_id, 'test_res_value')['output']
113 self.assertEqual('env_is_test', test_res_value['output_value'])
114
115 prod_resource = self.client.stacks.output_show(
116 stack_id, 'prod_resource')['output']
117 self.assertEqual('no_prod_res', prod_resource['output_value'])
118
huangtianhua99a25de2016-07-26 10:58:33 +0800119 def test_stack_create_update_cfn_template_test_to_prod(self):
120 stack_identifier = self.stack_create(template=cfn_template)
121 resources = self.client.resources.list(stack_identifier)
122 self.res_assert_for_test(resources)
huangtianhua27075a92016-07-26 14:32:40 +0800123 self.output_assert_for_test(stack_identifier)
huangtianhua99a25de2016-07-26 10:58:33 +0800124
125 parms = {'env_type': 'prod'}
126 self.update_stack(stack_identifier,
127 template=cfn_template,
128 parameters=parms)
129
130 resources = self.client.resources.list(stack_identifier)
131 self.res_assert_for_prod(resources)
huangtianhua27075a92016-07-26 14:32:40 +0800132 self.output_assert_for_prod(stack_identifier)
huangtianhua99a25de2016-07-26 10:58:33 +0800133
134 def test_stack_create_update_cfn_template_prod_to_test(self):
135 parms = {'env_type': 'prod'}
136 stack_identifier = self.stack_create(template=cfn_template,
137 parameters=parms)
138 resources = self.client.resources.list(stack_identifier)
139 self.res_assert_for_prod(resources)
huangtianhua27075a92016-07-26 14:32:40 +0800140 self.output_assert_for_prod(stack_identifier)
huangtianhua99a25de2016-07-26 10:58:33 +0800141
142 parms = {'env_type': 'test'}
143 self.update_stack(stack_identifier,
144 template=cfn_template,
145 parameters=parms)
146
147 resources = self.client.resources.list(stack_identifier)
148 self.res_assert_for_test(resources)
huangtianhua27075a92016-07-26 14:32:40 +0800149 self.output_assert_for_test(stack_identifier)
huangtianhua99a25de2016-07-26 10:58:33 +0800150
151 def test_stack_create_update_hot_template_test_to_prod(self):
152 stack_identifier = self.stack_create(template=hot_template)
153 resources = self.client.resources.list(stack_identifier)
154 self.res_assert_for_test(resources)
huangtianhua27075a92016-07-26 14:32:40 +0800155 self.output_assert_for_test(stack_identifier)
huangtianhua99a25de2016-07-26 10:58:33 +0800156
157 parms = {'env_type': 'prod'}
158 self.update_stack(stack_identifier,
159 template=hot_template,
160 parameters=parms)
161
162 resources = self.client.resources.list(stack_identifier)
163 self.res_assert_for_prod(resources)
huangtianhua27075a92016-07-26 14:32:40 +0800164 self.output_assert_for_prod(stack_identifier)
huangtianhua99a25de2016-07-26 10:58:33 +0800165
166 def test_stack_create_update_hot_template_prod_to_test(self):
167 parms = {'env_type': 'prod'}
168 stack_identifier = self.stack_create(template=hot_template,
169 parameters=parms)
170 resources = self.client.resources.list(stack_identifier)
171 self.res_assert_for_prod(resources)
huangtianhua27075a92016-07-26 14:32:40 +0800172 self.output_assert_for_prod(stack_identifier)
huangtianhua99a25de2016-07-26 10:58:33 +0800173
174 parms = {'env_type': 'test'}
175 self.update_stack(stack_identifier,
176 template=hot_template,
177 parameters=parms)
178
179 resources = self.client.resources.list(stack_identifier)
180 self.res_assert_for_test(resources)
huangtianhua27075a92016-07-26 14:32:40 +0800181 self.output_assert_for_test(stack_identifier)