Steven Hardy | 6f0bda8 | 2014-12-12 17:49:10 +0000 | [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 | |
Angus Salkeld | b61f8f1 | 2015-01-19 23:00:45 +1000 | [diff] [blame] | 13 | import copy |
Angus Salkeld | 011acc7 | 2015-01-16 20:26:34 +1000 | [diff] [blame] | 14 | import json |
| 15 | |
Steven Hardy | 6f0bda8 | 2014-12-12 17:49:10 +0000 | [diff] [blame] | 16 | from heatclient import exc |
Pavlo Shchelokovskyy | 60e0ecd | 2014-12-14 22:17:21 +0200 | [diff] [blame] | 17 | import six |
Angus Salkeld | 011acc7 | 2015-01-16 20:26:34 +1000 | [diff] [blame] | 18 | import yaml |
Steven Hardy | 6f0bda8 | 2014-12-12 17:49:10 +0000 | [diff] [blame] | 19 | |
Rabi Mishra | 477efc9 | 2015-07-31 13:01:45 +0530 | [diff] [blame] | 20 | from heat_integrationtests.functional import functional_base |
Steven Hardy | 6f0bda8 | 2014-12-12 17:49:10 +0000 | [diff] [blame] | 21 | |
| 22 | |
Rabi Mishra | 477efc9 | 2015-07-31 13:01:45 +0530 | [diff] [blame] | 23 | class ResourceGroupTest(functional_base.FunctionalTestsBase): |
Angus Salkeld | 665d86c | 2015-01-19 22:15:48 +1000 | [diff] [blame] | 24 | template = ''' |
Unmesh Gurjar | 0a25a73 | 2014-12-23 17:28:33 +0530 | [diff] [blame] | 25 | heat_template_version: 2013-05-23 |
| 26 | resources: |
| 27 | random_group: |
| 28 | type: OS::Heat::ResourceGroup |
| 29 | properties: |
Angus Salkeld | 665d86c | 2015-01-19 22:15:48 +1000 | [diff] [blame] | 30 | count: 0 |
Unmesh Gurjar | 0a25a73 | 2014-12-23 17:28:33 +0530 | [diff] [blame] | 31 | resource_def: |
Angus Salkeld | 665d86c | 2015-01-19 22:15:48 +1000 | [diff] [blame] | 32 | type: My::RandomString |
Unmesh Gurjar | 0a25a73 | 2014-12-23 17:28:33 +0530 | [diff] [blame] | 33 | properties: |
| 34 | length: 30 |
Angus Salkeld | b61f8f1 | 2015-01-19 23:00:45 +1000 | [diff] [blame] | 35 | salt: initial |
Unmesh Gurjar | 0a25a73 | 2014-12-23 17:28:33 +0530 | [diff] [blame] | 36 | outputs: |
| 37 | random1: |
| 38 | value: {get_attr: [random_group, resource.0.value]} |
| 39 | random2: |
| 40 | value: {get_attr: [random_group, resource.1.value]} |
| 41 | all_values: |
| 42 | value: {get_attr: [random_group, value]} |
| 43 | ''' |
| 44 | |
Steven Hardy | 6f0bda8 | 2014-12-12 17:49:10 +0000 | [diff] [blame] | 45 | def test_resource_group_zero_novalidate(self): |
| 46 | # Nested resources should be validated only when size > 0 |
| 47 | # This allows features to be disabled via size=0 without |
gecong1973 | f6df13b | 2016-06-23 12:39:48 +0800 | [diff] [blame] | 48 | # triggering validation of nested resource custom constraints |
Steven Hardy | 6f0bda8 | 2014-12-12 17:49:10 +0000 | [diff] [blame] | 49 | # e.g images etc in the nested schema. |
| 50 | nested_template_fail = ''' |
| 51 | heat_template_version: 2013-05-23 |
Angus Salkeld | 665d86c | 2015-01-19 22:15:48 +1000 | [diff] [blame] | 52 | parameters: |
| 53 | length: |
| 54 | type: string |
| 55 | default: 50 |
Angus Salkeld | b61f8f1 | 2015-01-19 23:00:45 +1000 | [diff] [blame] | 56 | salt: |
| 57 | type: string |
| 58 | default: initial |
Steven Hardy | 6f0bda8 | 2014-12-12 17:49:10 +0000 | [diff] [blame] | 59 | resources: |
| 60 | random: |
| 61 | type: OS::Heat::RandomString |
| 62 | properties: |
Angus Salkeld | 665d86c | 2015-01-19 22:15:48 +1000 | [diff] [blame] | 63 | length: BAD |
Steven Hardy | 6f0bda8 | 2014-12-12 17:49:10 +0000 | [diff] [blame] | 64 | ''' |
| 65 | |
| 66 | files = {'provider.yaml': nested_template_fail} |
| 67 | env = {'resource_registry': |
| 68 | {'My::RandomString': 'provider.yaml'}} |
| 69 | stack_identifier = self.stack_create( |
Angus Salkeld | 665d86c | 2015-01-19 22:15:48 +1000 | [diff] [blame] | 70 | template=self.template, |
Steven Hardy | 6f0bda8 | 2014-12-12 17:49:10 +0000 | [diff] [blame] | 71 | files=files, |
| 72 | environment=env |
| 73 | ) |
| 74 | |
| 75 | self.assertEqual({u'random_group': u'OS::Heat::ResourceGroup'}, |
| 76 | self.list_resources(stack_identifier)) |
| 77 | |
| 78 | # Check we created an empty nested stack |
Rabi Mishra | 8bcff8a | 2015-09-21 18:15:04 +0530 | [diff] [blame] | 79 | nested_identifier = self.group_nested_identifier(stack_identifier, |
| 80 | 'random_group') |
Steven Hardy | 6f0bda8 | 2014-12-12 17:49:10 +0000 | [diff] [blame] | 81 | self.assertEqual({}, self.list_resources(nested_identifier)) |
| 82 | |
| 83 | # Prove validation works for non-zero create/update |
Angus Salkeld | 665d86c | 2015-01-19 22:15:48 +1000 | [diff] [blame] | 84 | template_two_nested = self.template.replace("count: 0", "count: 2") |
rabi | 2a5bfcd | 2017-05-22 15:06:57 +0530 | [diff] [blame] | 85 | expected_err = ("resources.random_group<nested_stack>.resources." |
| 86 | "0<provider.yaml>.resources.random: : " |
| 87 | "Value 'BAD' is not an integer") |
Steven Hardy | 6f0bda8 | 2014-12-12 17:49:10 +0000 | [diff] [blame] | 88 | ex = self.assertRaises(exc.HTTPBadRequest, self.update_stack, |
| 89 | stack_identifier, template_two_nested, |
| 90 | environment=env, files=files) |
| 91 | self.assertIn(expected_err, six.text_type(ex)) |
| 92 | |
| 93 | ex = self.assertRaises(exc.HTTPBadRequest, self.stack_create, |
| 94 | template=template_two_nested, |
| 95 | environment=env, files=files) |
| 96 | self.assertIn(expected_err, six.text_type(ex)) |
Unmesh Gurjar | 0a25a73 | 2014-12-23 17:28:33 +0530 | [diff] [blame] | 97 | |
Steven Hardy | 0145b8d | 2015-04-23 14:14:26 +1000 | [diff] [blame] | 98 | def _validate_resources(self, stack_identifier, expected_count): |
Rabi Mishra | 8bcff8a | 2015-09-21 18:15:04 +0530 | [diff] [blame] | 99 | resources = self.list_group_resources(stack_identifier, |
| 100 | 'random_group') |
Unmesh Gurjar | 0a25a73 | 2014-12-23 17:28:33 +0530 | [diff] [blame] | 101 | self.assertEqual(expected_count, len(resources)) |
| 102 | expected_resources = dict( |
Angus Salkeld | 665d86c | 2015-01-19 22:15:48 +1000 | [diff] [blame] | 103 | (str(idx), 'My::RandomString') |
Unmesh Gurjar | 0a25a73 | 2014-12-23 17:28:33 +0530 | [diff] [blame] | 104 | for idx in range(expected_count)) |
| 105 | |
| 106 | self.assertEqual(expected_resources, resources) |
| 107 | |
| 108 | def test_create(self): |
| 109 | def validate_output(stack, output_key, length): |
| 110 | output_value = self._stack_output(stack, output_key) |
| 111 | self.assertEqual(length, len(output_value)) |
| 112 | return output_value |
| 113 | # verify that the resources in resource group are identically |
| 114 | # configured, resource names and outputs are appropriate. |
Angus Salkeld | 665d86c | 2015-01-19 22:15:48 +1000 | [diff] [blame] | 115 | env = {'resource_registry': |
| 116 | {'My::RandomString': 'OS::Heat::RandomString'}} |
| 117 | create_template = self.template.replace("count: 0", "count: 2") |
| 118 | stack_identifier = self.stack_create(template=create_template, |
| 119 | environment=env) |
Unmesh Gurjar | 0a25a73 | 2014-12-23 17:28:33 +0530 | [diff] [blame] | 120 | self.assertEqual({u'random_group': u'OS::Heat::ResourceGroup'}, |
| 121 | self.list_resources(stack_identifier)) |
| 122 | |
| 123 | # validate count, type and name of resources in a resource group. |
| 124 | self._validate_resources(stack_identifier, 2) |
| 125 | |
| 126 | # validate outputs |
| 127 | stack = self.client.stacks.get(stack_identifier) |
| 128 | outputs = [] |
| 129 | outputs.append(validate_output(stack, 'random1', 30)) |
| 130 | outputs.append(validate_output(stack, 'random2', 30)) |
| 131 | self.assertEqual(outputs, self._stack_output(stack, 'all_values')) |
| 132 | |
| 133 | def test_update_increase_decrease_count(self): |
| 134 | # create stack with resource group count 2 |
Angus Salkeld | 665d86c | 2015-01-19 22:15:48 +1000 | [diff] [blame] | 135 | env = {'resource_registry': |
| 136 | {'My::RandomString': 'OS::Heat::RandomString'}} |
| 137 | create_template = self.template.replace("count: 0", "count: 2") |
| 138 | stack_identifier = self.stack_create(template=create_template, |
| 139 | environment=env) |
Unmesh Gurjar | 0a25a73 | 2014-12-23 17:28:33 +0530 | [diff] [blame] | 140 | self.assertEqual({u'random_group': u'OS::Heat::ResourceGroup'}, |
| 141 | self.list_resources(stack_identifier)) |
| 142 | # verify that the resource group has 2 resources |
| 143 | self._validate_resources(stack_identifier, 2) |
| 144 | |
| 145 | # increase the resource group count to 5 |
Angus Salkeld | 665d86c | 2015-01-19 22:15:48 +1000 | [diff] [blame] | 146 | update_template = self.template.replace("count: 0", "count: 5") |
| 147 | self.update_stack(stack_identifier, update_template, environment=env) |
Unmesh Gurjar | 0a25a73 | 2014-12-23 17:28:33 +0530 | [diff] [blame] | 148 | # verify that the resource group has 5 resources |
| 149 | self._validate_resources(stack_identifier, 5) |
| 150 | |
| 151 | # decrease the resource group count to 3 |
Angus Salkeld | 665d86c | 2015-01-19 22:15:48 +1000 | [diff] [blame] | 152 | update_template = self.template.replace("count: 0", "count: 3") |
| 153 | self.update_stack(stack_identifier, update_template, environment=env) |
Unmesh Gurjar | 0a25a73 | 2014-12-23 17:28:33 +0530 | [diff] [blame] | 154 | # verify that the resource group has 3 resources |
| 155 | self._validate_resources(stack_identifier, 3) |
Angus Salkeld | 011acc7 | 2015-01-16 20:26:34 +1000 | [diff] [blame] | 156 | |
Steven Hardy | 0145b8d | 2015-04-23 14:14:26 +1000 | [diff] [blame] | 157 | def test_update_removal_policies(self): |
| 158 | rp_template = ''' |
| 159 | heat_template_version: 2014-10-16 |
| 160 | resources: |
| 161 | random_group: |
| 162 | type: OS::Heat::ResourceGroup |
| 163 | properties: |
| 164 | count: 5 |
| 165 | removal_policies: [] |
| 166 | resource_def: |
| 167 | type: OS::Heat::RandomString |
| 168 | ''' |
| 169 | |
| 170 | # create stack with resource group, initial count 5 |
| 171 | stack_identifier = self.stack_create(template=rp_template) |
| 172 | self.assertEqual({u'random_group': u'OS::Heat::ResourceGroup'}, |
| 173 | self.list_resources(stack_identifier)) |
Rabi Mishra | 8bcff8a | 2015-09-21 18:15:04 +0530 | [diff] [blame] | 174 | group_resources = self.list_group_resources(stack_identifier, |
| 175 | 'random_group') |
Steven Hardy | 0145b8d | 2015-04-23 14:14:26 +1000 | [diff] [blame] | 176 | expected_resources = {u'0': u'OS::Heat::RandomString', |
| 177 | u'1': u'OS::Heat::RandomString', |
| 178 | u'2': u'OS::Heat::RandomString', |
| 179 | u'3': u'OS::Heat::RandomString', |
| 180 | u'4': u'OS::Heat::RandomString'} |
| 181 | self.assertEqual(expected_resources, group_resources) |
| 182 | |
| 183 | # Remove three, specifying the middle resources to be removed |
| 184 | update_template = rp_template.replace( |
| 185 | 'removal_policies: []', |
| 186 | 'removal_policies: [{resource_list: [\'1\', \'2\', \'3\']}]') |
| 187 | self.update_stack(stack_identifier, update_template) |
Rabi Mishra | 8bcff8a | 2015-09-21 18:15:04 +0530 | [diff] [blame] | 188 | group_resources = self.list_group_resources(stack_identifier, |
| 189 | 'random_group') |
Steven Hardy | 0145b8d | 2015-04-23 14:14:26 +1000 | [diff] [blame] | 190 | expected_resources = {u'0': u'OS::Heat::RandomString', |
| 191 | u'4': u'OS::Heat::RandomString', |
| 192 | u'5': u'OS::Heat::RandomString', |
| 193 | u'6': u'OS::Heat::RandomString', |
| 194 | u'7': u'OS::Heat::RandomString'} |
| 195 | self.assertEqual(expected_resources, group_resources) |
| 196 | |
Angus Salkeld | b61f8f1 | 2015-01-19 23:00:45 +1000 | [diff] [blame] | 197 | def test_props_update(self): |
| 198 | """Test update of resource_def properties behaves as expected.""" |
| 199 | |
| 200 | env = {'resource_registry': |
| 201 | {'My::RandomString': 'OS::Heat::RandomString'}} |
| 202 | template_one = self.template.replace("count: 0", "count: 1") |
| 203 | stack_identifier = self.stack_create(template=template_one, |
| 204 | environment=env) |
| 205 | self.assertEqual({u'random_group': u'OS::Heat::ResourceGroup'}, |
| 206 | self.list_resources(stack_identifier)) |
| 207 | |
Rabi Mishra | 8bcff8a | 2015-09-21 18:15:04 +0530 | [diff] [blame] | 208 | initial_nested_ident = self.group_nested_identifier(stack_identifier, |
| 209 | 'random_group') |
Angus Salkeld | b61f8f1 | 2015-01-19 23:00:45 +1000 | [diff] [blame] | 210 | self.assertEqual({'0': 'My::RandomString'}, |
| 211 | self.list_resources(initial_nested_ident)) |
| 212 | # get the resource id |
| 213 | res = self.client.resources.get(initial_nested_ident, '0') |
| 214 | initial_res_id = res.physical_resource_id |
| 215 | |
| 216 | # change the salt (this should replace the RandomString but |
| 217 | # not the nested stack or resource group. |
| 218 | template_salt = template_one.replace("salt: initial", "salt: more") |
| 219 | self.update_stack(stack_identifier, template_salt, environment=env) |
Rabi Mishra | 8bcff8a | 2015-09-21 18:15:04 +0530 | [diff] [blame] | 220 | updated_nested_ident = self.group_nested_identifier(stack_identifier, |
| 221 | 'random_group') |
Angus Salkeld | b61f8f1 | 2015-01-19 23:00:45 +1000 | [diff] [blame] | 222 | self.assertEqual(initial_nested_ident, updated_nested_ident) |
| 223 | |
| 224 | # compare the resource id, we expect a change. |
| 225 | res = self.client.resources.get(updated_nested_ident, '0') |
| 226 | updated_res_id = res.physical_resource_id |
| 227 | self.assertNotEqual(initial_res_id, updated_res_id) |
| 228 | |
| 229 | def test_update_nochange(self): |
| 230 | """Test update with no properties change.""" |
| 231 | |
| 232 | env = {'resource_registry': |
| 233 | {'My::RandomString': 'OS::Heat::RandomString'}} |
Angus Salkeld | a89a028 | 2015-07-24 15:47:38 +1000 | [diff] [blame] | 234 | template_one = self.template.replace("count: 0", "count: 2") |
Angus Salkeld | b61f8f1 | 2015-01-19 23:00:45 +1000 | [diff] [blame] | 235 | stack_identifier = self.stack_create(template=template_one, |
| 236 | environment=env) |
| 237 | self.assertEqual({u'random_group': u'OS::Heat::ResourceGroup'}, |
| 238 | self.list_resources(stack_identifier)) |
| 239 | |
Rabi Mishra | 8bcff8a | 2015-09-21 18:15:04 +0530 | [diff] [blame] | 240 | initial_nested_ident = self.group_nested_identifier(stack_identifier, |
| 241 | 'random_group') |
Angus Salkeld | a89a028 | 2015-07-24 15:47:38 +1000 | [diff] [blame] | 242 | self.assertEqual({'0': 'My::RandomString', '1': 'My::RandomString'}, |
Angus Salkeld | b61f8f1 | 2015-01-19 23:00:45 +1000 | [diff] [blame] | 243 | self.list_resources(initial_nested_ident)) |
| 244 | # get the output |
| 245 | stack0 = self.client.stacks.get(stack_identifier) |
| 246 | initial_rand = self._stack_output(stack0, 'random1') |
| 247 | |
| 248 | template_copy = copy.deepcopy(template_one) |
| 249 | self.update_stack(stack_identifier, template_copy, environment=env) |
Rabi Mishra | 8bcff8a | 2015-09-21 18:15:04 +0530 | [diff] [blame] | 250 | updated_nested_ident = self.group_nested_identifier(stack_identifier, |
| 251 | 'random_group') |
Angus Salkeld | b61f8f1 | 2015-01-19 23:00:45 +1000 | [diff] [blame] | 252 | self.assertEqual(initial_nested_ident, updated_nested_ident) |
| 253 | |
| 254 | # compare the random number, we expect no change. |
| 255 | stack1 = self.client.stacks.get(stack_identifier) |
| 256 | updated_rand = self._stack_output(stack1, 'random1') |
| 257 | self.assertEqual(initial_rand, updated_rand) |
| 258 | |
| 259 | def test_update_nochange_resource_needs_update(self): |
Peter Razumovsky | f0ac958 | 2015-09-24 16:49:03 +0300 | [diff] [blame] | 260 | """Test update when the resource definition has changed. |
| 261 | |
| 262 | Test the scenario when the ResourceGroup update happens without |
| 263 | any changed properties, this can happen if the definition of |
| 264 | a contained provider resource changes (files map changes), then |
| 265 | the group and underlying nested stack should end up updated. |
| 266 | """ |
Angus Salkeld | b61f8f1 | 2015-01-19 23:00:45 +1000 | [diff] [blame] | 267 | |
| 268 | random_templ1 = ''' |
| 269 | heat_template_version: 2013-05-23 |
| 270 | parameters: |
| 271 | length: |
| 272 | type: string |
| 273 | default: not-used |
| 274 | salt: |
| 275 | type: string |
| 276 | default: not-used |
| 277 | resources: |
| 278 | random1: |
| 279 | type: OS::Heat::RandomString |
| 280 | properties: |
| 281 | salt: initial |
| 282 | outputs: |
| 283 | value: |
| 284 | value: {get_attr: [random1, value]} |
| 285 | ''' |
| 286 | files1 = {'my_random.yaml': random_templ1} |
| 287 | |
| 288 | random_templ2 = random_templ1.replace('salt: initial', |
| 289 | 'salt: more') |
| 290 | files2 = {'my_random.yaml': random_templ2} |
| 291 | |
| 292 | env = {'resource_registry': |
| 293 | {'My::RandomString': 'my_random.yaml'}} |
| 294 | |
Angus Salkeld | a89a028 | 2015-07-24 15:47:38 +1000 | [diff] [blame] | 295 | template_one = self.template.replace("count: 0", "count: 2") |
Angus Salkeld | b61f8f1 | 2015-01-19 23:00:45 +1000 | [diff] [blame] | 296 | stack_identifier = self.stack_create(template=template_one, |
| 297 | environment=env, |
| 298 | files=files1) |
| 299 | self.assertEqual({u'random_group': u'OS::Heat::ResourceGroup'}, |
| 300 | self.list_resources(stack_identifier)) |
Steven Hardy | 78d0993 | 2016-07-12 14:04:06 +0100 | [diff] [blame] | 301 | self.assertEqual(files1, self.client.stacks.files(stack_identifier)) |
Angus Salkeld | b61f8f1 | 2015-01-19 23:00:45 +1000 | [diff] [blame] | 302 | |
Rabi Mishra | 8bcff8a | 2015-09-21 18:15:04 +0530 | [diff] [blame] | 303 | initial_nested_ident = self.group_nested_identifier(stack_identifier, |
| 304 | 'random_group') |
Angus Salkeld | a89a028 | 2015-07-24 15:47:38 +1000 | [diff] [blame] | 305 | self.assertEqual({'0': 'My::RandomString', '1': 'My::RandomString'}, |
Angus Salkeld | b61f8f1 | 2015-01-19 23:00:45 +1000 | [diff] [blame] | 306 | self.list_resources(initial_nested_ident)) |
| 307 | # get the output |
| 308 | stack0 = self.client.stacks.get(stack_identifier) |
| 309 | initial_rand = self._stack_output(stack0, 'random1') |
| 310 | |
| 311 | # change the environment so we use a different TemplateResource. |
| 312 | # note "files2". |
| 313 | self.update_stack(stack_identifier, template_one, |
| 314 | environment=env, files=files2) |
Rabi Mishra | 8bcff8a | 2015-09-21 18:15:04 +0530 | [diff] [blame] | 315 | updated_nested_ident = self.group_nested_identifier(stack_identifier, |
| 316 | 'random_group') |
Angus Salkeld | b61f8f1 | 2015-01-19 23:00:45 +1000 | [diff] [blame] | 317 | self.assertEqual(initial_nested_ident, updated_nested_ident) |
Steven Hardy | 78d0993 | 2016-07-12 14:04:06 +0100 | [diff] [blame] | 318 | self.assertEqual(files2, self.client.stacks.files(stack_identifier)) |
Angus Salkeld | b61f8f1 | 2015-01-19 23:00:45 +1000 | [diff] [blame] | 319 | |
| 320 | # compare the output, we expect a change. |
| 321 | stack1 = self.client.stacks.get(stack_identifier) |
| 322 | updated_rand = self._stack_output(stack1, 'random1') |
| 323 | self.assertNotEqual(initial_rand, updated_rand) |
| 324 | |
Angus Salkeld | 011acc7 | 2015-01-16 20:26:34 +1000 | [diff] [blame] | 325 | |
Rabi Mishra | 477efc9 | 2015-07-31 13:01:45 +0530 | [diff] [blame] | 326 | class ResourceGroupTestNullParams(functional_base.FunctionalTestsBase): |
Angus Salkeld | 59b8f41 | 2015-02-25 21:01:12 +1000 | [diff] [blame] | 327 | template = ''' |
| 328 | heat_template_version: 2013-05-23 |
| 329 | parameters: |
| 330 | param: |
| 331 | type: empty |
| 332 | resources: |
| 333 | random_group: |
| 334 | type: OS::Heat::ResourceGroup |
| 335 | properties: |
| 336 | count: 1 |
| 337 | resource_def: |
| 338 | type: My::RandomString |
| 339 | properties: |
| 340 | param: {get_param: param} |
| 341 | outputs: |
| 342 | val: |
| 343 | value: {get_attr: [random_group, val]} |
| 344 | ''' |
| 345 | |
| 346 | nested_template_file = ''' |
| 347 | heat_template_version: 2013-05-23 |
| 348 | parameters: |
| 349 | param: |
| 350 | type: empty |
| 351 | outputs: |
| 352 | val: |
| 353 | value: {get_param: param} |
| 354 | ''' |
| 355 | |
| 356 | scenarios = [ |
| 357 | ('string_empty', dict( |
| 358 | param='', |
| 359 | p_type='string', |
| 360 | )), |
| 361 | ('boolean_false', dict( |
| 362 | param=False, |
| 363 | p_type='boolean', |
| 364 | )), |
| 365 | ('number_zero', dict( |
| 366 | param=0, |
| 367 | p_type='number', |
| 368 | )), |
| 369 | ('comma_delimited_list', dict( |
| 370 | param=[], |
| 371 | p_type='comma_delimited_list', |
| 372 | )), |
| 373 | ('json_empty', dict( |
| 374 | param={}, |
| 375 | p_type='json', |
| 376 | )), |
| 377 | ] |
| 378 | |
Angus Salkeld | 59b8f41 | 2015-02-25 21:01:12 +1000 | [diff] [blame] | 379 | def test_create_pass_zero_parameter(self): |
| 380 | templ = self.template.replace('type: empty', |
| 381 | 'type: %s' % self.p_type) |
| 382 | n_t_f = self.nested_template_file.replace('type: empty', |
| 383 | 'type: %s' % self.p_type) |
| 384 | files = {'provider.yaml': n_t_f} |
| 385 | env = {'resource_registry': |
| 386 | {'My::RandomString': 'provider.yaml'}} |
| 387 | stack_identifier = self.stack_create( |
| 388 | template=templ, |
| 389 | files=files, |
| 390 | environment=env, |
| 391 | parameters={'param': self.param} |
| 392 | ) |
| 393 | stack = self.client.stacks.get(stack_identifier) |
| 394 | self.assertEqual(self.param, self._stack_output(stack, 'val')[0]) |
| 395 | |
| 396 | |
Rabi Mishra | 477efc9 | 2015-07-31 13:01:45 +0530 | [diff] [blame] | 397 | class ResourceGroupAdoptTest(functional_base.FunctionalTestsBase): |
Angus Salkeld | 011acc7 | 2015-01-16 20:26:34 +1000 | [diff] [blame] | 398 | """Prove that we can do resource group adopt.""" |
| 399 | |
| 400 | main_template = ''' |
| 401 | heat_template_version: "2013-05-23" |
| 402 | resources: |
| 403 | group1: |
| 404 | type: OS::Heat::ResourceGroup |
| 405 | properties: |
| 406 | count: 2 |
| 407 | resource_def: |
| 408 | type: OS::Heat::RandomString |
| 409 | outputs: |
| 410 | test0: |
| 411 | value: {get_attr: [group1, resource.0.value]} |
| 412 | test1: |
| 413 | value: {get_attr: [group1, resource.1.value]} |
| 414 | ''' |
| 415 | |
Angus Salkeld | 011acc7 | 2015-01-16 20:26:34 +1000 | [diff] [blame] | 416 | def _yaml_to_json(self, yaml_templ): |
Bo Wang | d8df4dd | 2016-02-16 21:23:53 +0800 | [diff] [blame] | 417 | return yaml.safe_load(yaml_templ) |
Angus Salkeld | 011acc7 | 2015-01-16 20:26:34 +1000 | [diff] [blame] | 418 | |
| 419 | def test_adopt(self): |
| 420 | data = { |
| 421 | "resources": { |
| 422 | "group1": { |
| 423 | "status": "COMPLETE", |
| 424 | "name": "group1", |
| 425 | "resource_data": {}, |
| 426 | "metadata": {}, |
| 427 | "resource_id": "test-group1-id", |
| 428 | "action": "CREATE", |
| 429 | "type": "OS::Heat::ResourceGroup", |
| 430 | "resources": { |
| 431 | "0": { |
| 432 | "status": "COMPLETE", |
| 433 | "name": "0", |
| 434 | "resource_data": {"value": "goopie"}, |
| 435 | "resource_id": "ID-0", |
| 436 | "action": "CREATE", |
| 437 | "type": "OS::Heat::RandomString", |
| 438 | "metadata": {} |
| 439 | }, |
| 440 | "1": { |
| 441 | "status": "COMPLETE", |
| 442 | "name": "1", |
| 443 | "resource_data": {"value": "different"}, |
| 444 | "resource_id": "ID-1", |
| 445 | "action": "CREATE", |
| 446 | "type": "OS::Heat::RandomString", |
| 447 | "metadata": {} |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | }, |
| 452 | "environment": {"parameters": {}}, |
Bo Wang | d8df4dd | 2016-02-16 21:23:53 +0800 | [diff] [blame] | 453 | "template": yaml.safe_load(self.main_template) |
Angus Salkeld | 011acc7 | 2015-01-16 20:26:34 +1000 | [diff] [blame] | 454 | } |
| 455 | stack_identifier = self.stack_adopt( |
| 456 | adopt_data=json.dumps(data)) |
| 457 | |
| 458 | self.assert_resource_is_a_stack(stack_identifier, 'group1') |
| 459 | stack = self.client.stacks.get(stack_identifier) |
| 460 | self.assertEqual('goopie', self._stack_output(stack, 'test0')) |
| 461 | self.assertEqual('different', self._stack_output(stack, 'test1')) |
Steve Baker | 75ee9d1 | 2015-07-22 10:56:55 +1200 | [diff] [blame] | 462 | |
| 463 | |
Rabi Mishra | 477efc9 | 2015-07-31 13:01:45 +0530 | [diff] [blame] | 464 | class ResourceGroupErrorResourceTest(functional_base.FunctionalTestsBase): |
Steve Baker | 75ee9d1 | 2015-07-22 10:56:55 +1200 | [diff] [blame] | 465 | template = ''' |
| 466 | heat_template_version: "2013-05-23" |
| 467 | resources: |
| 468 | group1: |
| 469 | type: OS::Heat::ResourceGroup |
| 470 | properties: |
| 471 | count: 2 |
| 472 | resource_def: |
| 473 | type: fail.yaml |
| 474 | ''' |
| 475 | nested_templ = ''' |
| 476 | heat_template_version: "2013-05-23" |
| 477 | resources: |
| 478 | oops: |
| 479 | type: OS::Heat::TestResource |
| 480 | properties: |
| 481 | fail: true |
| 482 | wait_secs: 2 |
| 483 | ''' |
| 484 | |
Steve Baker | 75ee9d1 | 2015-07-22 10:56:55 +1200 | [diff] [blame] | 485 | def test_fail(self): |
| 486 | stack_identifier = self.stack_create( |
| 487 | template=self.template, |
| 488 | files={'fail.yaml': self.nested_templ}, |
| 489 | expected_status='CREATE_FAILED', |
| 490 | enable_cleanup=False) |
| 491 | stack = self.client.stacks.get(stack_identifier) |
| 492 | |
| 493 | self.assertEqual('CREATE_FAILED', stack.stack_status) |
| 494 | self.client.stacks.delete(stack_identifier) |
| 495 | self._wait_for_stack_status( |
| 496 | stack_identifier, 'DELETE_COMPLETE', |
| 497 | success_on_not_found=True) |
Rabi Mishra | 8bcff8a | 2015-09-21 18:15:04 +0530 | [diff] [blame] | 498 | |
| 499 | |
| 500 | class ResourceGroupUpdatePolicyTest(functional_base.FunctionalTestsBase): |
| 501 | |
| 502 | template = ''' |
| 503 | heat_template_version: '2015-04-30' |
| 504 | resources: |
| 505 | random_group: |
| 506 | type: OS::Heat::ResourceGroup |
| 507 | update_policy: |
| 508 | rolling_update: |
| 509 | min_in_service: 1 |
| 510 | max_batch_size: 2 |
| 511 | pause_time: 1 |
| 512 | properties: |
| 513 | count: 10 |
| 514 | resource_def: |
| 515 | type: OS::Heat::TestResource |
| 516 | properties: |
| 517 | value: initial |
| 518 | update_replace: False |
| 519 | ''' |
| 520 | |
| 521 | def update_resource_group(self, update_template, |
| 522 | updated, created, deleted): |
| 523 | stack_identifier = self.stack_create(template=self.template) |
| 524 | group_resources = self.list_group_resources(stack_identifier, |
| 525 | 'random_group', |
| 526 | minimal=False) |
| 527 | |
| 528 | init_names = [res.physical_resource_id for res in group_resources] |
| 529 | |
| 530 | self.update_stack(stack_identifier, update_template) |
| 531 | group_resources = self.list_group_resources(stack_identifier, |
| 532 | 'random_group', |
| 533 | minimal=False) |
| 534 | |
| 535 | updt_names = [res.physical_resource_id for res in group_resources] |
| 536 | |
| 537 | matched_names = set(updt_names) & set(init_names) |
| 538 | |
| 539 | self.assertEqual(updated, len(matched_names)) |
| 540 | |
| 541 | self.assertEqual(created, len(set(updt_names) - set(init_names))) |
| 542 | |
| 543 | self.assertEqual(deleted, len(set(init_names) - set(updt_names))) |
| 544 | |
| 545 | def test_resource_group_update(self): |
| 546 | """Test rolling update with no conflict. |
| 547 | |
| 548 | Simple rolling update with no conflict in batch size |
| 549 | and minimum instances in service. |
| 550 | """ |
Bo Wang | d8df4dd | 2016-02-16 21:23:53 +0800 | [diff] [blame] | 551 | updt_template = yaml.safe_load(copy.deepcopy(self.template)) |
Rabi Mishra | 8bcff8a | 2015-09-21 18:15:04 +0530 | [diff] [blame] | 552 | grp = updt_template['resources']['random_group'] |
| 553 | policy = grp['update_policy']['rolling_update'] |
| 554 | policy['min_in_service'] = '1' |
| 555 | policy['max_batch_size'] = '3' |
| 556 | res_def = grp['properties']['resource_def'] |
| 557 | res_def['properties']['value'] = 'updated' |
| 558 | |
| 559 | self.update_resource_group(updt_template, |
| 560 | updated=10, |
| 561 | created=0, |
| 562 | deleted=0) |
| 563 | |
| 564 | def test_resource_group_update_replace(self): |
| 565 | """Test rolling update(replace)with no conflict. |
| 566 | |
| 567 | Simple rolling update replace with no conflict in batch size |
| 568 | and minimum instances in service. |
| 569 | """ |
Bo Wang | d8df4dd | 2016-02-16 21:23:53 +0800 | [diff] [blame] | 570 | updt_template = yaml.safe_load(copy.deepcopy(self.template)) |
Rabi Mishra | 8bcff8a | 2015-09-21 18:15:04 +0530 | [diff] [blame] | 571 | grp = updt_template['resources']['random_group'] |
| 572 | policy = grp['update_policy']['rolling_update'] |
| 573 | policy['min_in_service'] = '1' |
| 574 | policy['max_batch_size'] = '3' |
| 575 | res_def = grp['properties']['resource_def'] |
| 576 | res_def['properties']['value'] = 'updated' |
| 577 | res_def['properties']['update_replace'] = True |
| 578 | |
| 579 | self.update_resource_group(updt_template, |
| 580 | updated=0, |
| 581 | created=10, |
| 582 | deleted=10) |
| 583 | |
| 584 | def test_resource_group_update_scaledown(self): |
| 585 | """Test rolling update with scaledown. |
| 586 | |
| 587 | Simple rolling update with reduced size. |
| 588 | """ |
Bo Wang | d8df4dd | 2016-02-16 21:23:53 +0800 | [diff] [blame] | 589 | updt_template = yaml.safe_load(copy.deepcopy(self.template)) |
Rabi Mishra | 8bcff8a | 2015-09-21 18:15:04 +0530 | [diff] [blame] | 590 | grp = updt_template['resources']['random_group'] |
| 591 | policy = grp['update_policy']['rolling_update'] |
| 592 | policy['min_in_service'] = '1' |
| 593 | policy['max_batch_size'] = '3' |
| 594 | grp['properties']['count'] = 6 |
| 595 | res_def = grp['properties']['resource_def'] |
| 596 | res_def['properties']['value'] = 'updated' |
| 597 | |
| 598 | self.update_resource_group(updt_template, |
| 599 | updated=6, |
| 600 | created=0, |
| 601 | deleted=4) |
| 602 | |
| 603 | def test_resource_group_update_scaleup(self): |
| 604 | """Test rolling update with scaleup. |
| 605 | |
| 606 | Simple rolling update with increased size. |
| 607 | """ |
Bo Wang | d8df4dd | 2016-02-16 21:23:53 +0800 | [diff] [blame] | 608 | updt_template = yaml.safe_load(copy.deepcopy(self.template)) |
Rabi Mishra | 8bcff8a | 2015-09-21 18:15:04 +0530 | [diff] [blame] | 609 | grp = updt_template['resources']['random_group'] |
| 610 | policy = grp['update_policy']['rolling_update'] |
| 611 | policy['min_in_service'] = '1' |
| 612 | policy['max_batch_size'] = '3' |
| 613 | grp['properties']['count'] = 12 |
| 614 | res_def = grp['properties']['resource_def'] |
| 615 | res_def['properties']['value'] = 'updated' |
| 616 | |
| 617 | self.update_resource_group(updt_template, |
| 618 | updated=10, |
| 619 | created=2, |
| 620 | deleted=0) |
| 621 | |
| 622 | def test_resource_group_update_adjusted(self): |
| 623 | """Test rolling update with enough available resources |
| 624 | |
| 625 | Update with capacity adjustment with enough resources. |
| 626 | """ |
Bo Wang | d8df4dd | 2016-02-16 21:23:53 +0800 | [diff] [blame] | 627 | updt_template = yaml.safe_load(copy.deepcopy(self.template)) |
Rabi Mishra | 8bcff8a | 2015-09-21 18:15:04 +0530 | [diff] [blame] | 628 | grp = updt_template['resources']['random_group'] |
| 629 | policy = grp['update_policy']['rolling_update'] |
| 630 | policy['min_in_service'] = '8' |
| 631 | policy['max_batch_size'] = '4' |
| 632 | grp['properties']['count'] = 6 |
| 633 | res_def = grp['properties']['resource_def'] |
| 634 | res_def['properties']['value'] = 'updated' |
| 635 | |
| 636 | self.update_resource_group(updt_template, |
| 637 | updated=6, |
| 638 | created=0, |
| 639 | deleted=4) |
| 640 | |
| 641 | def test_resource_group_update_with_adjusted_capacity(self): |
| 642 | """Test rolling update with capacity adjustment. |
| 643 | |
| 644 | Rolling update with capacity adjustment due to conflict in |
| 645 | batch size and minimum instances in service. |
| 646 | """ |
Bo Wang | d8df4dd | 2016-02-16 21:23:53 +0800 | [diff] [blame] | 647 | updt_template = yaml.safe_load(copy.deepcopy(self.template)) |
Rabi Mishra | 8bcff8a | 2015-09-21 18:15:04 +0530 | [diff] [blame] | 648 | grp = updt_template['resources']['random_group'] |
| 649 | policy = grp['update_policy']['rolling_update'] |
| 650 | policy['min_in_service'] = '8' |
| 651 | policy['max_batch_size'] = '4' |
| 652 | res_def = grp['properties']['resource_def'] |
| 653 | res_def['properties']['value'] = 'updated' |
| 654 | |
| 655 | self.update_resource_group(updt_template, |
| 656 | updated=10, |
| 657 | created=0, |
| 658 | deleted=0) |
| 659 | |
| 660 | def test_resource_group_update_huge_batch_size(self): |
| 661 | """Test rolling update with huge batch size. |
| 662 | |
| 663 | Rolling Update with a huge batch size(more than |
| 664 | current size). |
| 665 | """ |
Bo Wang | d8df4dd | 2016-02-16 21:23:53 +0800 | [diff] [blame] | 666 | updt_template = yaml.safe_load(copy.deepcopy(self.template)) |
Rabi Mishra | 8bcff8a | 2015-09-21 18:15:04 +0530 | [diff] [blame] | 667 | grp = updt_template['resources']['random_group'] |
| 668 | policy = grp['update_policy']['rolling_update'] |
| 669 | policy['min_in_service'] = '0' |
| 670 | policy['max_batch_size'] = '20' |
| 671 | res_def = grp['properties']['resource_def'] |
| 672 | res_def['properties']['value'] = 'updated' |
| 673 | self.update_resource_group(updt_template, |
| 674 | updated=10, |
| 675 | created=0, |
| 676 | deleted=0) |
| 677 | |
| 678 | def test_resource_group_update_huge_min_in_service(self): |
| 679 | """Test rolling update with huge minimum capacity. |
| 680 | |
| 681 | Rolling Update with a huge number of minimum instances |
| 682 | in service. |
| 683 | """ |
Bo Wang | d8df4dd | 2016-02-16 21:23:53 +0800 | [diff] [blame] | 684 | updt_template = yaml.safe_load(copy.deepcopy(self.template)) |
Rabi Mishra | 8bcff8a | 2015-09-21 18:15:04 +0530 | [diff] [blame] | 685 | grp = updt_template['resources']['random_group'] |
| 686 | policy = grp['update_policy']['rolling_update'] |
| 687 | policy['min_in_service'] = '20' |
| 688 | policy['max_batch_size'] = '1' |
| 689 | res_def = grp['properties']['resource_def'] |
| 690 | res_def['properties']['value'] = 'updated' |
| 691 | |
| 692 | self.update_resource_group(updt_template, |
| 693 | updated=10, |
| 694 | created=0, |
| 695 | deleted=0) |