huangtianhua | 80519eb | 2015-10-31 17:55:54 +0800 | [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 | from heat_integrationtests.functional import functional_base |
| 14 | |
| 15 | |
| 16 | test_template = ''' |
| 17 | heat_template_version: 2015-04-30 |
| 18 | description: Test template to create/update subnet with allocation_pools. |
| 19 | resources: |
| 20 | net: |
| 21 | type: OS::Neutron::Net |
| 22 | subnet: |
| 23 | type: OS::Neutron::Subnet |
| 24 | properties: |
| 25 | network: { get_resource: net } |
| 26 | cidr: 11.11.11.0/24 |
huangtianhua | 9593716 | 2015-10-31 11:26:55 +0800 | [diff] [blame] | 27 | gateway_ip: 11.11.11.5 |
huangtianhua | 80519eb | 2015-10-31 17:55:54 +0800 | [diff] [blame] | 28 | allocation_pools: [{start: 11.11.11.10, end: 11.11.11.250}] |
| 29 | outputs: |
| 30 | alloc_pools: |
| 31 | value: {get_attr: [subnet, allocation_pools]} |
huangtianhua | 9593716 | 2015-10-31 11:26:55 +0800 | [diff] [blame] | 32 | gateway_ip: |
| 33 | value: {get_attr: [subnet, gateway_ip]} |
huangtianhua | 80519eb | 2015-10-31 17:55:54 +0800 | [diff] [blame] | 34 | ''' |
| 35 | |
rabi | f9681be | 2016-09-07 09:08:50 +0530 | [diff] [blame] | 36 | test_template_with_translation = ''' |
| 37 | heat_template_version: 2016-10-14 |
| 38 | description: Test template to create/update subnet with translation |
| 39 | parameters: |
| 40 | net_cidr: |
| 41 | type: string |
| 42 | resources: |
| 43 | net: |
| 44 | type: OS::Neutron::Net |
| 45 | net_value: |
| 46 | type: OS::Heat::Value |
| 47 | properties: |
| 48 | value: {get_resource: net} |
| 49 | subnet: |
| 50 | type: OS::Neutron::Subnet |
| 51 | properties: |
| 52 | network: { get_attr: [net_value, value] } |
| 53 | cidr: {get_param: net_cidr} |
| 54 | ''' |
| 55 | |
huangtianhua | 80519eb | 2015-10-31 17:55:54 +0800 | [diff] [blame] | 56 | |
| 57 | class UpdateSubnetTest(functional_base.FunctionalTestsBase): |
| 58 | |
huangtianhua | 9593716 | 2015-10-31 11:26:55 +0800 | [diff] [blame] | 59 | def get_outputs(self, stack_identifier, output_key): |
huangtianhua | 80519eb | 2015-10-31 17:55:54 +0800 | [diff] [blame] | 60 | stack = self.client.stacks.get(stack_identifier) |
huangtianhua | 9593716 | 2015-10-31 11:26:55 +0800 | [diff] [blame] | 61 | output = self._stack_output(stack, output_key) |
| 62 | return output |
huangtianhua | 80519eb | 2015-10-31 17:55:54 +0800 | [diff] [blame] | 63 | |
| 64 | def test_update_allocation_pools(self): |
| 65 | stack_identifier = self.stack_create(template=test_template) |
huangtianhua | 9593716 | 2015-10-31 11:26:55 +0800 | [diff] [blame] | 66 | alloc_pools = self.get_outputs(stack_identifier, 'alloc_pools') |
huangtianhua | 80519eb | 2015-10-31 17:55:54 +0800 | [diff] [blame] | 67 | self.assertEqual([{'start': '11.11.11.10', 'end': '11.11.11.250'}], |
| 68 | alloc_pools) |
| 69 | |
| 70 | # Update allocation_pools with a new range |
| 71 | templ_other_pool = test_template.replace( |
| 72 | 'allocation_pools: [{start: 11.11.11.10, end: 11.11.11.250}]', |
| 73 | 'allocation_pools: [{start: 11.11.11.10, end: 11.11.11.100}]') |
| 74 | self.update_stack(stack_identifier, templ_other_pool) |
huangtianhua | 9593716 | 2015-10-31 11:26:55 +0800 | [diff] [blame] | 75 | new_alloc_pools = self.get_outputs(stack_identifier, 'alloc_pools') |
huangtianhua | 80519eb | 2015-10-31 17:55:54 +0800 | [diff] [blame] | 76 | # the new pools should be the new range |
| 77 | self.assertEqual([{'start': '11.11.11.10', 'end': '11.11.11.100'}], |
| 78 | new_alloc_pools) |
| 79 | |
| 80 | def test_update_allocation_pools_to_empty(self): |
| 81 | stack_identifier = self.stack_create(template=test_template) |
huangtianhua | 9593716 | 2015-10-31 11:26:55 +0800 | [diff] [blame] | 82 | alloc_pools = self.get_outputs(stack_identifier, 'alloc_pools') |
huangtianhua | 80519eb | 2015-10-31 17:55:54 +0800 | [diff] [blame] | 83 | self.assertEqual([{'start': '11.11.11.10', 'end': '11.11.11.250'}], |
| 84 | alloc_pools) |
| 85 | |
| 86 | # Update allocation_pools with [] |
| 87 | templ_empty_pools = test_template.replace( |
| 88 | 'allocation_pools: [{start: 11.11.11.10, end: 11.11.11.250}]', |
| 89 | 'allocation_pools: []') |
| 90 | self.update_stack(stack_identifier, templ_empty_pools) |
huangtianhua | 9593716 | 2015-10-31 11:26:55 +0800 | [diff] [blame] | 91 | new_alloc_pools = self.get_outputs(stack_identifier, 'alloc_pools') |
huangtianhua | 80519eb | 2015-10-31 17:55:54 +0800 | [diff] [blame] | 92 | # new_alloc_pools should be [] |
| 93 | self.assertEqual([], new_alloc_pools) |
| 94 | |
| 95 | def test_update_to_no_allocation_pools(self): |
| 96 | stack_identifier = self.stack_create(template=test_template) |
huangtianhua | 9593716 | 2015-10-31 11:26:55 +0800 | [diff] [blame] | 97 | alloc_pools = self.get_outputs(stack_identifier, 'alloc_pools') |
huangtianhua | 80519eb | 2015-10-31 17:55:54 +0800 | [diff] [blame] | 98 | self.assertEqual([{'start': '11.11.11.10', 'end': '11.11.11.250'}], |
| 99 | alloc_pools) |
| 100 | |
| 101 | # Remove the allocation_pools from template |
| 102 | templ_no_pools = test_template.replace( |
| 103 | 'allocation_pools: [{start: 11.11.11.10, end: 11.11.11.250}]', |
| 104 | '') |
| 105 | self.update_stack(stack_identifier, templ_no_pools) |
huangtianhua | 9593716 | 2015-10-31 11:26:55 +0800 | [diff] [blame] | 106 | last_alloc_pools = self.get_outputs(stack_identifier, 'alloc_pools') |
huangtianhua | 80519eb | 2015-10-31 17:55:54 +0800 | [diff] [blame] | 107 | # last_alloc_pools should be [] |
| 108 | self.assertEqual([], last_alloc_pools) |
huangtianhua | 9593716 | 2015-10-31 11:26:55 +0800 | [diff] [blame] | 109 | |
| 110 | def test_update_gateway_ip(self): |
| 111 | stack_identifier = self.stack_create(template=test_template) |
| 112 | gw_ip = self.get_outputs(stack_identifier, 'gateway_ip') |
| 113 | self.assertEqual('11.11.11.5', gw_ip) |
| 114 | |
| 115 | # Update gateway_ip |
| 116 | templ_other_gw_ip = test_template.replace( |
| 117 | 'gateway_ip: 11.11.11.5', 'gateway_ip: 11.11.11.9') |
| 118 | self.update_stack(stack_identifier, templ_other_gw_ip) |
| 119 | new_gw_ip = self.get_outputs(stack_identifier, 'gateway_ip') |
| 120 | # the gateway_ip should be the new one |
| 121 | self.assertEqual('11.11.11.9', new_gw_ip) |
| 122 | |
| 123 | def test_update_gateway_ip_to_empty(self): |
| 124 | stack_identifier = self.stack_create(template=test_template) |
| 125 | gw_ip = self.get_outputs(stack_identifier, 'gateway_ip') |
| 126 | self.assertEqual('11.11.11.5', gw_ip) |
| 127 | |
| 128 | # Update gateway_ip to null(resolve to '') |
| 129 | templ_empty_gw_ip = test_template.replace( |
| 130 | 'gateway_ip: 11.11.11.5', 'gateway_ip: null') |
| 131 | self.update_stack(stack_identifier, templ_empty_gw_ip) |
| 132 | new_gw_ip = self.get_outputs(stack_identifier, 'gateway_ip') |
| 133 | # new gateway_ip should be None |
| 134 | self.assertIsNone(new_gw_ip) |
| 135 | |
| 136 | def test_update_to_no_gateway_ip(self): |
| 137 | stack_identifier = self.stack_create(template=test_template) |
| 138 | gw_ip = self.get_outputs(stack_identifier, 'gateway_ip') |
| 139 | self.assertEqual('11.11.11.5', gw_ip) |
| 140 | |
| 141 | # Remove the gateway from template |
| 142 | templ_no_gw_ip = test_template.replace( |
| 143 | 'gateway_ip: 11.11.11.5', '') |
| 144 | self.update_stack(stack_identifier, templ_no_gw_ip) |
| 145 | new_gw_ip = self.get_outputs(stack_identifier, 'gateway_ip') |
| 146 | # new gateway_ip should be None |
| 147 | self.assertIsNone(new_gw_ip) |
rabi | f9681be | 2016-09-07 09:08:50 +0530 | [diff] [blame] | 148 | |
| 149 | def test_update_with_network_translation(self): |
| 150 | # Just create and update where network is translated properly. |
| 151 | env = {'parameters': {'net_cidr': '11.11.11.0/24'}} |
| 152 | stack_identifier = self.stack_create( |
| 153 | template=test_template_with_translation, |
| 154 | environment=env) |
| 155 | env = {'parameters': {'net_cidr': '11.11.12.0/24'}} |
| 156 | self.update_stack(stack_identifier, |
| 157 | template=test_template_with_translation, |
| 158 | environment=env) |