Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 1 | # 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 Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 15 | import json |
Oleksii Butenko | 71d76f3 | 2018-06-05 17:46:34 +0300 | [diff] [blame] | 16 | import os |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 17 | |
Oleksii Butenko | b0ca4cc | 2018-10-22 16:29:54 +0300 | [diff] [blame] | 18 | from devops.helpers import helpers |
| 19 | |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 20 | from tcp_tests import logger |
| 21 | from tcp_tests import settings |
| 22 | |
Oleksii Butenko | b0ca4cc | 2018-10-22 16:29:54 +0300 | [diff] [blame] | 23 | |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 24 | LOG = logger.logger |
| 25 | |
| 26 | TEMPEST_CFG_DIR = '/tmp/test' |
| 27 | |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 28 | |
| 29 | class 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 Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 36 | class_name = "runtest" |
| 37 | run_cmd = '/bin/bash -c "run-tempest"' |
| 38 | |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 39 | def __init__(self, config, underlay, salt_api, cluster_name, |
Oleksii Butenko | 7456610 | 2018-09-11 11:55:13 +0300 | [diff] [blame] | 40 | domain_name, tempest_threads, |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 41 | tempest_pattern=settings.TEMPEST_PATTERN, |
obutenko | c431435 | 2018-12-26 16:14:24 +0200 | [diff] [blame] | 42 | run_cmd=None): |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 43 | self.__config = config |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 44 | self.underlay = underlay |
| 45 | self.__salt_api = salt_api |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 46 | self.cluster_name = cluster_name |
| 47 | self.domain_name = domain_name |
| 48 | self.tempest_threads = tempest_threads |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 49 | self.tempest_pattern = tempest_pattern |
| 50 | self.run_cmd = run_cmd or self.run_cmd |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 51 | self.master_name = self.underlay.get_target_node_names( |
| 52 | self.master_host)[0] |
obutenko | c431435 | 2018-12-26 16:14:24 +0200 | [diff] [blame] | 53 | self.__target_name = None |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 54 | |
| 55 | @property |
| 56 | def salt_api(self): |
| 57 | return self.__salt_api |
| 58 | |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 59 | @property |
obutenko | c431435 | 2018-12-26 16:14:24 +0200 | [diff] [blame] | 60 | 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 |
obutenko | 3e44b60 | 2018-11-13 12:50:22 +0200 | [diff] [blame] | 71 | |
Oleksii Butenko | 71d76f3 | 2018-06-05 17:46:34 +0300 | [diff] [blame] | 72 | def fetch_arficats(self, username=None, file_format='xml'): |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 73 | with self.underlay.remote(node_name=self.target_name, |
| 74 | username=None) as tgt: |
Oleksii Butenko | 71d76f3 | 2018-06-05 17:46:34 +0300 | [diff] [blame] | 75 | 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 Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 82 | tgt.download( |
Oleksii Butenko | 71d76f3 | 2018-06-05 17:46:34 +0300 | [diff] [blame] | 83 | destination=report, # noqa |
| 84 | target=os.getcwd()) |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 85 | |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 86 | def save_runtime_logs(self, logs=None, inspect=None): |
| 87 | if logs: |
| 88 | with open("{path}/{target}_tempest_run.log".format( |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 89 | path=settings.LOGS_DIR, |
| 90 | target=self.target_name), 'w') as f: |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 91 | LOG.info("Save tempest console log") |
| 92 | container_log = logs |
Oleksii Butenko | 71d76f3 | 2018-06-05 17:46:34 +0300 | [diff] [blame] | 93 | f.write(container_log.encode('ascii', 'ignore')) |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 94 | |
| 95 | if inspect: |
Oleksii Butenko | 71d76f3 | 2018-06-05 17:46:34 +0300 | [diff] [blame] | 96 | with open("{path}/{target}_tempest_container_info.json.log".format( |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 97 | path=settings.LOGS_DIR, |
| 98 | target=self.target_name), 'w') as f: |
Oleksii Butenko | 71d76f3 | 2018-06-05 17:46:34 +0300 | [diff] [blame] | 99 | LOG.info("Save tempest container inspect data") |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 100 | |
| 101 | container_inspect = json.dumps(inspect, |
| 102 | indent=4, sort_keys=True) |
| 103 | f.write(container_inspect) |
| 104 | |
obutenko | c431435 | 2018-12-26 16:14:24 +0200 | [diff] [blame] | 105 | def prepare(self): |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 106 | salt_call_cmd = "salt-call -l info --hard-crash --state-output=mixed " |
| 107 | commands = [ |
| 108 | { |
Tatyana Leontovich | eea03e9 | 2018-11-20 12:51:50 +0200 | [diff] [blame] | 109 | 'description': ("Install docker-ce package and " |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 110 | "enable packets forwarding"), |
| 111 | 'node_name': self.target_name, |
| 112 | 'cmd': ("set -ex;" + |
Tatyana Leontovich | eea03e9 | 2018-11-20 12:51:50 +0200 | [diff] [blame] | 113 | salt_call_cmd + " pkg.install docker-ce && " + |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 114 | " 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 Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 122 | 'description': "Generate config for Tempest", |
| 123 | 'node_name': self.master_name, |
| 124 | 'cmd': ("set -ex;" + |
obutenko | c431435 | 2018-12-26 16:14:24 +0200 | [diff] [blame] | 125 | "salt-run state.orchestrate " + |
| 126 | "runtest.orchestrate.tempest")}, |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 127 | ] |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 128 | self.__salt_api.execute_commands(commands=commands, |
| 129 | label="Prepare for Tempest") |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 130 | |
| 131 | def run_tempest(self, timeout=600): |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 132 | tgt = self.target_name |
| 133 | image_nameversion = "{}:{}".format(self.image_name, self.image_version) |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 134 | |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 135 | docker_args = ( |
Oleksii Butenko | b0ca4cc | 2018-10-22 16:29:54 +0300 | [diff] [blame] | 136 | " -t " |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 137 | " --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 Butenko | b0ca4cc | 2018-10-22 16:29:54 +0300 | [diff] [blame] | 143 | " -d " |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 144 | " {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 Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 153 | |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 154 | 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 Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 166 | |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 167 | self.__salt_api.execute_commands(commands=commands, |
| 168 | label="Run Tempest tests") |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 169 | |
Oleksii Butenko | b0ca4cc | 2018-10-22 16:29:54 +0300 | [diff] [blame] | 170 | 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 Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 189 | 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 Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 194 | 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 Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 199 | |
| 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 Butenko | e82441d | 2018-06-12 16:01:33 +0300 | [diff] [blame] | 206 | |
obutenko | c431435 | 2018-12-26 16:14:24 +0200 | [diff] [blame] | 207 | def prepare_and_run_tempest(self, username='root'): |
Oleksii Butenko | e82441d | 2018-06-12 16:01:33 +0300 | [diff] [blame] | 208 | """ |
| 209 | Run tempest tests |
| 210 | """ |
| 211 | tempest_timeout = settings.TEMPEST_TIMEOUT |
obutenko | c431435 | 2018-12-26 16:14:24 +0200 | [diff] [blame] | 212 | self.prepare() |
Oleksii Butenko | e82441d | 2018-06-12 16:01:33 +0300 | [diff] [blame] | 213 | test_res = self.run_tempest(tempest_timeout) |
| 214 | self.fetch_arficats(username=username) |
| 215 | self.save_runtime_logs(**test_res) |