blob: 9a3d04c5e0639a38993723244756d322fcf614a1 [file] [log] [blame]
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +03001from __future__ import print_function
2
gstepanov023c1e42015-04-08 15:50:19 +03003import os
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -08004import sys
koder aka kdanilov57ce4db2015-04-25 21:25:51 +03005import time
koder aka kdanilov2c473092015-03-29 17:12:13 +03006import Queue
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -08007import pprint
koder aka kdanilov416b87a2015-05-12 00:26:04 +03008import signal
koder aka kdanilove21d7472015-02-14 19:02:04 -08009import logging
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080010import argparse
koder aka kdanilov168f6092015-04-19 02:33:38 +030011import functools
koder aka kdanilov2c473092015-03-29 17:12:13 +030012import threading
koder aka kdanilovd5ed4da2015-05-07 23:33:23 +030013import contextlib
koder aka kdanilov2c473092015-03-29 17:12:13 +030014import collections
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080015
koder aka kdanilov66839a92015-04-11 13:22:31 +030016import yaml
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +030017import texttable
koder aka kdanilovbb5fe072015-05-21 02:50:23 +030018
19try:
20 import faulthandler
21except ImportError:
22 faulthandler = None
23
koder aka kdanilov2c473092015-03-29 17:12:13 +030024from concurrent.futures import ThreadPoolExecutor
koder aka kdanilov6c491062015-04-09 22:33:13 +030025
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030026from wally import pretty_yaml
koder aka kdanilovf86d7af2015-05-06 04:01:54 +030027from wally.hw_info import get_hw_info
28from wally.discover import discover, Node
koder aka kdanilov63ad2062015-04-27 13:11:40 +030029from wally.timeseries import SensorDatastore
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030030from wally import utils, report, ssh_utils, start_vms
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030031from wally.suits import IOPerfTest, PgBenchTest, MysqlTest
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +030032from wally.config import (cfg_dict, load_config, setup_loggers,
33 get_test_files)
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030034from wally.sensors_utils import with_sensors_util, sensors_info_util
35
36TOOL_TYPE_MAPPER = {
37 "io": IOPerfTest,
38 "pgbench": PgBenchTest,
39 "mysql": MysqlTest,
40}
koder aka kdanilov63ad2062015-04-27 13:11:40 +030041
koder aka kdanilov57ce4db2015-04-25 21:25:51 +030042
43try:
44 from wally import webui
45except ImportError:
46 webui = None
koder aka kdanilov2c473092015-03-29 17:12:13 +030047
48
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030049logger = logging.getLogger("wally")
koder aka kdanilovcee43342015-04-14 22:52:53 +030050
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080051
Yulia Portnova7ddfa732015-02-24 17:32:58 +020052def format_result(res, formatter):
koder aka kdanilove21d7472015-02-14 19:02:04 -080053 data = "\n{0}\n".format("=" * 80)
54 data += pprint.pformat(res) + "\n"
55 data += "{0}\n".format("=" * 80)
koder aka kdanilovfe056622015-02-19 08:46:15 -080056 templ = "{0}\n\n====> {1}\n\n{2}\n\n"
Yulia Portnova7ddfa732015-02-24 17:32:58 +020057 return templ.format(data, formatter(res), "=" * 80)
koder aka kdanilove21d7472015-02-14 19:02:04 -080058
59
koder aka kdanilov1c2b5112015-04-10 16:53:51 +030060class Context(object):
61 def __init__(self):
62 self.build_meta = {}
63 self.nodes = []
64 self.clear_calls_stack = []
65 self.openstack_nodes_ids = []
koder aka kdanilov168f6092015-04-19 02:33:38 +030066 self.sensors_mon_q = None
koder aka kdanilovf86d7af2015-05-06 04:01:54 +030067 self.hw_info = []
koder aka kdanilov1c2b5112015-04-10 16:53:51 +030068
69
koder aka kdanilov168f6092015-04-19 02:33:38 +030070def connect_one(node, vm=False):
koder aka kdanilov0c598a12015-04-21 03:01:40 +030071 if node.conn_url == 'local':
72 node.connection = ssh_utils.connect(node.conn_url)
73 return
74
koder aka kdanilov5d589b42015-03-26 12:25:51 +020075 try:
koder aka kdanilov2c473092015-03-29 17:12:13 +030076 ssh_pref = "ssh://"
77 if node.conn_url.startswith(ssh_pref):
78 url = node.conn_url[len(ssh_pref):]
koder aka kdanilov168f6092015-04-19 02:33:38 +030079
80 if vm:
koder aka kdanilov6b1341a2015-04-21 22:44:21 +030081 conn_timeout = 240
koder aka kdanilov168f6092015-04-19 02:33:38 +030082 else:
koder aka kdanilov6b1341a2015-04-21 22:44:21 +030083 conn_timeout = 30
koder aka kdanilov168f6092015-04-19 02:33:38 +030084
85 node.connection = ssh_utils.connect(url,
koder aka kdanilov6b1341a2015-04-21 22:44:21 +030086 conn_timeout=conn_timeout)
koder aka kdanilov2c473092015-03-29 17:12:13 +030087 else:
88 raise ValueError("Unknown url type {0}".format(node.conn_url))
koder aka kdanilove87ae652015-04-20 02:14:35 +030089 except Exception as exc:
90 # logger.exception("During connect to " + node.get_conn_id())
koder aka kdanilovec1b9732015-04-23 20:43:29 +030091 msg = "During connect to {0}: {1!s}".format(node.get_conn_id(),
92 exc)
koder aka kdanilove87ae652015-04-20 02:14:35 +030093 logger.error(msg)
koder aka kdanilov168f6092015-04-19 02:33:38 +030094 node.connection = None
koder aka kdanilov5d589b42015-03-26 12:25:51 +020095
96
koder aka kdanilov168f6092015-04-19 02:33:38 +030097def connect_all(nodes, vm=False):
koder aka kdanilov2c473092015-03-29 17:12:13 +030098 logger.info("Connecting to nodes")
99 with ThreadPoolExecutor(32) as pool:
koder aka kdanilov168f6092015-04-19 02:33:38 +0300100 connect_one_f = functools.partial(connect_one, vm=vm)
101 list(pool.map(connect_one_f, nodes))
koder aka kdanilov2c473092015-03-29 17:12:13 +0300102
103
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300104def collect_hw_info_stage(cfg, ctx):
105 if os.path.exists(cfg['hwreport_fname']):
106 msg = "{0} already exists. Skip hw info"
107 logger.info(msg.format(cfg['hwreport_fname']))
108 return
109
110 with ThreadPoolExecutor(32) as pool:
111 connections = (node.connection for node in ctx.nodes)
112 ctx.hw_info.extend(pool.map(get_hw_info, connections))
113
114 with open(cfg['hwreport_fname'], 'w') as hwfd:
115 for node, info in zip(ctx.nodes, ctx.hw_info):
116 hwfd.write("-" * 60 + "\n")
117 hwfd.write("Roles : " + ", ".join(node.roles) + "\n")
118 hwfd.write(str(info) + "\n")
119 hwfd.write("-" * 60 + "\n\n")
120
121 if info.hostname is not None:
122 fname = os.path.join(
123 cfg_dict['hwinfo_directory'],
124 info.hostname + "_lshw.xml")
125
126 with open(fname, "w") as fd:
127 fd.write(info.raw)
128 logger.info("Hardware report stored in " + cfg['hwreport_fname'])
129 logger.debug("Raw hardware info in " + cfg['hwinfo_directory'] + " folder")
130
131
koder aka kdanilov652cd802015-04-13 12:21:07 +0300132def test_thread(test, node, barrier, res_q):
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300133 exc = None
koder aka kdanilov2c473092015-03-29 17:12:13 +0300134 try:
koder aka kdanilova047e1b2015-04-21 23:16:59 +0300135 logger.debug("Run preparation for {0}".format(node.get_conn_id()))
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300136 test.pre_run()
koder aka kdanilova047e1b2015-04-21 23:16:59 +0300137 logger.debug("Run test for {0}".format(node.get_conn_id()))
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300138 test.run(barrier)
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300139 except utils.StopTestError as exc:
140 pass
koder aka kdanilov652cd802015-04-13 12:21:07 +0300141 except Exception as exc:
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300142 msg = "In test {0} for node {1}"
143 msg = msg.format(test, node.get_conn_id())
144 logger.exception(msg)
145 exc = utils.StopTestError(msg, exc)
koder aka kdanilov2c473092015-03-29 17:12:13 +0300146
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300147 try:
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300148 test.cleanup()
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300149 except utils.StopTestError as exc1:
150 if exc is None:
151 exc = exc1
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300152 except:
153 msg = "Duringf cleanup - in test {0} for node {1}"
154 logger.exception(msg.format(test, node))
155
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300156 if exc is not None:
157 res_q.put(exc)
koder aka kdanilov2c473092015-03-29 17:12:13 +0300158
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300159
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +0300160def run_single_test(test_nodes, name, test_cls, params, log_directory,
161 test_local_folder, run_uuid):
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300162 logger.info("Starting {0} tests".format(name))
163 res_q = Queue.Queue()
164 threads = []
165 coord_q = Queue.Queue()
166 rem_folder = test_local_folder.format(name=name)
167
168 barrier = utils.Barrier(len(test_nodes))
169 for idx, node in enumerate(test_nodes):
170 msg = "Starting {0} test on {1} node"
171 logger.debug(msg.format(name, node.conn_url))
172
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300173 params = params.copy()
174 params['testnodes_count'] = len(test_nodes)
175 test = test_cls(options=params,
176 is_primary=(idx == 0),
177 on_result_cb=res_q.put,
178 test_uuid=run_uuid,
179 node=node,
180 remote_dir=rem_folder,
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +0300181 log_directory=log_directory,
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300182 coordination_queue=coord_q,
183 total_nodes_count=len(test_nodes))
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +0300184 th = threading.Thread(None, test_thread,
185 "Test:" + node.get_conn_id(),
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300186 (test, node, barrier, res_q))
187 threads.append(th)
188 th.daemon = True
189 th.start()
190
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +0300191 th = threading.Thread(None, test_cls.coordination_th,
192 "Coordination thread",
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300193 (coord_q, barrier, len(threads)))
194 threads.append(th)
195 th.daemon = True
196 th.start()
197
198 results = []
199 coord_q.put(None)
200
201 while len(threads) != 0:
202 nthreads = []
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +0300203 time.sleep(0.1)
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300204
205 for th in threads:
206 if not th.is_alive():
207 th.join()
208 else:
209 nthreads.append(th)
210
211 threads = nthreads
212
213 while not res_q.empty():
214 val = res_q.get()
215
216 if isinstance(val, utils.StopTestError):
217 raise val
218
219 if isinstance(val, Exception):
220 msg = "Exception during test execution: {0!s}"
221 raise ValueError(msg.format(val))
222
223 results.append(val)
224
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300225 return results
226
227
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300228def suspend_vm_nodes(unused_nodes):
229 pausable_nodes_ids = [node.os_vm_id for node in unused_nodes
230 if node.os_vm_id is not None]
231 non_pausable = len(unused_nodes) - len(pausable_nodes_ids)
koder aka kdanilov2c473092015-03-29 17:12:13 +0300232
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300233 if 0 != non_pausable:
234 logger.warning("Can't pause {0} nodes".format(
235 non_pausable))
236
237 if len(pausable_nodes_ids) != 0:
238 logger.debug("Try to pause {0} unused nodes".format(
239 len(pausable_nodes_ids)))
240 start_vms.pause(pausable_nodes_ids)
241
242 return pausable_nodes_ids
243
244
245def run_tests(cfg, test_block, nodes):
koder aka kdanilov2c473092015-03-29 17:12:13 +0300246 test_nodes = [node for node in nodes
247 if 'testnode' in node.roles]
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300248
249 not_test_nodes = [node for node in nodes
250 if 'testnode' not in node.roles]
251
koder aka kdanilovd5ed4da2015-05-07 23:33:23 +0300252 if len(test_nodes) == 0:
253 logger.error("No test nodes found")
254 return
255
koder aka kdanilovcee43342015-04-14 22:52:53 +0300256 for name, params in test_block.items():
koder aka kdanilovcee43342015-04-14 22:52:53 +0300257 results = []
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300258 limit = params.get('node_limit')
259 if isinstance(limit, (int, long)):
260 vm_limits = [limit]
261 elif limit is None:
262 vm_limits = [len(test_nodes)]
263 else:
264 vm_limits = limit
koder aka kdanilov652cd802015-04-13 12:21:07 +0300265
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300266 for vm_count in vm_limits:
267 if vm_count == 'all':
268 curr_test_nodes = test_nodes
269 unused_nodes = []
270 else:
271 curr_test_nodes = test_nodes[:vm_count]
272 unused_nodes = test_nodes[vm_count:]
koder aka kdanilove87ae652015-04-20 02:14:35 +0300273
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300274 if 0 == len(curr_test_nodes):
275 continue
koder aka kdanilov652cd802015-04-13 12:21:07 +0300276
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +0300277 # make a directory for results
278 all_tests_dirs = os.listdir(cfg_dict['results'])
279
280 if 'name' in params:
281 dir_name = "{0}_{1}".format(name, params['name'])
282 else:
283 for idx in range(len(all_tests_dirs) + 1):
284 dir_name = "{0}_{1}".format(name, idx)
285 if dir_name not in all_tests_dirs:
286 break
287 else:
288 raise utils.StopTestError(
289 "Can't select directory for test results")
290
291 dir_path = os.path.join(cfg_dict['results'], dir_name)
292 if not os.path.exists(dir_path):
293 os.mkdir(dir_path)
294
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300295 if cfg.get('suspend_unused_vms', True):
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300296 pausable_nodes_ids = suspend_vm_nodes(unused_nodes)
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300297
298 resumable_nodes_ids = [node.os_vm_id for node in curr_test_nodes
299 if node.os_vm_id is not None]
300
301 if len(resumable_nodes_ids) != 0:
302 logger.debug("Check and unpause {0} nodes".format(
303 len(resumable_nodes_ids)))
304 start_vms.unpause(resumable_nodes_ids)
305
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300306 test_cls = TOOL_TYPE_MAPPER[name]
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300307 try:
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300308 sens_nodes = curr_test_nodes + not_test_nodes
309 with sensors_info_util(cfg, sens_nodes) as sensor_data:
310 t_start = time.time()
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +0300311 res = run_single_test(curr_test_nodes,
312 name,
313 test_cls,
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300314 params,
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +0300315 dir_path,
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300316 cfg['default_test_local_folder'],
317 cfg['run_uuid'])
318 t_end = time.time()
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300319 finally:
320 if cfg.get('suspend_unused_vms', True):
321 if len(pausable_nodes_ids) != 0:
322 logger.debug("Unpausing {0} nodes".format(
323 len(pausable_nodes_ids)))
324 start_vms.unpause(pausable_nodes_ids)
325
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300326 if sensor_data is not None:
327 fname = "{0}_{1}.csv".format(int(t_start), int(t_end))
328 fpath = os.path.join(cfg['sensor_storage'], fname)
329
330 with open(fpath, "w") as fd:
331 fd.write("\n\n".join(sensor_data))
332
333 results.extend(res)
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300334
335 yield name, results
koder aka kdanilov2c473092015-03-29 17:12:13 +0300336
337
koder aka kdanilovda45e882015-04-06 02:24:42 +0300338def log_nodes_statistic(_, ctx):
339 nodes = ctx.nodes
koder aka kdanilov2c473092015-03-29 17:12:13 +0300340 logger.info("Found {0} nodes total".format(len(nodes)))
341 per_role = collections.defaultdict(lambda: 0)
342 for node in nodes:
343 for role in node.roles:
344 per_role[role] += 1
345
346 for role, count in sorted(per_role.items()):
347 logger.debug("Found {0} nodes with role {1}".format(count, role))
348
349
koder aka kdanilovda45e882015-04-06 02:24:42 +0300350def connect_stage(cfg, ctx):
351 ctx.clear_calls_stack.append(disconnect_stage)
352 connect_all(ctx.nodes)
353
koder aka kdanilov168f6092015-04-19 02:33:38 +0300354 all_ok = True
koder aka kdanilovda45e882015-04-06 02:24:42 +0300355
koder aka kdanilov168f6092015-04-19 02:33:38 +0300356 for node in ctx.nodes:
357 if node.connection is None:
358 if 'testnode' in node.roles:
359 msg = "Can't connect to testnode {0}"
koder aka kdanilovd5ed4da2015-05-07 23:33:23 +0300360 msg = msg.format(node.get_conn_id())
361 logger.error(msg)
362 raise utils.StopTestError(msg)
koder aka kdanilov168f6092015-04-19 02:33:38 +0300363 else:
364 msg = "Node {0} would be excluded - can't connect"
365 logger.warning(msg.format(node.get_conn_id()))
366 all_ok = False
367
368 if all_ok:
369 logger.info("All nodes connected successfully")
370
371 ctx.nodes = [node for node in ctx.nodes
372 if node.connection is not None]
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300373
374
koder aka kdanilovda45e882015-04-06 02:24:42 +0300375def discover_stage(cfg, ctx):
koder aka kdanilov652cd802015-04-13 12:21:07 +0300376 if cfg.get('discover') is not None:
koder aka kdanilovda45e882015-04-06 02:24:42 +0300377 discover_objs = [i.strip() for i in cfg['discover'].strip().split(",")]
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300378
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300379 nodes = discover(ctx,
380 discover_objs,
381 cfg['clouds'],
382 cfg['var_dir'],
383 not cfg['dont_discover_nodes'])
koder aka kdanilov168f6092015-04-19 02:33:38 +0300384
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300385 ctx.nodes.extend(nodes)
koder aka kdanilovda45e882015-04-06 02:24:42 +0300386
387 for url, roles in cfg.get('explicit_nodes', {}).items():
388 ctx.nodes.append(Node(url, roles.split(",")))
389
390
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300391def save_nodes_stage(cfg, ctx):
392 cluster = {}
393 for node in ctx.nodes:
394 roles = node.roles[:]
395 if 'testnode' in roles:
396 roles.remove('testnode')
397
398 if len(roles) != 0:
399 cluster[node.conn_url] = roles
400
401 with open(cfg['nodes_report_file'], "w") as fd:
402 fd.write(pretty_yaml.dumps(cluster))
403
404
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300405def reuse_vms_stage(cfg, ctx):
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +0300406 p = cfg.get('clouds', {}).get('openstack', {}).get('vms', [])
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300407
408 for creds in p:
409 vm_name_pattern, conn_pattern = creds.split(",")
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300410 msg = "Vm like {0} lookup failed".format(vm_name_pattern)
411 with utils.log_error(msg):
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300412 msg = "Looking for vm with name like {0}".format(vm_name_pattern)
413 logger.debug(msg)
414
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300415 if not start_vms.is_connected():
416 os_creds = get_OS_credentials(cfg, ctx)
417 else:
418 os_creds = {}
419
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300420 conn = start_vms.nova_connect(**os_creds)
koder aka kdanilovd5ed4da2015-05-07 23:33:23 +0300421 for ip, vm_id in start_vms.find_vms(conn, vm_name_pattern):
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300422 node = Node(conn_pattern.format(ip=ip), ['testnode'])
koder aka kdanilovd5ed4da2015-05-07 23:33:23 +0300423 node.os_vm_id = vm_id
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300424 ctx.nodes.append(node)
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300425
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300426
427def get_creds_openrc(path):
428 fc = open(path).read()
429
430 echo = 'echo "$OS_TENANT_NAME:$OS_USERNAME:$OS_PASSWORD@$OS_AUTH_URL"'
431
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300432 msg = "Failed to get creads from openrc file"
433 with utils.log_error(msg):
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300434 data = utils.run_locally(['/bin/bash'],
435 input_data=fc + "\n" + echo)
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300436
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300437 msg = "Failed to get creads from openrc file: " + data
438 with utils.log_error(msg):
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300439 data = data.strip()
440 user, tenant, passwd_auth_url = data.split(':', 2)
441 passwd, auth_url = passwd_auth_url.rsplit("@", 1)
442 assert (auth_url.startswith("https://") or
443 auth_url.startswith("http://"))
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300444
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300445 return user, passwd, tenant, auth_url
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300446
447
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300448def get_OS_credentials(cfg, ctx):
koder aka kdanilovcee43342015-04-14 22:52:53 +0300449 creds = None
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +0300450 tenant = None
451
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300452 if 'openstack' in cfg['clouds']:
453 os_cfg = cfg['clouds']['openstack']
454 if 'OPENRC' in os_cfg:
455 logger.info("Using OS credentials from " + os_cfg['OPENRC'])
456 user, passwd, tenant, auth_url = \
457 get_creds_openrc(os_cfg['OPENRC'])
458 elif 'ENV' in os_cfg:
459 logger.info("Using OS credentials from shell environment")
460 user, passwd, tenant, auth_url = start_vms.ostack_get_creds()
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +0300461 elif 'OS_TENANT_NAME' in os_cfg:
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300462 logger.info("Using predefined credentials")
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300463 tenant = os_cfg['OS_TENANT_NAME'].strip()
464 user = os_cfg['OS_USERNAME'].strip()
465 passwd = os_cfg['OS_PASSWORD'].strip()
466 auth_url = os_cfg['OS_AUTH_URL'].strip()
467
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +0300468 if tenant is None and 'fuel' in cfg['clouds'] and \
469 'openstack_env' in cfg['clouds']['fuel']:
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300470 logger.info("Using fuel creds")
471 creds = ctx.fuel_openstack_creds
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +0300472 elif tenant is None:
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300473 logger.error("Can't found OS credentials")
474 raise utils.StopTestError("Can't found OS credentials", None)
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300475
koder aka kdanilovcee43342015-04-14 22:52:53 +0300476 if creds is None:
477 creds = {'name': user,
478 'passwd': passwd,
479 'tenant': tenant,
480 'auth_url': auth_url}
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300481
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300482 msg = "OS_CREDS: user={name} tenant={tenant} auth_url={auth_url}"
483 logger.debug(msg.format(**creds))
koder aka kdanilovcee43342015-04-14 22:52:53 +0300484 return creds
koder aka kdanilov4e9f3ed2015-04-14 11:26:12 +0300485
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300486
koder aka kdanilov168f6092015-04-19 02:33:38 +0300487@contextlib.contextmanager
koder aka kdanilovd5ed4da2015-05-07 23:33:23 +0300488def create_vms_ctx(ctx, cfg, config, already_has_count=0):
koder aka kdanilovc368eb62015-04-28 18:22:01 +0300489 params = cfg['vm_configs'][config['cfg_name']].copy()
koder aka kdanilov168f6092015-04-19 02:33:38 +0300490 os_nodes_ids = []
491
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300492 if not start_vms.is_connected():
493 os_creds = get_OS_credentials(cfg, ctx)
494 else:
495 os_creds = {}
koder aka kdanilov168f6092015-04-19 02:33:38 +0300496 start_vms.nova_connect(**os_creds)
497
koder aka kdanilovc368eb62015-04-28 18:22:01 +0300498 params.update(config)
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +0300499 if 'keypair_file_private' not in params:
500 params['keypair_file_private'] = params['keypair_name'] + ".pem"
501
koder aka kdanilovc368eb62015-04-28 18:22:01 +0300502 params['group_name'] = cfg_dict['run_uuid']
503
koder aka kdanilovd5ed4da2015-05-07 23:33:23 +0300504 if not config.get('skip_preparation', False):
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300505 logger.info("Preparing openstack")
koder aka kdanilovd5ed4da2015-05-07 23:33:23 +0300506 start_vms.prepare_os_subpr(params=params, **os_creds)
koder aka kdanilov168f6092015-04-19 02:33:38 +0300507
508 new_nodes = []
509 try:
koder aka kdanilovd5ed4da2015-05-07 23:33:23 +0300510 for new_node, node_id in start_vms.launch_vms(params,
511 already_has_count):
koder aka kdanilov168f6092015-04-19 02:33:38 +0300512 new_node.roles.append('testnode')
513 ctx.nodes.append(new_node)
514 os_nodes_ids.append(node_id)
515 new_nodes.append(new_node)
516
517 store_nodes_in_log(cfg, os_nodes_ids)
518 ctx.openstack_nodes_ids = os_nodes_ids
519
520 yield new_nodes
521
522 finally:
523 if not cfg['keep_vm']:
524 shut_down_vms_stage(cfg, ctx)
525
526
koder aka kdanilovcee43342015-04-14 22:52:53 +0300527def run_tests_stage(cfg, ctx):
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +0300528 ctx.results = collections.defaultdict(lambda: [])
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300529
koder aka kdanilovcee43342015-04-14 22:52:53 +0300530 if 'tests' not in cfg:
531 return
gstepanov023c1e42015-04-08 15:50:19 +0300532
koder aka kdanilovcee43342015-04-14 22:52:53 +0300533 for group in cfg['tests']:
534
535 assert len(group.items()) == 1
536 key, config = group.items()[0]
537
538 if 'start_test_nodes' == key:
koder aka kdanilovc368eb62015-04-28 18:22:01 +0300539 if 'openstack' not in config:
540 msg = "No openstack block in config - can't spawn vm's"
541 logger.error(msg)
542 raise utils.StopTestError(msg)
543
koder aka kdanilovd5ed4da2015-05-07 23:33:23 +0300544 num_test_nodes = sum(1 for node in ctx.nodes
545 if 'testnode' in node.roles)
546
547 vm_ctx = create_vms_ctx(ctx, cfg, config['openstack'],
548 num_test_nodes)
549 with vm_ctx as new_nodes:
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300550 if len(new_nodes) != 0:
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300551 logger.debug("Connecting to new nodes")
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300552 connect_all(new_nodes, True)
koder aka kdanilovcee43342015-04-14 22:52:53 +0300553
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300554 for node in new_nodes:
555 if node.connection is None:
556 msg = "Failed to connect to vm {0}"
557 raise RuntimeError(msg.format(node.get_conn_id()))
koder aka kdanilovcee43342015-04-14 22:52:53 +0300558
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300559 with with_sensors_util(cfg_dict, ctx.nodes):
560 for test_group in config.get('tests', []):
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +0300561 for tp, res in run_tests(cfg, test_group, ctx.nodes):
562 ctx.results[tp].extend(res)
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300563 else:
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300564 with with_sensors_util(cfg_dict, ctx.nodes):
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +0300565 for tp, res in run_tests(cfg, group, ctx.nodes):
566 ctx.results[tp].extend(res)
koder aka kdanilovda45e882015-04-06 02:24:42 +0300567
gstepanov023c1e42015-04-08 15:50:19 +0300568
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300569def shut_down_vms_stage(cfg, ctx):
koder aka kdanilov66839a92015-04-11 13:22:31 +0300570 vm_ids_fname = cfg_dict['vm_ids_fname']
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300571 if ctx.openstack_nodes_ids is None:
koder aka kdanilov66839a92015-04-11 13:22:31 +0300572 nodes_ids = open(vm_ids_fname).read().split()
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300573 else:
574 nodes_ids = ctx.openstack_nodes_ids
575
koder aka kdanilov652cd802015-04-13 12:21:07 +0300576 if len(nodes_ids) != 0:
577 logger.info("Removing nodes")
578 start_vms.clear_nodes(nodes_ids)
579 logger.info("Nodes has been removed")
gstepanov023c1e42015-04-08 15:50:19 +0300580
koder aka kdanilov66839a92015-04-11 13:22:31 +0300581 if os.path.exists(vm_ids_fname):
582 os.remove(vm_ids_fname)
gstepanov023c1e42015-04-08 15:50:19 +0300583
koder aka kdanilov66839a92015-04-11 13:22:31 +0300584
585def store_nodes_in_log(cfg, nodes_ids):
586 with open(cfg['vm_ids_fname'], 'w') as fd:
587 fd.write("\n".join(nodes_ids))
gstepanov023c1e42015-04-08 15:50:19 +0300588
589
590def clear_enviroment(cfg, ctx):
koder aka kdanilov66839a92015-04-11 13:22:31 +0300591 if os.path.exists(cfg_dict['vm_ids_fname']):
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300592 shut_down_vms_stage(cfg, ctx)
gstepanov023c1e42015-04-08 15:50:19 +0300593
594
koder aka kdanilovda45e882015-04-06 02:24:42 +0300595def disconnect_stage(cfg, ctx):
koder aka kdanilov652cd802015-04-13 12:21:07 +0300596 ssh_utils.close_all_sessions()
597
koder aka kdanilovda45e882015-04-06 02:24:42 +0300598 for node in ctx.nodes:
599 if node.connection is not None:
600 node.connection.close()
601
602
koder aka kdanilov66839a92015-04-11 13:22:31 +0300603def store_raw_results_stage(cfg, ctx):
604
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +0300605 raw_results = cfg_dict['raw_results']
koder aka kdanilov66839a92015-04-11 13:22:31 +0300606
607 if os.path.exists(raw_results):
608 cont = yaml.load(open(raw_results).read())
609 else:
610 cont = []
611
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +0300612 cont.extend(utils.yamable(ctx.results).items())
koder aka kdanilov66839a92015-04-11 13:22:31 +0300613 raw_data = pretty_yaml.dumps(cont)
614
615 with open(raw_results, "w") as fd:
616 fd.write(raw_data)
617
618
619def console_report_stage(cfg, ctx):
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +0300620 first_report = True
621 text_rep_fname = cfg['text_report_file']
622 with open(text_rep_fname, "w") as fd:
623 for tp, data in ctx.results.items():
624 if 'io' == tp and data is not None:
625 dinfo = report.process_disk_info(data)
626 rep = IOPerfTest.format_for_console(data, dinfo)
627 elif tp in ['mysql', 'pgbench'] and data is not None:
628 rep = MysqlTest.format_for_console(data)
629 else:
630 logger.warning("Can't generate text report for " + tp)
631 continue
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300632
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +0300633 fd.write(rep)
634 fd.write("\n")
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300635
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +0300636 if first_report:
637 logger.info("Text report were stored in " + text_rep_fname)
638 first_report = False
639
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300640 print("\n" + rep + "\n")
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300641
koder aka kdanilov66839a92015-04-11 13:22:31 +0300642
koder aka kdanilove87ae652015-04-20 02:14:35 +0300643def html_report_stage(cfg, ctx):
Yulia Portnova8ca20572015-04-14 14:09:39 +0300644 html_rep_fname = cfg['html_report_file']
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300645 found = False
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +0300646 for tp, data in ctx.results.items():
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300647 if 'io' == tp and data is not None:
648 if found:
649 logger.error("Making reports for more than one " +
650 "io block isn't supported! All " +
651 "report, except first are skipped")
652 continue
653 found = True
654 dinfo = report.process_disk_info(data)
655 report.make_io_report(dinfo, data, html_rep_fname,
656 lab_info=ctx.hw_info)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300657
koder aka kdanilovda45e882015-04-06 02:24:42 +0300658
659def complete_log_nodes_statistic(cfg, ctx):
660 nodes = ctx.nodes
661 for node in nodes:
662 logger.debug(str(node))
663
664
koder aka kdanilov66839a92015-04-11 13:22:31 +0300665def load_data_from(var_dir):
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300666 def load_data_from_file(_, ctx):
koder aka kdanilov66839a92015-04-11 13:22:31 +0300667 raw_results = os.path.join(var_dir, 'raw_results.yaml')
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +0300668 ctx.results = {}
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300669 for tp, results in yaml.load(open(raw_results).read()):
670 cls = TOOL_TYPE_MAPPER[tp]
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +0300671 ctx.results[tp] = map(cls.load, results)
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300672
koder aka kdanilov4e9f3ed2015-04-14 11:26:12 +0300673 return load_data_from_file
gstepanovcd256d62015-04-07 17:47:32 +0300674
675
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300676def start_web_ui(cfg, ctx):
677 if webui is None:
678 logger.error("Can't start webui. Install cherrypy module")
679 ctx.web_thread = None
680 else:
681 th = threading.Thread(None, webui.web_main_thread, "webui", (None,))
682 th.daemon = True
683 th.start()
684 ctx.web_thread = th
685
686
687def stop_web_ui(cfg, ctx):
688 webui.web_main_stop()
689 time.sleep(1)
690
691
koder aka kdanilovcee43342015-04-14 22:52:53 +0300692def parse_args(argv):
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300693 descr = "Disk io performance test suite"
694 parser = argparse.ArgumentParser(prog='wally', description=descr)
koder aka kdanilovcee43342015-04-14 22:52:53 +0300695
696 parser.add_argument("-l", dest='extra_logs',
697 action='store_true', default=False,
698 help="print some extra log info")
koder aka kdanilovcee43342015-04-14 22:52:53 +0300699 parser.add_argument("-b", '--build_description',
700 type=str, default="Build info")
701 parser.add_argument("-i", '--build_id', type=str, default="id")
702 parser.add_argument("-t", '--build_type', type=str, default="GA")
703 parser.add_argument("-u", '--username', type=str, default="admin")
koder aka kdanilove87ae652015-04-20 02:14:35 +0300704 parser.add_argument("-n", '--no-tests', action='store_true',
705 help="Don't run tests", default=False)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300706 parser.add_argument("-p", '--post-process-only', metavar="VAR_DIR",
707 help="Only process data from previour run")
708 parser.add_argument("-k", '--keep-vm', action='store_true',
709 help="Don't remove test vm's", default=False)
koder aka kdanilove87ae652015-04-20 02:14:35 +0300710 parser.add_argument("-d", '--dont-discover-nodes', action='store_true',
711 help="Don't connect/discover fuel nodes",
712 default=False)
koder aka kdanilova047e1b2015-04-21 23:16:59 +0300713 parser.add_argument("-r", '--no-html-report', action='store_true',
714 help="Skip html report", default=False)
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300715 parser.add_argument("--params", metavar="testname.paramname",
koder aka kdanilov63ad2062015-04-27 13:11:40 +0300716 help="Test params", default=[])
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +0300717 parser.add_argument("--ls", action='store_true', default=False)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300718 parser.add_argument("config_file")
koder aka kdanilovcee43342015-04-14 22:52:53 +0300719
720 return parser.parse_args(argv[1:])
721
722
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300723def get_stage_name(func):
724 if func.__name__.endswith("stage"):
725 return func.__name__
726 else:
727 return func.__name__ + " stage"
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300728
729
koder aka kdanilovbb5fe072015-05-21 02:50:23 +0300730def get_test_names(block):
731 assert len(block.items()) == 1
732 name, data = block.items()[0]
733 if name == 'start_test_nodes':
734 for in_blk in data['tests']:
735 for i in get_test_names(in_blk):
736 yield i
737 else:
738 yield name
739
740
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +0300741def list_results(path):
742 results = []
743
744 for dname in os.listdir(path):
745
746 cfg = get_test_files(os.path.join(path, dname))
747
748 if not os.path.isfile(cfg['raw_results']):
749 continue
750
751 res_mtime = time.ctime(os.path.getmtime(cfg['raw_results']))
752 cfg = yaml.load(open(cfg['saved_config_file']).read())
753
754 test_names = []
755
756 for block in cfg['tests']:
koder aka kdanilovbb5fe072015-05-21 02:50:23 +0300757 test_names.extend(get_test_names(block))
758
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +0300759 results.append((dname, test_names, res_mtime))
760
761 tab = texttable.Texttable(max_width=120)
762 tab.set_deco(tab.HEADER | tab.VLINES | tab.BORDER)
763 tab.set_cols_align(["l", "l", "l"])
764 results.sort(key=lambda x: x[2])
765
766 for data in results:
767 dname, tests, mtime = data
768 tab.add_row([dname, ', '.join(tests), mtime])
769
770 tab.header(["Name", "Tests", "etime"])
771
772 print(tab.draw())
773
774
koder aka kdanilov3f356262015-02-13 08:06:14 -0800775def main(argv):
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +0300776 if '--ls' in argv:
777 list_results(argv[-1])
778 exit(0)
779
koder aka kdanilovbb5fe072015-05-21 02:50:23 +0300780 if faulthandler is not None:
781 faulthandler.register(signal.SIGUSR1, all_threads=True)
782
koder aka kdanilove06762a2015-03-22 23:32:09 +0200783 opts = parse_args(argv)
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300784 load_config(opts.config_file, opts.post_process_only)
koder aka kdanilov2c473092015-03-29 17:12:13 +0300785
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300786 if cfg_dict.get('logging', {}).get("extra_logs", False) or opts.extra_logs:
787 level = logging.DEBUG
788 else:
789 level = logging.WARNING
790
791 setup_loggers(level, cfg_dict['log_file'])
792
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +0300793 if not os.path.exists(cfg_dict['saved_config_file']):
794 with open(cfg_dict['saved_config_file'], 'w') as fd:
795 fd.write(open(opts.config_file).read())
796
koder aka kdanilov66839a92015-04-11 13:22:31 +0300797 if opts.post_process_only is not None:
798 stages = [
koder aka kdanilove87ae652015-04-20 02:14:35 +0300799 load_data_from(opts.post_process_only)
koder aka kdanilov66839a92015-04-11 13:22:31 +0300800 ]
801 else:
802 stages = [
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300803 discover_stage
804 ]
805
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300806 stages.extend([
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300807 reuse_vms_stage,
koder aka kdanilov66839a92015-04-11 13:22:31 +0300808 log_nodes_statistic,
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300809 save_nodes_stage,
810 connect_stage])
811
812 if cfg_dict.get('collect_info', True):
813 stages.append(collect_hw_info_stage)
814
815 stages.extend([
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300816 # deploy_sensors_stage,
koder aka kdanilov66839a92015-04-11 13:22:31 +0300817 run_tests_stage,
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300818 store_raw_results_stage,
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300819 # gather_sensors_stage
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300820 ])
koder aka kdanilov66839a92015-04-11 13:22:31 +0300821
koder aka kdanilove87ae652015-04-20 02:14:35 +0300822 report_stages = [
823 console_report_stage,
koder aka kdanilove87ae652015-04-20 02:14:35 +0300824 ]
825
koder aka kdanilova047e1b2015-04-21 23:16:59 +0300826 if not opts.no_html_report:
827 report_stages.append(html_report_stage)
828
koder aka kdanilov652cd802015-04-13 12:21:07 +0300829 logger.info("All info would be stored into {0}".format(
830 cfg_dict['var_dir']))
gstepanovcd256d62015-04-07 17:47:32 +0300831
koder aka kdanilovda45e882015-04-06 02:24:42 +0300832 ctx = Context()
gstepanovaffcdb12015-04-07 17:18:29 +0300833 ctx.build_meta['build_id'] = opts.build_id
834 ctx.build_meta['build_descrption'] = opts.build_description
835 ctx.build_meta['build_type'] = opts.build_type
836 ctx.build_meta['username'] = opts.username
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300837 ctx.sensors_data = SensorDatastore()
koder aka kdanilove87ae652015-04-20 02:14:35 +0300838
koder aka kdanilov168f6092015-04-19 02:33:38 +0300839 cfg_dict['keep_vm'] = opts.keep_vm
koder aka kdanilove87ae652015-04-20 02:14:35 +0300840 cfg_dict['no_tests'] = opts.no_tests
841 cfg_dict['dont_discover_nodes'] = opts.dont_discover_nodes
koder aka kdanilov6c491062015-04-09 22:33:13 +0300842
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300843 if cfg_dict.get('run_web_ui', False):
844 start_web_ui(cfg_dict, ctx)
845
koder aka kdanilovc368eb62015-04-28 18:22:01 +0300846 msg_templ = "Exception during {0.__name__}: {1!s}"
847 msg_templ_no_exc = "During {0.__name__}"
848
koder aka kdanilovda45e882015-04-06 02:24:42 +0300849 try:
850 for stage in stages:
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300851 logger.info("Start " + get_stage_name(stage))
koder aka kdanilovda45e882015-04-06 02:24:42 +0300852 stage(cfg_dict, ctx)
koder aka kdanilovc368eb62015-04-28 18:22:01 +0300853 except utils.StopTestError as exc:
854 logger.error(msg_templ.format(stage, exc))
855 except Exception:
856 logger.exception(msg_templ_no_exc.format(stage))
koder aka kdanilovda45e882015-04-06 02:24:42 +0300857 finally:
858 exc, cls, tb = sys.exc_info()
859 for stage in ctx.clear_calls_stack[::-1]:
860 try:
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300861 logger.info("Start " + get_stage_name(stage))
koder aka kdanilovda45e882015-04-06 02:24:42 +0300862 stage(cfg_dict, ctx)
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300863 except utils.StopTestError as cleanup_exc:
864 logger.error(msg_templ.format(stage, cleanup_exc))
865 except Exception:
866 logger.exception(msg_templ_no_exc.format(stage))
867
868 logger.debug("Start utils.cleanup")
869 for clean_func, args, kwargs in utils.iter_clean_func():
870 try:
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300871 logger.info("Start " + get_stage_name(clean_func))
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300872 clean_func(*args, **kwargs)
873 except utils.StopTestError as cleanup_exc:
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300874 logger.error(msg_templ.format(clean_func, cleanup_exc))
koder aka kdanilovc368eb62015-04-28 18:22:01 +0300875 except Exception:
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300876 logger.exception(msg_templ_no_exc.format(clean_func))
koder aka kdanilov2c473092015-03-29 17:12:13 +0300877
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300878 if exc is None:
879 for report_stage in report_stages:
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300880 logger.info("Start " + get_stage_name(report_stage))
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300881 report_stage(cfg_dict, ctx)
koder aka kdanilove87ae652015-04-20 02:14:35 +0300882
883 logger.info("All info stored in {0} folder".format(cfg_dict['var_dir']))
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300884
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300885 if cfg_dict.get('run_web_ui', False):
886 stop_web_ui(cfg_dict, ctx)
887
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300888 if exc is None:
889 logger.info("Tests finished successfully")
890 return 0
891 else:
892 logger.error("Tests are failed. See detailed error above")
893 return 1