blob: e43f1f92782fe5ad224860902a70496fdbbbf63c [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
10from typing import List, Tuple, Any, Callable, IO, cast, Optional, Iterator
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030011from yaml import load as _yaml_load
12
koder aka kdanilov22d134e2016-11-08 11:33:19 +020013
14YLoader = Callable[[IO], Any]
15yaml_load = None # type: YLoader
16
17
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030018try:
19 from yaml import CLoader
koder aka kdanilov22d134e2016-11-08 11:33:19 +020020 yaml_load = cast(YLoader, functools.partial(_yaml_load, Loader=CLoader))
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030021except ImportError:
koder aka kdanilov22d134e2016-11-08 11:33:19 +020022 yaml_load = cast(YLoader, _yaml_load)
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030023
kdanylov aka koderb0833332017-05-13 20:39:17 +030024from cephlib.texttable import Texttable
25from cephlib.istorage import IStorage
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030026
27try:
28 import faulthandler
29except ImportError:
30 faulthandler = None
31
kdanylov aka koder150b2192017-04-01 16:53:01 +030032from cephlib.common import setup_logging
kdanylov aka koderb0833332017-05-13 20:39:17 +030033from cephlib.storage import make_storage
kdanylov aka koder150b2192017-04-01 16:53:01 +030034
kdanylov aka koderb0833332017-05-13 20:39:17 +030035from . import utils, node, report_profiles, report
koder aka kdanilov108ac362017-01-19 20:17:16 +020036from .node_utils import log_nodes_statistic
koder aka kdanilov22d134e2016-11-08 11:33:19 +020037from .config import Config
koder aka kdanilov39e449e2016-12-17 15:15:26 +020038from .stage import Stage
koder aka kdanilov22d134e2016-11-08 11:33:19 +020039from .test_run_class import TestRun
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020040from .ssh import set_ssh_key_passwd
kdanylov aka koderb0833332017-05-13 20:39:17 +030041from .result_storage import ResultStorage
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030042
koder aka kdanilov39e449e2016-12-17 15:15:26 +020043# stages
kdanylov aka koderb0833332017-05-13 20:39:17 +030044from .ceph import DiscoverCephStage, CollectCephInfoStage
koder aka kdanilov39e449e2016-12-17 15:15:26 +020045from .openstack import DiscoverOSStage
46from .fuel import DiscoverFuelStage
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
koder aka kdanilov39e449e2016-12-17 15:15:26 +020052from .sensors import StartSensorsStage, CollectSensorsStage
kdanylov aka koder150b2192017-04-01 16:53:01 +030053from .console_report import ConsoleReportStage
koder aka kdanilov39e449e2016-12-17 15:15:26 +020054
55
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030056logger = logging.getLogger("wally")
57
58
koder aka kdanilov39e449e2016-12-17 15:15:26 +020059@contextlib.contextmanager
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020060def log_stage(stage: Stage, cleanup: bool = False) -> Iterator[None]:
61 logger.info("Start " + stage.name() + ("::cleanup" if cleanup else ""))
koder aka kdanilov39e449e2016-12-17 15:15:26 +020062 try:
63 yield
64 except utils.StopTestError as exc:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020065 raise
koder aka kdanilov39e449e2016-12-17 15:15:26 +020066 except Exception:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020067 logger.exception("During %s", stage.name() + ("::cleanup" if cleanup else ""))
68 raise
koder aka kdanilov39e449e2016-12-17 15:15:26 +020069
70
koder aka kdanilov22d134e2016-11-08 11:33:19 +020071def list_results(path: str) -> List[Tuple[str, str, str, str]]:
koder aka kdanilov73084622016-11-16 21:51:08 +020072 results = [] # type: List[Tuple[float, str, str, str, str]]
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030073
koder aka kdanilov22d134e2016-11-08 11:33:19 +020074 for dir_name in os.listdir(path):
75 full_path = os.path.join(path, dir_name)
76
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030077 try:
koder aka kdanilov22d134e2016-11-08 11:33:19 +020078 stor = make_storage(full_path, existing=True)
79 except Exception as exc:
80 logger.warning("Can't load folder {}. Error {}".format(full_path, exc))
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030081
koder aka kdanilov7f59d562016-12-26 01:34:23 +020082 comment = cast(str, stor.get('info/comment'))
83 run_uuid = cast(str, stor.get('info/run_uuid'))
84 run_time = cast(float, stor.get('info/run_time'))
koder aka kdanilov22d134e2016-11-08 11:33:19 +020085 test_types = ""
koder aka kdanilov73084622016-11-16 21:51:08 +020086 results.append((run_time,
koder aka kdanilov22d134e2016-11-08 11:33:19 +020087 run_uuid,
88 test_types,
koder aka kdanilov73084622016-11-16 21:51:08 +020089 time.ctime(run_time),
koder aka kdanilov22d134e2016-11-08 11:33:19 +020090 '-' if comment is None else comment))
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030091
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030092 results.sort()
koder aka kdanilov22d134e2016-11-08 11:33:19 +020093 return [i[1:] for i in results]
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030094
95
koder aka kdanilov22d134e2016-11-08 11:33:19 +020096def log_nodes_statistic_stage(ctx: TestRun) -> None:
koder aka kdanilov108ac362017-01-19 20:17:16 +020097 log_nodes_statistic(ctx.nodes)
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030098
99
100def parse_args(argv):
101 descr = "Disk io performance test suite"
102 parser = argparse.ArgumentParser(prog='wally', description=descr)
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200103 parser.add_argument("-l", '--log-level', help="print some extra log info")
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200104 parser.add_argument("--ssh-key-passwd", default=None, help="Pass ssh key password")
koder aka kdanilovf2865172016-12-30 03:35:11 +0200105 parser.add_argument("--ssh-key-passwd-kbd", action="store_true", help="Enter ssh key password interactivelly")
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +0200106 parser.add_argument("-s", '--settings-dir', default=None,
107 help="Folder to store key/settings/history files")
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300108
109 subparsers = parser.add_subparsers(dest='subparser_name')
110
111 # ---------------------------------------------------------------------
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200112 report_parser = subparsers.add_parser('ls', help='list all results')
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300113 report_parser.add_argument("result_storage", help="Folder with test results")
114
115 # ---------------------------------------------------------------------
116 compare_help = 'compare two results'
117 report_parser = subparsers.add_parser('compare', help=compare_help)
118 report_parser.add_argument("data_path1", help="First folder with test results")
119 report_parser.add_argument("data_path2", help="Second folder with test results")
120
121 # ---------------------------------------------------------------------
122 report_help = 'run report on previously obtained results'
123 report_parser = subparsers.add_parser('report', help=report_help)
kdanylov aka koder150b2192017-04-01 16:53:01 +0300124 report_parser.add_argument('-R', '--reporters', help="Comma-separated list of reportes - html,txt",
125 default='html,txt')
kdanylov aka koderb0833332017-05-13 20:39:17 +0300126 report_parser.add_argument('-f', '--format', help="Images format, default is " + report_profiles.default_format,
127 choices=('svg', 'png'), default=report_profiles.default_format)
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300128 report_parser.add_argument("data_dir", help="folder with rest results")
129
130 # ---------------------------------------------------------------------
koder aka kdanilov108ac362017-01-19 20:17:16 +0200131 ipython_help = 'run ipython in prepared environment'
132 ipython_parser = subparsers.add_parser('ipython', help=ipython_help)
133 ipython_parser.add_argument("storage_dir", help="Storage path")
134 # ---------------------------------------------------------------------
135 jupyter_help = 'run ipython in prepared environment'
136 jupyter_parser = subparsers.add_parser('jupyter', help=jupyter_help)
137 jupyter_parser.add_argument("storage_dir", help="Storage path")
138
139 # ---------------------------------------------------------------------
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300140 test_parser = subparsers.add_parser('test', help='run tests')
kdanylov aka koder150b2192017-04-01 16:53:01 +0300141 test_parser.add_argument("-d", '--dont-discover-nodes', action='store_true', help="Don't discover nodes")
142 test_parser.add_argument('-D', '--dont-collect', action='store_true', help="Don't collect cluster info")
143 test_parser.add_argument("-k", '--keep-vm', action='store_true', help="Don't remove test vm's")
144 test_parser.add_argument('-L', '--load-report', action='store_true', help="Create cluster load report")
145 test_parser.add_argument('-n', '--no-tests', action='store_true', help="Don't run tests")
146 test_parser.add_argument('-N', '--no-report', action='store_true', help="Skip report stages")
147 test_parser.add_argument('-r', '--result-dir', default=None, help="Save results to DIR", metavar="DIR")
148 test_parser.add_argument('-R', '--reporters', help="Comma-separated list of reportes - html,txt",
149 default='html,txt')
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200150 test_parser.add_argument('--build-description', type=str, default="Build info")
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300151 test_parser.add_argument('--build-id', type=str, default="id")
152 test_parser.add_argument('--build-type', type=str, default="GA")
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300153 test_parser.add_argument("comment", help="Test information")
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200154 test_parser.add_argument("config_file", help="Yaml config file")
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300155
156 # ---------------------------------------------------------------------
koder aka kdanilove7e1a4d2016-12-17 20:29:52 +0200157 test_parser = subparsers.add_parser('resume', help='resume tests')
158 test_parser.add_argument("storage_dir", help="Path to test directory")
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300159
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200160 # ---------------------------------------------------------------------
koder aka kdanilov108ac362017-01-19 20:17:16 +0200161 test_parser = subparsers.add_parser('db', help='Exec command on DB')
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200162 test_parser.add_argument("cmd", choices=("show",), help="Command to execute")
163 test_parser.add_argument("params", nargs='*', help="Command params")
164 test_parser.add_argument("storage_dir", help="Storage path")
165
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300166 return parser.parse_args(argv[1:])
167
168
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +0200169def get_config_path(config: Config, opts_value: Optional[str]) -> str:
170 if opts_value is None and 'settings_dir' not in config:
171 val = "~/.wally"
172 elif opts_value is not None:
173 val = opts_value
174 else:
175 val = config.settings_dir
176
177 return os.path.abspath(os.path.expanduser(val))
178
179
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200180def find_cfg_file(name: str, included_from: str = None) -> str:
181 paths = [".", os.path.expanduser('~/.wally')]
182 if included_from is not None:
183 paths.append(os.path.dirname(included_from))
184
185 search_paths = set(os.path.abspath(path) for path in paths if os.path.isdir(path))
186
187 for folder in search_paths:
188 path = os.path.join(folder, name)
189 if os.path.exists(path):
190 return path
191
192 raise FileNotFoundError(name)
193
194
195def load_config(path: str) -> Config:
196 path = os.path.abspath(path)
197 cfg_dict = yaml_load(open(path).read())
198
199 while 'include' in cfg_dict:
200 inc = cfg_dict.pop('include')
201 if isinstance(inc, str):
202 inc = [inc]
203
204 for fname in inc:
205 inc_path = find_cfg_file(fname, path)
206 inc_dict = yaml_load(open(inc_path).read())
207 inc_dict.update(cfg_dict)
208 cfg_dict = inc_dict
209
210 return Config(cfg_dict)
211
212
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200213def get_run_stages() -> List[Stage]:
214 return [DiscoverCephStage(),
kdanylov aka koderb0833332017-05-13 20:39:17 +0300215 CollectCephInfoStage(),
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200216 DiscoverOSStage(),
217 DiscoverFuelStage(),
218 ExplicitNodesStage(),
219 StartSensorsStage(),
220 RunTestsStage(),
221 CollectSensorsStage(),
222 ConnectStage(),
223 SleepStage(),
224 PrepareNodes()]
225
226
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200227def main(argv: List[str]) -> int:
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300228 if faulthandler is not None:
229 faulthandler.register(signal.SIGUSR1, all_threads=True)
230
231 opts = parse_args(argv)
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200232 stages = [] # type: List[Stage]
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200233
234 # stop mypy from telling that config & storage might be undeclared
235 config = None # type: Config
kdanylov aka koderb0833332017-05-13 20:39:17 +0300236 storage = None # type: IStorage
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300237
238 if opts.subparser_name == 'test':
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200239 config = load_config(opts.config_file)
kdanylov aka koder150b2192017-04-01 16:53:01 +0300240 config.storage_url, config.run_uuid = utils.get_uniq_path_uuid(config.results_storage)
koder aka kdanilove7e1a4d2016-12-17 20:29:52 +0200241 config.comment = opts.comment
242 config.keep_vm = opts.keep_vm
243 config.no_tests = opts.no_tests
244 config.dont_discover_nodes = opts.dont_discover_nodes
245 config.build_id = opts.build_id
246 config.build_description = opts.build_description
247 config.build_type = opts.build_type
248 config.settings_dir = get_config_path(config, opts.settings_dir)
kdanylov aka kodercdfcdaf2017-04-29 10:03:39 +0300249 config.discover = set(name for name in config.get('discover', '').split(",") if name)
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300250
koder aka kdanilove7e1a4d2016-12-17 20:29:52 +0200251 storage = make_storage(config.storage_url)
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200252 storage.put(config, 'config')
koder aka kdanilovffaf48d2016-12-27 02:25:29 +0200253
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200254 stages.extend(get_run_stages())
koder aka kdanilovffaf48d2016-12-27 02:25:29 +0200255 stages.append(SaveNodesStage())
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300256
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200257 if not opts.dont_collect:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200258 stages.append(CollectInfoStage())
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300259
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200260 argv2 = argv[:]
261 if '--ssh-key-passwd' in argv2:
262 # don't save ssh key password to storage
263 argv2[argv2.index("--ssh-key-passwd") + 1] = "<removed from output>"
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200264 storage.put(argv2, 'cli')
koder aka kdanilove7e1a4d2016-12-17 20:29:52 +0200265
266 elif opts.subparser_name == 'resume':
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200267 opts.resumed = True
koder aka kdanilove7e1a4d2016-12-17 20:29:52 +0200268 storage = make_storage(opts.storage_dir, existing=True)
269 config = storage.load(Config, 'config')
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200270 stages.extend(get_run_stages())
271 stages.append(LoadStoredNodesStage())
koder aka kdanilov108ac362017-01-19 20:17:16 +0200272 prev_opts = storage.get('cli') # type: List[str]
273
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200274 if '--ssh-key-passwd' in prev_opts and opts.ssh_key_passwd:
275 prev_opts[prev_opts.index("--ssh-key-passwd") + 1] = opts.ssh_key_passwd
276
277 restored_opts = parse_args(prev_opts)
278 opts.__dict__.update(restored_opts.__dict__)
279 opts.subparser_name = 'resume'
koder aka kdanilove7e1a4d2016-12-17 20:29:52 +0200280
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300281 elif opts.subparser_name == 'ls':
kdanylov aka koderb0833332017-05-13 20:39:17 +0300282 tab = Texttable(max_width=200)
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200283 tab.set_deco(tab.HEADER | tab.VLINES | tab.BORDER)
284 tab.set_cols_align(["l", "l", "l", "l"])
285 tab.header(["Name", "Tests", "Run at", "Comment"])
286 tab.add_rows(list_results(opts.result_storage))
287 print(tab.draw())
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300288 return 0
289
290 elif opts.subparser_name == 'report':
koder aka kdanilovffaf48d2016-12-27 02:25:29 +0200291 if getattr(opts, "no_report", False):
292 print(" --no-report option can't be used with 'report' cmd")
293 return 1
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200294 storage = make_storage(opts.data_dir, existing=True)
koder aka kdanilovffaf48d2016-12-27 02:25:29 +0200295 config = storage.load(Config, 'config')
kdanylov aka koderb0833332017-05-13 20:39:17 +0300296 report_profiles.default_format = opts.format
297 report.default_format = opts.format
koder aka kdanilovffaf48d2016-12-27 02:25:29 +0200298 stages.append(LoadStoredNodesStage())
kdanylov aka koder736e5c12017-05-07 17:27:14 +0300299 stages.append(SaveNodesStage())
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300300 elif opts.subparser_name == 'compare':
koder aka kdanilov73084622016-11-16 21:51:08 +0200301 # x = run_test.load_data_from_path(opts.data_path1)
302 # y = run_test.load_data_from_path(opts.data_path2)
303 # print(run_test.IOPerfTest.format_diff_for_console(
304 # [x['io'][0], y['io'][0]]))
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300305 return 0
306
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200307 elif opts.subparser_name == 'db':
308 storage = make_storage(opts.storage_dir, existing=True)
309 if opts.cmd == 'show':
310 if len(opts.params) != 1:
311 print("'show' command requires parameter - key to show")
312 return 1
313 pprint.pprint(storage.get(opts.params[0]))
314 else:
315 print("Unknown/not_implemented command {!r}".format(opts.cmd))
316 return 1
317 return 0
koder aka kdanilov108ac362017-01-19 20:17:16 +0200318 elif opts.subparser_name == 'ipython':
319 storage = make_storage(opts.storage_dir, existing=True)
koder aka kdanilov108ac362017-01-19 20:17:16 +0200320 rstorage = ResultStorage(storage=storage)
321
322 import IPython
323 IPython.embed()
324
325 return 0
koder aka kdanilova732a602017-02-01 20:29:56 +0200326 else:
327 print("Subparser {!r} is not supported".format(opts.subparser_name))
328 return 1
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200329
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200330 report_stages = [] # type: List[Stage]
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200331 if not getattr(opts, "no_report", False):
kdanylov aka koder150b2192017-04-01 16:53:01 +0300332 reporters = opts.reporters.split(",")
333 assert len(set(reporters)) == len(reporters)
334 assert set(reporters).issubset({'txt', 'html'})
335 if 'txt' in reporters:
336 report_stages.append(ConsoleReportStage())
337 if 'html' in reporters:
338 report_stages.append(HtmlReportStage())
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300339
kdanylov aka koder150b2192017-04-01 16:53:01 +0300340 log_config_obj = config.raw().get('logging')
341 assert isinstance(log_config_obj, dict) or log_config_obj is None, "Broken 'logging' option in config"
342 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 +0200343
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200344 logger.info("All info would be stored into %r", config.storage_url)
345
kdanylov aka koderb0833332017-05-13 20:39:17 +0300346 ctx = TestRun(config, storage, ResultStorage(storage))
koder aka kdanilove7e1a4d2016-12-17 20:29:52 +0200347 ctx.rpc_code, ctx.default_rpc_plugins = node.get_rpc_server_code()
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300348
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200349 if opts.ssh_key_passwd is not None:
350 set_ssh_key_passwd(opts.ssh_key_passwd)
koder aka kdanilovf2865172016-12-30 03:35:11 +0200351 elif opts.ssh_key_passwd_kbd:
352 set_ssh_key_passwd(getpass.getpass("Ssh key password: ").strip())
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200353
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200354 stages.sort(key=lambda x: x.priority)
355
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200356 # TODO: run only stages, which have config
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200357 failed = False
358 cleanup_stages = []
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200359
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300360 for stage in stages:
koder aka kdanilove7e1a4d2016-12-17 20:29:52 +0200361 if stage.config_block is not None:
362 if stage.config_block not in ctx.config:
kdanylov aka koder150b2192017-04-01 16:53:01 +0300363 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 +0200364 continue
365
366 cleanup_stages.append(stage)
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200367 try:
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200368 with log_stage(stage):
369 stage.run(ctx)
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200370 except (Exception, KeyboardInterrupt):
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200371 failed = True
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300372 break
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200373 ctx.storage.sync()
374 ctx.storage.sync()
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300375
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200376 logger.debug("Start cleanup")
377 cleanup_failed = False
378 for stage in cleanup_stages[::-1]:
379 try:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200380 with log_stage(stage, cleanup=True):
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200381 stage.cleanup(ctx)
382 except:
383 cleanup_failed = True
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200384 ctx.storage.sync()
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300385
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200386 if not failed:
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300387 for report_stage in report_stages:
388 with log_stage(report_stage):
koder aka kdanilovffaf48d2016-12-27 02:25:29 +0200389 try:
390 report_stage.run(ctx)
391 except utils.StopTestError:
392 logger.error("Report stage %s requested stop execution", report_stage.name())
393 failed = True
394 break
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300395
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200396 ctx.storage.sync()
397
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200398 logger.info("All info is stored into %r", config.storage_url)
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300399
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200400 if failed or cleanup_failed:
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200401 logger.error("Tests are failed. See error details in log above")
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300402 return 1
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200403 else:
404 logger.info("Tests finished successfully")
405 return 0