koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 1 | import os |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 2 | import time |
| 3 | import signal |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 4 | import pprint |
koder aka kdanilov | f286517 | 2016-12-30 03:35:11 +0200 | [diff] [blame] | 5 | import getpass |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 6 | import logging |
| 7 | import argparse |
| 8 | import functools |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 9 | import contextlib |
kdanylov aka koder | 938f75f | 2018-06-27 01:52:44 +0300 | [diff] [blame] | 10 | import datetime |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 11 | from typing import List, Tuple, Any, Callable, IO, cast, Optional, Iterator |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 12 | from yaml import load as _yaml_load |
| 13 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 14 | YLoader = Callable[[IO], Any] |
kdanylov aka koder | 13e5845 | 2018-07-15 02:51:51 +0300 | [diff] [blame] | 15 | yaml_load: YLoader = None # type: ignore |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 16 | |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 17 | try: |
| 18 | from yaml import CLoader |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 19 | yaml_load = cast(YLoader, functools.partial(_yaml_load, Loader=CLoader)) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 20 | except ImportError: |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 21 | yaml_load = cast(YLoader, _yaml_load) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 22 | |
kdanylov aka koder | b083333 | 2017-05-13 20:39:17 +0300 | [diff] [blame] | 23 | from cephlib.texttable import Texttable |
| 24 | from cephlib.istorage import IStorage |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 25 | |
| 26 | try: |
| 27 | import faulthandler |
| 28 | except ImportError: |
| 29 | faulthandler = None |
| 30 | |
kdanylov aka koder | 150b219 | 2017-04-01 16:53:01 +0300 | [diff] [blame] | 31 | from cephlib.common import setup_logging |
kdanylov aka koder | b083333 | 2017-05-13 20:39:17 +0300 | [diff] [blame] | 32 | from cephlib.storage import make_storage |
kdanylov aka koder | 84de1e4 | 2017-05-22 14:00:07 +0300 | [diff] [blame] | 33 | from cephlib.wally_storage import WallyDB |
kdanylov aka koder | 026e5f2 | 2017-05-15 01:04:39 +0300 | [diff] [blame] | 34 | from cephlib.ssh import set_ssh_key_passwd |
| 35 | from cephlib.node import log_nodes_statistic |
| 36 | from cephlib.node_impl import get_rpc_server_code |
kdanylov aka koder | 150b219 | 2017-04-01 16:53:01 +0300 | [diff] [blame] | 37 | |
kdanylov aka koder | 026e5f2 | 2017-05-15 01:04:39 +0300 | [diff] [blame] | 38 | from . import utils, report_profiles, report |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 39 | from .config import Config |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 40 | from .stage import Stage |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 41 | from .test_run_class import TestRun |
kdanylov aka koder | 026e5f2 | 2017-05-15 01:04:39 +0300 | [diff] [blame] | 42 | from .result_storage import WallyStorage |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 43 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 44 | # stages |
kdanylov aka koder | b083333 | 2017-05-13 20:39:17 +0300 | [diff] [blame] | 45 | from .ceph import DiscoverCephStage, CollectCephInfoStage |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 46 | from .openstack import DiscoverOSStage |
koder aka kdanilov | 23e6bdf | 2016-12-24 02:18:54 +0200 | [diff] [blame] | 47 | from .run_test import (CollectInfoStage, ExplicitNodesStage, SaveNodesStage, |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 48 | RunTestsStage, ConnectStage, SleepStage, PrepareNodes, |
| 49 | LoadStoredNodesStage) |
kdanylov aka koder | 150b219 | 2017-04-01 16:53:01 +0300 | [diff] [blame] | 50 | |
| 51 | from .report import HtmlReportStage |
kdanylov aka koder | aaad8b8 | 2018-07-17 04:24:14 +0300 | [diff] [blame] | 52 | from .sensors import StartSensorsStage, CollectSensorsStage, StopSensorsStage |
kdanylov aka koder | 150b219 | 2017-04-01 16:53:01 +0300 | [diff] [blame] | 53 | from .console_report import ConsoleReportStage |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 54 | |
| 55 | |
kdanylov aka koder | 938f75f | 2018-06-27 01:52:44 +0300 | [diff] [blame] | 56 | try: |
| 57 | assert False |
| 58 | except AssertionError: |
| 59 | pass |
| 60 | else: |
| 61 | print("Must not run this code with -o python option. Assertions are important!") |
| 62 | exit(1) |
| 63 | |
| 64 | |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 65 | logger = logging.getLogger("wally") |
| 66 | |
| 67 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 68 | @contextlib.contextmanager |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 69 | def log_stage(stage: Stage, cleanup: bool = False) -> Iterator[None]: |
| 70 | logger.info("Start " + stage.name() + ("::cleanup" if cleanup else "")) |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 71 | try: |
| 72 | yield |
kdanylov aka koder | 938f75f | 2018-06-27 01:52:44 +0300 | [diff] [blame] | 73 | except utils.StopTestError: |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 74 | raise |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 75 | except Exception: |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 76 | logger.exception("During %s", stage.name() + ("::cleanup" if cleanup else "")) |
| 77 | raise |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 78 | |
| 79 | |
kdanylov aka koder | 84de1e4 | 2017-05-22 14:00:07 +0300 | [diff] [blame] | 80 | def list_results(path: str, limit: int = None) -> List[Tuple[str, str, str, str, str]]: |
| 81 | dirs = [] |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 82 | for dir_name in os.listdir(path): |
| 83 | full_path = os.path.join(path, dir_name) |
kdanylov aka koder | 84de1e4 | 2017-05-22 14:00:07 +0300 | [diff] [blame] | 84 | dirs.append((os.stat(full_path).st_ctime, full_path)) |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 85 | |
kdanylov aka koder | 84de1e4 | 2017-05-22 14:00:07 +0300 | [diff] [blame] | 86 | dirs.sort() |
kdanylov aka koder | 938f75f | 2018-06-27 01:52:44 +0300 | [diff] [blame] | 87 | results: List[Tuple[str, str, str, str, str]] = [] |
kdanylov aka koder | 84de1e4 | 2017-05-22 14:00:07 +0300 | [diff] [blame] | 88 | for _, full_path in dirs[::-1]: |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 89 | try: |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 90 | stor = make_storage(full_path, existing=True) |
| 91 | except Exception as exc: |
kdanylov aka koder | 938f75f | 2018-06-27 01:52:44 +0300 | [diff] [blame] | 92 | logger.warning(f"Can't load folder {full_path}. Error {exc}") |
| 93 | continue |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 94 | |
kdanylov aka koder | 84de1e4 | 2017-05-22 14:00:07 +0300 | [diff] [blame] | 95 | try: |
| 96 | try: |
| 97 | cfg = stor.load(Config, WallyDB.config) |
| 98 | except KeyError: |
| 99 | cfg = stor.load(Config, "config") |
| 100 | except Exception as exc: |
kdanylov aka koder | 938f75f | 2018-06-27 01:52:44 +0300 | [diff] [blame] | 101 | print(f"Fail to load {os.path.basename(full_path)}. {exc}") |
kdanylov aka koder | 84de1e4 | 2017-05-22 14:00:07 +0300 | [diff] [blame] | 102 | continue |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 103 | |
kdanylov aka koder | 84de1e4 | 2017-05-22 14:00:07 +0300 | [diff] [blame] | 104 | if WallyDB.run_interval in stor: |
| 105 | run_time = stor.get(WallyDB.run_interval)[0] |
| 106 | else: |
| 107 | run_time = os.stat(full_path).st_ctime |
| 108 | |
kdanylov aka koder | 938f75f | 2018-06-27 01:52:44 +0300 | [diff] [blame] | 109 | ftime = f"{datetime.datetime.fromtimestamp(run_time):%d %b %H:%M}" |
kdanylov aka koder | 84de1e4 | 2017-05-22 14:00:07 +0300 | [diff] [blame] | 110 | |
| 111 | test_types = [] |
| 112 | for suite_cfg in cfg.get('tests', []): |
| 113 | for suite_name, params in suite_cfg.items(): |
| 114 | if suite_name == 'fio': |
| 115 | test_types.append("{}.{}".format(suite_name, params['load'])) |
| 116 | else: |
| 117 | test_types.append(suite_name) |
| 118 | results.append((cfg.run_uuid, |
| 119 | ",".join(test_types), |
| 120 | ftime, |
| 121 | '-' if cfg.comment is None else cfg.comment, |
| 122 | '-')) |
| 123 | |
| 124 | if limit and len(results) >= limit: |
| 125 | break |
| 126 | |
| 127 | return results |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 128 | |
| 129 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 130 | def log_nodes_statistic_stage(ctx: TestRun) -> None: |
koder aka kdanilov | 108ac36 | 2017-01-19 20:17:16 +0200 | [diff] [blame] | 131 | log_nodes_statistic(ctx.nodes) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 132 | |
| 133 | |
| 134 | def parse_args(argv): |
| 135 | descr = "Disk io performance test suite" |
| 136 | parser = argparse.ArgumentParser(prog='wally', description=descr) |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 137 | parser.add_argument("-l", '--log-level', help="print some extra log info") |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 138 | parser.add_argument("--ssh-key-passwd", default=None, help="Pass ssh key password") |
kdanylov aka koder | 84de1e4 | 2017-05-22 14:00:07 +0300 | [diff] [blame] | 139 | parser.add_argument("--ssh-key-passwd-kbd", action="store_true", help="Enter ssh key password interactively") |
| 140 | parser.add_argument("--profile", action="store_true", help="Profile execution") |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 141 | parser.add_argument("-s", '--settings-dir', default=None, |
| 142 | help="Folder to store key/settings/history files") |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 143 | subparsers = parser.add_subparsers(dest='subparser_name') |
| 144 | |
| 145 | # --------------------------------------------------------------------- |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 146 | report_parser = subparsers.add_parser('ls', help='list all results') |
kdanylov aka koder | 84de1e4 | 2017-05-22 14:00:07 +0300 | [diff] [blame] | 147 | report_parser.add_argument("-l", "--limit", metavar='LIMIT', help="Show only LIMIT last results", |
| 148 | default=None, type=int) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 149 | report_parser.add_argument("result_storage", help="Folder with test results") |
| 150 | |
| 151 | # --------------------------------------------------------------------- |
| 152 | compare_help = 'compare two results' |
| 153 | report_parser = subparsers.add_parser('compare', help=compare_help) |
| 154 | report_parser.add_argument("data_path1", help="First folder with test results") |
| 155 | report_parser.add_argument("data_path2", help="Second folder with test results") |
| 156 | |
| 157 | # --------------------------------------------------------------------- |
| 158 | report_help = 'run report on previously obtained results' |
| 159 | report_parser = subparsers.add_parser('report', help=report_help) |
kdanylov aka koder | 150b219 | 2017-04-01 16:53:01 +0300 | [diff] [blame] | 160 | report_parser.add_argument('-R', '--reporters', help="Comma-separated list of reportes - html,txt", |
| 161 | default='html,txt') |
kdanylov aka koder | b083333 | 2017-05-13 20:39:17 +0300 | [diff] [blame] | 162 | report_parser.add_argument('-f', '--format', help="Images format, default is " + report_profiles.default_format, |
| 163 | choices=('svg', 'png'), default=report_profiles.default_format) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 164 | report_parser.add_argument("data_dir", help="folder with rest results") |
| 165 | |
| 166 | # --------------------------------------------------------------------- |
koder aka kdanilov | 108ac36 | 2017-01-19 20:17:16 +0200 | [diff] [blame] | 167 | ipython_help = 'run ipython in prepared environment' |
| 168 | ipython_parser = subparsers.add_parser('ipython', help=ipython_help) |
| 169 | ipython_parser.add_argument("storage_dir", help="Storage path") |
| 170 | # --------------------------------------------------------------------- |
kdanylov aka koder | 470a8fa | 2017-07-14 21:07:58 +0300 | [diff] [blame] | 171 | # jupyter_help = 'run jupyter in prepared environment' |
| 172 | # jupyter_parser = subparsers.add_parser('jupyter', help=jupyter_help) |
| 173 | # jupyter_parser.add_argument("storage_dir", help="Storage path") |
koder aka kdanilov | 108ac36 | 2017-01-19 20:17:16 +0200 | [diff] [blame] | 174 | |
| 175 | # --------------------------------------------------------------------- |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 176 | test_parser = subparsers.add_parser('test', help='run tests') |
kdanylov aka koder | 150b219 | 2017-04-01 16:53:01 +0300 | [diff] [blame] | 177 | test_parser.add_argument("-d", '--dont-discover-nodes', action='store_true', help="Don't discover nodes") |
| 178 | test_parser.add_argument('-D', '--dont-collect', action='store_true', help="Don't collect cluster info") |
| 179 | test_parser.add_argument("-k", '--keep-vm', action='store_true', help="Don't remove test vm's") |
| 180 | test_parser.add_argument('-L', '--load-report', action='store_true', help="Create cluster load report") |
| 181 | test_parser.add_argument('-n', '--no-tests', action='store_true', help="Don't run tests") |
| 182 | test_parser.add_argument('-N', '--no-report', action='store_true', help="Skip report stages") |
| 183 | test_parser.add_argument('-r', '--result-dir', default=None, help="Save results to DIR", metavar="DIR") |
| 184 | test_parser.add_argument('-R', '--reporters', help="Comma-separated list of reportes - html,txt", |
| 185 | default='html,txt') |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 186 | test_parser.add_argument('--build-description', type=str, default="Build info") |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 187 | test_parser.add_argument('--build-id', type=str, default="id") |
| 188 | test_parser.add_argument('--build-type', type=str, default="GA") |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 189 | test_parser.add_argument("comment", help="Test information") |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 190 | test_parser.add_argument("config_file", help="Yaml config file") |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 191 | |
| 192 | # --------------------------------------------------------------------- |
koder aka kdanilov | e7e1a4d | 2016-12-17 20:29:52 +0200 | [diff] [blame] | 193 | test_parser = subparsers.add_parser('resume', help='resume tests') |
| 194 | test_parser.add_argument("storage_dir", help="Path to test directory") |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 195 | |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 196 | # --------------------------------------------------------------------- |
koder aka kdanilov | 108ac36 | 2017-01-19 20:17:16 +0200 | [diff] [blame] | 197 | test_parser = subparsers.add_parser('db', help='Exec command on DB') |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 198 | test_parser.add_argument("cmd", choices=("show",), help="Command to execute") |
| 199 | test_parser.add_argument("params", nargs='*', help="Command params") |
| 200 | test_parser.add_argument("storage_dir", help="Storage path") |
| 201 | |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 202 | return parser.parse_args(argv[1:]) |
| 203 | |
| 204 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 205 | def get_config_path(config: Config, opts_value: Optional[str]) -> str: |
| 206 | if opts_value is None and 'settings_dir' not in config: |
| 207 | val = "~/.wally" |
| 208 | elif opts_value is not None: |
| 209 | val = opts_value |
| 210 | else: |
| 211 | val = config.settings_dir |
| 212 | |
| 213 | return os.path.abspath(os.path.expanduser(val)) |
| 214 | |
| 215 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 216 | def find_cfg_file(name: str, included_from: str = None) -> str: |
| 217 | paths = [".", os.path.expanduser('~/.wally')] |
| 218 | if included_from is not None: |
| 219 | paths.append(os.path.dirname(included_from)) |
| 220 | |
| 221 | search_paths = set(os.path.abspath(path) for path in paths if os.path.isdir(path)) |
| 222 | |
| 223 | for folder in search_paths: |
| 224 | path = os.path.join(folder, name) |
| 225 | if os.path.exists(path): |
| 226 | return path |
| 227 | |
| 228 | raise FileNotFoundError(name) |
| 229 | |
| 230 | |
| 231 | def load_config(path: str) -> Config: |
| 232 | path = os.path.abspath(path) |
kdanylov aka koder | 938f75f | 2018-06-27 01:52:44 +0300 | [diff] [blame] | 233 | cfg_dict = yaml_load(open(path)) |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 234 | |
| 235 | while 'include' in cfg_dict: |
| 236 | inc = cfg_dict.pop('include') |
| 237 | if isinstance(inc, str): |
| 238 | inc = [inc] |
| 239 | |
| 240 | for fname in inc: |
| 241 | inc_path = find_cfg_file(fname, path) |
kdanylov aka koder | 938f75f | 2018-06-27 01:52:44 +0300 | [diff] [blame] | 242 | inc_dict = yaml_load(open(inc_path)) |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 243 | inc_dict.update(cfg_dict) |
| 244 | cfg_dict = inc_dict |
| 245 | |
| 246 | return Config(cfg_dict) |
| 247 | |
| 248 | |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 249 | def get_run_stages() -> List[Stage]: |
| 250 | return [DiscoverCephStage(), |
kdanylov aka koder | b083333 | 2017-05-13 20:39:17 +0300 | [diff] [blame] | 251 | CollectCephInfoStage(), |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 252 | DiscoverOSStage(), |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 253 | ExplicitNodesStage(), |
| 254 | StartSensorsStage(), |
| 255 | RunTestsStage(), |
kdanylov aka koder | aaad8b8 | 2018-07-17 04:24:14 +0300 | [diff] [blame] | 256 | StopSensorsStage(), |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 257 | ConnectStage(), |
| 258 | SleepStage(), |
| 259 | PrepareNodes()] |
| 260 | |
| 261 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 262 | def main(argv: List[str]) -> int: |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 263 | if faulthandler is not None: |
| 264 | faulthandler.register(signal.SIGUSR1, all_threads=True) |
| 265 | |
| 266 | opts = parse_args(argv) |
kdanylov aka koder | 13e5845 | 2018-07-15 02:51:51 +0300 | [diff] [blame] | 267 | stages: List[Stage] = [] |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 268 | |
| 269 | # stop mypy from telling that config & storage might be undeclared |
kdanylov aka koder | 13e5845 | 2018-07-15 02:51:51 +0300 | [diff] [blame] | 270 | config: Config = None # type: ignore |
| 271 | storage: IStorage = None # type: ignore |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 272 | |
kdanylov aka koder | 84de1e4 | 2017-05-22 14:00:07 +0300 | [diff] [blame] | 273 | if opts.profile: |
| 274 | import cProfile |
kdanylov aka koder | 13e5845 | 2018-07-15 02:51:51 +0300 | [diff] [blame] | 275 | pr: Optional[cProfile.Profile] = cProfile.Profile() |
| 276 | pr.enable() # type: ignore |
kdanylov aka koder | 84de1e4 | 2017-05-22 14:00:07 +0300 | [diff] [blame] | 277 | else: |
| 278 | pr = None |
| 279 | |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 280 | if opts.subparser_name == 'test': |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 281 | config = load_config(opts.config_file) |
kdanylov aka koder | 150b219 | 2017-04-01 16:53:01 +0300 | [diff] [blame] | 282 | config.storage_url, config.run_uuid = utils.get_uniq_path_uuid(config.results_storage) |
koder aka kdanilov | e7e1a4d | 2016-12-17 20:29:52 +0200 | [diff] [blame] | 283 | config.comment = opts.comment |
| 284 | config.keep_vm = opts.keep_vm |
| 285 | config.no_tests = opts.no_tests |
| 286 | config.dont_discover_nodes = opts.dont_discover_nodes |
| 287 | config.build_id = opts.build_id |
| 288 | config.build_description = opts.build_description |
| 289 | config.build_type = opts.build_type |
| 290 | config.settings_dir = get_config_path(config, opts.settings_dir) |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 291 | config.discover = set(name for name in config.get('discover', '').split(",") if name) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 292 | |
koder aka kdanilov | e7e1a4d | 2016-12-17 20:29:52 +0200 | [diff] [blame] | 293 | storage = make_storage(config.storage_url) |
kdanylov aka koder | 84de1e4 | 2017-05-22 14:00:07 +0300 | [diff] [blame] | 294 | storage.put(config, WallyDB.config) |
koder aka kdanilov | ffaf48d | 2016-12-27 02:25:29 +0200 | [diff] [blame] | 295 | |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 296 | stages.extend(get_run_stages()) |
koder aka kdanilov | ffaf48d | 2016-12-27 02:25:29 +0200 | [diff] [blame] | 297 | stages.append(SaveNodesStage()) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 298 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 299 | if not opts.dont_collect: |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 300 | stages.append(CollectInfoStage()) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 301 | |
kdanylov aka koder | aaad8b8 | 2018-07-17 04:24:14 +0300 | [diff] [blame] | 302 | if config.no_tests: |
| 303 | stages.append(CollectSensorsStage()) |
| 304 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 305 | argv2 = argv[:] |
| 306 | if '--ssh-key-passwd' in argv2: |
| 307 | # don't save ssh key password to storage |
| 308 | argv2[argv2.index("--ssh-key-passwd") + 1] = "<removed from output>" |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 309 | storage.put(argv2, 'cli') |
koder aka kdanilov | e7e1a4d | 2016-12-17 20:29:52 +0200 | [diff] [blame] | 310 | |
| 311 | elif opts.subparser_name == 'resume': |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 312 | opts.resumed = True |
koder aka kdanilov | e7e1a4d | 2016-12-17 20:29:52 +0200 | [diff] [blame] | 313 | storage = make_storage(opts.storage_dir, existing=True) |
kdanylov aka koder | 84de1e4 | 2017-05-22 14:00:07 +0300 | [diff] [blame] | 314 | config = storage.load(Config, WallyDB.config) |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 315 | stages.extend(get_run_stages()) |
| 316 | stages.append(LoadStoredNodesStage()) |
koder aka kdanilov | 108ac36 | 2017-01-19 20:17:16 +0200 | [diff] [blame] | 317 | prev_opts = storage.get('cli') # type: List[str] |
| 318 | |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 319 | if '--ssh-key-passwd' in prev_opts and opts.ssh_key_passwd: |
| 320 | prev_opts[prev_opts.index("--ssh-key-passwd") + 1] = opts.ssh_key_passwd |
| 321 | |
| 322 | restored_opts = parse_args(prev_opts) |
| 323 | opts.__dict__.update(restored_opts.__dict__) |
| 324 | opts.subparser_name = 'resume' |
koder aka kdanilov | e7e1a4d | 2016-12-17 20:29:52 +0200 | [diff] [blame] | 325 | |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 326 | elif opts.subparser_name == 'ls': |
kdanylov aka koder | b083333 | 2017-05-13 20:39:17 +0300 | [diff] [blame] | 327 | tab = Texttable(max_width=200) |
kdanylov aka koder | 84de1e4 | 2017-05-22 14:00:07 +0300 | [diff] [blame] | 328 | tab.set_cols_align(["l", "l", "l", "l", 'c']) |
| 329 | tab.set_deco(Texttable.VLINES | Texttable.BORDER | Texttable.HEADER) |
| 330 | tab.header(["Name", "Tests", "Started at", "Comment", "Result"]) |
| 331 | tab.add_rows(list_results(opts.result_storage, opts.limit), header=False) |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 332 | print(tab.draw()) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 333 | return 0 |
| 334 | |
| 335 | elif opts.subparser_name == 'report': |
koder aka kdanilov | ffaf48d | 2016-12-27 02:25:29 +0200 | [diff] [blame] | 336 | if getattr(opts, "no_report", False): |
| 337 | print(" --no-report option can't be used with 'report' cmd") |
| 338 | return 1 |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 339 | storage = make_storage(opts.data_dir, existing=True) |
kdanylov aka koder | 84de1e4 | 2017-05-22 14:00:07 +0300 | [diff] [blame] | 340 | config = storage.load(Config, WallyDB.config) |
kdanylov aka koder | b083333 | 2017-05-13 20:39:17 +0300 | [diff] [blame] | 341 | report_profiles.default_format = opts.format |
| 342 | report.default_format = opts.format |
koder aka kdanilov | ffaf48d | 2016-12-27 02:25:29 +0200 | [diff] [blame] | 343 | stages.append(LoadStoredNodesStage()) |
kdanylov aka koder | 736e5c1 | 2017-05-07 17:27:14 +0300 | [diff] [blame] | 344 | stages.append(SaveNodesStage()) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 345 | elif opts.subparser_name == 'compare': |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 346 | # x = run_test.load_data_from_path(opts.data_path1) |
| 347 | # y = run_test.load_data_from_path(opts.data_path2) |
| 348 | # print(run_test.IOPerfTest.format_diff_for_console( |
| 349 | # [x['io'][0], y['io'][0]])) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 350 | return 0 |
| 351 | |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 352 | elif opts.subparser_name == 'db': |
| 353 | storage = make_storage(opts.storage_dir, existing=True) |
| 354 | if opts.cmd == 'show': |
| 355 | if len(opts.params) != 1: |
| 356 | print("'show' command requires parameter - key to show") |
| 357 | return 1 |
| 358 | pprint.pprint(storage.get(opts.params[0])) |
| 359 | else: |
| 360 | print("Unknown/not_implemented command {!r}".format(opts.cmd)) |
| 361 | return 1 |
| 362 | return 0 |
koder aka kdanilov | 108ac36 | 2017-01-19 20:17:16 +0200 | [diff] [blame] | 363 | elif opts.subparser_name == 'ipython': |
| 364 | storage = make_storage(opts.storage_dir, existing=True) |
kdanylov aka koder | 026e5f2 | 2017-05-15 01:04:39 +0300 | [diff] [blame] | 365 | rstorage = WallyStorage(storage=storage) |
koder aka kdanilov | 108ac36 | 2017-01-19 20:17:16 +0200 | [diff] [blame] | 366 | |
| 367 | import IPython |
| 368 | IPython.embed() |
| 369 | |
| 370 | return 0 |
koder aka kdanilov | a732a60 | 2017-02-01 20:29:56 +0200 | [diff] [blame] | 371 | else: |
| 372 | print("Subparser {!r} is not supported".format(opts.subparser_name)) |
| 373 | return 1 |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 374 | |
kdanylov aka koder | 84de1e4 | 2017-05-22 14:00:07 +0300 | [diff] [blame] | 375 | start_time = int(time.time()) |
| 376 | |
kdanylov aka koder | 938f75f | 2018-06-27 01:52:44 +0300 | [diff] [blame] | 377 | report_stages: List[Stage] = [] |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 378 | if not getattr(opts, "no_report", False): |
kdanylov aka koder | 150b219 | 2017-04-01 16:53:01 +0300 | [diff] [blame] | 379 | reporters = opts.reporters.split(",") |
| 380 | assert len(set(reporters)) == len(reporters) |
| 381 | assert set(reporters).issubset({'txt', 'html'}) |
| 382 | if 'txt' in reporters: |
| 383 | report_stages.append(ConsoleReportStage()) |
| 384 | if 'html' in reporters: |
| 385 | report_stages.append(HtmlReportStage()) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 386 | |
kdanylov aka koder | 150b219 | 2017-04-01 16:53:01 +0300 | [diff] [blame] | 387 | log_config_obj = config.raw().get('logging') |
| 388 | assert isinstance(log_config_obj, dict) or log_config_obj is None, "Broken 'logging' option in config" |
| 389 | setup_logging(log_config_obj=log_config_obj, log_level=opts.log_level, log_file=storage.get_fname('log')) |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 390 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 391 | logger.info("All info would be stored into %r", config.storage_url) |
| 392 | |
kdanylov aka koder | 026e5f2 | 2017-05-15 01:04:39 +0300 | [diff] [blame] | 393 | ctx = TestRun(config, storage, WallyStorage(storage)) |
| 394 | ctx.rpc_code, ctx.default_rpc_plugins = get_rpc_server_code() |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 395 | |
kdanylov aka koder | 84de1e4 | 2017-05-22 14:00:07 +0300 | [diff] [blame] | 396 | if 'dev_roles' in ctx.config: |
| 397 | ctx.devs_locator = ctx.config.dev_roles |
| 398 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 399 | if opts.ssh_key_passwd is not None: |
| 400 | set_ssh_key_passwd(opts.ssh_key_passwd) |
koder aka kdanilov | f286517 | 2016-12-30 03:35:11 +0200 | [diff] [blame] | 401 | elif opts.ssh_key_passwd_kbd: |
| 402 | set_ssh_key_passwd(getpass.getpass("Ssh key password: ").strip()) |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 403 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 404 | stages.sort(key=lambda x: x.priority) |
| 405 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 406 | # TODO: run only stages, which have config |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 407 | failed = False |
| 408 | cleanup_stages = [] |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 409 | |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 410 | for stage in stages: |
koder aka kdanilov | e7e1a4d | 2016-12-17 20:29:52 +0200 | [diff] [blame] | 411 | if stage.config_block is not None: |
| 412 | if stage.config_block not in ctx.config: |
kdanylov aka koder | 150b219 | 2017-04-01 16:53:01 +0300 | [diff] [blame] | 413 | logger.debug("Skip stage %r, as config has no required block %r", stage.name(), stage.config_block) |
koder aka kdanilov | e7e1a4d | 2016-12-17 20:29:52 +0200 | [diff] [blame] | 414 | continue |
| 415 | |
| 416 | cleanup_stages.append(stage) |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 417 | try: |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 418 | with log_stage(stage): |
| 419 | stage.run(ctx) |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 420 | except (Exception, KeyboardInterrupt): |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 421 | failed = True |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 422 | break |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 423 | ctx.storage.sync() |
| 424 | ctx.storage.sync() |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 425 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 426 | logger.debug("Start cleanup") |
| 427 | cleanup_failed = False |
| 428 | for stage in cleanup_stages[::-1]: |
| 429 | try: |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 430 | with log_stage(stage, cleanup=True): |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 431 | stage.cleanup(ctx) |
| 432 | except: |
| 433 | cleanup_failed = True |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 434 | ctx.storage.sync() |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 435 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 436 | if not failed: |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 437 | for report_stage in report_stages: |
| 438 | with log_stage(report_stage): |
koder aka kdanilov | ffaf48d | 2016-12-27 02:25:29 +0200 | [diff] [blame] | 439 | try: |
| 440 | report_stage.run(ctx) |
| 441 | except utils.StopTestError: |
| 442 | logger.error("Report stage %s requested stop execution", report_stage.name()) |
| 443 | failed = True |
| 444 | break |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 445 | |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 446 | ctx.storage.sync() |
| 447 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 448 | logger.info("All info is stored into %r", config.storage_url) |
kdanylov aka koder | 84de1e4 | 2017-05-22 14:00:07 +0300 | [diff] [blame] | 449 | end_time = int(time.time()) |
| 450 | storage.put([start_time, end_time], WallyDB.run_interval) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 451 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 452 | if failed or cleanup_failed: |
kdanylov aka koder | 026e5f2 | 2017-05-15 01:04:39 +0300 | [diff] [blame] | 453 | if opts.subparser_name == 'report': |
| 454 | logger.error("Report generation failed. See error details in log above") |
| 455 | else: |
| 456 | logger.error("Tests are failed. See error details in log above") |
kdanylov aka koder | 84de1e4 | 2017-05-22 14:00:07 +0300 | [diff] [blame] | 457 | code = 1 |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 458 | else: |
kdanylov aka koder | 026e5f2 | 2017-05-15 01:04:39 +0300 | [diff] [blame] | 459 | if opts.subparser_name == 'report': |
| 460 | logger.info("Report successfully generated") |
| 461 | else: |
| 462 | logger.info("Tests finished successfully") |
kdanylov aka koder | 84de1e4 | 2017-05-22 14:00:07 +0300 | [diff] [blame] | 463 | code = 0 |
| 464 | |
| 465 | if opts.profile: |
| 466 | assert pr is not None |
kdanylov aka koder | 13e5845 | 2018-07-15 02:51:51 +0300 | [diff] [blame] | 467 | pr.disable() # type: ignore |
kdanylov aka koder | 84de1e4 | 2017-05-22 14:00:07 +0300 | [diff] [blame] | 468 | import pstats |
| 469 | pstats.Stats(pr).sort_stats('tottime').print_stats(30) |
| 470 | |
| 471 | if opts.subparser_name == 'test': |
| 472 | storage.put(code, WallyDB.res_code) |
| 473 | |
| 474 | storage.sync() |
| 475 | return code |