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: |
obutenko | a1e3cea | 2019-01-12 21:11:55 +0200 | [diff] [blame] | 62 | target_host = self.__salt_api.get_single_pillar( |
obutenko | c431435 | 2018-12-26 16:14:24 +0200 | [diff] [blame] | 63 | tgt=self.master_name, |
| 64 | pillar="runtest:tempest:test_target") |
obutenko | c431435 | 2018-12-26 16:14:24 +0200 | [diff] [blame] | 65 | 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 |
obutenko | 3e44b60 | 2018-11-13 12:50:22 +0200 | [diff] [blame] | 70 | |
Oleksii Butenko | 71d76f3 | 2018-06-05 17:46:34 +0300 | [diff] [blame] | 71 | def fetch_arficats(self, username=None, file_format='xml'): |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 72 | with self.underlay.remote(node_name=self.target_name, |
| 73 | username=None) as tgt: |
Oleksii Butenko | 71d76f3 | 2018-06-05 17:46:34 +0300 | [diff] [blame] | 74 | 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 Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 81 | tgt.download( |
Oleksii Butenko | 71d76f3 | 2018-06-05 17:46:34 +0300 | [diff] [blame] | 82 | destination=report, # noqa |
| 83 | target=os.getcwd()) |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 84 | |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 85 | def save_runtime_logs(self, logs=None, inspect=None): |
| 86 | if logs: |
| 87 | with open("{path}/{target}_tempest_run.log".format( |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 88 | path=settings.LOGS_DIR, |
| 89 | target=self.target_name), 'w') as f: |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 90 | LOG.info("Save tempest console log") |
| 91 | container_log = logs |
Oleksii Butenko | 71d76f3 | 2018-06-05 17:46:34 +0300 | [diff] [blame] | 92 | f.write(container_log.encode('ascii', 'ignore')) |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 93 | |
| 94 | if inspect: |
Oleksii Butenko | 71d76f3 | 2018-06-05 17:46:34 +0300 | [diff] [blame] | 95 | with open("{path}/{target}_tempest_container_info.json.log".format( |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 96 | path=settings.LOGS_DIR, |
| 97 | target=self.target_name), 'w') as f: |
Oleksii Butenko | 71d76f3 | 2018-06-05 17:46:34 +0300 | [diff] [blame] | 98 | LOG.info("Save tempest container inspect data") |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 99 | |
| 100 | container_inspect = json.dumps(inspect, |
| 101 | indent=4, sort_keys=True) |
| 102 | f.write(container_inspect) |
| 103 | |
obutenko | c431435 | 2018-12-26 16:14:24 +0200 | [diff] [blame] | 104 | def prepare(self): |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 105 | salt_call_cmd = "salt-call -l info --hard-crash --state-output=mixed " |
obutenko | a1e3cea | 2019-01-12 21:11:55 +0200 | [diff] [blame] | 106 | barbican_integration = self.__salt_api.get_single_pillar( |
Tatyana Leontovich | 8b1fda0 | 2019-01-23 13:14:33 +0200 | [diff] [blame] | 107 | tgt="ctl01*", |
obutenko | a1e3cea | 2019-01-12 21:11:55 +0200 | [diff] [blame] | 108 | pillar="_param:barbican_integration_enabled") |
Tatyana Leontovich | 8b1fda0 | 2019-01-23 13:14:33 +0200 | [diff] [blame] | 109 | |
obutenko | c48f435 | 2019-01-24 17:03:06 +0200 | [diff] [blame^] | 110 | LOG.info("Barbican integration {0}".format(barbican_integration)) |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 111 | commands = [ |
| 112 | { |
Tatyana Leontovich | eea03e9 | 2018-11-20 12:51:50 +0200 | [diff] [blame] | 113 | 'description': ("Install docker-ce package and " |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 114 | "enable packets forwarding"), |
| 115 | 'node_name': self.target_name, |
| 116 | 'cmd': ("set -ex;" + |
Tatyana Leontovich | eea03e9 | 2018-11-20 12:51:50 +0200 | [diff] [blame] | 117 | salt_call_cmd + " pkg.install docker-ce && " + |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 118 | " 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 Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 126 | 'description': "Generate config for Tempest", |
| 127 | 'node_name': self.master_name, |
| 128 | 'cmd': ("set -ex;" + |
obutenko | c431435 | 2018-12-26 16:14:24 +0200 | [diff] [blame] | 129 | "salt-run state.orchestrate " + |
| 130 | "runtest.orchestrate.tempest")}, |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 131 | ] |
obutenko | a1e3cea | 2019-01-12 21:11:55 +0200 | [diff] [blame] | 132 | |
obutenko | c48f435 | 2019-01-24 17:03:06 +0200 | [diff] [blame^] | 133 | if barbican_integration: |
obutenko | a1e3cea | 2019-01-12 21:11:55 +0200 | [diff] [blame] | 134 | 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 + |
obutenko | c48f435 | 2019-01-24 17:03:06 +0200 | [diff] [blame^] | 141 | " state.sls runtest.test_accounts" + |
obutenko | a1e3cea | 2019-01-12 21:11:55 +0200 | [diff] [blame] | 142 | salt_call_cmd + |
| 143 | " state.sls runtest.barbican_sign_image")}, |
| 144 | ) |
| 145 | |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 146 | self.__salt_api.execute_commands(commands=commands, |
| 147 | label="Prepare for Tempest") |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 148 | |
| 149 | def run_tempest(self, timeout=600): |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 150 | tgt = self.target_name |
| 151 | image_nameversion = "{}:{}".format(self.image_name, self.image_version) |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 152 | |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 153 | docker_args = ( |
Oleksii Butenko | b0ca4cc | 2018-10-22 16:29:54 +0300 | [diff] [blame] | 154 | " -t " |
obutenko | a1e3cea | 2019-01-12 21:11:55 +0200 | [diff] [blame] | 155 | " --net host " |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 156 | " --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 Butenko | b0ca4cc | 2018-10-22 16:29:54 +0300 | [diff] [blame] | 162 | " -d " |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 163 | " {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 Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 172 | |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 173 | 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 Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 185 | |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 186 | self.__salt_api.execute_commands(commands=commands, |
| 187 | label="Run Tempest tests") |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 188 | |
Oleksii Butenko | b0ca4cc | 2018-10-22 16:29:54 +0300 | [diff] [blame] | 189 | 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 Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 208 | 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 Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 213 | 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 Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 218 | |
| 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 Butenko | e82441d | 2018-06-12 16:01:33 +0300 | [diff] [blame] | 225 | |
obutenko | c431435 | 2018-12-26 16:14:24 +0200 | [diff] [blame] | 226 | def prepare_and_run_tempest(self, username='root'): |
Oleksii Butenko | e82441d | 2018-06-12 16:01:33 +0300 | [diff] [blame] | 227 | """ |
| 228 | Run tempest tests |
| 229 | """ |
| 230 | tempest_timeout = settings.TEMPEST_TIMEOUT |
obutenko | c431435 | 2018-12-26 16:14:24 +0200 | [diff] [blame] | 231 | self.prepare() |
Oleksii Butenko | e82441d | 2018-06-12 16:01:33 +0300 | [diff] [blame] | 232 | test_res = self.run_tempest(tempest_timeout) |
| 233 | self.fetch_arficats(username=username) |
| 234 | self.save_runtime_logs(**test_res) |