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 | |
| 13 | import logging |
| 14 | |
| 15 | from heat_integrationtests.common import test |
| 16 | |
| 17 | |
| 18 | LOG = logging.getLogger(__name__) |
| 19 | |
| 20 | |
| 21 | class UpdateStackTest(test.HeatIntegrationTest): |
| 22 | |
| 23 | template = ''' |
| 24 | heat_template_version: 2013-05-23 |
| 25 | resources: |
| 26 | random1: |
| 27 | type: OS::Heat::RandomString |
| 28 | ''' |
| 29 | update_template = ''' |
| 30 | heat_template_version: 2013-05-23 |
| 31 | resources: |
| 32 | random1: |
| 33 | type: OS::Heat::RandomString |
| 34 | random2: |
| 35 | type: OS::Heat::RandomString |
| 36 | ''' |
| 37 | |
Steven Hardy | be1ce2d | 2014-11-18 12:54:50 +0000 | [diff] [blame^] | 38 | provider_template = ''' |
| 39 | heat_template_version: 2013-05-23 |
| 40 | resources: |
| 41 | random1: |
| 42 | type: My::RandomString |
| 43 | ''' |
| 44 | |
Steve Baker | f547ec3 | 2014-10-20 14:34:41 +1300 | [diff] [blame] | 45 | def setUp(self): |
| 46 | super(UpdateStackTest, self).setUp() |
| 47 | self.client = self.orchestration_client |
| 48 | |
Steven Hardy | be1ce2d | 2014-11-18 12:54:50 +0000 | [diff] [blame^] | 49 | def update_stack(self, stack_identifier, template, environment=None, |
| 50 | files=None): |
| 51 | env = environment or {} |
| 52 | env_files = files or {} |
Steve Baker | f547ec3 | 2014-10-20 14:34:41 +1300 | [diff] [blame] | 53 | stack_name = stack_identifier.split('/')[0] |
| 54 | self.client.stacks.update( |
| 55 | stack_id=stack_identifier, |
| 56 | stack_name=stack_name, |
| 57 | template=template, |
Steven Hardy | be1ce2d | 2014-11-18 12:54:50 +0000 | [diff] [blame^] | 58 | files=env_files, |
Steve Baker | f547ec3 | 2014-10-20 14:34:41 +1300 | [diff] [blame] | 59 | disable_rollback=True, |
| 60 | parameters={}, |
Steven Hardy | be1ce2d | 2014-11-18 12:54:50 +0000 | [diff] [blame^] | 61 | environment=env |
Steve Baker | f547ec3 | 2014-10-20 14:34:41 +1300 | [diff] [blame] | 62 | ) |
| 63 | self._wait_for_stack_status(stack_identifier, 'UPDATE_COMPLETE') |
| 64 | |
| 65 | def list_resources(self, stack_identifier): |
| 66 | resources = self.client.resources.list(stack_identifier) |
| 67 | return dict((r.resource_name, r.resource_type) for r in resources) |
| 68 | |
| 69 | def test_stack_update_nochange(self): |
| 70 | stack_name = self._stack_rand_name() |
| 71 | self.client.stacks.create( |
| 72 | stack_name=stack_name, |
| 73 | template=self.template, |
| 74 | files={}, |
| 75 | disable_rollback=True, |
| 76 | parameters={}, |
| 77 | environment={} |
| 78 | ) |
| 79 | self.addCleanup(self.client.stacks.delete, stack_name) |
| 80 | |
| 81 | stack = self.client.stacks.get(stack_name) |
| 82 | stack_identifier = '%s/%s' % (stack_name, stack.id) |
| 83 | |
| 84 | self._wait_for_stack_status(stack_identifier, 'CREATE_COMPLETE') |
| 85 | expected_resources = {'random1': 'OS::Heat::RandomString'} |
| 86 | self.assertEqual(expected_resources, |
| 87 | self.list_resources(stack_identifier)) |
| 88 | |
| 89 | # Update with no changes, resources should be unchanged |
| 90 | self.update_stack(stack_identifier, self.template) |
| 91 | self.assertEqual(expected_resources, |
| 92 | self.list_resources(stack_identifier)) |
| 93 | |
| 94 | def test_stack_update_add_remove(self): |
| 95 | stack_name = self._stack_rand_name() |
| 96 | |
| 97 | self.client.stacks.create( |
| 98 | stack_name=stack_name, |
| 99 | template=self.template, |
| 100 | files={}, |
| 101 | disable_rollback=True, |
| 102 | parameters={}, |
| 103 | environment={} |
| 104 | ) |
| 105 | self.addCleanup(self.client.stacks.delete, stack_name) |
| 106 | |
| 107 | stack = self.client.stacks.get(stack_name) |
| 108 | stack_identifier = '%s/%s' % (stack_name, stack.id) |
| 109 | |
| 110 | self._wait_for_stack_status(stack_identifier, 'CREATE_COMPLETE') |
| 111 | initial_resources = {'random1': 'OS::Heat::RandomString'} |
| 112 | self.assertEqual(initial_resources, |
| 113 | self.list_resources(stack_identifier)) |
| 114 | |
| 115 | # Add one resource via a stack update |
| 116 | self.update_stack(stack_identifier, self.update_template) |
| 117 | stack = self.client.stacks.get(stack_identifier) |
| 118 | updated_resources = {'random1': 'OS::Heat::RandomString', |
| 119 | 'random2': 'OS::Heat::RandomString'} |
| 120 | self.assertEqual(updated_resources, |
| 121 | self.list_resources(stack_identifier)) |
| 122 | |
| 123 | # Then remove it by updating with the original template |
| 124 | self.update_stack(stack_identifier, self.template) |
| 125 | self.assertEqual(initial_resources, |
| 126 | self.list_resources(stack_identifier)) |
Steven Hardy | be1ce2d | 2014-11-18 12:54:50 +0000 | [diff] [blame^] | 127 | |
| 128 | def test_stack_update_provider(self): |
| 129 | stack_name = self._stack_rand_name() |
| 130 | files = {'provider.yaml': self.template} |
| 131 | env = {'resource_registry': |
| 132 | {'My::RandomString': 'provider.yaml'}} |
| 133 | |
| 134 | self.client.stacks.create( |
| 135 | stack_name=stack_name, |
| 136 | template=self.provider_template, |
| 137 | files=files, |
| 138 | disable_rollback=True, |
| 139 | parameters={}, |
| 140 | environment=env |
| 141 | ) |
| 142 | self.addCleanup(self.client.stacks.delete, stack_name) |
| 143 | |
| 144 | stack = self.client.stacks.get(stack_name) |
| 145 | stack_identifier = '%s/%s' % (stack_name, stack.id) |
| 146 | |
| 147 | self._wait_for_stack_status(stack_identifier, 'CREATE_COMPLETE') |
| 148 | initial_resources = {'random1': 'My::RandomString'} |
| 149 | self.assertEqual(initial_resources, |
| 150 | self.list_resources(stack_identifier)) |
| 151 | |
| 152 | # Prove the resource is backed by a nested stack, save the ID |
| 153 | rsrc = self.client.resources.get(stack_identifier, 'random1') |
| 154 | nested_link = [l for l in rsrc.links if l['rel'] == 'nested'] |
| 155 | nested_href = nested_link[0]['href'] |
| 156 | nested_id = nested_href.split('/')[-1] |
| 157 | nested_identifier = '/'.join(nested_href.split('/')[-2:]) |
| 158 | physical_resource_id = rsrc.physical_resource_id |
| 159 | self.assertEqual(physical_resource_id, nested_id) |
| 160 | |
| 161 | # Then check the expected resources are in the nested stack |
| 162 | nested_resources = {'random1': 'OS::Heat::RandomString'} |
| 163 | self.assertEqual(nested_resources, |
| 164 | self.list_resources(nested_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_template, |
| 169 | environment=env, files=files) |
| 170 | stack = self.client.stacks.get(stack_identifier) |
| 171 | |
| 172 | # Parent resources should be unchanged and the nested stack |
| 173 | # should have been updated in-place without replacement |
| 174 | self.assertEqual(initial_resources, |
| 175 | self.list_resources(stack_identifier)) |
| 176 | rsrc = self.client.resources.get(stack_identifier, 'random1') |
| 177 | self.assertEqual(rsrc.physical_resource_id, nested_id) |
| 178 | |
| 179 | # Then check the expected resources are in the nested stack |
| 180 | nested_resources = {'random1': 'OS::Heat::RandomString', |
| 181 | 'random2': 'OS::Heat::RandomString'} |
| 182 | self.assertEqual(nested_resources, |
| 183 | self.list_resources(nested_identifier)) |