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