blob: 20d38d804ba42f0112478e9713e0658d956e31e3 [file] [log] [blame]
Steve Baker10a4bc42015-07-23 10:21:02 +12001# 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 Herve2ce4bda2016-01-22 17:55:55 +010013import json
14import os
Steve Baker9d3cd4f2015-08-07 10:45:26 +120015import requests
Thomas Herve2ce4bda2016-01-22 17:55:55 +010016import subprocess
17import tempfile
Steve Baker9d3cd4f2015-08-07 10:45:26 +120018import time
19import yaml
20
Thomas Herve2ce4bda2016-01-22 17:55:55 +010021from oslo_utils import timeutils
22
Steve Baker9d3cd4f2015-08-07 10:45:26 +120023from heat_integrationtests.common import exceptions
Rabi Mishra477efc92015-07-31 13:01:45 +053024from heat_integrationtests.functional import functional_base
Steve Baker10a4bc42015-07-23 10:21:02 +120025
26
Rabi Mishra477efc92015-07-31 13:01:45 +053027class ParallelDeploymentsTest(functional_base.FunctionalTestsBase):
Steve Baker9d3cd4f2015-08-07 10:45:26 +120028 server_template = '''
Steve Baker10a4bc42015-07-23 10:21:02 +120029heat_template_version: "2013-05-23"
30parameters:
31 flavor:
32 type: string
33 image:
34 type: string
35 network:
36 type: string
37resources:
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 Baker9d3cd4f2015-08-07 10:45:26 +120044 networks: [{network: {get_param: network}}]
45outputs:
46 server:
47 value: {get_resource: server}
48'''
49
50 config_template = '''
51heat_template_version: "2013-05-23"
52parameters:
53 server:
54 type: string
55resources:
Steve Baker10a4bc42015-07-23 10:21:02 +120056 config:
57 type: OS::Heat::SoftwareConfig
58 properties:
Steve Baker10a4bc42015-07-23 10:21:02 +120059'''
60
Steve Baker9d3cd4f2015-08-07 10:45:26 +120061 deployment_snippet = '''
62type: OS::Heat::SoftwareDeployments
63properties:
64 config: {get_resource: config}
65 servers: {'0': {get_param: server}}
66'''
Steve Baker10a4bc42015-07-23 10:21:02 +120067
Steve Baker9d3cd4f2015-08-07 10:45:26 +120068 enable_cleanup = True
69
70 def test_deployments_metadata(self):
Steve Baker10a4bc42015-07-23 10:21:02 +120071 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 Baker9d3cd4f2015-08-07 10:45:26 +120076 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 Chuprykova064e282015-08-30 09:05:32 +030091 self.deploy_many_configs(
Steve Baker9d3cd4f2015-08-07 10:45:26 +120092 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 Chuprykova064e282015-08-30 09:05:32 +0300103 def deploy_many_configs(self, stack, server, config_stacks,
Steve Baker9d3cd4f2015-08-07 10:45:26 +1200104 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 Baker9d3cd4f2015-08-07 10:45:26 +1200110 new_count = deploy_count_start + stack_count * deploys_per_stack
Oleksii Chuprykova064e282015-08-30 09:05:32 +0300111 self.wait_for_deploy_metadata_set(stack, new_count)
Steve Baker9d3cd4f2015-08-07 10:45:26 +1200112 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 Chuprykova064e282015-08-30 09:05:32 +0300127 def wait_for_deploy_metadata_set(self, stack, deploy_count):
Steve Baker9d3cd4f2015-08-07 10:45:26 +1200128 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 Chuprykova064e282015-08-30 09:05:32 +0300134 server_metadata = self.client.resources.metadata(
135 stack, 'server')
136 if len(server_metadata['deployments']) == deploy_count:
Steve Baker9d3cd4f2015-08-07 10:45:26 +1200137 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 Herve2ce4bda2016-01-22 17:55:55 +0100153
154
155class ZaqarSignalTransportTest(functional_base.FunctionalTestsBase):
156 server_template = '''
157heat_template_version: "2013-05-23"
158
159parameters:
160 flavor:
161 type: string
162 image:
163 type: string
164 network:
165 type: string
166
167resources:
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
187outputs:
188 data:
189 value: {get_attr: [deployment, deploy_stdout]}
190'''
191
192 conf_template = '''
193[zaqar]
194user_id = %(user_id)s
195password = %(password)s
196project_id = %(project_id)s
197auth_url = %(auth_url)s
198queue_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)