Steven Hardy | 7419382 | 2015-09-15 09:38:26 +0100 | [diff] [blame] | 1 | # 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 | |
| 14 | import six |
| 15 | |
| 16 | from heatclient import exc |
| 17 | |
| 18 | from heat_integrationtests.functional import functional_base |
| 19 | |
| 20 | |
| 21 | class StackTemplateValidateTest(functional_base.FunctionalTestsBase): |
| 22 | |
| 23 | random_template = ''' |
| 24 | heat_template_version: 2014-10-16 |
| 25 | description: the stack description |
| 26 | parameters: |
| 27 | aparam: |
| 28 | type: number |
| 29 | default: 10 |
| 30 | description: the param description |
| 31 | resources: |
| 32 | myres: |
| 33 | type: OS::Heat::RandomString |
| 34 | properties: |
| 35 | length: {get_param: aparam} |
| 36 | ''' |
| 37 | |
Steven Hardy | 03767a8 | 2015-09-16 14:07:58 +0100 | [diff] [blame] | 38 | parent_template = ''' |
| 39 | heat_template_version: 2014-10-16 |
| 40 | description: the parent template |
| 41 | parameters: |
| 42 | pparam: |
| 43 | type: number |
| 44 | default: 5 |
| 45 | description: the param description |
| 46 | resources: |
| 47 | nres: |
| 48 | type: mynested.yaml |
| 49 | properties: |
| 50 | aparam: {get_param: pparam} |
| 51 | ''' |
| 52 | |
| 53 | parent_template_noprop = ''' |
| 54 | heat_template_version: 2014-10-16 |
| 55 | description: the parent template |
| 56 | resources: |
| 57 | nres: |
| 58 | type: mynested.yaml |
| 59 | ''' |
| 60 | |
Steven Hardy | 7419382 | 2015-09-15 09:38:26 +0100 | [diff] [blame] | 61 | random_template_groups = ''' |
| 62 | heat_template_version: 2014-10-16 |
| 63 | description: the stack description |
| 64 | parameters: |
| 65 | aparam: |
| 66 | type: number |
| 67 | default: 10 |
| 68 | description: the param description |
| 69 | bparam: |
| 70 | type: string |
| 71 | default: foo |
| 72 | cparam: |
| 73 | type: string |
| 74 | default: secret |
| 75 | hidden: true |
| 76 | parameter_groups: |
| 77 | - label: str_params |
| 78 | description: The string params |
| 79 | parameters: |
| 80 | - bparam |
| 81 | - cparam |
| 82 | resources: |
| 83 | myres: |
| 84 | type: OS::Heat::RandomString |
| 85 | properties: |
| 86 | length: {get_param: aparam} |
| 87 | ''' |
| 88 | |
| 89 | def test_template_validate_basic(self): |
| 90 | ret = self.client.stacks.validate(template=self.random_template) |
| 91 | expected = {'Description': 'the stack description', |
| 92 | 'Parameters': { |
| 93 | 'aparam': {'Default': 10, |
| 94 | 'Description': 'the param description', |
| 95 | 'Label': 'aparam', |
| 96 | 'NoEcho': 'false', |
| 97 | 'Type': 'Number'}}} |
| 98 | self.assertEqual(expected, ret) |
| 99 | |
Steven Hardy | 1c8499d | 2015-09-18 15:49:12 +0100 | [diff] [blame] | 100 | def test_template_validate_override_default(self): |
| 101 | env = {'parameters': {'aparam': 5}} |
| 102 | ret = self.client.stacks.validate(template=self.random_template, |
| 103 | environment=env) |
| 104 | expected = {'Description': 'the stack description', |
| 105 | 'Parameters': { |
| 106 | 'aparam': {'Default': 10, |
| 107 | 'Value': 5, |
| 108 | 'Description': 'the param description', |
| 109 | 'Label': 'aparam', |
| 110 | 'NoEcho': 'false', |
| 111 | 'Type': 'Number'}}} |
| 112 | self.assertEqual(expected, ret) |
| 113 | |
Steven Hardy | dca518b | 2015-09-18 18:19:40 +0100 | [diff] [blame] | 114 | def test_template_validate_override_none(self): |
| 115 | env = {'resource_registry': { |
| 116 | 'OS::Heat::RandomString': 'OS::Heat::None'}} |
| 117 | ret = self.client.stacks.validate(template=self.random_template, |
| 118 | environment=env) |
| 119 | expected = {'Description': 'the stack description', |
| 120 | 'Parameters': { |
| 121 | 'aparam': {'Default': 10, |
| 122 | 'Description': 'the param description', |
| 123 | 'Label': 'aparam', |
| 124 | 'NoEcho': 'false', |
| 125 | 'Type': 'Number'}}} |
| 126 | self.assertEqual(expected, ret) |
| 127 | |
Steven Hardy | 7419382 | 2015-09-15 09:38:26 +0100 | [diff] [blame] | 128 | def test_template_validate_basic_required_param(self): |
| 129 | tmpl = self.random_template.replace('default: 10', '') |
| 130 | ret = self.client.stacks.validate(template=tmpl) |
| 131 | expected = {'Description': 'the stack description', |
| 132 | 'Parameters': { |
| 133 | 'aparam': {'Description': 'the param description', |
| 134 | 'Label': 'aparam', |
| 135 | 'NoEcho': 'false', |
| 136 | 'Type': 'Number'}}} |
| 137 | self.assertEqual(expected, ret) |
| 138 | |
| 139 | def test_template_validate_fail_version(self): |
| 140 | fail_template = self.random_template.replace('2014-10-16', 'invalid') |
| 141 | ex = self.assertRaises(exc.HTTPBadRequest, |
| 142 | self.client.stacks.validate, |
| 143 | template=fail_template) |
| 144 | self.assertIn('The template version is invalid', six.text_type(ex)) |
| 145 | |
| 146 | def test_template_validate_parameter_groups(self): |
| 147 | ret = self.client.stacks.validate(template=self.random_template_groups) |
| 148 | expected = {'Description': 'the stack description', |
| 149 | 'ParameterGroups': |
| 150 | [{'description': 'The string params', |
| 151 | 'label': 'str_params', |
| 152 | 'parameters': ['bparam', 'cparam']}], |
| 153 | 'Parameters': |
| 154 | {'aparam': |
| 155 | {'Default': 10, |
| 156 | 'Description': 'the param description', |
| 157 | 'Label': 'aparam', |
| 158 | 'NoEcho': 'false', |
| 159 | 'Type': 'Number'}, |
| 160 | 'bparam': |
| 161 | {'Default': 'foo', |
| 162 | 'Description': '', |
| 163 | 'Label': 'bparam', |
| 164 | 'NoEcho': 'false', |
| 165 | 'Type': 'String'}, |
| 166 | 'cparam': |
| 167 | {'Default': 'secret', |
| 168 | 'Description': '', |
| 169 | 'Label': 'cparam', |
| 170 | 'NoEcho': 'true', |
| 171 | 'Type': 'String'}}} |
| 172 | self.assertEqual(expected, ret) |
Steven Hardy | 03767a8 | 2015-09-16 14:07:58 +0100 | [diff] [blame] | 173 | |
| 174 | def test_template_validate_nested_off(self): |
| 175 | files = {'mynested.yaml': self.random_template} |
| 176 | ret = self.client.stacks.validate(template=self.parent_template, |
| 177 | files=files) |
| 178 | expected = {'Description': 'the parent template', |
| 179 | 'Parameters': { |
| 180 | 'pparam': {'Default': 5, |
| 181 | 'Description': 'the param description', |
| 182 | 'Label': 'pparam', |
| 183 | 'NoEcho': 'false', |
| 184 | 'Type': 'Number'}}} |
| 185 | self.assertEqual(expected, ret) |
| 186 | |
| 187 | def test_template_validate_nested_on(self): |
| 188 | files = {'mynested.yaml': self.random_template} |
| 189 | ret = self.client.stacks.validate(template=self.parent_template_noprop, |
| 190 | files=files, |
| 191 | show_nested=True) |
| 192 | expected = {'Description': 'the parent template', |
| 193 | 'Parameters': {}, |
| 194 | 'NestedParameters': { |
| 195 | 'nres': {'Description': 'the stack description', |
| 196 | 'Parameters': {'aparam': {'Default': 10, |
| 197 | 'Description': |
| 198 | 'the param ' |
| 199 | 'description', |
| 200 | 'Label': 'aparam', |
| 201 | 'NoEcho': 'false', |
| 202 | 'Type': 'Number'}}, |
| 203 | 'Type': 'mynested.yaml'}}} |
| 204 | self.assertEqual(expected, ret) |
| 205 | |
| 206 | def test_template_validate_nested_on_multiple(self): |
| 207 | # parent_template -> nested_template -> random_template |
| 208 | nested_template = self.random_template.replace( |
| 209 | 'OS::Heat::RandomString', 'mynested2.yaml') |
| 210 | files = {'mynested.yaml': nested_template, |
| 211 | 'mynested2.yaml': self.random_template} |
| 212 | ret = self.client.stacks.validate(template=self.parent_template, |
| 213 | files=files, |
| 214 | show_nested=True) |
| 215 | |
| 216 | n_param2 = {'myres': {'Description': 'the stack description', |
| 217 | 'Parameters': {'aparam': {'Default': 10, |
| 218 | 'Description': |
| 219 | 'the param ' |
| 220 | 'description', |
| 221 | 'Label': 'aparam', |
| 222 | 'NoEcho': 'false', |
| 223 | 'Type': 'Number'}}, |
| 224 | 'Type': 'mynested2.yaml'}} |
| 225 | expected = {'Description': 'the parent template', |
| 226 | 'Parameters': { |
| 227 | 'pparam': {'Default': 5, |
| 228 | 'Description': 'the param description', |
| 229 | 'Label': 'pparam', |
| 230 | 'NoEcho': 'false', |
| 231 | 'Type': 'Number'}}, |
| 232 | 'NestedParameters': { |
| 233 | 'nres': {'Description': 'the stack description', |
| 234 | 'Parameters': {'aparam': {'Default': 10, |
| 235 | 'Description': |
| 236 | 'the param ' |
| 237 | 'description', |
| 238 | 'Label': 'aparam', |
| 239 | 'Value': 5, |
| 240 | 'NoEcho': 'false', |
| 241 | 'Type': 'Number'}}, |
| 242 | 'NestedParameters': n_param2, |
| 243 | 'Type': 'mynested.yaml'}}} |
| 244 | self.assertEqual(expected, ret) |