koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 1 | import os |
| 2 | import sys |
| 3 | import time |
| 4 | import signal |
| 5 | import logging |
| 6 | import argparse |
| 7 | import functools |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 8 | from typing import List, Tuple, Any, Callable, IO, cast, Optional |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 9 | from yaml import load as _yaml_load |
| 10 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 11 | |
| 12 | YLoader = Callable[[IO], Any] |
| 13 | yaml_load = None # type: YLoader |
| 14 | |
| 15 | |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 16 | try: |
| 17 | from yaml import CLoader |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 18 | yaml_load = cast(YLoader, functools.partial(_yaml_load, Loader=CLoader)) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 19 | except ImportError: |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 20 | yaml_load = cast(YLoader, _yaml_load) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 21 | |
| 22 | |
| 23 | import texttable |
| 24 | |
| 25 | try: |
| 26 | import faulthandler |
| 27 | except ImportError: |
| 28 | faulthandler = None |
| 29 | |
| 30 | |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 31 | from . import utils, run_test, pretty_yaml |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 32 | from .storage import make_storage, Storage |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 33 | from .config import Config |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 34 | from .logger import setup_loggers |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 35 | from .stage import log_stage, StageType |
| 36 | from .test_run_class import TestRun |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 37 | |
| 38 | |
| 39 | logger = logging.getLogger("wally") |
| 40 | |
| 41 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 42 | def list_results(path: str) -> List[Tuple[str, str, str, str]]: |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame^] | 43 | results = [] # type: List[Tuple[float, str, str, str, str]] |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 44 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 45 | for dir_name in os.listdir(path): |
| 46 | full_path = os.path.join(path, dir_name) |
| 47 | |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 48 | try: |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 49 | stor = make_storage(full_path, existing=True) |
| 50 | except Exception as exc: |
| 51 | logger.warning("Can't load folder {}. Error {}".format(full_path, exc)) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 52 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame^] | 53 | comment = cast(str, stor['info/comment']) |
| 54 | run_uuid = cast(str, stor['info/run_uuid']) |
| 55 | run_time = cast(float, stor['info/run_time']) |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 56 | test_types = "" |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame^] | 57 | results.append((run_time, |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 58 | run_uuid, |
| 59 | test_types, |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame^] | 60 | time.ctime(run_time), |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 61 | '-' if comment is None else comment)) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 62 | |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 63 | results.sort() |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 64 | return [i[1:] for i in results] |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 65 | |
| 66 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 67 | def log_nodes_statistic_stage(ctx: TestRun) -> None: |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 68 | utils.log_nodes_statistic(ctx.nodes) |
| 69 | |
| 70 | |
| 71 | def parse_args(argv): |
| 72 | descr = "Disk io performance test suite" |
| 73 | parser = argparse.ArgumentParser(prog='wally', description=descr) |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 74 | parser.add_argument("-l", '--log-level', help="print some extra log info") |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 75 | parser.add_argument("-s", '--settings-dir', default=None, |
| 76 | help="Folder to store key/settings/history files") |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 77 | |
| 78 | subparsers = parser.add_subparsers(dest='subparser_name') |
| 79 | |
| 80 | # --------------------------------------------------------------------- |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 81 | report_parser = subparsers.add_parser('ls', help='list all results') |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 82 | report_parser.add_argument("result_storage", help="Folder with test results") |
| 83 | |
| 84 | # --------------------------------------------------------------------- |
| 85 | compare_help = 'compare two results' |
| 86 | report_parser = subparsers.add_parser('compare', help=compare_help) |
| 87 | report_parser.add_argument("data_path1", help="First folder with test results") |
| 88 | report_parser.add_argument("data_path2", help="Second folder with test results") |
| 89 | |
| 90 | # --------------------------------------------------------------------- |
| 91 | report_help = 'run report on previously obtained results' |
| 92 | report_parser = subparsers.add_parser('report', help=report_help) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 93 | report_parser.add_argument("data_dir", help="folder with rest results") |
| 94 | |
| 95 | # --------------------------------------------------------------------- |
| 96 | test_parser = subparsers.add_parser('test', help='run tests') |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 97 | test_parser.add_argument('--build-description', type=str, default="Build info") |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 98 | test_parser.add_argument('--build-id', type=str, default="id") |
| 99 | test_parser.add_argument('--build-type', type=str, default="GA") |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 100 | test_parser.add_argument('-n', '--no-tests', action='store_true', help="Don't run tests") |
| 101 | test_parser.add_argument('--load-report', action='store_true') |
| 102 | test_parser.add_argument("-k", '--keep-vm', action='store_true', help="Don't remove test vm's") |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 103 | test_parser.add_argument("-d", '--dont-discover-nodes', action='store_true', |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 104 | help="Don't connect/discover fuel nodes") |
| 105 | test_parser.add_argument('--no-report', action='store_true', help="Skip report stages") |
| 106 | test_parser.add_argument('-r', '--resume', default=None, help="Resume previously stopped test, stored in DIR", |
| 107 | metavar="DIR") |
| 108 | test_parser.add_argument('--result-dir', default=None, help="Save results to DIR", metavart="DIR") |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 109 | test_parser.add_argument("comment", help="Test information") |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 110 | test_parser.add_argument("config_file", help="Yaml config file", nargs='?', default=None) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 111 | |
| 112 | # --------------------------------------------------------------------- |
| 113 | |
| 114 | return parser.parse_args(argv[1:]) |
| 115 | |
| 116 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 117 | def get_config_path(config: Config, opts_value: Optional[str]) -> str: |
| 118 | if opts_value is None and 'settings_dir' not in config: |
| 119 | val = "~/.wally" |
| 120 | elif opts_value is not None: |
| 121 | val = opts_value |
| 122 | else: |
| 123 | val = config.settings_dir |
| 124 | |
| 125 | return os.path.abspath(os.path.expanduser(val)) |
| 126 | |
| 127 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 128 | def main(argv: List[str]) -> int: |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 129 | if faulthandler is not None: |
| 130 | faulthandler.register(signal.SIGUSR1, all_threads=True) |
| 131 | |
| 132 | opts = parse_args(argv) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 133 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 134 | stages = [] # type: List[StageType] |
| 135 | report_stages = [] # type: List[StageType] |
| 136 | |
| 137 | # stop mypy from telling that config & storage might be undeclared |
| 138 | config = None # type: Config |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 139 | storage = None # type: Storage |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 140 | |
| 141 | if opts.subparser_name == 'test': |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 142 | if opts.resume: |
| 143 | storage = make_storage(opts.resume, existing=True) |
| 144 | config = storage.load('config', Config) |
| 145 | else: |
| 146 | file_name = os.path.abspath(opts.config_file) |
| 147 | with open(file_name) as fd: |
| 148 | config = Config(yaml_load(fd.read())) # type: ignore |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 149 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame^] | 150 | config.storage_url, config.run_uuid = utils.get_uniq_path_uuid(config.results_dir) |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 151 | config.comment = opts.comment |
| 152 | config.keep_vm = opts.keep_vm |
| 153 | config.no_tests = opts.no_tests |
| 154 | config.dont_discover_nodes = opts.dont_discover_nodes |
| 155 | config.build_id = opts.build_id |
| 156 | config.build_description = opts.build_description |
| 157 | config.build_type = opts.build_type |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 158 | config.settings_dir = get_config_path(config, opts.settings_dir) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 159 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 160 | storage = make_storage(config.storage_url) |
| 161 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame^] | 162 | storage['config'] = config # type: ignore |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 163 | |
| 164 | stages.extend([ |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 165 | run_test.discover_stage, |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 166 | run_test.reuse_vms_stage, |
| 167 | log_nodes_statistic_stage, |
| 168 | run_test.save_nodes_stage, |
| 169 | run_test.connect_stage]) |
| 170 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 171 | if config.get("collect_info", True): |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame^] | 172 | stages.append(run_test.collect_info_stage) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 173 | |
| 174 | stages.extend([ |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 175 | run_test.run_tests_stage, |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 176 | ]) |
| 177 | |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 178 | elif opts.subparser_name == 'ls': |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 179 | tab = texttable.Texttable(max_width=200) |
| 180 | tab.set_deco(tab.HEADER | tab.VLINES | tab.BORDER) |
| 181 | tab.set_cols_align(["l", "l", "l", "l"]) |
| 182 | tab.header(["Name", "Tests", "Run at", "Comment"]) |
| 183 | tab.add_rows(list_results(opts.result_storage)) |
| 184 | print(tab.draw()) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 185 | return 0 |
| 186 | |
| 187 | elif opts.subparser_name == 'report': |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 188 | storage = make_storage(opts.data_dir, existing=True) |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 189 | config.settings_dir = get_config_path(config, opts.settings_dir) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 190 | |
| 191 | elif opts.subparser_name == 'compare': |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame^] | 192 | # x = run_test.load_data_from_path(opts.data_path1) |
| 193 | # y = run_test.load_data_from_path(opts.data_path2) |
| 194 | # print(run_test.IOPerfTest.format_diff_for_console( |
| 195 | # [x['io'][0], y['io'][0]])) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 196 | return 0 |
| 197 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 198 | if not getattr(opts, "no_report", False): |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 199 | report_stages.append(run_test.console_report_stage) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 200 | report_stages.append(run_test.html_report_stage) |
| 201 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 202 | # log level is not a part of config |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 203 | if opts.log_level is not None: |
| 204 | str_level = opts.log_level |
| 205 | else: |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 206 | str_level = config.get('logging/log_level', 'INFO') |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 207 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 208 | setup_loggers(getattr(logging, str_level), log_fd=storage.get_stream('log')) |
| 209 | logger.info("All info would be stored into %r", config.storage_url) |
| 210 | |
| 211 | ctx = TestRun(config, storage) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 212 | |
| 213 | for stage in stages: |
| 214 | ok = False |
| 215 | with log_stage(stage): |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 216 | stage(ctx) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 217 | ok = True |
| 218 | if not ok: |
| 219 | break |
| 220 | |
| 221 | exc, cls, tb = sys.exc_info() |
| 222 | for stage in ctx.clear_calls_stack[::-1]: |
| 223 | with log_stage(stage): |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 224 | stage(ctx) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 225 | |
| 226 | logger.debug("Start utils.cleanup") |
| 227 | for clean_func, args, kwargs in utils.iter_clean_func(): |
| 228 | with log_stage(clean_func): |
| 229 | clean_func(*args, **kwargs) |
| 230 | |
| 231 | if exc is None: |
| 232 | for report_stage in report_stages: |
| 233 | with log_stage(report_stage): |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 234 | report_stage(ctx) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 235 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 236 | logger.info("All info is stored into %r", config.storage_url) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 237 | |
| 238 | if exc is None: |
| 239 | logger.info("Tests finished successfully") |
| 240 | return 0 |
| 241 | else: |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 242 | logger.error("Tests are failed. See error details in log above") |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 243 | return 1 |