blob: 7325e2e86a64de1d0633e685695d7c745287ed41 [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:
obutenkoa1e3cea2019-01-12 21:11:55 +020062 target_host = self.__salt_api.get_single_pillar(
obutenkoc4314352018-12-26 16:14:24 +020063 tgt=self.master_name,
64 pillar="runtest:tempest:test_target")
obutenkoc4314352018-12-26 16:14:24 +020065 if target_host[-1] == "*":
66 target_host = target_host[:-1]
67 self.__target_name = self.underlay.get_target_node_names(
68 target_host)[0]
69 return self.__target_name
obutenko3e44b602018-11-13 12:50:22 +020070
Oleksii Butenko71d76f32018-06-05 17:46:34 +030071 def fetch_arficats(self, username=None, file_format='xml'):
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000072 with self.underlay.remote(node_name=self.target_name,
73 username=None) as tgt:
Oleksii Butenko71d76f32018-06-05 17:46:34 +030074 result = tgt.execute('find {} -name "report_*.{}"'.format(
75 TEMPEST_CFG_DIR, file_format))
76 LOG.debug("Find result {0}".format(result))
77 assert len(result['stdout']) > 0, ('No report found, please check'
78 ' if test run was successful.')
79 report = result['stdout'][0].rstrip()
80 LOG.debug("Found files {0}".format(report))
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030081 tgt.download(
Oleksii Butenko71d76f32018-06-05 17:46:34 +030082 destination=report, # noqa
83 target=os.getcwd())
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030084
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030085 def save_runtime_logs(self, logs=None, inspect=None):
86 if logs:
87 with open("{path}/{target}_tempest_run.log".format(
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000088 path=settings.LOGS_DIR,
89 target=self.target_name), 'w') as f:
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030090 LOG.info("Save tempest console log")
91 container_log = logs
Oleksii Butenko71d76f32018-06-05 17:46:34 +030092 f.write(container_log.encode('ascii', 'ignore'))
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030093
94 if inspect:
Oleksii Butenko71d76f32018-06-05 17:46:34 +030095 with open("{path}/{target}_tempest_container_info.json.log".format(
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000096 path=settings.LOGS_DIR,
97 target=self.target_name), 'w') as f:
Oleksii Butenko71d76f32018-06-05 17:46:34 +030098 LOG.info("Save tempest container inspect data")
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030099
100 container_inspect = json.dumps(inspect,
101 indent=4, sort_keys=True)
102 f.write(container_inspect)
103
obutenkoc4314352018-12-26 16:14:24 +0200104 def prepare(self):
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000105 salt_call_cmd = "salt-call -l info --hard-crash --state-output=mixed "
obutenkoa1e3cea2019-01-12 21:11:55 +0200106 barbican_integration = self.__salt_api.get_single_pillar(
Tatyana Leontovich8b1fda02019-01-23 13:14:33 +0200107 tgt="ctl01*",
obutenkoa1e3cea2019-01-12 21:11:55 +0200108 pillar="_param:barbican_integration_enabled")
Tatyana Leontovich8b1fda02019-01-23 13:14:33 +0200109
obutenkoc48f4352019-01-24 17:03:06 +0200110 LOG.info("Barbican integration {0}".format(barbican_integration))
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000111 commands = [
112 {
Tatyana Leontovicheea03e92018-11-20 12:51:50 +0200113 'description': ("Install docker-ce package and "
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000114 "enable packets forwarding"),
115 'node_name': self.target_name,
116 'cmd': ("set -ex;" +
Tatyana Leontovicheea03e92018-11-20 12:51:50 +0200117 salt_call_cmd + " pkg.install docker-ce && " +
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000118 " iptables --policy FORWARD ACCEPT")},
119 {
120 'description': "Install PyPI docker package",
121 'node_name': self.target_name,
122 'cmd': ("set -ex;" +
123 salt_call_cmd + " pip.install setuptools && " +
124 salt_call_cmd + " pip.install docker")},
125 {
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000126 'description': "Generate config for Tempest",
127 'node_name': self.master_name,
128 'cmd': ("set -ex;" +
obutenkoc4314352018-12-26 16:14:24 +0200129 "salt-run state.orchestrate " +
130 "runtest.orchestrate.tempest")},
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000131 ]
obutenkoa1e3cea2019-01-12 21:11:55 +0200132
obutenkoc48f4352019-01-24 17:03:06 +0200133 if barbican_integration:
obutenkoa1e3cea2019-01-12 21:11:55 +0200134 commands.append({
135 'description': "Configure barbican",
136 'node_name': self.master_name,
137 'cmd': ("set -ex;" +
138 salt_call_cmd +
139 " state.sls barbican.client && " +
140 salt_call_cmd +
obutenkoc48f4352019-01-24 17:03:06 +0200141 " state.sls runtest.test_accounts" +
obutenkoa1e3cea2019-01-12 21:11:55 +0200142 salt_call_cmd +
143 " state.sls runtest.barbican_sign_image")},
144 )
145
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000146 self.__salt_api.execute_commands(commands=commands,
147 label="Prepare for Tempest")
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300148
149 def run_tempest(self, timeout=600):
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000150 tgt = self.target_name
151 image_nameversion = "{}:{}".format(self.image_name, self.image_version)
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300152
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000153 docker_args = (
Oleksii Butenkob0ca4cc2018-10-22 16:29:54 +0300154 " -t "
obutenkoa1e3cea2019-01-12 21:11:55 +0200155 " --net host "
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000156 " --name {container_name} "
157 " -e ARGS=\"-r {tempest_pattern} -w {tempest_threads}\""
158 " -v {cfg_dir}/tempest.conf:/etc/tempest/tempest.conf"
159 " -v /tmp/:/tmp/"
160 " -v {cfg_dir}:/root/tempest"
161 " -v /etc/ssl/certs/:/etc/ssl/certs/"
Oleksii Butenkob0ca4cc2018-10-22 16:29:54 +0300162 " -d "
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000163 " {image_nameversion} {run_cmd}"
164 .format(
165 container_name=self.container_name,
166 image_nameversion=image_nameversion,
167 tempest_pattern=self.tempest_pattern,
168 tempest_threads=self.tempest_threads,
169 cfg_dir=TEMPEST_CFG_DIR,
170 run_cmd=self.run_cmd,
171 ))
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300172
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000173 commands = [
174 {
175 'description': "Run Tempest tests {0}".format(
176 image_nameversion),
177 'node_name': self.target_name,
178 'cmd': ("set -ex;" +
179 " docker rm --force {container_name} || true;"
180 " docker run {docker_args}"
181 .format(container_name=self.container_name,
182 docker_args=docker_args)),
183 'timeout': timeout},
184 ]
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300185
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000186 self.__salt_api.execute_commands(commands=commands,
187 label="Run Tempest tests")
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300188
Oleksii Butenkob0ca4cc2018-10-22 16:29:54 +0300189 def wait_status(s):
190 inspect_res = self.salt_api.local(tgt,
191 'dockerng.inspect',
192 self.container_name)
193 if 'return' in inspect_res:
194 inspect = inspect_res['return']
195 inspect = inspect[0]
196 inspect = next(inspect.iteritems())[1]
197 status = inspect['State']['Status']
198
199 return status.lower() == s.lower()
200
201 return False
202
203 helpers.wait(lambda: wait_status('exited'),
204 timeout=timeout,
205 timeout_msg=('Tempest run didnt finished '
206 'in {}'.format(timeout)))
207
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300208 inspect_res = self.salt_api.local(tgt,
209 'dockerng.inspect',
210 self.container_name)
211 inspect = inspect_res['return'][0]
212 inspect = next(inspect.iteritems())[1]
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300213 logs_res = self.salt_api.local(tgt,
214 'dockerng.logs',
215 self.container_name)
216 logs = logs_res['return'][0]
217 logs = next(logs.iteritems())[1]
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300218
219 res = self.salt_api.local(tgt, 'dockerng.rm', self.container_name)
220 LOG.info("Tempest container was removed".format(
221 json.dumps(res, indent=4)))
222
223 return {'inspect': inspect,
224 'logs': logs}
Oleksii Butenkoe82441d2018-06-12 16:01:33 +0300225
obutenkoc4314352018-12-26 16:14:24 +0200226 def prepare_and_run_tempest(self, username='root'):
Oleksii Butenkoe82441d2018-06-12 16:01:33 +0300227 """
228 Run tempest tests
229 """
230 tempest_timeout = settings.TEMPEST_TIMEOUT
obutenkoc4314352018-12-26 16:14:24 +0200231 self.prepare()
Oleksii Butenkoe82441d2018-06-12 16:01:33 +0300232 test_res = self.run_tempest(tempest_timeout)
233 self.fetch_arficats(username=username)
234 self.save_runtime_logs(**test_res)