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] |
Dennis Dmitriev | 1566e3f | 2019-01-11 17:35:43 +0200 | [diff] [blame] | 53 | self.master_minion = self.underlay.get_target_minion_ids( |
| 54 | self.master_host)[0] |
obutenko | c431435 | 2018-12-26 16:14:24 +0200 | [diff] [blame] | 55 | self.__target_name = None |
sgudz | 6571a5e | 2019-02-26 15:49:22 +0200 | [diff] [blame] | 56 | self.__target_minion = None |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 57 | |
| 58 | @property |
| 59 | def salt_api(self): |
| 60 | return self.__salt_api |
| 61 | |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 62 | @property |
obutenko | c431435 | 2018-12-26 16:14:24 +0200 | [diff] [blame] | 63 | def target_name(self): |
| 64 | if not self.__target_name: |
obutenko | a1e3cea | 2019-01-12 21:11:55 +0200 | [diff] [blame] | 65 | target_host = self.__salt_api.get_single_pillar( |
Dennis Dmitriev | 1566e3f | 2019-01-11 17:35:43 +0200 | [diff] [blame] | 66 | tgt=self.master_minion, |
obutenko | c431435 | 2018-12-26 16:14:24 +0200 | [diff] [blame] | 67 | pillar="runtest:tempest:test_target") |
obutenko | c431435 | 2018-12-26 16:14:24 +0200 | [diff] [blame] | 68 | if target_host[-1] == "*": |
| 69 | target_host = target_host[:-1] |
| 70 | self.__target_name = self.underlay.get_target_node_names( |
| 71 | target_host)[0] |
| 72 | return self.__target_name |
obutenko | 3e44b60 | 2018-11-13 12:50:22 +0200 | [diff] [blame] | 73 | |
sgudz | 6571a5e | 2019-02-26 15:49:22 +0200 | [diff] [blame] | 74 | @property |
| 75 | def target_minion(self): |
| 76 | if not self.__target_minion: |
| 77 | target_host = self.__salt_api.get_single_pillar( |
| 78 | tgt=self.master_minion, |
| 79 | pillar="runtest:tempest:test_target") |
| 80 | if target_host[-1] == "*": |
| 81 | target_host = target_host[:-1] |
| 82 | self.__target_minion = self.underlay.get_target_minion_ids( |
| 83 | target_host)[0] |
| 84 | return self.__target_minion |
| 85 | |
Oleksii Butenko | 71d76f3 | 2018-06-05 17:46:34 +0300 | [diff] [blame] | 86 | def fetch_arficats(self, username=None, file_format='xml'): |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 87 | with self.underlay.remote(node_name=self.target_name, |
| 88 | username=None) as tgt: |
Oleksii Butenko | 71d76f3 | 2018-06-05 17:46:34 +0300 | [diff] [blame] | 89 | result = tgt.execute('find {} -name "report_*.{}"'.format( |
| 90 | TEMPEST_CFG_DIR, file_format)) |
| 91 | LOG.debug("Find result {0}".format(result)) |
| 92 | assert len(result['stdout']) > 0, ('No report found, please check' |
| 93 | ' if test run was successful.') |
| 94 | report = result['stdout'][0].rstrip() |
| 95 | LOG.debug("Found files {0}".format(report)) |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 96 | tgt.download( |
Oleksii Butenko | 71d76f3 | 2018-06-05 17:46:34 +0300 | [diff] [blame] | 97 | destination=report, # noqa |
| 98 | target=os.getcwd()) |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 99 | |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 100 | def save_runtime_logs(self, logs=None, inspect=None): |
| 101 | if logs: |
| 102 | with open("{path}/{target}_tempest_run.log".format( |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 103 | path=settings.LOGS_DIR, |
| 104 | target=self.target_name), 'w') as f: |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 105 | LOG.info("Save tempest console log") |
| 106 | container_log = logs |
Oleksii Butenko | 71d76f3 | 2018-06-05 17:46:34 +0300 | [diff] [blame] | 107 | f.write(container_log.encode('ascii', 'ignore')) |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 108 | |
| 109 | if inspect: |
Oleksii Butenko | 71d76f3 | 2018-06-05 17:46:34 +0300 | [diff] [blame] | 110 | with open("{path}/{target}_tempest_container_info.json.log".format( |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 111 | path=settings.LOGS_DIR, |
| 112 | target=self.target_name), 'w') as f: |
Oleksii Butenko | 71d76f3 | 2018-06-05 17:46:34 +0300 | [diff] [blame] | 113 | LOG.info("Save tempest container inspect data") |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 114 | |
| 115 | container_inspect = json.dumps(inspect, |
| 116 | indent=4, sort_keys=True) |
| 117 | f.write(container_inspect) |
| 118 | |
obutenko | c431435 | 2018-12-26 16:14:24 +0200 | [diff] [blame] | 119 | def prepare(self): |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 120 | salt_call_cmd = "salt-call -l info --hard-crash --state-output=mixed " |
obutenko | a1e3cea | 2019-01-12 21:11:55 +0200 | [diff] [blame] | 121 | barbican_integration = self.__salt_api.get_single_pillar( |
Tatyana Leontovich | 8b1fda0 | 2019-01-23 13:14:33 +0200 | [diff] [blame] | 122 | tgt="ctl01*", |
obutenko | a1e3cea | 2019-01-12 21:11:55 +0200 | [diff] [blame] | 123 | pillar="_param:barbican_integration_enabled") |
Tatyana Leontovich | 8b1fda0 | 2019-01-23 13:14:33 +0200 | [diff] [blame] | 124 | |
obutenko | c48f435 | 2019-01-24 17:03:06 +0200 | [diff] [blame] | 125 | LOG.info("Barbican integration {0}".format(barbican_integration)) |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 126 | commands = [ |
| 127 | { |
Tatyana Leontovich | eea03e9 | 2018-11-20 12:51:50 +0200 | [diff] [blame] | 128 | 'description': ("Install docker-ce package and " |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 129 | "enable packets forwarding"), |
| 130 | 'node_name': self.target_name, |
| 131 | 'cmd': ("set -ex;" + |
Tatyana Leontovich | eea03e9 | 2018-11-20 12:51:50 +0200 | [diff] [blame] | 132 | salt_call_cmd + " pkg.install docker-ce && " + |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 133 | " iptables --policy FORWARD ACCEPT")}, |
| 134 | { |
| 135 | 'description': "Install PyPI docker package", |
| 136 | 'node_name': self.target_name, |
| 137 | 'cmd': ("set -ex;" + |
| 138 | salt_call_cmd + " pip.install setuptools && " + |
| 139 | salt_call_cmd + " pip.install docker")}, |
| 140 | { |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 141 | 'description': "Generate config for Tempest", |
| 142 | 'node_name': self.master_name, |
| 143 | 'cmd': ("set -ex;" + |
obutenko | c431435 | 2018-12-26 16:14:24 +0200 | [diff] [blame] | 144 | "salt-run state.orchestrate " + |
| 145 | "runtest.orchestrate.tempest")}, |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 146 | ] |
obutenko | a1e3cea | 2019-01-12 21:11:55 +0200 | [diff] [blame] | 147 | |
obutenko | c48f435 | 2019-01-24 17:03:06 +0200 | [diff] [blame] | 148 | if barbican_integration: |
obutenko | a1e3cea | 2019-01-12 21:11:55 +0200 | [diff] [blame] | 149 | commands.append({ |
| 150 | 'description': "Configure barbican", |
| 151 | 'node_name': self.master_name, |
| 152 | 'cmd': ("set -ex;" + |
| 153 | salt_call_cmd + |
| 154 | " state.sls barbican.client && " + |
| 155 | salt_call_cmd + |
obutenko | c48f435 | 2019-01-24 17:03:06 +0200 | [diff] [blame] | 156 | " state.sls runtest.test_accounts" + |
obutenko | a1e3cea | 2019-01-12 21:11:55 +0200 | [diff] [blame] | 157 | salt_call_cmd + |
| 158 | " state.sls runtest.barbican_sign_image")}, |
| 159 | ) |
| 160 | |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 161 | self.__salt_api.execute_commands(commands=commands, |
| 162 | label="Prepare for Tempest") |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 163 | |
| 164 | def run_tempest(self, timeout=600): |
sgudz | 6571a5e | 2019-02-26 15:49:22 +0200 | [diff] [blame] | 165 | tgt = self.target_minion |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 166 | image_nameversion = "{}:{}".format(self.image_name, self.image_version) |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 167 | |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 168 | docker_args = ( |
Oleksii Butenko | b0ca4cc | 2018-10-22 16:29:54 +0300 | [diff] [blame] | 169 | " -t " |
obutenko | a1e3cea | 2019-01-12 21:11:55 +0200 | [diff] [blame] | 170 | " --net host " |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 171 | " --name {container_name} " |
| 172 | " -e ARGS=\"-r {tempest_pattern} -w {tempest_threads}\"" |
| 173 | " -v {cfg_dir}/tempest.conf:/etc/tempest/tempest.conf" |
| 174 | " -v /tmp/:/tmp/" |
| 175 | " -v {cfg_dir}:/root/tempest" |
| 176 | " -v /etc/ssl/certs/:/etc/ssl/certs/" |
Oleksii Butenko | b0ca4cc | 2018-10-22 16:29:54 +0300 | [diff] [blame] | 177 | " -d " |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 178 | " {image_nameversion} {run_cmd}" |
| 179 | .format( |
| 180 | container_name=self.container_name, |
| 181 | image_nameversion=image_nameversion, |
| 182 | tempest_pattern=self.tempest_pattern, |
| 183 | tempest_threads=self.tempest_threads, |
| 184 | cfg_dir=TEMPEST_CFG_DIR, |
| 185 | run_cmd=self.run_cmd, |
| 186 | )) |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 187 | |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 188 | commands = [ |
| 189 | { |
| 190 | 'description': "Run Tempest tests {0}".format( |
| 191 | image_nameversion), |
| 192 | 'node_name': self.target_name, |
| 193 | 'cmd': ("set -ex;" + |
| 194 | " docker rm --force {container_name} || true;" |
| 195 | " docker run {docker_args}" |
| 196 | .format(container_name=self.container_name, |
| 197 | docker_args=docker_args)), |
| 198 | 'timeout': timeout}, |
| 199 | ] |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 200 | |
Dennis Dmitriev | b6bcc5c | 2018-09-26 11:07:53 +0000 | [diff] [blame] | 201 | self.__salt_api.execute_commands(commands=commands, |
| 202 | label="Run Tempest tests") |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 203 | |
sgudz | 6571a5e | 2019-02-26 15:49:22 +0200 | [diff] [blame] | 204 | def simplify_salt_api_return(api_return, only_first_match=True): |
| 205 | """ |
| 206 | Salt API always returns a dict with one key as 'return' |
| 207 | and value as a list with dict. For example: |
| 208 | For single node: |
| 209 | api.local('cfg01*', 'test.ping', expr_form='compound') |
| 210 | {u'return':[{u'cfg01.cookied-cicd-queens-dvr-sl.local':True}]} |
| 211 | For multinode: |
| 212 | api.local('ctl*', 'test.ping', expr_form='compound') |
| 213 | {u'return': [{u'ctl01.cookied-cicd-queens-dvr-sl.local': True, |
| 214 | u'ctl02.cookied-cicd-queens-dvr-sl.local': True, |
| 215 | u'ctl03.cookied-cicd-queens-dvr-sl.local': True}]} |
| 216 | When wrong function is given: |
| 217 | api.local('ctl01*', 'wrong_func', expr_form='compound') |
| 218 | {u'return': [{u'ctl01.cookied-cicd-queens-dvr-sl.local': |
| 219 | u"'wrong_func' is not available."}]} |
| 220 | Empty return: |
| 221 | api.local('wrong_target', 'test.ping', expr_form='compound') |
| 222 | {u'return': [{}]} |
| 223 | """ |
| 224 | if api_return.get('return', [{}]) != [{}]: |
| 225 | api_return = api_return['return'][0] |
| 226 | if only_first_match: |
| 227 | api_return = next(api_return.iteritems())[1] |
sgudz | 6571a5e | 2019-02-26 15:49:22 +0200 | [diff] [blame] | 228 | return api_return |
| 229 | else: |
| 230 | LOG.info('''Salt api returns empty result: [{}]''') |
| 231 | return False |
| 232 | |
Oleksii Butenko | b0ca4cc | 2018-10-22 16:29:54 +0300 | [diff] [blame] | 233 | def wait_status(s): |
sgudz | 6571a5e | 2019-02-26 15:49:22 +0200 | [diff] [blame] | 234 | inspect_res = self.salt_api.local(tgt, 'dockerng.inspect', |
Oleksii Butenko | b0ca4cc | 2018-10-22 16:29:54 +0300 | [diff] [blame] | 235 | self.container_name) |
sgudz | 6571a5e | 2019-02-26 15:49:22 +0200 | [diff] [blame] | 236 | inspect = simplify_salt_api_return(inspect_res) |
| 237 | if inspect: |
Oleksii Butenko | b0ca4cc | 2018-10-22 16:29:54 +0300 | [diff] [blame] | 238 | status = inspect['State']['Status'] |
Oleksii Butenko | b0ca4cc | 2018-10-22 16:29:54 +0300 | [diff] [blame] | 239 | return status.lower() == s.lower() |
Oleksii Butenko | b0ca4cc | 2018-10-22 16:29:54 +0300 | [diff] [blame] | 240 | return False |
| 241 | |
sgudz | 6571a5e | 2019-02-26 15:49:22 +0200 | [diff] [blame] | 242 | if wait_status('running'): |
| 243 | helpers.wait(lambda: wait_status('exited'), |
| 244 | timeout=timeout, |
| 245 | timeout_msg=('Tempest run didnt finished ' |
| 246 | 'in {}'.format(timeout))) |
Oleksii Butenko | b0ca4cc | 2018-10-22 16:29:54 +0300 | [diff] [blame] | 247 | |
sgudz | 6571a5e | 2019-02-26 15:49:22 +0200 | [diff] [blame] | 248 | inspect_res = self.salt_api.local(tgt, 'dockerng.inspect', |
| 249 | self.container_name) |
| 250 | inspect = simplify_salt_api_return(inspect_res) |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 251 | |
sgudz | 6571a5e | 2019-02-26 15:49:22 +0200 | [diff] [blame] | 252 | logs_res = self.salt_api.local(tgt, 'dockerng.logs', |
| 253 | self.container_name) |
| 254 | logs = simplify_salt_api_return(logs_res) |
sgudz | 2457db8 | 2019-03-01 16:06:36 +0200 | [diff] [blame^] | 255 | rm_res = self.salt_api.local(tgt, 'dockerng.rm', |
| 256 | self.container_name) |
| 257 | LOG.info("Tempest container was removed: {}".format( |
| 258 | json.dumps(rm_res, indent=4))) |
sgudz | 6571a5e | 2019-02-26 15:49:22 +0200 | [diff] [blame] | 259 | else: |
| 260 | inspect_res = self.salt_api.local(tgt, 'dockerng.inspect', |
| 261 | self.container_name) |
| 262 | inspect = simplify_salt_api_return(inspect_res) |
| 263 | if inspect: |
| 264 | status = inspect['State']['Status'] |
| 265 | LOG.info("Container is not in RUNNING state. " |
| 266 | "Current container status is {}".format(status)) |
| 267 | logs_res = self.salt_api.local(tgt, |
| 268 | 'dockerng.logs', |
| 269 | self.container_name) |
| 270 | logs = simplify_salt_api_return(logs_res) |
| 271 | else: |
| 272 | LOG.info("dockerng returns unexpected" |
| 273 | " result: {}".format(inspect_res)) |
| 274 | logs = None |
| 275 | inspect = None |
Dmitry Tyzhnenko | c56b77e | 2018-05-21 11:01:43 +0300 | [diff] [blame] | 276 | |
| 277 | return {'inspect': inspect, |
| 278 | 'logs': logs} |
Oleksii Butenko | e82441d | 2018-06-12 16:01:33 +0300 | [diff] [blame] | 279 | |
obutenko | c431435 | 2018-12-26 16:14:24 +0200 | [diff] [blame] | 280 | def prepare_and_run_tempest(self, username='root'): |
Oleksii Butenko | e82441d | 2018-06-12 16:01:33 +0300 | [diff] [blame] | 281 | """ |
| 282 | Run tempest tests |
| 283 | """ |
| 284 | tempest_timeout = settings.TEMPEST_TIMEOUT |
obutenko | c431435 | 2018-12-26 16:14:24 +0200 | [diff] [blame] | 285 | self.prepare() |
Oleksii Butenko | e82441d | 2018-06-12 16:01:33 +0300 | [diff] [blame] | 286 | test_res = self.run_tempest(tempest_timeout) |
| 287 | self.fetch_arficats(username=username) |
| 288 | self.save_runtime_logs(**test_res) |