blob: 8850c499ced0e184df2d1b94bd15ff7c7a506b9b [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:
29 value: test_res
30 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
huangtianhua99a25de2016-07-26 10:58:33 +080039'''
40
41hot_template = '''
42heat_template_version: 2016-10-14
43parameters:
44 env_type:
45 default: test
46 type: string
47 constraints:
48 - allowed_values: [prod, test]
49conditions:
50 prod: {equals : [{get_param: env_type}, "prod"]}
51resources:
52 test_res:
53 type: OS::Heat::TestResource
54 properties:
55 value: test_res
56 prod_res:
57 type: OS::Heat::TestResource
58 properties:
59 value: prod_res
60 condition: prod
huangtianhua27075a92016-07-26 14:32:40 +080061outputs:
62 res_value:
63 value: {get_attr: [prod_res, output]}
64 condition: prod
huangtianhua99a25de2016-07-26 10:58:33 +080065'''
66
67
68class CreateUpdateResConditionTest(functional_base.FunctionalTestsBase):
69
70 def setUp(self):
71 super(CreateUpdateResConditionTest, self).setUp()
72
73 def res_assert_for_prod(self, resources):
74 self.assertEqual(2, len(resources))
75 res_names = [res.resource_name for res in resources]
76 self.assertIn('prod_res', res_names)
77 self.assertIn('test_res', res_names)
78
79 def res_assert_for_test(self, resources):
80 self.assertEqual(1, len(resources))
81 res_names = [res.resource_name for res in resources]
82 self.assertIn('test_res', res_names)
83 self.assertNotIn('prod_res', res_names)
84
huangtianhua27075a92016-07-26 14:32:40 +080085 def output_assert_for_prod(self, stack_id):
86 output = self.client.stacks.output_show(stack_id,
87 'res_value')['output']
88 self.assertEqual('prod_res', output['output_value'])
89
90 def output_assert_for_test(self, stack_id):
91 output = self.client.stacks.output_show(stack_id,
92 'res_value')['output']
93 self.assertIsNone(output['output_value'])
94
huangtianhua99a25de2016-07-26 10:58:33 +080095 def test_stack_create_update_cfn_template_test_to_prod(self):
96 stack_identifier = self.stack_create(template=cfn_template)
97 resources = self.client.resources.list(stack_identifier)
98 self.res_assert_for_test(resources)
huangtianhua27075a92016-07-26 14:32:40 +080099 self.output_assert_for_test(stack_identifier)
huangtianhua99a25de2016-07-26 10:58:33 +0800100
101 parms = {'env_type': 'prod'}
102 self.update_stack(stack_identifier,
103 template=cfn_template,
104 parameters=parms)
105
106 resources = self.client.resources.list(stack_identifier)
107 self.res_assert_for_prod(resources)
huangtianhua27075a92016-07-26 14:32:40 +0800108 self.output_assert_for_prod(stack_identifier)
huangtianhua99a25de2016-07-26 10:58:33 +0800109
110 def test_stack_create_update_cfn_template_prod_to_test(self):
111 parms = {'env_type': 'prod'}
112 stack_identifier = self.stack_create(template=cfn_template,
113 parameters=parms)
114 resources = self.client.resources.list(stack_identifier)
115 self.res_assert_for_prod(resources)
huangtianhua27075a92016-07-26 14:32:40 +0800116 self.output_assert_for_prod(stack_identifier)
huangtianhua99a25de2016-07-26 10:58:33 +0800117
118 parms = {'env_type': 'test'}
119 self.update_stack(stack_identifier,
120 template=cfn_template,
121 parameters=parms)
122
123 resources = self.client.resources.list(stack_identifier)
124 self.res_assert_for_test(resources)
huangtianhua27075a92016-07-26 14:32:40 +0800125 self.output_assert_for_test(stack_identifier)
huangtianhua99a25de2016-07-26 10:58:33 +0800126
127 def test_stack_create_update_hot_template_test_to_prod(self):
128 stack_identifier = self.stack_create(template=hot_template)
129 resources = self.client.resources.list(stack_identifier)
130 self.res_assert_for_test(resources)
huangtianhua27075a92016-07-26 14:32:40 +0800131 self.output_assert_for_test(stack_identifier)
huangtianhua99a25de2016-07-26 10:58:33 +0800132
133 parms = {'env_type': 'prod'}
134 self.update_stack(stack_identifier,
135 template=hot_template,
136 parameters=parms)
137
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 def test_stack_create_update_hot_template_prod_to_test(self):
143 parms = {'env_type': 'prod'}
144 stack_identifier = self.stack_create(template=hot_template,
145 parameters=parms)
146 resources = self.client.resources.list(stack_identifier)
147 self.res_assert_for_prod(resources)
huangtianhua27075a92016-07-26 14:32:40 +0800148 self.output_assert_for_prod(stack_identifier)
huangtianhua99a25de2016-07-26 10:58:33 +0800149
150 parms = {'env_type': 'test'}
151 self.update_stack(stack_identifier,
152 template=hot_template,
153 parameters=parms)
154
155 resources = self.client.resources.list(stack_identifier)
156 self.res_assert_for_test(resources)
huangtianhua27075a92016-07-26 14:32:40 +0800157 self.output_assert_for_test(stack_identifier)