blob: a354d0101e1078bcb660204d8fcfe21ec70948c2 [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"
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030036 class_name = "runtest"
37 run_cmd = '/bin/bash -c "run-tempest"'
38
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000039 def __init__(self, config, underlay, salt_api, cluster_name,
Oleksii Butenko74566102018-09-11 11:55:13 +030040 domain_name, tempest_threads,
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030041 tempest_pattern=settings.TEMPEST_PATTERN,
obutenkoc4314352018-12-26 16:14:24 +020042 run_cmd=None):
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000043 self.__config = config
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030044 self.underlay = underlay
45 self.__salt_api = salt_api
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030046 self.cluster_name = cluster_name
47 self.domain_name = domain_name
48 self.tempest_threads = tempest_threads
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030049 self.tempest_pattern = tempest_pattern
50 self.run_cmd = run_cmd or self.run_cmd
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000051 self.master_name = self.underlay.get_target_node_names(
52 self.master_host)[0]
obutenkoc4314352018-12-26 16:14:24 +020053 self.__target_name = None
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030054
55 @property
56 def salt_api(self):
57 return self.__salt_api
58
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000059 @property
obutenkoc4314352018-12-26 16:14:24 +020060 def target_name(self):
61 if not self.__target_name:
62 result = self.salt_api.get_pillar(
63 tgt=self.master_name,
64 pillar="runtest:tempest:test_target")
65 target_host = (result[0].get(self.master_name))
66 if target_host[-1] == "*":
67 target_host = target_host[:-1]
68 self.__target_name = self.underlay.get_target_node_names(
69 target_host)[0]
70 return self.__target_name
obutenko3e44b602018-11-13 12:50:22 +020071
Oleksii Butenko71d76f32018-06-05 17:46:34 +030072 def fetch_arficats(self, username=None, file_format='xml'):
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000073 with self.underlay.remote(node_name=self.target_name,
74 username=None) as tgt:
Oleksii Butenko71d76f32018-06-05 17:46:34 +030075 result = tgt.execute('find {} -name "report_*.{}"'.format(
76 TEMPEST_CFG_DIR, file_format))
77 LOG.debug("Find result {0}".format(result))
78 assert len(result['stdout']) > 0, ('No report found, please check'
79 ' if test run was successful.')
80 report = result['stdout'][0].rstrip()
81 LOG.debug("Found files {0}".format(report))
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030082 tgt.download(
Oleksii Butenko71d76f32018-06-05 17:46:34 +030083 destination=report, # noqa
84 target=os.getcwd())
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030085
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030086 def save_runtime_logs(self, logs=None, inspect=None):
87 if logs:
88 with open("{path}/{target}_tempest_run.log".format(
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000089 path=settings.LOGS_DIR,
90 target=self.target_name), 'w') as f:
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030091 LOG.info("Save tempest console log")
92 container_log = logs
Oleksii Butenko71d76f32018-06-05 17:46:34 +030093 f.write(container_log.encode('ascii', 'ignore'))
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030094
95 if inspect:
Oleksii Butenko71d76f32018-06-05 17:46:34 +030096 with open("{path}/{target}_tempest_container_info.json.log".format(
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000097 path=settings.LOGS_DIR,
98 target=self.target_name), 'w') as f:
Oleksii Butenko71d76f32018-06-05 17:46:34 +030099 LOG.info("Save tempest container inspect data")
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300100
101 container_inspect = json.dumps(inspect,
102 indent=4, sort_keys=True)
103 f.write(container_inspect)
104
obutenkoc4314352018-12-26 16:14:24 +0200105 def prepare(self):
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000106 salt_call_cmd = "salt-call -l info --hard-crash --state-output=mixed "
107 commands = [
108 {
Tatyana Leontovicheea03e92018-11-20 12:51:50 +0200109 'description': ("Install docker-ce package and "
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000110 "enable packets forwarding"),
111 'node_name': self.target_name,
112 'cmd': ("set -ex;" +
Tatyana Leontovicheea03e92018-11-20 12:51:50 +0200113 salt_call_cmd + " pkg.install docker-ce && " +
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000114 " iptables --policy FORWARD ACCEPT")},
115 {
116 'description': "Install PyPI docker package",
117 'node_name': self.target_name,
118 'cmd': ("set -ex;" +
119 salt_call_cmd + " pip.install setuptools && " +
120 salt_call_cmd + " pip.install docker")},
121 {
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000122 'description': "Generate config for Tempest",
123 'node_name': self.master_name,
124 'cmd': ("set -ex;" +
obutenkoc4314352018-12-26 16:14:24 +0200125 "salt-run state.orchestrate " +
126 "runtest.orchestrate.tempest")},
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000127 ]
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000128 self.__salt_api.execute_commands(commands=commands,
129 label="Prepare for Tempest")
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300130
131 def run_tempest(self, timeout=600):
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000132 tgt = self.target_name
133 image_nameversion = "{}:{}".format(self.image_name, self.image_version)
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300134
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000135 docker_args = (
Oleksii Butenkob0ca4cc2018-10-22 16:29:54 +0300136 " -t "
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000137 " --name {container_name} "
138 " -e ARGS=\"-r {tempest_pattern} -w {tempest_threads}\""
139 " -v {cfg_dir}/tempest.conf:/etc/tempest/tempest.conf"
140 " -v /tmp/:/tmp/"
141 " -v {cfg_dir}:/root/tempest"
142 " -v /etc/ssl/certs/:/etc/ssl/certs/"
Oleksii Butenkob0ca4cc2018-10-22 16:29:54 +0300143 " -d "
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000144 " {image_nameversion} {run_cmd}"
145 .format(
146 container_name=self.container_name,
147 image_nameversion=image_nameversion,
148 tempest_pattern=self.tempest_pattern,
149 tempest_threads=self.tempest_threads,
150 cfg_dir=TEMPEST_CFG_DIR,
151 run_cmd=self.run_cmd,
152 ))
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300153
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000154 commands = [
155 {
156 'description': "Run Tempest tests {0}".format(
157 image_nameversion),
158 'node_name': self.target_name,
159 'cmd': ("set -ex;" +
160 " docker rm --force {container_name} || true;"
161 " docker run {docker_args}"
162 .format(container_name=self.container_name,
163 docker_args=docker_args)),
164 'timeout': timeout},
165 ]
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300166
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000167 self.__salt_api.execute_commands(commands=commands,
168 label="Run Tempest tests")
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300169
Oleksii Butenkob0ca4cc2018-10-22 16:29:54 +0300170 def wait_status(s):
171 inspect_res = self.salt_api.local(tgt,
172 'dockerng.inspect',
173 self.container_name)
174 if 'return' in inspect_res:
175 inspect = inspect_res['return']
176 inspect = inspect[0]
177 inspect = next(inspect.iteritems())[1]
178 status = inspect['State']['Status']
179
180 return status.lower() == s.lower()
181
182 return False
183
184 helpers.wait(lambda: wait_status('exited'),
185 timeout=timeout,
186 timeout_msg=('Tempest run didnt finished '
187 'in {}'.format(timeout)))
188
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300189 inspect_res = self.salt_api.local(tgt,
190 'dockerng.inspect',
191 self.container_name)
192 inspect = inspect_res['return'][0]
193 inspect = next(inspect.iteritems())[1]
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300194 logs_res = self.salt_api.local(tgt,
195 'dockerng.logs',
196 self.container_name)
197 logs = logs_res['return'][0]
198 logs = next(logs.iteritems())[1]
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300199
200 res = self.salt_api.local(tgt, 'dockerng.rm', self.container_name)
201 LOG.info("Tempest container was removed".format(
202 json.dumps(res, indent=4)))
203
204 return {'inspect': inspect,
205 'logs': logs}
Oleksii Butenkoe82441d2018-06-12 16:01:33 +0300206
obutenkoc4314352018-12-26 16:14:24 +0200207 def prepare_and_run_tempest(self, username='root'):
Oleksii Butenkoe82441d2018-06-12 16:01:33 +0300208 """
209 Run tempest tests
210 """
211 tempest_timeout = settings.TEMPEST_TIMEOUT
obutenkoc4314352018-12-26 16:14:24 +0200212 self.prepare()
Oleksii Butenkoe82441d2018-06-12 16:01:33 +0300213 test_res = self.run_tempest(tempest_timeout)
214 self.fetch_arficats(username=username)
215 self.save_runtime_logs(**test_res)