blob: a45663bdb9f84abfabbd8edaef8b6f5e19a1f0e2 [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(
107 tgt="I@barbican:client and ctl*",
108 pillar="_param:barbican_integration_enabled")
109 LOG.info("barbican_integration: {}".format(barbican_integration))
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000110 commands = [
111 {
Tatyana Leontovicheea03e92018-11-20 12:51:50 +0200112 'description': ("Install docker-ce package and "
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000113 "enable packets forwarding"),
114 'node_name': self.target_name,
115 'cmd': ("set -ex;" +
Tatyana Leontovicheea03e92018-11-20 12:51:50 +0200116 salt_call_cmd + " pkg.install docker-ce && " +
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000117 " iptables --policy FORWARD ACCEPT")},
118 {
119 'description': "Install PyPI docker package",
120 'node_name': self.target_name,
121 'cmd': ("set -ex;" +
122 salt_call_cmd + " pip.install setuptools && " +
123 salt_call_cmd + " pip.install docker")},
124 {
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000125 'description': "Generate config for Tempest",
126 'node_name': self.master_name,
127 'cmd': ("set -ex;" +
obutenkoc4314352018-12-26 16:14:24 +0200128 "salt-run state.orchestrate " +
129 "runtest.orchestrate.tempest")},
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000130 ]
obutenkoa1e3cea2019-01-12 21:11:55 +0200131
132 if barbican_integration == 'True':
133 commands.append({
134 'description': "Configure barbican",
135 'node_name': self.master_name,
136 'cmd': ("set -ex;" +
137 salt_call_cmd +
138 " state.sls barbican.client && " +
139 salt_call_cmd +
140 " state.sls runtest.test_accounts && " +
141 salt_call_cmd +
142 " state.sls runtest.barbican_sign_image")},
143 )
144
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000145 self.__salt_api.execute_commands(commands=commands,
146 label="Prepare for Tempest")
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300147
148 def run_tempest(self, timeout=600):
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000149 tgt = self.target_name
150 image_nameversion = "{}:{}".format(self.image_name, self.image_version)
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300151
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000152 docker_args = (
Oleksii Butenkob0ca4cc2018-10-22 16:29:54 +0300153 " -t "
obutenkoa1e3cea2019-01-12 21:11:55 +0200154 " --net host "
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000155 " --name {container_name} "
156 " -e ARGS=\"-r {tempest_pattern} -w {tempest_threads}\""
157 " -v {cfg_dir}/tempest.conf:/etc/tempest/tempest.conf"
158 " -v /tmp/:/tmp/"
159 " -v {cfg_dir}:/root/tempest"
160 " -v /etc/ssl/certs/:/etc/ssl/certs/"
Oleksii Butenkob0ca4cc2018-10-22 16:29:54 +0300161 " -d "
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000162 " {image_nameversion} {run_cmd}"
163 .format(
164 container_name=self.container_name,
165 image_nameversion=image_nameversion,
166 tempest_pattern=self.tempest_pattern,
167 tempest_threads=self.tempest_threads,
168 cfg_dir=TEMPEST_CFG_DIR,
169 run_cmd=self.run_cmd,
170 ))
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300171
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000172 commands = [
173 {
174 'description': "Run Tempest tests {0}".format(
175 image_nameversion),
176 'node_name': self.target_name,
177 'cmd': ("set -ex;" +
178 " docker rm --force {container_name} || true;"
179 " docker run {docker_args}"
180 .format(container_name=self.container_name,
181 docker_args=docker_args)),
182 'timeout': timeout},
183 ]
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300184
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000185 self.__salt_api.execute_commands(commands=commands,
186 label="Run Tempest tests")
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300187
Oleksii Butenkob0ca4cc2018-10-22 16:29:54 +0300188 def wait_status(s):
189 inspect_res = self.salt_api.local(tgt,
190 'dockerng.inspect',
191 self.container_name)
192 if 'return' in inspect_res:
193 inspect = inspect_res['return']
194 inspect = inspect[0]
195 inspect = next(inspect.iteritems())[1]
196 status = inspect['State']['Status']
197
198 return status.lower() == s.lower()
199
200 return False
201
202 helpers.wait(lambda: wait_status('exited'),
203 timeout=timeout,
204 timeout_msg=('Tempest run didnt finished '
205 'in {}'.format(timeout)))
206
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300207 inspect_res = self.salt_api.local(tgt,
208 'dockerng.inspect',
209 self.container_name)
210 inspect = inspect_res['return'][0]
211 inspect = next(inspect.iteritems())[1]
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300212 logs_res = self.salt_api.local(tgt,
213 'dockerng.logs',
214 self.container_name)
215 logs = logs_res['return'][0]
216 logs = next(logs.iteritems())[1]
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300217
218 res = self.salt_api.local(tgt, 'dockerng.rm', self.container_name)
219 LOG.info("Tempest container was removed".format(
220 json.dumps(res, indent=4)))
221
222 return {'inspect': inspect,
223 'logs': logs}
Oleksii Butenkoe82441d2018-06-12 16:01:33 +0300224
obutenkoc4314352018-12-26 16:14:24 +0200225 def prepare_and_run_tempest(self, username='root'):
Oleksii Butenkoe82441d2018-06-12 16:01:33 +0300226 """
227 Run tempest tests
228 """
229 tempest_timeout = settings.TEMPEST_TIMEOUT
obutenkoc4314352018-12-26 16:14:24 +0200230 self.prepare()
Oleksii Butenkoe82441d2018-06-12 16:01:33 +0300231 test_res = self.run_tempest(tempest_timeout)
232 self.fetch_arficats(username=username)
233 self.save_runtime_logs(**test_res)