blob: 8a2e1938489b6971b658a9a0d62dda3633e81c90 [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
Thomas Herve4d1362b2017-03-24 10:33:01 +010017import sys
Thomas Herve2ce4bda2016-01-22 17:55:55 +010018import tempfile
Steve Baker9d3cd4f2015-08-07 10:45:26 +120019import time
20import yaml
21
Thomas Herve2ce4bda2016-01-22 17:55:55 +010022from oslo_utils import timeutils
23
Steve Baker9d3cd4f2015-08-07 10:45:26 +120024from heat_integrationtests.common import exceptions
Rabi Mishra477efc92015-07-31 13:01:45 +053025from heat_integrationtests.functional import functional_base
Steve Baker10a4bc42015-07-23 10:21:02 +120026
27
Rabi Mishra477efc92015-07-31 13:01:45 +053028class ParallelDeploymentsTest(functional_base.FunctionalTestsBase):
Steve Baker9d3cd4f2015-08-07 10:45:26 +120029 server_template = '''
Steve Baker10a4bc42015-07-23 10:21:02 +120030heat_template_version: "2013-05-23"
31parameters:
32 flavor:
33 type: string
34 image:
35 type: string
36 network:
37 type: string
38resources:
39 server:
40 type: OS::Nova::Server
41 properties:
42 image: {get_param: image}
43 flavor: {get_param: flavor}
44 user_data_format: SOFTWARE_CONFIG
Steve Baker9d3cd4f2015-08-07 10:45:26 +120045 networks: [{network: {get_param: network}}]
46outputs:
47 server:
48 value: {get_resource: server}
49'''
50
51 config_template = '''
52heat_template_version: "2013-05-23"
53parameters:
54 server:
55 type: string
56resources:
Steve Baker10a4bc42015-07-23 10:21:02 +120057 config:
58 type: OS::Heat::SoftwareConfig
59 properties:
Steve Baker10a4bc42015-07-23 10:21:02 +120060'''
61
Steve Baker9d3cd4f2015-08-07 10:45:26 +120062 deployment_snippet = '''
63type: OS::Heat::SoftwareDeployments
64properties:
65 config: {get_resource: config}
66 servers: {'0': {get_param: server}}
67'''
Steve Baker10a4bc42015-07-23 10:21:02 +120068
Steve Baker9d3cd4f2015-08-07 10:45:26 +120069 enable_cleanup = True
70
71 def test_deployments_metadata(self):
Steve Baker10a4bc42015-07-23 10:21:02 +120072 parms = {'flavor': self.conf.minimal_instance_type,
73 'network': self.conf.fixed_network_name,
74 'image': self.conf.minimal_image_ref}
75 stack_identifier = self.stack_create(
76 parameters=parms,
Steve Baker9d3cd4f2015-08-07 10:45:26 +120077 template=self.server_template,
78 enable_cleanup=self.enable_cleanup)
79 server_stack = self.client.stacks.get(stack_identifier)
80 server = server_stack.outputs[0]['output_value']
81
82 config_stacks = []
83 # add up to 3 stacks each with up to 3 deployments
84 deploy_count = 0
85 deploy_count = self.deploy_many_configs(
86 stack_identifier,
87 server,
88 config_stacks,
89 2,
90 5,
91 deploy_count)
Oleksii Chuprykova064e282015-08-30 09:05:32 +030092 self.deploy_many_configs(
Steve Baker9d3cd4f2015-08-07 10:45:26 +120093 stack_identifier,
94 server,
95 config_stacks,
96 3,
97 3,
98 deploy_count)
99
100 self.signal_deployments(stack_identifier)
101 for config_stack in config_stacks:
102 self._wait_for_stack_status(config_stack, 'CREATE_COMPLETE')
103
Oleksii Chuprykova064e282015-08-30 09:05:32 +0300104 def deploy_many_configs(self, stack, server, config_stacks,
Steve Baker9d3cd4f2015-08-07 10:45:26 +1200105 stack_count, deploys_per_stack,
106 deploy_count_start):
107 for a in range(stack_count):
108 config_stacks.append(
109 self.deploy_config(server, deploys_per_stack))
110
Steve Baker9d3cd4f2015-08-07 10:45:26 +1200111 new_count = deploy_count_start + stack_count * deploys_per_stack
Oleksii Chuprykova064e282015-08-30 09:05:32 +0300112 self.wait_for_deploy_metadata_set(stack, new_count)
Steve Baker9d3cd4f2015-08-07 10:45:26 +1200113 return new_count
114
115 def deploy_config(self, server, deploy_count):
116 parms = {'server': server}
117 template = yaml.safe_load(self.config_template)
118 resources = template['resources']
119 resources['config']['properties'] = {'config': 'x' * 10000}
120 for a in range(deploy_count):
121 resources['dep_%s' % a] = yaml.safe_load(self.deployment_snippet)
122 return self.stack_create(
123 parameters=parms,
124 template=template,
125 enable_cleanup=self.enable_cleanup,
126 expected_status=None)
127
Oleksii Chuprykova064e282015-08-30 09:05:32 +0300128 def wait_for_deploy_metadata_set(self, stack, deploy_count):
Steve Baker9d3cd4f2015-08-07 10:45:26 +1200129 build_timeout = self.conf.build_timeout
130 build_interval = self.conf.build_interval
131
132 start = timeutils.utcnow()
133 while timeutils.delta_seconds(start,
134 timeutils.utcnow()) < build_timeout:
Oleksii Chuprykova064e282015-08-30 09:05:32 +0300135 server_metadata = self.client.resources.metadata(
136 stack, 'server')
137 if len(server_metadata['deployments']) == deploy_count:
Steve Baker9d3cd4f2015-08-07 10:45:26 +1200138 return
139 time.sleep(build_interval)
140
141 message = ('Deployment resources failed to be created within '
142 'the required time (%s s).' %
143 (build_timeout))
144 raise exceptions.TimeoutException(message)
145
146 def signal_deployments(self, stack_identifier):
147 server_metadata = self.client.resources.metadata(
148 stack_identifier, 'server')
149 for dep in server_metadata['deployments']:
150 iv = dict((i['name'], i['value']) for i in dep['inputs'])
151 sigurl = iv.get('deploy_signal_id')
152 requests.post(sigurl, data='{}',
rabi10a9b102017-01-12 08:09:11 +0530153 headers={'content-type': 'application/json'},
tyagi39aa11a2016-03-07 04:47:00 -0800154 verify=self.verify_cert)
Thomas Herve2ce4bda2016-01-22 17:55:55 +0100155
156
157class ZaqarSignalTransportTest(functional_base.FunctionalTestsBase):
158 server_template = '''
159heat_template_version: "2013-05-23"
160
161parameters:
162 flavor:
163 type: string
164 image:
165 type: string
166 network:
167 type: string
168
169resources:
170 server:
171 type: OS::Nova::Server
172 properties:
173 image: {get_param: image}
174 flavor: {get_param: flavor}
175 user_data_format: SOFTWARE_CONFIG
176 software_config_transport: ZAQAR_MESSAGE
177 networks: [{network: {get_param: network}}]
178 config:
179 type: OS::Heat::SoftwareConfig
180 properties:
181 config: echo 'foo'
182 deployment:
183 type: OS::Heat::SoftwareDeployment
184 properties:
185 config: {get_resource: config}
186 server: {get_resource: server}
187 signal_transport: ZAQAR_SIGNAL
188
189outputs:
190 data:
191 value: {get_attr: [deployment, deploy_stdout]}
192'''
193
194 conf_template = '''
195[zaqar]
196user_id = %(user_id)s
197password = %(password)s
198project_id = %(project_id)s
199auth_url = %(auth_url)s
200queue_id = %(queue_id)s
201 '''
202
203 def test_signal_queues(self):
204 parms = {'flavor': self.conf.minimal_instance_type,
205 'network': self.conf.fixed_network_name,
206 'image': self.conf.minimal_image_ref}
207 stack_identifier = self.stack_create(
208 parameters=parms,
209 template=self.server_template,
210 expected_status=None)
211 metadata = self.wait_for_deploy_metadata_set(stack_identifier)
212 config = metadata['os-collect-config']['zaqar']
213 conf_content = self.conf_template % config
214 fd, temp_path = tempfile.mkstemp()
Thomas Herve4d1362b2017-03-24 10:33:01 +0100215 os.write(fd, conf_content.encode('utf-8'))
Thomas Herve2ce4bda2016-01-22 17:55:55 +0100216 os.close(fd)
217 cmd = ['os-collect-config', '--one-time',
218 '--config-file=%s' % temp_path, 'zaqar']
219 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
220 stdout_value = proc.communicate()[0]
Thomas Herve4d1362b2017-03-24 10:33:01 +0100221 data = json.loads(stdout_value.decode('utf-8'))
Thomas Herve2ce4bda2016-01-22 17:55:55 +0100222 self.assertEqual(config, data['zaqar']['os-collect-config']['zaqar'])
223 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
224 stdout_value = proc.communicate()[0]
Thomas Herve4d1362b2017-03-24 10:33:01 +0100225 data = json.loads(stdout_value.decode('utf-8'))
Thomas Herve2ce4bda2016-01-22 17:55:55 +0100226
227 fd, temp_path = tempfile.mkstemp()
Thomas Herve4d1362b2017-03-24 10:33:01 +0100228 os.write(fd,
229 json.dumps(data['zaqar']['deployments'][0]).encode('utf-8'))
Thomas Herve2ce4bda2016-01-22 17:55:55 +0100230 os.close(fd)
Thomas Herve4d1362b2017-03-24 10:33:01 +0100231 cmd = [sys.executable, self.conf.heat_config_notify_script, temp_path]
Thomas Herve2ce4bda2016-01-22 17:55:55 +0100232 proc = subprocess.Popen(cmd,
233 stderr=subprocess.PIPE,
234 stdin=subprocess.PIPE)
Thomas Herve4d1362b2017-03-24 10:33:01 +0100235 proc.communicate(
236 json.dumps({'deploy_stdout': 'here!'}).encode('utf-8'))
Thomas Herve2ce4bda2016-01-22 17:55:55 +0100237 self._wait_for_stack_status(stack_identifier, 'CREATE_COMPLETE')
238 stack = self.client.stacks.get(stack_identifier)
239 self.assertEqual('here!', stack.outputs[0]['output_value'])
240
241 def wait_for_deploy_metadata_set(self, stack):
242 build_timeout = self.conf.build_timeout
243 build_interval = self.conf.build_interval
244
245 start = timeutils.utcnow()
246 while timeutils.delta_seconds(start,
247 timeutils.utcnow()) < build_timeout:
248 server_metadata = self.client.resources.metadata(
249 stack, 'server')
250 if server_metadata.get('deployments'):
251 return server_metadata
252 time.sleep(build_interval)
253
254 message = ('Deployment resources failed to be created within '
255 'the required time (%s s).' %
256 (build_timeout))
257 raise exceptions.TimeoutException(message)