blob: 56d72db1897535f3964071f1f012e04656b8273c [file] [log] [blame]
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +03001import os
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +03002import time
3import signal
koder aka kdanilov7f59d562016-12-26 01:34:23 +02004import pprint
koder aka kdanilovf2865172016-12-30 03:35:11 +02005import getpass
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +03006import logging
7import argparse
8import functools
koder aka kdanilov39e449e2016-12-17 15:15:26 +02009import contextlib
kdanylov aka koder938f75f2018-06-27 01:52:44 +030010import datetime
koder aka kdanilov39e449e2016-12-17 15:15:26 +020011from typing import List, Tuple, Any, Callable, IO, cast, Optional, Iterator
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030012from yaml import load as _yaml_load
13
koder aka kdanilov22d134e2016-11-08 11:33:19 +020014YLoader = Callable[[IO], Any]
kdanylov aka koder13e58452018-07-15 02:51:51 +030015yaml_load: YLoader = None # type: ignore
koder aka kdanilov22d134e2016-11-08 11:33:19 +020016
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030017try:
18 from yaml import CLoader
koder aka kdanilov22d134e2016-11-08 11:33:19 +020019 yaml_load = cast(YLoader, functools.partial(_yaml_load, Loader=CLoader))
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030020except ImportError:
koder aka kdanilov22d134e2016-11-08 11:33:19 +020021 yaml_load = cast(YLoader, _yaml_load)
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030022
kdanylov aka koderb0833332017-05-13 20:39:17 +030023from cephlib.texttable import Texttable
24from cephlib.istorage import IStorage
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030025
26try:
27 import faulthandler
28except ImportError:
29 faulthandler = None
30
kdanylov aka koder150b2192017-04-01 16:53:01 +030031from cephlib.common import setup_logging
kdanylov aka koderb0833332017-05-13 20:39:17 +030032from cephlib.storage import make_storage
kdanylov aka koder84de1e42017-05-22 14:00:07 +030033from cephlib.wally_storage import WallyDB
kdanylov aka koder026e5f22017-05-15 01:04:39 +030034from cephlib.ssh import set_ssh_key_passwd
35from cephlib.node import log_nodes_statistic
36from cephlib.node_impl import get_rpc_server_code
kdanylov aka koder150b2192017-04-01 16:53:01 +030037
kdanylov aka koder026e5f22017-05-15 01:04:39 +030038from . import utils, report_profiles, report
koder aka kdanilov22d134e2016-11-08 11:33:19 +020039from .config import Config
koder aka kdanilov39e449e2016-12-17 15:15:26 +020040from .stage import Stage
koder aka kdanilov22d134e2016-11-08 11:33:19 +020041from .test_run_class import TestRun
kdanylov aka koder026e5f22017-05-15 01:04:39 +030042from .result_storage import WallyStorage
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030043
koder aka kdanilov39e449e2016-12-17 15:15:26 +020044# stages
kdanylov aka koderb0833332017-05-13 20:39:17 +030045from .ceph import DiscoverCephStage, CollectCephInfoStage
koder aka kdanilov39e449e2016-12-17 15:15:26 +020046from .openstack import DiscoverOSStage
koder aka kdanilov23e6bdf2016-12-24 02:18:54 +020047from .run_test import (CollectInfoStage, ExplicitNodesStage, SaveNodesStage,
koder aka kdanilov7f59d562016-12-26 01:34:23 +020048 RunTestsStage, ConnectStage, SleepStage, PrepareNodes,
49 LoadStoredNodesStage)
kdanylov aka koder150b2192017-04-01 16:53:01 +030050
51from .report import HtmlReportStage
kdanylov aka koderaaad8b82018-07-17 04:24:14 +030052from .sensors import StartSensorsStage, CollectSensorsStage, StopSensorsStage
kdanylov aka koder150b2192017-04-01 16:53:01 +030053from .console_report import ConsoleReportStage
koder aka kdanilov39e449e2016-12-17 15:15:26 +020054
55
kdanylov aka koder938f75f2018-06-27 01:52:44 +030056try:
57 assert False
58except AssertionError:
59 pass
60else:
61 print("Must not run this code with -o python option. Assertions are important!")
62 exit(1)
63
64
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030065logger = logging.getLogger("wally")
66
67
koder aka kdanilov39e449e2016-12-17 15:15:26 +020068@contextlib.contextmanager
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020069def log_stage(stage: Stage, cleanup: bool = False) -> Iterator[None]:
70 logger.info("Start " + stage.name() + ("::cleanup" if cleanup else ""))
koder aka kdanilov39e449e2016-12-17 15:15:26 +020071 try:
72 yield
kdanylov aka koder938f75f2018-06-27 01:52:44 +030073 except utils.StopTestError:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020074 raise
koder aka kdanilov39e449e2016-12-17 15:15:26 +020075 except Exception:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020076 logger.exception("During %s", stage.name() + ("::cleanup" if cleanup else ""))
77 raise
koder aka kdanilov39e449e2016-12-17 15:15:26 +020078
79
kdanylov aka koder84de1e42017-05-22 14:00:07 +030080def list_results(path: str, limit: int = None) -> List[Tuple[str, str, str, str, str]]:
81 dirs = []
koder aka kdanilov22d134e2016-11-08 11:33:19 +020082 for dir_name in os.listdir(path):
83 full_path = os.path.join(path, dir_name)
kdanylov aka koder84de1e42017-05-22 14:00:07 +030084 dirs.append((os.stat(full_path).st_ctime, full_path))
koder aka kdanilov22d134e2016-11-08 11:33:19 +020085
kdanylov aka koder84de1e42017-05-22 14:00:07 +030086 dirs.sort()
kdanylov aka koder938f75f2018-06-27 01:52:44 +030087 results: List[Tuple[str, str, str, str, str]] = []
kdanylov aka koder84de1e42017-05-22 14:00:07 +030088 for _, full_path in dirs[::-1]:
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030089 try:
koder aka kdanilov22d134e2016-11-08 11:33:19 +020090 stor = make_storage(full_path, existing=True)
91 except Exception as exc:
kdanylov aka koder938f75f2018-06-27 01:52:44 +030092 logger.warning(f"Can't load folder {full_path}. Error {exc}")
93 continue
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030094
kdanylov aka koder84de1e42017-05-22 14:00:07 +030095 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 koder938f75f2018-06-27 01:52:44 +0300101 print(f"Fail to load {os.path.basename(full_path)}. {exc}")
kdanylov aka koder84de1e42017-05-22 14:00:07 +0300102 continue
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300103
kdanylov aka koder84de1e42017-05-22 14:00:07 +0300104 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 koder938f75f2018-06-27 01:52:44 +0300109 ftime = f"{datetime.datetime.fromtimestamp(run_time):%d %b %H:%M}"
kdanylov aka koder84de1e42017-05-22 14:00:07 +0300110
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 kdanilov0fdaaee2015-06-30 11:10:48 +0300128
129
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200130def log_nodes_statistic_stage(ctx: TestRun) -> None:
koder aka kdanilov108ac362017-01-19 20:17:16 +0200131 log_nodes_statistic(ctx.nodes)
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300132
133
134def parse_args(argv):
135 descr = "Disk io performance test suite"
136 parser = argparse.ArgumentParser(prog='wally', description=descr)
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200137 parser.add_argument("-l", '--log-level', help="print some extra log info")
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200138 parser.add_argument("--ssh-key-passwd", default=None, help="Pass ssh key password")
kdanylov aka koder84de1e42017-05-22 14:00:07 +0300139 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 kdanilov3d2bc4f2016-11-12 18:31:18 +0200141 parser.add_argument("-s", '--settings-dir', default=None,
142 help="Folder to store key/settings/history files")
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300143 subparsers = parser.add_subparsers(dest='subparser_name')
144
145 # ---------------------------------------------------------------------
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200146 report_parser = subparsers.add_parser('ls', help='list all results')
kdanylov aka koder84de1e42017-05-22 14:00:07 +0300147 report_parser.add_argument("-l", "--limit", metavar='LIMIT', help="Show only LIMIT last results",
148 default=None, type=int)
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300149 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 koder150b2192017-04-01 16:53:01 +0300160 report_parser.add_argument('-R', '--reporters', help="Comma-separated list of reportes - html,txt",
161 default='html,txt')
kdanylov aka koderb0833332017-05-13 20:39:17 +0300162 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 kdanilov0fdaaee2015-06-30 11:10:48 +0300164 report_parser.add_argument("data_dir", help="folder with rest results")
165
166 # ---------------------------------------------------------------------
koder aka kdanilov108ac362017-01-19 20:17:16 +0200167 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 koder470a8fa2017-07-14 21:07:58 +0300171 # 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 kdanilov108ac362017-01-19 20:17:16 +0200174
175 # ---------------------------------------------------------------------
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300176 test_parser = subparsers.add_parser('test', help='run tests')
kdanylov aka koder150b2192017-04-01 16:53:01 +0300177 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 kdanilov22d134e2016-11-08 11:33:19 +0200186 test_parser.add_argument('--build-description', type=str, default="Build info")
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300187 test_parser.add_argument('--build-id', type=str, default="id")
188 test_parser.add_argument('--build-type', type=str, default="GA")
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300189 test_parser.add_argument("comment", help="Test information")
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200190 test_parser.add_argument("config_file", help="Yaml config file")
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300191
192 # ---------------------------------------------------------------------
koder aka kdanilove7e1a4d2016-12-17 20:29:52 +0200193 test_parser = subparsers.add_parser('resume', help='resume tests')
194 test_parser.add_argument("storage_dir", help="Path to test directory")
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300195
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200196 # ---------------------------------------------------------------------
koder aka kdanilov108ac362017-01-19 20:17:16 +0200197 test_parser = subparsers.add_parser('db', help='Exec command on DB')
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200198 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 kdanilov0fdaaee2015-06-30 11:10:48 +0300202 return parser.parse_args(argv[1:])
203
204
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +0200205def 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 kdanilov962ee5f2016-12-19 02:40:08 +0200216def 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
231def load_config(path: str) -> Config:
232 path = os.path.abspath(path)
kdanylov aka koder938f75f2018-06-27 01:52:44 +0300233 cfg_dict = yaml_load(open(path))
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200234
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 koder938f75f2018-06-27 01:52:44 +0300242 inc_dict = yaml_load(open(inc_path))
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200243 inc_dict.update(cfg_dict)
244 cfg_dict = inc_dict
245
246 return Config(cfg_dict)
247
248
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200249def get_run_stages() -> List[Stage]:
250 return [DiscoverCephStage(),
kdanylov aka koderb0833332017-05-13 20:39:17 +0300251 CollectCephInfoStage(),
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200252 DiscoverOSStage(),
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200253 ExplicitNodesStage(),
254 StartSensorsStage(),
255 RunTestsStage(),
kdanylov aka koderaaad8b82018-07-17 04:24:14 +0300256 StopSensorsStage(),
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200257 ConnectStage(),
258 SleepStage(),
259 PrepareNodes()]
260
261
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200262def main(argv: List[str]) -> int:
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300263 if faulthandler is not None:
264 faulthandler.register(signal.SIGUSR1, all_threads=True)
265
266 opts = parse_args(argv)
kdanylov aka koder13e58452018-07-15 02:51:51 +0300267 stages: List[Stage] = []
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200268
269 # stop mypy from telling that config & storage might be undeclared
kdanylov aka koder13e58452018-07-15 02:51:51 +0300270 config: Config = None # type: ignore
271 storage: IStorage = None # type: ignore
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300272
kdanylov aka koder84de1e42017-05-22 14:00:07 +0300273 if opts.profile:
274 import cProfile
kdanylov aka koder13e58452018-07-15 02:51:51 +0300275 pr: Optional[cProfile.Profile] = cProfile.Profile()
276 pr.enable() # type: ignore
kdanylov aka koder84de1e42017-05-22 14:00:07 +0300277 else:
278 pr = None
279
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300280 if opts.subparser_name == 'test':
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200281 config = load_config(opts.config_file)
kdanylov aka koder150b2192017-04-01 16:53:01 +0300282 config.storage_url, config.run_uuid = utils.get_uniq_path_uuid(config.results_storage)
koder aka kdanilove7e1a4d2016-12-17 20:29:52 +0200283 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 kodercdfcdaf2017-04-29 10:03:39 +0300291 config.discover = set(name for name in config.get('discover', '').split(",") if name)
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300292
koder aka kdanilove7e1a4d2016-12-17 20:29:52 +0200293 storage = make_storage(config.storage_url)
kdanylov aka koder84de1e42017-05-22 14:00:07 +0300294 storage.put(config, WallyDB.config)
koder aka kdanilovffaf48d2016-12-27 02:25:29 +0200295
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200296 stages.extend(get_run_stages())
koder aka kdanilovffaf48d2016-12-27 02:25:29 +0200297 stages.append(SaveNodesStage())
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300298
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200299 if not opts.dont_collect:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200300 stages.append(CollectInfoStage())
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300301
kdanylov aka koderaaad8b82018-07-17 04:24:14 +0300302 if config.no_tests:
303 stages.append(CollectSensorsStage())
304
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200305 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 kdanilov7f59d562016-12-26 01:34:23 +0200309 storage.put(argv2, 'cli')
koder aka kdanilove7e1a4d2016-12-17 20:29:52 +0200310
311 elif opts.subparser_name == 'resume':
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200312 opts.resumed = True
koder aka kdanilove7e1a4d2016-12-17 20:29:52 +0200313 storage = make_storage(opts.storage_dir, existing=True)
kdanylov aka koder84de1e42017-05-22 14:00:07 +0300314 config = storage.load(Config, WallyDB.config)
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200315 stages.extend(get_run_stages())
316 stages.append(LoadStoredNodesStage())
koder aka kdanilov108ac362017-01-19 20:17:16 +0200317 prev_opts = storage.get('cli') # type: List[str]
318
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200319 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 kdanilove7e1a4d2016-12-17 20:29:52 +0200325
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300326 elif opts.subparser_name == 'ls':
kdanylov aka koderb0833332017-05-13 20:39:17 +0300327 tab = Texttable(max_width=200)
kdanylov aka koder84de1e42017-05-22 14:00:07 +0300328 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 kdanilov22d134e2016-11-08 11:33:19 +0200332 print(tab.draw())
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300333 return 0
334
335 elif opts.subparser_name == 'report':
koder aka kdanilovffaf48d2016-12-27 02:25:29 +0200336 if getattr(opts, "no_report", False):
337 print(" --no-report option can't be used with 'report' cmd")
338 return 1
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200339 storage = make_storage(opts.data_dir, existing=True)
kdanylov aka koder84de1e42017-05-22 14:00:07 +0300340 config = storage.load(Config, WallyDB.config)
kdanylov aka koderb0833332017-05-13 20:39:17 +0300341 report_profiles.default_format = opts.format
342 report.default_format = opts.format
koder aka kdanilovffaf48d2016-12-27 02:25:29 +0200343 stages.append(LoadStoredNodesStage())
kdanylov aka koder736e5c12017-05-07 17:27:14 +0300344 stages.append(SaveNodesStage())
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300345 elif opts.subparser_name == 'compare':
koder aka kdanilov73084622016-11-16 21:51:08 +0200346 # 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 kdanilov0fdaaee2015-06-30 11:10:48 +0300350 return 0
351
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200352 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 kdanilov108ac362017-01-19 20:17:16 +0200363 elif opts.subparser_name == 'ipython':
364 storage = make_storage(opts.storage_dir, existing=True)
kdanylov aka koder026e5f22017-05-15 01:04:39 +0300365 rstorage = WallyStorage(storage=storage)
koder aka kdanilov108ac362017-01-19 20:17:16 +0200366
367 import IPython
368 IPython.embed()
369
370 return 0
koder aka kdanilova732a602017-02-01 20:29:56 +0200371 else:
372 print("Subparser {!r} is not supported".format(opts.subparser_name))
373 return 1
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200374
kdanylov aka koder84de1e42017-05-22 14:00:07 +0300375 start_time = int(time.time())
376
kdanylov aka koder938f75f2018-06-27 01:52:44 +0300377 report_stages: List[Stage] = []
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200378 if not getattr(opts, "no_report", False):
kdanylov aka koder150b2192017-04-01 16:53:01 +0300379 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 kdanilov0fdaaee2015-06-30 11:10:48 +0300386
kdanylov aka koder150b2192017-04-01 16:53:01 +0300387 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 kdanilov962ee5f2016-12-19 02:40:08 +0200390
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200391 logger.info("All info would be stored into %r", config.storage_url)
392
kdanylov aka koder026e5f22017-05-15 01:04:39 +0300393 ctx = TestRun(config, storage, WallyStorage(storage))
394 ctx.rpc_code, ctx.default_rpc_plugins = get_rpc_server_code()
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300395
kdanylov aka koder84de1e42017-05-22 14:00:07 +0300396 if 'dev_roles' in ctx.config:
397 ctx.devs_locator = ctx.config.dev_roles
398
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200399 if opts.ssh_key_passwd is not None:
400 set_ssh_key_passwd(opts.ssh_key_passwd)
koder aka kdanilovf2865172016-12-30 03:35:11 +0200401 elif opts.ssh_key_passwd_kbd:
402 set_ssh_key_passwd(getpass.getpass("Ssh key password: ").strip())
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200403
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200404 stages.sort(key=lambda x: x.priority)
405
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200406 # TODO: run only stages, which have config
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200407 failed = False
408 cleanup_stages = []
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200409
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300410 for stage in stages:
koder aka kdanilove7e1a4d2016-12-17 20:29:52 +0200411 if stage.config_block is not None:
412 if stage.config_block not in ctx.config:
kdanylov aka koder150b2192017-04-01 16:53:01 +0300413 logger.debug("Skip stage %r, as config has no required block %r", stage.name(), stage.config_block)
koder aka kdanilove7e1a4d2016-12-17 20:29:52 +0200414 continue
415
416 cleanup_stages.append(stage)
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200417 try:
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200418 with log_stage(stage):
419 stage.run(ctx)
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200420 except (Exception, KeyboardInterrupt):
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200421 failed = True
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300422 break
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200423 ctx.storage.sync()
424 ctx.storage.sync()
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300425
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200426 logger.debug("Start cleanup")
427 cleanup_failed = False
428 for stage in cleanup_stages[::-1]:
429 try:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200430 with log_stage(stage, cleanup=True):
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200431 stage.cleanup(ctx)
432 except:
433 cleanup_failed = True
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200434 ctx.storage.sync()
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300435
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200436 if not failed:
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300437 for report_stage in report_stages:
438 with log_stage(report_stage):
koder aka kdanilovffaf48d2016-12-27 02:25:29 +0200439 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 kdanilov0fdaaee2015-06-30 11:10:48 +0300445
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200446 ctx.storage.sync()
447
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200448 logger.info("All info is stored into %r", config.storage_url)
kdanylov aka koder84de1e42017-05-22 14:00:07 +0300449 end_time = int(time.time())
450 storage.put([start_time, end_time], WallyDB.run_interval)
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300451
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200452 if failed or cleanup_failed:
kdanylov aka koder026e5f22017-05-15 01:04:39 +0300453 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 koder84de1e42017-05-22 14:00:07 +0300457 code = 1
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200458 else:
kdanylov aka koder026e5f22017-05-15 01:04:39 +0300459 if opts.subparser_name == 'report':
460 logger.info("Report successfully generated")
461 else:
462 logger.info("Tests finished successfully")
kdanylov aka koder84de1e42017-05-22 14:00:07 +0300463 code = 0
464
465 if opts.profile:
466 assert pr is not None
kdanylov aka koder13e58452018-07-15 02:51:51 +0300467 pr.disable() # type: ignore
kdanylov aka koder84de1e42017-05-22 14:00:07 +0300468 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