blob: 6c1e87413fe7ac3ef40efeabc0f3f426552c6db7 [file] [log] [blame]
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +03001# Copyright 2018 Mirantis, Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030015import json
Oleksii Butenko71d76f32018-06-05 17:46:34 +030016import os
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030017
Oleksii Butenkob0ca4cc2018-10-22 16:29:54 +030018from devops.helpers import helpers
19
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030020from tcp_tests import logger
21from tcp_tests import settings
22
Oleksii Butenkob0ca4cc2018-10-22 16:29:54 +030023
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030024LOG = logger.logger
25
26TEMPEST_CFG_DIR = '/tmp/test'
27
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030028
29class RuntestManager(object):
30 """Helper manager for execution tempest via runtest-formula"""
31
32 image_name = settings.TEMPEST_IMAGE
33 image_version = settings.TEMPEST_IMAGE_VERSION
34 container_name = 'run-tempest-ci'
35 master_host = "cfg01"
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000036 control_host = "ctl01"
sgudza5c901d2018-10-29 15:08:33 +020037 compute_host = "cmp"
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030038 class_name = "runtest"
39 run_cmd = '/bin/bash -c "run-tempest"'
40
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000041 def __init__(self, config, underlay, salt_api, cluster_name,
Oleksii Butenko74566102018-09-11 11:55:13 +030042 domain_name, tempest_threads,
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030043 tempest_pattern=settings.TEMPEST_PATTERN,
44 run_cmd=None, target='gtw01'):
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000045 self.__config = config
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030046 self.underlay = underlay
47 self.__salt_api = salt_api
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030048 self.cluster_name = cluster_name
49 self.domain_name = domain_name
50 self.tempest_threads = tempest_threads
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030051 self.tempest_pattern = tempest_pattern
52 self.run_cmd = run_cmd or self.run_cmd
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000053 self.target_name = self.underlay.get_target_node_names(target)[0]
54 self.master_name = self.underlay.get_target_node_names(
55 self.master_host)[0]
56 self.control_name = self.underlay.get_target_node_names(
57 self.control_host)[0]
sgudza5c901d2018-10-29 15:08:33 +020058 self.compute_name = self.underlay.get_target_node_names(
59 self.compute_host)[0]
obutenko3e44b602018-11-13 12:50:22 +020060 self.barbican = False
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030061
62 @property
63 def salt_api(self):
64 return self.__salt_api
65
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000066 @property
67 def runtest_pillar(self):
68 public_net = self.__config.underlay.dhcp_ranges[
69 settings.EXTERNAL_ADDRESS_POOL_NAME]
70 public_gateway = public_net["gateway"].encode("ascii")
71 public_cidr = public_net["cidr"].encode("ascii")
72 public_allocation_start = public_net["start"].encode("ascii")
73 public_allocation_end = public_net["end"].encode("ascii")
Dennis Dmitrievcf52b8e2018-11-06 17:28:38 +020074 tempest_test_target = self.target_name.encode("ascii") + "*"
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030075
obutenko3e44b602018-11-13 12:50:22 +020076 pillar = {
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000077 'classes': ['service.runtest.tempest',
78 'service.runtest.tempest.public_net',
79 'service.runtest.tempest.services.manila.glance'],
80 'parameters': {
81 '_param': {
82 'runtest_tempest_cfg_dir': TEMPEST_CFG_DIR,
83 'runtest_tempest_cfg_name': 'tempest.conf',
84 'runtest_tempest_public_net': 'public',
85 'openstack_public_neutron_subnet_gateway': public_gateway,
86 'openstack_public_neutron_subnet_cidr': public_cidr,
87 'openstack_public_neutron_subnet_allocation_start':
88 public_allocation_start,
89 'openstack_public_neutron_subnet_allocation_end':
90 public_allocation_end,
Dennis Dmitrievcf52b8e2018-11-06 17:28:38 +020091 'tempest_test_target': tempest_test_target,
sgudz2f5aaf62018-10-30 13:20:04 +020092 'glance_image_cirros_location':
93 'http://cz8133.bud.mirantis.net:8099'
94 '/cirros-0.3.5-x86_64-disk.img',
95 'glance_image_fedora_location':
96 'http://cz8133.bud.mirantis.net:8099'
97 '/Fedora-Cloud-Base-27-1.6.x86_64.qcow2',
98 'glance_image_manila_location':
99 'http://cz8133.bud.mirantis.net:8099'
100 '/manila-service-image-master.qcow2',
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000101 },
102 'neutron': {
103 'client': {
104 'enabled': True
105 }
106 },
107 'runtest': {
108 'enabled': True,
109 'keystonerc_node': 'ctl01*',
110 'tempest': {
111 'enabled': True,
112 'cfg_dir': '${_param:runtest_tempest_cfg_dir}',
113 'cfg_name': '${_param:runtest_tempest_cfg_name}',
Oleksii Butenko09cc0cd2018-10-16 14:31:57 +0300114 'put_keystone_rc_enabled': True,
115 'put_local_image_file_enabled': False,
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000116 'DEFAULT': {
117 'log_file': 'tempest.log'
118 },
119 'compute': {
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000120 'min_compute_nodes': 2,
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000121 },
122 'convert_to_uuid': {
123 'network': {
124 'public_network_id':
125 '${_param:runtest_tempest_public_net}'
126 }
127 },
Oleksii Butenko173e7702018-10-24 18:31:12 +0300128 'heat_plugin': {
129 'build_timeout': '600'
130 },
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000131 'share': {
132 'capability_snapshot_support': True,
133 'run_driver_assisted_migration_tests': False,
134 'run_manage_unmanage_snapshot_tests': False,
135 'run_manage_unmanage_tests': False,
136 'run_migration_with_preserve_snapshots_tests':
137 False,
138 'run_quota_tests': True,
139 'run_replication_tests': False,
140 'run_snapshot_tests': True,
141 }}}}}
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300142
obutenko3e44b602018-11-13 12:50:22 +0200143 if self.barbican:
144 pillar['classes'].append('service.runtest.tempest.barbican')
145
146 return pillar
147
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300148 def fetch_arficats(self, username=None, file_format='xml'):
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000149 with self.underlay.remote(node_name=self.target_name,
150 username=None) as tgt:
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300151 result = tgt.execute('find {} -name "report_*.{}"'.format(
152 TEMPEST_CFG_DIR, file_format))
153 LOG.debug("Find result {0}".format(result))
154 assert len(result['stdout']) > 0, ('No report found, please check'
155 ' if test run was successful.')
156 report = result['stdout'][0].rstrip()
157 LOG.debug("Found files {0}".format(report))
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300158 tgt.download(
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300159 destination=report, # noqa
160 target=os.getcwd())
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300161
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000162 def store_runtest_model(self, runtest_pillar=None):
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300163 with self.underlay.yaml_editor(
164 file_path="/srv/salt/reclass/classes/cluster/"
165 "{cluster_name}/infra/"
166 "{class_name}.yml".format(
167 cluster_name=self.cluster_name,
168 class_name=self.class_name),
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000169 node_name=self.master_name) as editor:
170 editor.content = runtest_pillar or self.runtest_pillar
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300171 with self.underlay.yaml_editor(
172 file_path="/srv/salt/reclass/nodes/_generated/"
173 "cfg01.{domain_name}.yml".format(
174 domain_name=self.domain_name),
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000175 node_name=self.master_name) as editor:
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300176 editor.content['classes'].append(
177 'cluster.{cluster_name}.infra.{class_name}'.format(
178 cluster_name=self.cluster_name,
179 class_name=self.class_name))
180
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300181 def save_runtime_logs(self, logs=None, inspect=None):
182 if logs:
183 with open("{path}/{target}_tempest_run.log".format(
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000184 path=settings.LOGS_DIR,
185 target=self.target_name), 'w') as f:
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300186 LOG.info("Save tempest console log")
187 container_log = logs
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300188 f.write(container_log.encode('ascii', 'ignore'))
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300189
190 if inspect:
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300191 with open("{path}/{target}_tempest_container_info.json.log".format(
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000192 path=settings.LOGS_DIR,
193 target=self.target_name), 'w') as f:
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300194 LOG.info("Save tempest container inspect data")
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300195
196 container_inspect = json.dumps(inspect,
197 indent=4, sort_keys=True)
198 f.write(container_inspect)
199
sgudza5c901d2018-10-29 15:08:33 +0200200 def prepare(self):
obutenko3e44b602018-11-13 12:50:22 +0200201 barbican_pillar = "nova:controller:barbican:enabled"
202 result = self.__salt_api.get_pillar(tgt=self.control_name,
203 pillar=barbican_pillar)
204 self.barbican = result[0].get(self.control_name, False)
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300205 self.store_runtest_model()
Oleksii Butenko09cc0cd2018-10-16 14:31:57 +0300206 cirros_pillar = ("salt-call --out=newline_values_only "
207 "pillar.get "
208 "glance:client:identity:"
209 "admin_identity:image:cirros:location")
sgudza5c901d2018-10-29 15:08:33 +0200210 dpdk_pillar = "linux:network:dpdk:enabled"
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000211 salt_cmd = "salt -l info --hard-crash --state-output=mixed "
212 salt_call_cmd = "salt-call -l info --hard-crash --state-output=mixed "
sgudza5c901d2018-10-29 15:08:33 +0200213
214 result = self.__salt_api.get_pillar(tgt=self.compute_name,
215 pillar=dpdk_pillar)
216
217 dpdk = result[0].get(self.compute_name, False)
218 LOG.info("DPDK enabled: {}".format(bool(dpdk)))
219
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000220 commands = [
221 {
222 'description': "Sync salt objects for runtest model",
223 'node_name': self.master_name,
224 'cmd': ("set -ex;" +
225 salt_cmd + "'*' saltutil.refresh_pillar && " +
226 salt_cmd + "'*' saltutil.sync_all")},
227 {
Tatyana Leontovicheea03e92018-11-20 12:51:50 +0200228 'description': ("Install docker-ce package and "
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000229 "enable packets forwarding"),
230 'node_name': self.target_name,
231 'cmd': ("set -ex;" +
Tatyana Leontovicheea03e92018-11-20 12:51:50 +0200232 salt_call_cmd + " pkg.install docker-ce && " +
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000233 " iptables --policy FORWARD ACCEPT")},
234 {
235 'description': "Install PyPI docker package",
236 'node_name': self.target_name,
237 'cmd': ("set -ex;" +
238 salt_call_cmd + " pip.install setuptools && " +
239 salt_call_cmd + " pip.install docker")},
240 {
241 'description': "Run salt.minion state for runtest formula",
242 'node_name': self.master_name,
243 'cmd': ("set -ex;" +
244 salt_call_cmd + " state.sls salt.minion && "
245 " sleep 20")},
246 {
247 'description': "Enforce keystone state for neutronv2",
248 'node_name': self.master_name,
249 'cmd': ("set -ex;" +
250 salt_call_cmd + " state.sls keystone.client")},
251 {
252 'description': "Create networks for Tempest tests",
253 'node_name': self.master_name,
254 'cmd': ("set -ex;" +
255 salt_call_cmd + " state.sls neutron.client")},
256 {
257 'description': "Create flavors for Tempest tests",
258 'node_name': self.master_name,
259 'cmd': ("set -ex;" +
260 salt_call_cmd + " state.sls nova.client")},
261 {
Oleksii Butenko09cc0cd2018-10-16 14:31:57 +0300262 'description': "Upload images for Tempest",
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000263 'node_name': self.master_name,
264 'cmd': ("set -ex;" +
265 salt_call_cmd + " state.sls glance.client")},
266 {
267 'description': "Generate config for Tempest",
268 'node_name': self.master_name,
269 'cmd': ("set -ex;" +
270 salt_call_cmd + " state.sls runtest")},
Oleksii Butenko09cc0cd2018-10-16 14:31:57 +0300271 {
272 'description': "Upload cirros image",
273 'node_name': self.master_name,
274 'cmd': ("set -ex;"
275 "cirros_url=$({}) && {} '{}' cmd.run "
276 "\"wget $cirros_url -O /tmp/TestCirros-0.3.5.img\""
277 .format(cirros_pillar, salt_cmd, self.target_name))},
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000278 ]
Oleksii Butenko5cd0a162018-06-14 18:18:10 +0300279
Tatyana Leontovich61a821d2018-08-21 12:30:51 +0300280 if dpdk:
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000281 commands.append({
282 'description': "Configure flavor for DPDK",
283 'node_name': self.control_name,
284 'cmd': ("set -ex;" +
285 salt_call_cmd + " cmd.run "
286 " '. /root/keystonercv3;"
sgudz905ec692018-10-16 15:30:39 +0300287 " openstack flavor set m1.extra_tiny_test"
288 " --property hw:mem_page_size=any;"
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000289 " openstack flavor set m1.tiny_test"
sgudz905ec692018-10-16 15:30:39 +0300290 " --property hw:mem_page_size=any'")},
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000291 )
Oleksii Butenko5cd0a162018-06-14 18:18:10 +0300292
obutenko3e44b602018-11-13 12:50:22 +0200293 if self.barbican:
294 commands.append({
295 'description': "Configure barbican",
296 'node_name': self.master_name,
297 'cmd': ("set -ex;" +
298 salt_call_cmd +
299 " state.sls barbican.client && " +
300 salt_call_cmd +
301 " state.sls runtest.test_accounts && " +
302 salt_call_cmd +
303 " state.sls runtest.barbican_sign_image")},
304 )
305
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000306 self.__salt_api.execute_commands(commands=commands,
307 label="Prepare for Tempest")
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300308
309 def run_tempest(self, timeout=600):
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000310 tgt = self.target_name
311 image_nameversion = "{}:{}".format(self.image_name, self.image_version)
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300312
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000313 docker_args = (
Oleksii Butenkob0ca4cc2018-10-22 16:29:54 +0300314 " -t "
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000315 " --name {container_name} "
316 " -e ARGS=\"-r {tempest_pattern} -w {tempest_threads}\""
317 " -v {cfg_dir}/tempest.conf:/etc/tempest/tempest.conf"
318 " -v /tmp/:/tmp/"
319 " -v {cfg_dir}:/root/tempest"
320 " -v /etc/ssl/certs/:/etc/ssl/certs/"
Oleksii Butenkob0ca4cc2018-10-22 16:29:54 +0300321 " -d "
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000322 " {image_nameversion} {run_cmd}"
323 .format(
324 container_name=self.container_name,
325 image_nameversion=image_nameversion,
326 tempest_pattern=self.tempest_pattern,
327 tempest_threads=self.tempest_threads,
328 cfg_dir=TEMPEST_CFG_DIR,
329 run_cmd=self.run_cmd,
330 ))
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300331
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000332 commands = [
333 {
334 'description': "Run Tempest tests {0}".format(
335 image_nameversion),
336 'node_name': self.target_name,
337 'cmd': ("set -ex;" +
338 " docker rm --force {container_name} || true;"
339 " docker run {docker_args}"
340 .format(container_name=self.container_name,
341 docker_args=docker_args)),
342 'timeout': timeout},
343 ]
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300344
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000345 self.__salt_api.execute_commands(commands=commands,
346 label="Run Tempest tests")
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300347
Oleksii Butenkob0ca4cc2018-10-22 16:29:54 +0300348 def wait_status(s):
349 inspect_res = self.salt_api.local(tgt,
350 'dockerng.inspect',
351 self.container_name)
352 if 'return' in inspect_res:
353 inspect = inspect_res['return']
354 inspect = inspect[0]
355 inspect = next(inspect.iteritems())[1]
356 status = inspect['State']['Status']
357
358 return status.lower() == s.lower()
359
360 return False
361
362 helpers.wait(lambda: wait_status('exited'),
363 timeout=timeout,
364 timeout_msg=('Tempest run didnt finished '
365 'in {}'.format(timeout)))
366
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300367 inspect_res = self.salt_api.local(tgt,
368 'dockerng.inspect',
369 self.container_name)
370 inspect = inspect_res['return'][0]
371 inspect = next(inspect.iteritems())[1]
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300372 logs_res = self.salt_api.local(tgt,
373 'dockerng.logs',
374 self.container_name)
375 logs = logs_res['return'][0]
376 logs = next(logs.iteritems())[1]
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300377
378 res = self.salt_api.local(tgt, 'dockerng.rm', self.container_name)
379 LOG.info("Tempest container was removed".format(
380 json.dumps(res, indent=4)))
381
382 return {'inspect': inspect,
383 'logs': logs}
Oleksii Butenkoe82441d2018-06-12 16:01:33 +0300384
sgudza5c901d2018-10-29 15:08:33 +0200385 def prepare_and_run_tempest(self, username='root'):
Oleksii Butenkoe82441d2018-06-12 16:01:33 +0300386 """
387 Run tempest tests
388 """
389 tempest_timeout = settings.TEMPEST_TIMEOUT
sgudza5c901d2018-10-29 15:08:33 +0200390 self.prepare()
Oleksii Butenkoe82441d2018-06-12 16:01:33 +0300391 test_res = self.run_tempest(tempest_timeout)
392 self.fetch_arficats(username=username)
393 self.save_runtime_logs(**test_res)