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