Steve Baker | 10a4bc4 | 2015-07-23 10:21:02 +1200 | [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 | |
Thomas Herve | 2ce4bda | 2016-01-22 17:55:55 +0100 | [diff] [blame^] | 13 | import json |
| 14 | import os |
Steve Baker | 9d3cd4f | 2015-08-07 10:45:26 +1200 | [diff] [blame] | 15 | import requests |
Thomas Herve | 2ce4bda | 2016-01-22 17:55:55 +0100 | [diff] [blame^] | 16 | import subprocess |
| 17 | import tempfile |
Steve Baker | 9d3cd4f | 2015-08-07 10:45:26 +1200 | [diff] [blame] | 18 | import time |
| 19 | import yaml |
| 20 | |
Thomas Herve | 2ce4bda | 2016-01-22 17:55:55 +0100 | [diff] [blame^] | 21 | from oslo_utils import timeutils |
| 22 | |
Steve Baker | 9d3cd4f | 2015-08-07 10:45:26 +1200 | [diff] [blame] | 23 | from heat_integrationtests.common import exceptions |
Rabi Mishra | 477efc9 | 2015-07-31 13:01:45 +0530 | [diff] [blame] | 24 | from heat_integrationtests.functional import functional_base |
Steve Baker | 10a4bc4 | 2015-07-23 10:21:02 +1200 | [diff] [blame] | 25 | |
| 26 | |
Rabi Mishra | 477efc9 | 2015-07-31 13:01:45 +0530 | [diff] [blame] | 27 | class ParallelDeploymentsTest(functional_base.FunctionalTestsBase): |
Steve Baker | 9d3cd4f | 2015-08-07 10:45:26 +1200 | [diff] [blame] | 28 | server_template = ''' |
Steve Baker | 10a4bc4 | 2015-07-23 10:21:02 +1200 | [diff] [blame] | 29 | heat_template_version: "2013-05-23" |
| 30 | parameters: |
| 31 | flavor: |
| 32 | type: string |
| 33 | image: |
| 34 | type: string |
| 35 | network: |
| 36 | type: string |
| 37 | resources: |
| 38 | server: |
| 39 | type: OS::Nova::Server |
| 40 | properties: |
| 41 | image: {get_param: image} |
| 42 | flavor: {get_param: flavor} |
| 43 | user_data_format: SOFTWARE_CONFIG |
Steve Baker | 9d3cd4f | 2015-08-07 10:45:26 +1200 | [diff] [blame] | 44 | networks: [{network: {get_param: network}}] |
| 45 | outputs: |
| 46 | server: |
| 47 | value: {get_resource: server} |
| 48 | ''' |
| 49 | |
| 50 | config_template = ''' |
| 51 | heat_template_version: "2013-05-23" |
| 52 | parameters: |
| 53 | server: |
| 54 | type: string |
| 55 | resources: |
Steve Baker | 10a4bc4 | 2015-07-23 10:21:02 +1200 | [diff] [blame] | 56 | config: |
| 57 | type: OS::Heat::SoftwareConfig |
| 58 | properties: |
Steve Baker | 10a4bc4 | 2015-07-23 10:21:02 +1200 | [diff] [blame] | 59 | ''' |
| 60 | |
Steve Baker | 9d3cd4f | 2015-08-07 10:45:26 +1200 | [diff] [blame] | 61 | deployment_snippet = ''' |
| 62 | type: OS::Heat::SoftwareDeployments |
| 63 | properties: |
| 64 | config: {get_resource: config} |
| 65 | servers: {'0': {get_param: server}} |
| 66 | ''' |
Steve Baker | 10a4bc4 | 2015-07-23 10:21:02 +1200 | [diff] [blame] | 67 | |
Steve Baker | 9d3cd4f | 2015-08-07 10:45:26 +1200 | [diff] [blame] | 68 | enable_cleanup = True |
| 69 | |
| 70 | def test_deployments_metadata(self): |
Steve Baker | 10a4bc4 | 2015-07-23 10:21:02 +1200 | [diff] [blame] | 71 | parms = {'flavor': self.conf.minimal_instance_type, |
| 72 | 'network': self.conf.fixed_network_name, |
| 73 | 'image': self.conf.minimal_image_ref} |
| 74 | stack_identifier = self.stack_create( |
| 75 | parameters=parms, |
Steve Baker | 9d3cd4f | 2015-08-07 10:45:26 +1200 | [diff] [blame] | 76 | template=self.server_template, |
| 77 | enable_cleanup=self.enable_cleanup) |
| 78 | server_stack = self.client.stacks.get(stack_identifier) |
| 79 | server = server_stack.outputs[0]['output_value'] |
| 80 | |
| 81 | config_stacks = [] |
| 82 | # add up to 3 stacks each with up to 3 deployments |
| 83 | deploy_count = 0 |
| 84 | deploy_count = self.deploy_many_configs( |
| 85 | stack_identifier, |
| 86 | server, |
| 87 | config_stacks, |
| 88 | 2, |
| 89 | 5, |
| 90 | deploy_count) |
Oleksii Chuprykov | a064e28 | 2015-08-30 09:05:32 +0300 | [diff] [blame] | 91 | self.deploy_many_configs( |
Steve Baker | 9d3cd4f | 2015-08-07 10:45:26 +1200 | [diff] [blame] | 92 | stack_identifier, |
| 93 | server, |
| 94 | config_stacks, |
| 95 | 3, |
| 96 | 3, |
| 97 | deploy_count) |
| 98 | |
| 99 | self.signal_deployments(stack_identifier) |
| 100 | for config_stack in config_stacks: |
| 101 | self._wait_for_stack_status(config_stack, 'CREATE_COMPLETE') |
| 102 | |
Oleksii Chuprykov | a064e28 | 2015-08-30 09:05:32 +0300 | [diff] [blame] | 103 | def deploy_many_configs(self, stack, server, config_stacks, |
Steve Baker | 9d3cd4f | 2015-08-07 10:45:26 +1200 | [diff] [blame] | 104 | stack_count, deploys_per_stack, |
| 105 | deploy_count_start): |
| 106 | for a in range(stack_count): |
| 107 | config_stacks.append( |
| 108 | self.deploy_config(server, deploys_per_stack)) |
| 109 | |
Steve Baker | 9d3cd4f | 2015-08-07 10:45:26 +1200 | [diff] [blame] | 110 | new_count = deploy_count_start + stack_count * deploys_per_stack |
Oleksii Chuprykov | a064e28 | 2015-08-30 09:05:32 +0300 | [diff] [blame] | 111 | self.wait_for_deploy_metadata_set(stack, new_count) |
Steve Baker | 9d3cd4f | 2015-08-07 10:45:26 +1200 | [diff] [blame] | 112 | return new_count |
| 113 | |
| 114 | def deploy_config(self, server, deploy_count): |
| 115 | parms = {'server': server} |
| 116 | template = yaml.safe_load(self.config_template) |
| 117 | resources = template['resources'] |
| 118 | resources['config']['properties'] = {'config': 'x' * 10000} |
| 119 | for a in range(deploy_count): |
| 120 | resources['dep_%s' % a] = yaml.safe_load(self.deployment_snippet) |
| 121 | return self.stack_create( |
| 122 | parameters=parms, |
| 123 | template=template, |
| 124 | enable_cleanup=self.enable_cleanup, |
| 125 | expected_status=None) |
| 126 | |
Oleksii Chuprykov | a064e28 | 2015-08-30 09:05:32 +0300 | [diff] [blame] | 127 | def wait_for_deploy_metadata_set(self, stack, deploy_count): |
Steve Baker | 9d3cd4f | 2015-08-07 10:45:26 +1200 | [diff] [blame] | 128 | build_timeout = self.conf.build_timeout |
| 129 | build_interval = self.conf.build_interval |
| 130 | |
| 131 | start = timeutils.utcnow() |
| 132 | while timeutils.delta_seconds(start, |
| 133 | timeutils.utcnow()) < build_timeout: |
Oleksii Chuprykov | a064e28 | 2015-08-30 09:05:32 +0300 | [diff] [blame] | 134 | server_metadata = self.client.resources.metadata( |
| 135 | stack, 'server') |
| 136 | if len(server_metadata['deployments']) == deploy_count: |
Steve Baker | 9d3cd4f | 2015-08-07 10:45:26 +1200 | [diff] [blame] | 137 | return |
| 138 | time.sleep(build_interval) |
| 139 | |
| 140 | message = ('Deployment resources failed to be created within ' |
| 141 | 'the required time (%s s).' % |
| 142 | (build_timeout)) |
| 143 | raise exceptions.TimeoutException(message) |
| 144 | |
| 145 | def signal_deployments(self, stack_identifier): |
| 146 | server_metadata = self.client.resources.metadata( |
| 147 | stack_identifier, 'server') |
| 148 | for dep in server_metadata['deployments']: |
| 149 | iv = dict((i['name'], i['value']) for i in dep['inputs']) |
| 150 | sigurl = iv.get('deploy_signal_id') |
| 151 | requests.post(sigurl, data='{}', |
| 152 | headers={'content-type': None}) |
Thomas Herve | 2ce4bda | 2016-01-22 17:55:55 +0100 | [diff] [blame^] | 153 | |
| 154 | |
| 155 | class ZaqarSignalTransportTest(functional_base.FunctionalTestsBase): |
| 156 | server_template = ''' |
| 157 | heat_template_version: "2013-05-23" |
| 158 | |
| 159 | parameters: |
| 160 | flavor: |
| 161 | type: string |
| 162 | image: |
| 163 | type: string |
| 164 | network: |
| 165 | type: string |
| 166 | |
| 167 | resources: |
| 168 | server: |
| 169 | type: OS::Nova::Server |
| 170 | properties: |
| 171 | image: {get_param: image} |
| 172 | flavor: {get_param: flavor} |
| 173 | user_data_format: SOFTWARE_CONFIG |
| 174 | software_config_transport: ZAQAR_MESSAGE |
| 175 | networks: [{network: {get_param: network}}] |
| 176 | config: |
| 177 | type: OS::Heat::SoftwareConfig |
| 178 | properties: |
| 179 | config: echo 'foo' |
| 180 | deployment: |
| 181 | type: OS::Heat::SoftwareDeployment |
| 182 | properties: |
| 183 | config: {get_resource: config} |
| 184 | server: {get_resource: server} |
| 185 | signal_transport: ZAQAR_SIGNAL |
| 186 | |
| 187 | outputs: |
| 188 | data: |
| 189 | value: {get_attr: [deployment, deploy_stdout]} |
| 190 | ''' |
| 191 | |
| 192 | conf_template = ''' |
| 193 | [zaqar] |
| 194 | user_id = %(user_id)s |
| 195 | password = %(password)s |
| 196 | project_id = %(project_id)s |
| 197 | auth_url = %(auth_url)s |
| 198 | queue_id = %(queue_id)s |
| 199 | ''' |
| 200 | |
| 201 | def test_signal_queues(self): |
| 202 | parms = {'flavor': self.conf.minimal_instance_type, |
| 203 | 'network': self.conf.fixed_network_name, |
| 204 | 'image': self.conf.minimal_image_ref} |
| 205 | stack_identifier = self.stack_create( |
| 206 | parameters=parms, |
| 207 | template=self.server_template, |
| 208 | expected_status=None) |
| 209 | metadata = self.wait_for_deploy_metadata_set(stack_identifier) |
| 210 | config = metadata['os-collect-config']['zaqar'] |
| 211 | conf_content = self.conf_template % config |
| 212 | fd, temp_path = tempfile.mkstemp() |
| 213 | os.write(fd, conf_content) |
| 214 | os.close(fd) |
| 215 | cmd = ['os-collect-config', '--one-time', |
| 216 | '--config-file=%s' % temp_path, 'zaqar'] |
| 217 | proc = subprocess.Popen(cmd, stdout=subprocess.PIPE) |
| 218 | stdout_value = proc.communicate()[0] |
| 219 | data = json.loads(stdout_value) |
| 220 | self.assertEqual(config, data['zaqar']['os-collect-config']['zaqar']) |
| 221 | proc = subprocess.Popen(cmd, stdout=subprocess.PIPE) |
| 222 | stdout_value = proc.communicate()[0] |
| 223 | data = json.loads(stdout_value) |
| 224 | |
| 225 | fd, temp_path = tempfile.mkstemp() |
| 226 | os.write(fd, json.dumps(data['zaqar']['deployments'][0])) |
| 227 | os.close(fd) |
| 228 | cmd = ['python', self.conf.heat_config_notify_script, temp_path] |
| 229 | proc = subprocess.Popen(cmd, |
| 230 | stderr=subprocess.PIPE, |
| 231 | stdin=subprocess.PIPE) |
| 232 | proc.communicate(json.dumps({'deploy_stdout': 'here!'})) |
| 233 | self._wait_for_stack_status(stack_identifier, 'CREATE_COMPLETE') |
| 234 | stack = self.client.stacks.get(stack_identifier) |
| 235 | self.assertEqual('here!', stack.outputs[0]['output_value']) |
| 236 | |
| 237 | def wait_for_deploy_metadata_set(self, stack): |
| 238 | build_timeout = self.conf.build_timeout |
| 239 | build_interval = self.conf.build_interval |
| 240 | |
| 241 | start = timeutils.utcnow() |
| 242 | while timeutils.delta_seconds(start, |
| 243 | timeutils.utcnow()) < build_timeout: |
| 244 | server_metadata = self.client.resources.metadata( |
| 245 | stack, 'server') |
| 246 | if server_metadata.get('deployments'): |
| 247 | return server_metadata |
| 248 | time.sleep(build_interval) |
| 249 | |
| 250 | message = ('Deployment resources failed to be created within ' |
| 251 | 'the required time (%s s).' % |
| 252 | (build_timeout)) |
| 253 | raise exceptions.TimeoutException(message) |