blob: b0e7c380412d4e89ef48a3bd7ad7af086a3eaabd [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,
Oleksii Butenko25a8f372019-03-22 17:25:47 +020042 tempest_extra_args=settings.TEMPEST_EXTRA_ARGS,
obutenkoc4314352018-12-26 16:14:24 +020043 run_cmd=None):
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000044 self.__config = config
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030045 self.underlay = underlay
46 self.__salt_api = salt_api
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030047 self.cluster_name = cluster_name
48 self.domain_name = domain_name
49 self.tempest_threads = tempest_threads
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030050 self.tempest_pattern = tempest_pattern
Oleksii Butenko25a8f372019-03-22 17:25:47 +020051 self.tempest_extra_args = tempest_extra_args
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030052 self.run_cmd = run_cmd or self.run_cmd
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000053 self.master_name = self.underlay.get_target_node_names(
54 self.master_host)[0]
Dennis Dmitriev1566e3f2019-01-11 17:35:43 +020055 self.master_minion = self.underlay.get_target_minion_ids(
56 self.master_host)[0]
obutenkoc4314352018-12-26 16:14:24 +020057 self.__target_name = None
sgudz6571a5e2019-02-26 15:49:22 +020058 self.__target_minion = None
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +030059
60 @property
61 def salt_api(self):
62 return self.__salt_api
63
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000064 @property
obutenkoc4314352018-12-26 16:14:24 +020065 def target_name(self):
66 if not self.__target_name:
obutenkoa1e3cea2019-01-12 21:11:55 +020067 target_host = self.__salt_api.get_single_pillar(
Dennis Dmitriev1566e3f2019-01-11 17:35:43 +020068 tgt=self.master_minion,
obutenkoc4314352018-12-26 16:14:24 +020069 pillar="runtest:tempest:test_target")
obutenkoc4314352018-12-26 16:14:24 +020070 if target_host[-1] == "*":
71 target_host = target_host[:-1]
72 self.__target_name = self.underlay.get_target_node_names(
73 target_host)[0]
74 return self.__target_name
obutenko3e44b602018-11-13 12:50:22 +020075
sgudz6571a5e2019-02-26 15:49:22 +020076 @property
77 def target_minion(self):
78 if not self.__target_minion:
79 target_host = self.__salt_api.get_single_pillar(
80 tgt=self.master_minion,
81 pillar="runtest:tempest:test_target")
82 if target_host[-1] == "*":
83 target_host = target_host[:-1]
84 self.__target_minion = self.underlay.get_target_minion_ids(
85 target_host)[0]
86 return self.__target_minion
87
Hanna Arhipovaa85bfa62020-01-03 18:49:15 +020088 def fetch_arficats(
89 self,
90 username=None,
91 file_format='xml',
92 report_pattern="report_*",
93 report_dir=TEMPEST_CFG_DIR):
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +000094 with self.underlay.remote(node_name=self.target_name,
95 username=None) as tgt:
Hanna Arhipovaa85bfa62020-01-03 18:49:15 +020096 result = tgt.execute('find {} -name "{}.{}"'.format(
97 report_dir, report_pattern, file_format))
Oleksii Butenko71d76f32018-06-05 17:46:34 +030098 LOG.debug("Find result {0}".format(result))
99 assert len(result['stdout']) > 0, ('No report found, please check'
100 ' if test run was successful.')
101 report = result['stdout'][0].rstrip()
102 LOG.debug("Found files {0}".format(report))
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300103 tgt.download(
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300104 destination=report, # noqa
105 target=os.getcwd())
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300106
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300107 def save_runtime_logs(self, logs=None, inspect=None):
108 if logs:
109 with open("{path}/{target}_tempest_run.log".format(
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000110 path=settings.LOGS_DIR,
111 target=self.target_name), 'w') as f:
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300112 LOG.info("Save tempest console log")
113 container_log = logs
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300114 f.write(container_log.encode('ascii', 'ignore'))
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300115
116 if inspect:
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300117 with open("{path}/{target}_tempest_container_info.json.log".format(
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000118 path=settings.LOGS_DIR,
119 target=self.target_name), 'w') as f:
Oleksii Butenko71d76f32018-06-05 17:46:34 +0300120 LOG.info("Save tempest container inspect data")
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300121
122 container_inspect = json.dumps(inspect,
123 indent=4, sort_keys=True)
124 f.write(container_inspect)
125
Hanna Arhipovaa85bfa62020-01-03 18:49:15 +0200126 def prepare(self, pipeline=False):
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000127 salt_call_cmd = "salt-call -l info --hard-crash --state-output=mixed "
obutenkoa1e3cea2019-01-12 21:11:55 +0200128 barbican_integration = self.__salt_api.get_single_pillar(
Tatyana Leontovich8b1fda02019-01-23 13:14:33 +0200129 tgt="ctl01*",
obutenkoa1e3cea2019-01-12 21:11:55 +0200130 pillar="_param:barbican_integration_enabled")
Oleksii Butenkod4b3dff2019-05-10 14:38:14 +0300131 if self.__salt_api.local('I@opencontrail:compute:enabled:true',
132 'match.pillar',
133 'opencontrail:compute:enabled:true'
134 ).get('return', [{}]) != [{}]:
135 contrail_integration = True
136 else:
137 contrail_integration = False
Tatyana Leontovich8b1fda02019-01-23 13:14:33 +0200138
obutenkoc48f4352019-01-24 17:03:06 +0200139 LOG.info("Barbican integration {0}".format(barbican_integration))
Oleksii Butenkod4b3dff2019-05-10 14:38:14 +0300140 LOG.info("Opencontrail integration {0}".format(contrail_integration))
141
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000142 commands = [
143 {
Pavel Glazov56f34372022-11-08 15:50:51 +0400144 'description': ("Enable packets forwarding"),
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000145 'node_name': self.target_name,
146 'cmd': ("set -ex;" +
Pavel Glazov56f34372022-11-08 15:50:51 +0400147 salt_call_cmd + "iptables --policy FORWARD ACCEPT")
148 }
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000149 ]
Hanna Arhipovaa85bfa62020-01-03 18:49:15 +0200150 if not pipeline:
151 commands.append(
152 {
153 'description': "Generate config for Tempest",
154 'node_name': self.master_name,
155 'cmd': ("set -ex;" +
156 "salt-run state.orchestrate " +
157 "runtest.orchestrate.tempest")
158 }
159 )
obutenkoa1e3cea2019-01-12 21:11:55 +0200160
Oleksii Butenkod4b3dff2019-05-10 14:38:14 +0300161 if contrail_integration:
162 vsrx_router = self.__salt_api.get_single_pillar(
163 tgt="I@opencontrail:control:role:primary",
Andrew Baraniuk6361cfc2019-06-04 12:58:14 +0300164 pillar="_param:opencontrail_router02_address")
Oleksii Butenko125dcf42019-07-23 16:34:40 +0300165 target_asn = '["target:64512:10000"]'
Oleksii Butenkod4b3dff2019-05-10 14:38:14 +0300166 public_network = "192.168.200.0"
167 contrail_commands = [
168 {
169 'description': "Iproute to vsrx router",
170 'node_name': self.target_name,
171 'cmd': ("set -ex; ip route replace " +
172 public_network + "/24 via " + vsrx_router)},
173 {
Andrew Baraniuk6361cfc2019-06-04 12:58:14 +0300174 'description': "Iptables for public",
175 'node_name': self.target_name,
176 'cmd': ("set -ex; iptables -t nat -A POSTROUTING -s " +
177 public_network + "/24 ! -d " + public_network +
178 "/24 -j MASQUERADE")},
Hanna Arhipovac5344b12020-12-08 17:06:40 +0200179 {
180 'description': "Run skipped in pipelines neutron.client",
181 'node_name': self.target_name,
182 'cmd': ("set -ex;" +
183 "salt -C 'I@neutron:client and cfg*' " +
184 "state.sls neutron.client|true")},
Oleksii Butenko125dcf42019-07-23 16:34:40 +0300185 {
Oleksii Butenkod4b3dff2019-05-10 14:38:14 +0300186 'description': "Create public network with target",
187 'node_name': self.target_name,
188 'cmd': ("set -ex;" +
189 "salt -C 'I@opencontrail:control:role:primary' " +
190 "contrail.virtual_network_create public " +
Oleksii Butenko125dcf42019-07-23 16:34:40 +0300191 "route_target_list='" + target_asn + "'")}
Oleksii Butenkod4b3dff2019-05-10 14:38:14 +0300192 ]
Oleksii Butenko125dcf42019-07-23 16:34:40 +0300193 commands = contrail_commands + commands
Oleksii Butenkod4b3dff2019-05-10 14:38:14 +0300194
obutenkoc48f4352019-01-24 17:03:06 +0200195 if barbican_integration:
obutenkoa1e3cea2019-01-12 21:11:55 +0200196 commands.append({
197 'description': "Configure barbican",
198 'node_name': self.master_name,
199 'cmd': ("set -ex;" +
200 salt_call_cmd +
201 " state.sls barbican.client && " +
202 salt_call_cmd +
obutenkoc48f4352019-01-24 17:03:06 +0200203 " state.sls runtest.test_accounts" +
obutenkoa1e3cea2019-01-12 21:11:55 +0200204 salt_call_cmd +
205 " state.sls runtest.barbican_sign_image")},
206 )
207
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000208 self.__salt_api.execute_commands(commands=commands,
209 label="Prepare for Tempest")
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300210
211 def run_tempest(self, timeout=600):
sgudz6571a5e2019-02-26 15:49:22 +0200212 tgt = self.target_minion
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000213 image_nameversion = "{}:{}".format(self.image_name, self.image_version)
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300214
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000215 docker_args = (
Oleksii Butenko25a8f372019-03-22 17:25:47 +0200216 " -t"
217 " --net host"
218 " --name {container_name}"
219 " -e ARGS=\"-r {tempest_pattern}"
220 " -w {tempest_threads} {tempest_extra_args}\""
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000221 " -v {cfg_dir}/tempest.conf:/etc/tempest/tempest.conf"
222 " -v /tmp/:/tmp/"
223 " -v {cfg_dir}:/root/tempest"
224 " -v /etc/ssl/certs/:/etc/ssl/certs/"
Oleksii Butenko25a8f372019-03-22 17:25:47 +0200225 " -d"
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000226 " {image_nameversion} {run_cmd}"
227 .format(
228 container_name=self.container_name,
229 image_nameversion=image_nameversion,
230 tempest_pattern=self.tempest_pattern,
231 tempest_threads=self.tempest_threads,
Oleksii Butenko25a8f372019-03-22 17:25:47 +0200232 tempest_extra_args=self.tempest_extra_args,
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000233 cfg_dir=TEMPEST_CFG_DIR,
234 run_cmd=self.run_cmd,
235 ))
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300236
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000237 commands = [
238 {
239 'description': "Run Tempest tests {0}".format(
240 image_nameversion),
241 'node_name': self.target_name,
242 'cmd': ("set -ex;" +
243 " docker rm --force {container_name} || true;"
244 " docker run {docker_args}"
245 .format(container_name=self.container_name,
246 docker_args=docker_args)),
247 'timeout': timeout},
248 ]
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300249
Dennis Dmitrievb6bcc5c2018-09-26 11:07:53 +0000250 self.__salt_api.execute_commands(commands=commands,
251 label="Run Tempest tests")
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300252
sgudz6571a5e2019-02-26 15:49:22 +0200253 def simplify_salt_api_return(api_return, only_first_match=True):
254 """
255 Salt API always returns a dict with one key as 'return'
256 and value as a list with dict. For example:
257 For single node:
258 api.local('cfg01*', 'test.ping', expr_form='compound')
259 {u'return':[{u'cfg01.cookied-cicd-queens-dvr-sl.local':True}]}
260 For multinode:
261 api.local('ctl*', 'test.ping', expr_form='compound')
262 {u'return': [{u'ctl01.cookied-cicd-queens-dvr-sl.local': True,
263 u'ctl02.cookied-cicd-queens-dvr-sl.local': True,
264 u'ctl03.cookied-cicd-queens-dvr-sl.local': True}]}
265 When wrong function is given:
266 api.local('ctl01*', 'wrong_func', expr_form='compound')
267 {u'return': [{u'ctl01.cookied-cicd-queens-dvr-sl.local':
268 u"'wrong_func' is not available."}]}
269 Empty return:
270 api.local('wrong_target', 'test.ping', expr_form='compound')
271 {u'return': [{}]}
272 """
273 if api_return.get('return', [{}]) != [{}]:
274 api_return = api_return['return'][0]
275 if only_first_match:
276 api_return = next(api_return.iteritems())[1]
sgudz6571a5e2019-02-26 15:49:22 +0200277 return api_return
278 else:
279 LOG.info('''Salt api returns empty result: [{}]''')
280 return False
281
Oleksii Butenkob0ca4cc2018-10-22 16:29:54 +0300282 def wait_status(s):
sgudz6571a5e2019-02-26 15:49:22 +0200283 inspect_res = self.salt_api.local(tgt, 'dockerng.inspect',
Oleksii Butenkob0ca4cc2018-10-22 16:29:54 +0300284 self.container_name)
sgudz6571a5e2019-02-26 15:49:22 +0200285 inspect = simplify_salt_api_return(inspect_res)
286 if inspect:
Oleksii Butenkob0ca4cc2018-10-22 16:29:54 +0300287 status = inspect['State']['Status']
Oleksii Butenkob0ca4cc2018-10-22 16:29:54 +0300288 return status.lower() == s.lower()
Oleksii Butenkob0ca4cc2018-10-22 16:29:54 +0300289 return False
290
sgudz6571a5e2019-02-26 15:49:22 +0200291 if wait_status('running'):
292 helpers.wait(lambda: wait_status('exited'),
293 timeout=timeout,
294 timeout_msg=('Tempest run didnt finished '
295 'in {}'.format(timeout)))
Oleksii Butenkob0ca4cc2018-10-22 16:29:54 +0300296
sgudz6571a5e2019-02-26 15:49:22 +0200297 inspect_res = self.salt_api.local(tgt, 'dockerng.inspect',
298 self.container_name)
299 inspect = simplify_salt_api_return(inspect_res)
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300300
sgudz6571a5e2019-02-26 15:49:22 +0200301 logs_res = self.salt_api.local(tgt, 'dockerng.logs',
302 self.container_name)
303 logs = simplify_salt_api_return(logs_res)
sgudz2457db82019-03-01 16:06:36 +0200304 rm_res = self.salt_api.local(tgt, 'dockerng.rm',
305 self.container_name)
306 LOG.info("Tempest container was removed: {}".format(
307 json.dumps(rm_res, indent=4)))
sgudz6571a5e2019-02-26 15:49:22 +0200308 else:
309 inspect_res = self.salt_api.local(tgt, 'dockerng.inspect',
310 self.container_name)
311 inspect = simplify_salt_api_return(inspect_res)
312 if inspect:
313 status = inspect['State']['Status']
314 LOG.info("Container is not in RUNNING state. "
315 "Current container status is {}".format(status))
316 logs_res = self.salt_api.local(tgt,
317 'dockerng.logs',
318 self.container_name)
319 logs = simplify_salt_api_return(logs_res)
320 else:
321 LOG.info("dockerng returns unexpected"
322 " result: {}".format(inspect_res))
323 logs = None
324 inspect = None
Dmitry Tyzhnenkoc56b77e2018-05-21 11:01:43 +0300325
326 return {'inspect': inspect,
327 'logs': logs}
Oleksii Butenkoe82441d2018-06-12 16:01:33 +0300328
obutenkoc4314352018-12-26 16:14:24 +0200329 def prepare_and_run_tempest(self, username='root'):
Oleksii Butenkoe82441d2018-06-12 16:01:33 +0300330 """
331 Run tempest tests
332 """
333 tempest_timeout = settings.TEMPEST_TIMEOUT
obutenkoc4314352018-12-26 16:14:24 +0200334 self.prepare()
Oleksii Butenkoe82441d2018-06-12 16:01:33 +0300335 test_res = self.run_tempest(tempest_timeout)
336 self.fetch_arficats(username=username)
337 self.save_runtime_logs(**test_res)