Steve Baker | f547ec3 | 2014-10-20 14:34:41 +1300 | [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 | |
Steve Baker | f547ec3 | 2014-10-20 14:34:41 +1300 | [diff] [blame] | 13 | |
| 14 | from heat_integrationtests.common import test |
| 15 | |
| 16 | |
Steve Baker | f547ec3 | 2014-10-20 14:34:41 +1300 | [diff] [blame] | 17 | class UpdateStackTest(test.HeatIntegrationTest): |
| 18 | |
| 19 | template = ''' |
| 20 | heat_template_version: 2013-05-23 |
| 21 | resources: |
| 22 | random1: |
| 23 | type: OS::Heat::RandomString |
| 24 | ''' |
| 25 | update_template = ''' |
| 26 | heat_template_version: 2013-05-23 |
| 27 | resources: |
| 28 | random1: |
| 29 | type: OS::Heat::RandomString |
| 30 | random2: |
| 31 | type: OS::Heat::RandomString |
| 32 | ''' |
| 33 | |
Steven Hardy | be1ce2d | 2014-11-18 12:54:50 +0000 | [diff] [blame] | 34 | provider_template = ''' |
| 35 | heat_template_version: 2013-05-23 |
| 36 | resources: |
| 37 | random1: |
| 38 | type: My::RandomString |
| 39 | ''' |
| 40 | |
Steven Hardy | 417097e | 2014-11-18 16:30:35 +0000 | [diff] [blame] | 41 | provider_group_template = ''' |
| 42 | heat_template_version: 2013-05-23 |
| 43 | resources: |
| 44 | random_group: |
| 45 | type: OS::Heat::ResourceGroup |
| 46 | properties: |
| 47 | count: 2 |
| 48 | resource_def: |
| 49 | type: My::RandomString |
| 50 | ''' |
| 51 | |
Steve Baker | f547ec3 | 2014-10-20 14:34:41 +1300 | [diff] [blame] | 52 | def setUp(self): |
| 53 | super(UpdateStackTest, self).setUp() |
| 54 | self.client = self.orchestration_client |
| 55 | |
Steve Baker | f547ec3 | 2014-10-20 14:34:41 +1300 | [diff] [blame] | 56 | def test_stack_update_nochange(self): |
Steven Hardy | f2c82c0 | 2014-11-20 14:02:17 +0000 | [diff] [blame] | 57 | stack_identifier = self.stack_create() |
Steve Baker | f547ec3 | 2014-10-20 14:34:41 +1300 | [diff] [blame] | 58 | expected_resources = {'random1': 'OS::Heat::RandomString'} |
| 59 | self.assertEqual(expected_resources, |
| 60 | self.list_resources(stack_identifier)) |
| 61 | |
| 62 | # Update with no changes, resources should be unchanged |
| 63 | self.update_stack(stack_identifier, self.template) |
| 64 | self.assertEqual(expected_resources, |
| 65 | self.list_resources(stack_identifier)) |
| 66 | |
| 67 | def test_stack_update_add_remove(self): |
Steven Hardy | f2c82c0 | 2014-11-20 14:02:17 +0000 | [diff] [blame] | 68 | stack_identifier = self.stack_create() |
Steve Baker | f547ec3 | 2014-10-20 14:34:41 +1300 | [diff] [blame] | 69 | initial_resources = {'random1': 'OS::Heat::RandomString'} |
| 70 | self.assertEqual(initial_resources, |
| 71 | self.list_resources(stack_identifier)) |
| 72 | |
| 73 | # Add one resource via a stack update |
| 74 | self.update_stack(stack_identifier, self.update_template) |
Steve Baker | f547ec3 | 2014-10-20 14:34:41 +1300 | [diff] [blame] | 75 | updated_resources = {'random1': 'OS::Heat::RandomString', |
| 76 | 'random2': 'OS::Heat::RandomString'} |
| 77 | self.assertEqual(updated_resources, |
| 78 | self.list_resources(stack_identifier)) |
| 79 | |
| 80 | # Then remove it by updating with the original template |
| 81 | self.update_stack(stack_identifier, self.template) |
| 82 | self.assertEqual(initial_resources, |
| 83 | self.list_resources(stack_identifier)) |
Steven Hardy | be1ce2d | 2014-11-18 12:54:50 +0000 | [diff] [blame] | 84 | |
| 85 | def test_stack_update_provider(self): |
Steven Hardy | be1ce2d | 2014-11-18 12:54:50 +0000 | [diff] [blame] | 86 | files = {'provider.yaml': self.template} |
| 87 | env = {'resource_registry': |
| 88 | {'My::RandomString': 'provider.yaml'}} |
Steven Hardy | f2c82c0 | 2014-11-20 14:02:17 +0000 | [diff] [blame] | 89 | stack_identifier = self.stack_create( |
Steven Hardy | be1ce2d | 2014-11-18 12:54:50 +0000 | [diff] [blame] | 90 | template=self.provider_template, |
| 91 | files=files, |
Steven Hardy | be1ce2d | 2014-11-18 12:54:50 +0000 | [diff] [blame] | 92 | environment=env |
| 93 | ) |
Steven Hardy | be1ce2d | 2014-11-18 12:54:50 +0000 | [diff] [blame] | 94 | |
Steven Hardy | be1ce2d | 2014-11-18 12:54:50 +0000 | [diff] [blame] | 95 | initial_resources = {'random1': 'My::RandomString'} |
| 96 | self.assertEqual(initial_resources, |
| 97 | self.list_resources(stack_identifier)) |
| 98 | |
| 99 | # Prove the resource is backed by a nested stack, save the ID |
Angus Salkeld | 2bd63a4 | 2015-01-07 11:11:29 +1000 | [diff] [blame] | 100 | nested_identifier = self.assert_resource_is_a_stack(stack_identifier, |
| 101 | 'random1') |
| 102 | nested_id = nested_identifier.split('/')[-1] |
Steven Hardy | be1ce2d | 2014-11-18 12:54:50 +0000 | [diff] [blame] | 103 | |
| 104 | # Then check the expected resources are in the nested stack |
| 105 | nested_resources = {'random1': 'OS::Heat::RandomString'} |
| 106 | self.assertEqual(nested_resources, |
| 107 | self.list_resources(nested_identifier)) |
| 108 | |
| 109 | # Add one resource via a stack update by changing the nested stack |
| 110 | files['provider.yaml'] = self.update_template |
| 111 | self.update_stack(stack_identifier, self.provider_template, |
| 112 | environment=env, files=files) |
Steven Hardy | be1ce2d | 2014-11-18 12:54:50 +0000 | [diff] [blame] | 113 | |
| 114 | # Parent resources should be unchanged and the nested stack |
| 115 | # should have been updated in-place without replacement |
| 116 | self.assertEqual(initial_resources, |
| 117 | self.list_resources(stack_identifier)) |
| 118 | rsrc = self.client.resources.get(stack_identifier, 'random1') |
| 119 | self.assertEqual(rsrc.physical_resource_id, nested_id) |
| 120 | |
| 121 | # Then check the expected resources are in the nested stack |
| 122 | nested_resources = {'random1': 'OS::Heat::RandomString', |
| 123 | 'random2': 'OS::Heat::RandomString'} |
| 124 | self.assertEqual(nested_resources, |
| 125 | self.list_resources(nested_identifier)) |
Steven Hardy | 417097e | 2014-11-18 16:30:35 +0000 | [diff] [blame] | 126 | |
| 127 | def test_stack_update_provider_group(self): |
| 128 | '''Test two-level nested update.''' |
| 129 | # Create a ResourceGroup (which creates a nested stack), |
| 130 | # containing provider resources (which create a nested |
| 131 | # stack), thus excercising an update which traverses |
| 132 | # two levels of nesting. |
Steven Hardy | 417097e | 2014-11-18 16:30:35 +0000 | [diff] [blame] | 133 | files = {'provider.yaml': self.template} |
| 134 | env = {'resource_registry': |
| 135 | {'My::RandomString': 'provider.yaml'}} |
| 136 | |
Steven Hardy | f2c82c0 | 2014-11-20 14:02:17 +0000 | [diff] [blame] | 137 | stack_identifier = self.stack_create( |
Steven Hardy | 417097e | 2014-11-18 16:30:35 +0000 | [diff] [blame] | 138 | template=self.provider_group_template, |
| 139 | files=files, |
Steven Hardy | 417097e | 2014-11-18 16:30:35 +0000 | [diff] [blame] | 140 | environment=env |
| 141 | ) |
Steven Hardy | 417097e | 2014-11-18 16:30:35 +0000 | [diff] [blame] | 142 | |
Steven Hardy | 417097e | 2014-11-18 16:30:35 +0000 | [diff] [blame] | 143 | initial_resources = {'random_group': 'OS::Heat::ResourceGroup'} |
| 144 | self.assertEqual(initial_resources, |
| 145 | self.list_resources(stack_identifier)) |
| 146 | |
| 147 | # Prove the resource is backed by a nested stack, save the ID |
Angus Salkeld | 2bd63a4 | 2015-01-07 11:11:29 +1000 | [diff] [blame] | 148 | nested_identifier = self.assert_resource_is_a_stack(stack_identifier, |
| 149 | 'random_group') |
Steven Hardy | 417097e | 2014-11-18 16:30:35 +0000 | [diff] [blame] | 150 | |
| 151 | # Then check the expected resources are in the nested stack |
| 152 | nested_resources = {'0': 'My::RandomString', |
| 153 | '1': 'My::RandomString'} |
| 154 | self.assertEqual(nested_resources, |
| 155 | self.list_resources(nested_identifier)) |
| 156 | |
| 157 | for n_rsrc in nested_resources: |
| 158 | rsrc = self.client.resources.get(nested_identifier, n_rsrc) |
| 159 | provider_stack = self.client.stacks.get(rsrc.physical_resource_id) |
| 160 | provider_identifier = '%s/%s' % (provider_stack.stack_name, |
| 161 | provider_stack.id) |
| 162 | provider_resources = {u'random1': u'OS::Heat::RandomString'} |
| 163 | self.assertEqual(provider_resources, |
| 164 | self.list_resources(provider_identifier)) |
| 165 | |
| 166 | # Add one resource via a stack update by changing the nested stack |
| 167 | files['provider.yaml'] = self.update_template |
| 168 | self.update_stack(stack_identifier, self.provider_group_template, |
| 169 | environment=env, files=files) |
Steven Hardy | 417097e | 2014-11-18 16:30:35 +0000 | [diff] [blame] | 170 | |
| 171 | # Parent resources should be unchanged and the nested stack |
| 172 | # should have been updated in-place without replacement |
| 173 | self.assertEqual(initial_resources, |
| 174 | self.list_resources(stack_identifier)) |
| 175 | |
| 176 | # Resource group stack should also be unchanged (but updated) |
| 177 | nested_stack = self.client.stacks.get(nested_identifier) |
| 178 | self.assertEqual('UPDATE_COMPLETE', nested_stack.stack_status) |
| 179 | self.assertEqual(nested_resources, |
| 180 | self.list_resources(nested_identifier)) |
| 181 | |
| 182 | for n_rsrc in nested_resources: |
| 183 | rsrc = self.client.resources.get(nested_identifier, n_rsrc) |
| 184 | provider_stack = self.client.stacks.get(rsrc.physical_resource_id) |
| 185 | provider_identifier = '%s/%s' % (provider_stack.stack_name, |
| 186 | provider_stack.id) |
| 187 | provider_resources = {'random1': 'OS::Heat::RandomString', |
| 188 | 'random2': 'OS::Heat::RandomString'} |
| 189 | self.assertEqual(provider_resources, |
| 190 | self.list_resources(provider_identifier)) |