blob: f57e1a5ffb9fc2110390adc638d1ee5a8dace527 [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
4import logging
5import argparse
6import functools
koder aka kdanilov39e449e2016-12-17 15:15:26 +02007import contextlib
8from typing import List, Tuple, Any, Callable, IO, cast, Optional, Iterator
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +03009from yaml import load as _yaml_load
10
koder aka kdanilov22d134e2016-11-08 11:33:19 +020011
12YLoader = Callable[[IO], Any]
13yaml_load = None # type: YLoader
14
15
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030016try:
17 from yaml import CLoader
koder aka kdanilov22d134e2016-11-08 11:33:19 +020018 yaml_load = cast(YLoader, functools.partial(_yaml_load, Loader=CLoader))
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030019except ImportError:
koder aka kdanilov22d134e2016-11-08 11:33:19 +020020 yaml_load = cast(YLoader, _yaml_load)
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030021
22
23import texttable
24
25try:
26 import faulthandler
27except ImportError:
28 faulthandler = None
29
koder aka kdanilove7e1a4d2016-12-17 20:29:52 +020030from . import utils, node
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +020031from .storage import make_storage, Storage
koder aka kdanilov22d134e2016-11-08 11:33:19 +020032from .config import Config
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030033from .logger import setup_loggers
koder aka kdanilov39e449e2016-12-17 15:15:26 +020034from .stage import Stage
koder aka kdanilov22d134e2016-11-08 11:33:19 +020035from .test_run_class import TestRun
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020036from .ssh import set_ssh_key_passwd
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030037
38
koder aka kdanilov39e449e2016-12-17 15:15:26 +020039# stages
40from .ceph import DiscoverCephStage
41from .openstack import DiscoverOSStage
42from .fuel import DiscoverFuelStage
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020043from .run_test import CollectInfoStage, ExplicitNodesStage, SaveNodesStage, RunTestsStage, ConnectStage, SleepStage
koder aka kdanilov39e449e2016-12-17 15:15:26 +020044from .report import ConsoleReportStage, HtmlReportStage
45from .sensors import StartSensorsStage, CollectSensorsStage
46
47
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030048logger = logging.getLogger("wally")
49
50
koder aka kdanilov39e449e2016-12-17 15:15:26 +020051@contextlib.contextmanager
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020052def log_stage(stage: Stage, cleanup: bool = False) -> Iterator[None]:
53 logger.info("Start " + stage.name() + ("::cleanup" if cleanup else ""))
koder aka kdanilov39e449e2016-12-17 15:15:26 +020054 try:
55 yield
56 except utils.StopTestError as exc:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020057 raise
koder aka kdanilov39e449e2016-12-17 15:15:26 +020058 except Exception:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020059 logger.exception("During %s", stage.name() + ("::cleanup" if cleanup else ""))
60 raise
koder aka kdanilov39e449e2016-12-17 15:15:26 +020061
62
koder aka kdanilov22d134e2016-11-08 11:33:19 +020063def list_results(path: str) -> List[Tuple[str, str, str, str]]:
koder aka kdanilov73084622016-11-16 21:51:08 +020064 results = [] # type: List[Tuple[float, str, str, str, str]]
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030065
koder aka kdanilov22d134e2016-11-08 11:33:19 +020066 for dir_name in os.listdir(path):
67 full_path = os.path.join(path, dir_name)
68
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030069 try:
koder aka kdanilov22d134e2016-11-08 11:33:19 +020070 stor = make_storage(full_path, existing=True)
71 except Exception as exc:
72 logger.warning("Can't load folder {}. Error {}".format(full_path, exc))
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030073
koder aka kdanilov73084622016-11-16 21:51:08 +020074 comment = cast(str, stor['info/comment'])
75 run_uuid = cast(str, stor['info/run_uuid'])
76 run_time = cast(float, stor['info/run_time'])
koder aka kdanilov22d134e2016-11-08 11:33:19 +020077 test_types = ""
koder aka kdanilov73084622016-11-16 21:51:08 +020078 results.append((run_time,
koder aka kdanilov22d134e2016-11-08 11:33:19 +020079 run_uuid,
80 test_types,
koder aka kdanilov73084622016-11-16 21:51:08 +020081 time.ctime(run_time),
koder aka kdanilov22d134e2016-11-08 11:33:19 +020082 '-' if comment is None else comment))
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030083
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030084 results.sort()
koder aka kdanilov22d134e2016-11-08 11:33:19 +020085 return [i[1:] for i in results]
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030086
87
koder aka kdanilov22d134e2016-11-08 11:33:19 +020088def log_nodes_statistic_stage(ctx: TestRun) -> None:
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030089 utils.log_nodes_statistic(ctx.nodes)
90
91
92def parse_args(argv):
93 descr = "Disk io performance test suite"
94 parser = argparse.ArgumentParser(prog='wally', description=descr)
koder aka kdanilov22d134e2016-11-08 11:33:19 +020095 parser.add_argument("-l", '--log-level', help="print some extra log info")
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020096 parser.add_argument("--ssh-key-passwd", default=None, help="Pass ssh key password")
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +020097 parser.add_argument("-s", '--settings-dir', default=None,
98 help="Folder to store key/settings/history files")
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030099
100 subparsers = parser.add_subparsers(dest='subparser_name')
101
102 # ---------------------------------------------------------------------
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200103 report_parser = subparsers.add_parser('ls', help='list all results')
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300104 report_parser.add_argument("result_storage", help="Folder with test results")
105
106 # ---------------------------------------------------------------------
107 compare_help = 'compare two results'
108 report_parser = subparsers.add_parser('compare', help=compare_help)
109 report_parser.add_argument("data_path1", help="First folder with test results")
110 report_parser.add_argument("data_path2", help="Second folder with test results")
111
112 # ---------------------------------------------------------------------
113 report_help = 'run report on previously obtained results'
114 report_parser = subparsers.add_parser('report', help=report_help)
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300115 report_parser.add_argument("data_dir", help="folder with rest results")
116
117 # ---------------------------------------------------------------------
118 test_parser = subparsers.add_parser('test', help='run tests')
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200119 test_parser.add_argument('--build-description', type=str, default="Build info")
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300120 test_parser.add_argument('--build-id', type=str, default="id")
121 test_parser.add_argument('--build-type', type=str, default="GA")
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200122 test_parser.add_argument('--dont-collect', action='store_true', help="Don't collect cluster info")
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200123 test_parser.add_argument('-n', '--no-tests', action='store_true', help="Don't run tests")
124 test_parser.add_argument('--load-report', action='store_true')
125 test_parser.add_argument("-k", '--keep-vm', action='store_true', help="Don't remove test vm's")
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300126 test_parser.add_argument("-d", '--dont-discover-nodes', action='store_true',
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200127 help="Don't connect/discover fuel nodes")
128 test_parser.add_argument('--no-report', action='store_true', help="Skip report stages")
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200129 test_parser.add_argument('--result-dir', default=None, help="Save results to DIR", metavar="DIR")
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300130 test_parser.add_argument("comment", help="Test information")
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200131 test_parser.add_argument("config_file", help="Yaml config file")
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300132
133 # ---------------------------------------------------------------------
koder aka kdanilove7e1a4d2016-12-17 20:29:52 +0200134 test_parser = subparsers.add_parser('resume', help='resume tests')
135 test_parser.add_argument("storage_dir", help="Path to test directory")
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300136
137 return parser.parse_args(argv[1:])
138
139
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +0200140def get_config_path(config: Config, opts_value: Optional[str]) -> str:
141 if opts_value is None and 'settings_dir' not in config:
142 val = "~/.wally"
143 elif opts_value is not None:
144 val = opts_value
145 else:
146 val = config.settings_dir
147
148 return os.path.abspath(os.path.expanduser(val))
149
150
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200151def find_cfg_file(name: str, included_from: str = None) -> str:
152 paths = [".", os.path.expanduser('~/.wally')]
153 if included_from is not None:
154 paths.append(os.path.dirname(included_from))
155
156 search_paths = set(os.path.abspath(path) for path in paths if os.path.isdir(path))
157
158 for folder in search_paths:
159 path = os.path.join(folder, name)
160 if os.path.exists(path):
161 return path
162
163 raise FileNotFoundError(name)
164
165
166def load_config(path: str) -> Config:
167 path = os.path.abspath(path)
168 cfg_dict = yaml_load(open(path).read())
169
170 while 'include' in cfg_dict:
171 inc = cfg_dict.pop('include')
172 if isinstance(inc, str):
173 inc = [inc]
174
175 for fname in inc:
176 inc_path = find_cfg_file(fname, path)
177 inc_dict = yaml_load(open(inc_path).read())
178 inc_dict.update(cfg_dict)
179 cfg_dict = inc_dict
180
181 return Config(cfg_dict)
182
183
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200184def main(argv: List[str]) -> int:
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300185 if faulthandler is not None:
186 faulthandler.register(signal.SIGUSR1, all_threads=True)
187
188 opts = parse_args(argv)
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200189 stages = [] # type: List[Stage]
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200190
191 # stop mypy from telling that config & storage might be undeclared
192 config = None # type: Config
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +0200193 storage = None # type: Storage
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300194
195 if opts.subparser_name == 'test':
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200196 config = load_config(opts.config_file)
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300197
koder aka kdanilove7e1a4d2016-12-17 20:29:52 +0200198 config.storage_url, config.run_uuid = utils.get_uniq_path_uuid(config.results_dir)
199 config.comment = opts.comment
200 config.keep_vm = opts.keep_vm
201 config.no_tests = opts.no_tests
202 config.dont_discover_nodes = opts.dont_discover_nodes
203 config.build_id = opts.build_id
204 config.build_description = opts.build_description
205 config.build_type = opts.build_type
206 config.settings_dir = get_config_path(config, opts.settings_dir)
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300207
koder aka kdanilove7e1a4d2016-12-17 20:29:52 +0200208 storage = make_storage(config.storage_url)
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200209
koder aka kdanilove7e1a4d2016-12-17 20:29:52 +0200210 storage['config'] = config # type: ignore
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300211
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200212 stages.append(DiscoverCephStage())
213 stages.append(DiscoverOSStage())
214 stages.append(DiscoverFuelStage())
215 stages.append(ExplicitNodesStage())
216 stages.append(SaveNodesStage())
217 stages.append(StartSensorsStage())
218 stages.append(RunTestsStage())
219 stages.append(CollectSensorsStage())
220 stages.append(ConnectStage())
221 stages.append(SleepStage())
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300222
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200223 if not opts.dont_collect:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200224 stages.append(CollectInfoStage())
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300225
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200226 argv2 = argv[:]
227 if '--ssh-key-passwd' in argv2:
228 # don't save ssh key password to storage
229 argv2[argv2.index("--ssh-key-passwd") + 1] = "<removed from output>"
230 storage['cli'] = argv2
koder aka kdanilove7e1a4d2016-12-17 20:29:52 +0200231
232 elif opts.subparser_name == 'resume':
233 storage = make_storage(opts.storage_dir, existing=True)
234 config = storage.load(Config, 'config')
235 # TODO: fix this
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200236 # TODO: add node loading from storage
237 # TODO: fill nodes conncreds with keys
koder aka kdanilove7e1a4d2016-12-17 20:29:52 +0200238 raise NotImplementedError("Resume in not fully implemented")
239
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300240 elif opts.subparser_name == 'ls':
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200241 tab = texttable.Texttable(max_width=200)
242 tab.set_deco(tab.HEADER | tab.VLINES | tab.BORDER)
243 tab.set_cols_align(["l", "l", "l", "l"])
244 tab.header(["Name", "Tests", "Run at", "Comment"])
245 tab.add_rows(list_results(opts.result_storage))
246 print(tab.draw())
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300247 return 0
248
249 elif opts.subparser_name == 'report':
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200250 storage = make_storage(opts.data_dir, existing=True)
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +0200251 config.settings_dir = get_config_path(config, opts.settings_dir)
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300252
253 elif opts.subparser_name == 'compare':
koder aka kdanilov73084622016-11-16 21:51:08 +0200254 # x = run_test.load_data_from_path(opts.data_path1)
255 # y = run_test.load_data_from_path(opts.data_path2)
256 # print(run_test.IOPerfTest.format_diff_for_console(
257 # [x['io'][0], y['io'][0]]))
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300258 return 0
259
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200260 report_stages = [] # type: List[Stage]
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200261 if not getattr(opts, "no_report", False):
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200262 report_stages.append(ConsoleReportStage())
263 report_stages.append(HtmlReportStage())
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300264
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200265 # log level is not a part of config
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300266 if opts.log_level is not None:
267 str_level = opts.log_level
268 else:
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200269 str_level = config.get('logging/log_level', 'INFO')
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300270
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200271 log_config_file = config.get('logging/config', None)
272
273 if log_config_file is not None:
274 log_config_file = find_cfg_file(log_config_file, opts.config_file)
275
276 setup_loggers(getattr(logging, str_level),
277 log_fd=storage.get_stream('log', "w"),
278 config_file=log_config_file)
279
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200280 logger.info("All info would be stored into %r", config.storage_url)
281
282 ctx = TestRun(config, storage)
koder aka kdanilove7e1a4d2016-12-17 20:29:52 +0200283 ctx.rpc_code, ctx.default_rpc_plugins = node.get_rpc_server_code()
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300284
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200285 if opts.ssh_key_passwd is not None:
286 set_ssh_key_passwd(opts.ssh_key_passwd)
287
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200288 stages.sort(key=lambda x: x.priority)
289
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200290 # TODO: run only stages, which have config
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200291 failed = False
292 cleanup_stages = []
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300293 for stage in stages:
koder aka kdanilove7e1a4d2016-12-17 20:29:52 +0200294 if stage.config_block is not None:
295 if stage.config_block not in ctx.config:
296 continue
297
298 cleanup_stages.append(stage)
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200299 try:
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200300 with log_stage(stage):
301 stage.run(ctx)
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200302 except (Exception, KeyboardInterrupt):
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200303 failed = True
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300304 break
305
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200306 logger.debug("Start cleanup")
307 cleanup_failed = False
308 for stage in cleanup_stages[::-1]:
309 try:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200310 with log_stage(stage, cleanup=True):
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200311 stage.cleanup(ctx)
312 except:
313 cleanup_failed = True
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300314
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200315 if not failed:
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300316 for report_stage in report_stages:
317 with log_stage(report_stage):
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200318 report_stage.run(ctx)
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300319
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200320 logger.info("All info is stored into %r", config.storage_url)
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300321
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200322 if failed or cleanup_failed:
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200323 logger.error("Tests are failed. See error details in log above")
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300324 return 1
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200325 else:
326 logger.info("Tests finished successfully")
327 return 0