blob: 22856b5e27b92a30797fa64dadfe04e4b00d2e20 [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 kdanilov416b87a2015-05-12 00:26:04 +030017import faulthandler
koder aka kdanilov2c473092015-03-29 17:12:13 +030018from concurrent.futures import ThreadPoolExecutor
koder aka kdanilov6c491062015-04-09 22:33:13 +030019
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030020from wally import pretty_yaml
koder aka kdanilovf86d7af2015-05-06 04:01:54 +030021from wally.hw_info import get_hw_info
22from wally.discover import discover, Node
koder aka kdanilov63ad2062015-04-27 13:11:40 +030023from wally.timeseries import SensorDatastore
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030024from wally import utils, report, ssh_utils, start_vms
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030025from wally.suits import IOPerfTest, PgBenchTest, MysqlTest
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030026from wally.config import cfg_dict, load_config, setup_loggers
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030027from wally.sensors_utils import with_sensors_util, sensors_info_util
28
29TOOL_TYPE_MAPPER = {
30 "io": IOPerfTest,
31 "pgbench": PgBenchTest,
32 "mysql": MysqlTest,
33}
koder aka kdanilov63ad2062015-04-27 13:11:40 +030034
koder aka kdanilov57ce4db2015-04-25 21:25:51 +030035
36try:
37 from wally import webui
38except ImportError:
39 webui = None
koder aka kdanilov2c473092015-03-29 17:12:13 +030040
41
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030042logger = logging.getLogger("wally")
koder aka kdanilovcee43342015-04-14 22:52:53 +030043
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080044
Yulia Portnova7ddfa732015-02-24 17:32:58 +020045def format_result(res, formatter):
koder aka kdanilove21d7472015-02-14 19:02:04 -080046 data = "\n{0}\n".format("=" * 80)
47 data += pprint.pformat(res) + "\n"
48 data += "{0}\n".format("=" * 80)
koder aka kdanilovfe056622015-02-19 08:46:15 -080049 templ = "{0}\n\n====> {1}\n\n{2}\n\n"
Yulia Portnova7ddfa732015-02-24 17:32:58 +020050 return templ.format(data, formatter(res), "=" * 80)
koder aka kdanilove21d7472015-02-14 19:02:04 -080051
52
koder aka kdanilov1c2b5112015-04-10 16:53:51 +030053class Context(object):
54 def __init__(self):
55 self.build_meta = {}
56 self.nodes = []
57 self.clear_calls_stack = []
58 self.openstack_nodes_ids = []
koder aka kdanilov168f6092015-04-19 02:33:38 +030059 self.sensors_mon_q = None
koder aka kdanilovf86d7af2015-05-06 04:01:54 +030060 self.hw_info = []
koder aka kdanilov1c2b5112015-04-10 16:53:51 +030061
62
koder aka kdanilov168f6092015-04-19 02:33:38 +030063def connect_one(node, vm=False):
koder aka kdanilov0c598a12015-04-21 03:01:40 +030064 if node.conn_url == 'local':
65 node.connection = ssh_utils.connect(node.conn_url)
66 return
67
koder aka kdanilov5d589b42015-03-26 12:25:51 +020068 try:
koder aka kdanilov2c473092015-03-29 17:12:13 +030069 ssh_pref = "ssh://"
70 if node.conn_url.startswith(ssh_pref):
71 url = node.conn_url[len(ssh_pref):]
koder aka kdanilov168f6092015-04-19 02:33:38 +030072
73 if vm:
koder aka kdanilov6b1341a2015-04-21 22:44:21 +030074 conn_timeout = 240
koder aka kdanilov168f6092015-04-19 02:33:38 +030075 else:
koder aka kdanilov6b1341a2015-04-21 22:44:21 +030076 conn_timeout = 30
koder aka kdanilov168f6092015-04-19 02:33:38 +030077
78 node.connection = ssh_utils.connect(url,
koder aka kdanilov6b1341a2015-04-21 22:44:21 +030079 conn_timeout=conn_timeout)
koder aka kdanilov2c473092015-03-29 17:12:13 +030080 else:
81 raise ValueError("Unknown url type {0}".format(node.conn_url))
koder aka kdanilove87ae652015-04-20 02:14:35 +030082 except Exception as exc:
83 # logger.exception("During connect to " + node.get_conn_id())
koder aka kdanilovec1b9732015-04-23 20:43:29 +030084 msg = "During connect to {0}: {1!s}".format(node.get_conn_id(),
85 exc)
koder aka kdanilove87ae652015-04-20 02:14:35 +030086 logger.error(msg)
koder aka kdanilov168f6092015-04-19 02:33:38 +030087 node.connection = None
koder aka kdanilov5d589b42015-03-26 12:25:51 +020088
89
koder aka kdanilov168f6092015-04-19 02:33:38 +030090def connect_all(nodes, vm=False):
koder aka kdanilov2c473092015-03-29 17:12:13 +030091 logger.info("Connecting to nodes")
92 with ThreadPoolExecutor(32) as pool:
koder aka kdanilov168f6092015-04-19 02:33:38 +030093 connect_one_f = functools.partial(connect_one, vm=vm)
94 list(pool.map(connect_one_f, nodes))
koder aka kdanilov2c473092015-03-29 17:12:13 +030095
96
koder aka kdanilovf86d7af2015-05-06 04:01:54 +030097def collect_hw_info_stage(cfg, ctx):
98 if os.path.exists(cfg['hwreport_fname']):
99 msg = "{0} already exists. Skip hw info"
100 logger.info(msg.format(cfg['hwreport_fname']))
101 return
102
103 with ThreadPoolExecutor(32) as pool:
104 connections = (node.connection for node in ctx.nodes)
105 ctx.hw_info.extend(pool.map(get_hw_info, connections))
106
107 with open(cfg['hwreport_fname'], 'w') as hwfd:
108 for node, info in zip(ctx.nodes, ctx.hw_info):
109 hwfd.write("-" * 60 + "\n")
110 hwfd.write("Roles : " + ", ".join(node.roles) + "\n")
111 hwfd.write(str(info) + "\n")
112 hwfd.write("-" * 60 + "\n\n")
113
114 if info.hostname is not None:
115 fname = os.path.join(
116 cfg_dict['hwinfo_directory'],
117 info.hostname + "_lshw.xml")
118
119 with open(fname, "w") as fd:
120 fd.write(info.raw)
121 logger.info("Hardware report stored in " + cfg['hwreport_fname'])
122 logger.debug("Raw hardware info in " + cfg['hwinfo_directory'] + " folder")
123
124
koder aka kdanilov652cd802015-04-13 12:21:07 +0300125def test_thread(test, node, barrier, res_q):
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300126 exc = None
koder aka kdanilov2c473092015-03-29 17:12:13 +0300127 try:
koder aka kdanilova047e1b2015-04-21 23:16:59 +0300128 logger.debug("Run preparation for {0}".format(node.get_conn_id()))
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300129 test.pre_run()
koder aka kdanilova047e1b2015-04-21 23:16:59 +0300130 logger.debug("Run test for {0}".format(node.get_conn_id()))
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300131 test.run(barrier)
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300132 except utils.StopTestError as exc:
133 pass
koder aka kdanilov652cd802015-04-13 12:21:07 +0300134 except Exception as exc:
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300135 msg = "In test {0} for node {1}"
136 msg = msg.format(test, node.get_conn_id())
137 logger.exception(msg)
138 exc = utils.StopTestError(msg, exc)
koder aka kdanilov2c473092015-03-29 17:12:13 +0300139
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300140 try:
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300141 test.cleanup()
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300142 except utils.StopTestError as exc1:
143 if exc is None:
144 exc = exc1
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300145 except:
146 msg = "Duringf cleanup - in test {0} for node {1}"
147 logger.exception(msg.format(test, node))
148
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300149 if exc is not None:
150 res_q.put(exc)
koder aka kdanilov2c473092015-03-29 17:12:13 +0300151
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300152
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300153def run_single_test(test_nodes, name, test_cls, params,
154 test_local_folder, run_uuid, counter=[0]):
155 logger.info("Starting {0} tests".format(name))
156 res_q = Queue.Queue()
157 threads = []
158 coord_q = Queue.Queue()
159 rem_folder = test_local_folder.format(name=name)
160
161 barrier = utils.Barrier(len(test_nodes))
162 for idx, node in enumerate(test_nodes):
163 msg = "Starting {0} test on {1} node"
164 logger.debug(msg.format(name, node.conn_url))
165
166 dname = "{0}_{1}_{2}".format(name, counter[0], node.get_ip())
167 counter[0] += 1
168 dr = os.path.join(cfg_dict['test_log_directory'], dname)
169
170 if not os.path.exists(dr):
171 os.makedirs(dr)
172
173 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,
181 log_directory=dr,
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300182 coordination_queue=coord_q,
183 total_nodes_count=len(test_nodes))
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300184 th = threading.Thread(None, test_thread, None,
185 (test, node, barrier, res_q))
186 threads.append(th)
187 th.daemon = True
188 th.start()
189
190 th = threading.Thread(None, test_cls.coordination_th, None,
191 (coord_q, barrier, len(threads)))
192 threads.append(th)
193 th.daemon = True
194 th.start()
195
196 results = []
197 coord_q.put(None)
198
199 while len(threads) != 0:
200 nthreads = []
201
202 for th in threads:
203 if not th.is_alive():
204 th.join()
205 else:
206 nthreads.append(th)
207
208 threads = nthreads
209
210 while not res_q.empty():
211 val = res_q.get()
212
213 if isinstance(val, utils.StopTestError):
214 raise val
215
216 if isinstance(val, Exception):
217 msg = "Exception during test execution: {0!s}"
218 raise ValueError(msg.format(val))
219
220 results.append(val)
221
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300222 return results
223
224
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300225def suspend_vm_nodes(unused_nodes):
226 pausable_nodes_ids = [node.os_vm_id for node in unused_nodes
227 if node.os_vm_id is not None]
228 non_pausable = len(unused_nodes) - len(pausable_nodes_ids)
koder aka kdanilov2c473092015-03-29 17:12:13 +0300229
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300230 if 0 != non_pausable:
231 logger.warning("Can't pause {0} nodes".format(
232 non_pausable))
233
234 if len(pausable_nodes_ids) != 0:
235 logger.debug("Try to pause {0} unused nodes".format(
236 len(pausable_nodes_ids)))
237 start_vms.pause(pausable_nodes_ids)
238
239 return pausable_nodes_ids
240
241
242def run_tests(cfg, test_block, nodes):
koder aka kdanilov2c473092015-03-29 17:12:13 +0300243 test_nodes = [node for node in nodes
244 if 'testnode' in node.roles]
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300245
246 not_test_nodes = [node for node in nodes
247 if 'testnode' not in node.roles]
248
koder aka kdanilovd5ed4da2015-05-07 23:33:23 +0300249 if len(test_nodes) == 0:
250 logger.error("No test nodes found")
251 return
252
koder aka kdanilovcee43342015-04-14 22:52:53 +0300253 for name, params in test_block.items():
koder aka kdanilovcee43342015-04-14 22:52:53 +0300254 results = []
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300255 limit = params.get('node_limit')
256 if isinstance(limit, (int, long)):
257 vm_limits = [limit]
258 elif limit is None:
259 vm_limits = [len(test_nodes)]
260 else:
261 vm_limits = limit
koder aka kdanilov652cd802015-04-13 12:21:07 +0300262
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300263 for vm_count in vm_limits:
264 if vm_count == 'all':
265 curr_test_nodes = test_nodes
266 unused_nodes = []
267 else:
268 curr_test_nodes = test_nodes[:vm_count]
269 unused_nodes = test_nodes[vm_count:]
koder aka kdanilove87ae652015-04-20 02:14:35 +0300270
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300271 if 0 == len(curr_test_nodes):
272 continue
koder aka kdanilov652cd802015-04-13 12:21:07 +0300273
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300274 if cfg.get('suspend_unused_vms', True):
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300275 pausable_nodes_ids = suspend_vm_nodes(unused_nodes)
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300276
277 resumable_nodes_ids = [node.os_vm_id for node in curr_test_nodes
278 if node.os_vm_id is not None]
279
280 if len(resumable_nodes_ids) != 0:
281 logger.debug("Check and unpause {0} nodes".format(
282 len(resumable_nodes_ids)))
283 start_vms.unpause(resumable_nodes_ids)
284
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300285 test_cls = TOOL_TYPE_MAPPER[name]
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300286 try:
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300287 sens_nodes = curr_test_nodes + not_test_nodes
288 with sensors_info_util(cfg, sens_nodes) as sensor_data:
289 t_start = time.time()
290 res = run_single_test(curr_test_nodes, name, test_cls,
291 params,
292 cfg['default_test_local_folder'],
293 cfg['run_uuid'])
294 t_end = time.time()
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300295 finally:
296 if cfg.get('suspend_unused_vms', True):
297 if len(pausable_nodes_ids) != 0:
298 logger.debug("Unpausing {0} nodes".format(
299 len(pausable_nodes_ids)))
300 start_vms.unpause(pausable_nodes_ids)
301
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300302 if sensor_data is not None:
303 fname = "{0}_{1}.csv".format(int(t_start), int(t_end))
304 fpath = os.path.join(cfg['sensor_storage'], fname)
305
306 with open(fpath, "w") as fd:
307 fd.write("\n\n".join(sensor_data))
308
309 results.extend(res)
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300310
311 yield name, results
koder aka kdanilov2c473092015-03-29 17:12:13 +0300312
313
koder aka kdanilovda45e882015-04-06 02:24:42 +0300314def log_nodes_statistic(_, ctx):
315 nodes = ctx.nodes
koder aka kdanilov2c473092015-03-29 17:12:13 +0300316 logger.info("Found {0} nodes total".format(len(nodes)))
317 per_role = collections.defaultdict(lambda: 0)
318 for node in nodes:
319 for role in node.roles:
320 per_role[role] += 1
321
322 for role, count in sorted(per_role.items()):
323 logger.debug("Found {0} nodes with role {1}".format(count, role))
324
325
koder aka kdanilovda45e882015-04-06 02:24:42 +0300326def connect_stage(cfg, ctx):
327 ctx.clear_calls_stack.append(disconnect_stage)
328 connect_all(ctx.nodes)
329
koder aka kdanilov168f6092015-04-19 02:33:38 +0300330 all_ok = True
koder aka kdanilovda45e882015-04-06 02:24:42 +0300331
koder aka kdanilov168f6092015-04-19 02:33:38 +0300332 for node in ctx.nodes:
333 if node.connection is None:
334 if 'testnode' in node.roles:
335 msg = "Can't connect to testnode {0}"
koder aka kdanilovd5ed4da2015-05-07 23:33:23 +0300336 msg = msg.format(node.get_conn_id())
337 logger.error(msg)
338 raise utils.StopTestError(msg)
koder aka kdanilov168f6092015-04-19 02:33:38 +0300339 else:
340 msg = "Node {0} would be excluded - can't connect"
341 logger.warning(msg.format(node.get_conn_id()))
342 all_ok = False
343
344 if all_ok:
345 logger.info("All nodes connected successfully")
346
347 ctx.nodes = [node for node in ctx.nodes
348 if node.connection is not None]
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300349
350
koder aka kdanilovda45e882015-04-06 02:24:42 +0300351def discover_stage(cfg, ctx):
koder aka kdanilov652cd802015-04-13 12:21:07 +0300352 if cfg.get('discover') is not None:
koder aka kdanilovda45e882015-04-06 02:24:42 +0300353 discover_objs = [i.strip() for i in cfg['discover'].strip().split(",")]
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300354
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300355 nodes = discover(ctx,
356 discover_objs,
357 cfg['clouds'],
358 cfg['var_dir'],
359 not cfg['dont_discover_nodes'])
koder aka kdanilov168f6092015-04-19 02:33:38 +0300360
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300361 ctx.nodes.extend(nodes)
koder aka kdanilovda45e882015-04-06 02:24:42 +0300362
363 for url, roles in cfg.get('explicit_nodes', {}).items():
364 ctx.nodes.append(Node(url, roles.split(",")))
365
366
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300367def save_nodes_stage(cfg, ctx):
368 cluster = {}
369 for node in ctx.nodes:
370 roles = node.roles[:]
371 if 'testnode' in roles:
372 roles.remove('testnode')
373
374 if len(roles) != 0:
375 cluster[node.conn_url] = roles
376
377 with open(cfg['nodes_report_file'], "w") as fd:
378 fd.write(pretty_yaml.dumps(cluster))
379
380
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300381def reuse_vms_stage(cfg, ctx):
382 p = cfg.get('clouds', {})
383 p = p.get('openstack', {})
384 p = p.get('vms', [])
385
386 for creds in p:
387 vm_name_pattern, conn_pattern = creds.split(",")
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300388 msg = "Vm like {0} lookup failed".format(vm_name_pattern)
389 with utils.log_error(msg):
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300390 msg = "Looking for vm with name like {0}".format(vm_name_pattern)
391 logger.debug(msg)
392
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300393 if not start_vms.is_connected():
394 os_creds = get_OS_credentials(cfg, ctx)
395 else:
396 os_creds = {}
397
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300398 conn = start_vms.nova_connect(**os_creds)
koder aka kdanilovd5ed4da2015-05-07 23:33:23 +0300399 for ip, vm_id in start_vms.find_vms(conn, vm_name_pattern):
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300400 node = Node(conn_pattern.format(ip=ip), ['testnode'])
koder aka kdanilovd5ed4da2015-05-07 23:33:23 +0300401 node.os_vm_id = vm_id
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300402 ctx.nodes.append(node)
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300403
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300404
405def get_creds_openrc(path):
406 fc = open(path).read()
407
408 echo = 'echo "$OS_TENANT_NAME:$OS_USERNAME:$OS_PASSWORD@$OS_AUTH_URL"'
409
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300410 msg = "Failed to get creads from openrc file"
411 with utils.log_error(msg):
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300412 data = utils.run_locally(['/bin/bash'],
413 input_data=fc + "\n" + echo)
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300414
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300415 msg = "Failed to get creads from openrc file: " + data
416 with utils.log_error(msg):
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300417 data = data.strip()
418 user, tenant, passwd_auth_url = data.split(':', 2)
419 passwd, auth_url = passwd_auth_url.rsplit("@", 1)
420 assert (auth_url.startswith("https://") or
421 auth_url.startswith("http://"))
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300422
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300423 return user, passwd, tenant, auth_url
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300424
425
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300426def get_OS_credentials(cfg, ctx):
koder aka kdanilovcee43342015-04-14 22:52:53 +0300427 creds = None
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300428 if 'openstack' in cfg['clouds']:
429 os_cfg = cfg['clouds']['openstack']
430 if 'OPENRC' in os_cfg:
431 logger.info("Using OS credentials from " + os_cfg['OPENRC'])
432 user, passwd, tenant, auth_url = \
433 get_creds_openrc(os_cfg['OPENRC'])
434 elif 'ENV' in os_cfg:
435 logger.info("Using OS credentials from shell environment")
436 user, passwd, tenant, auth_url = start_vms.ostack_get_creds()
437 else:
438 logger.info("Using predefined credentials")
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300439 tenant = os_cfg['OS_TENANT_NAME'].strip()
440 user = os_cfg['OS_USERNAME'].strip()
441 passwd = os_cfg['OS_PASSWORD'].strip()
442 auth_url = os_cfg['OS_AUTH_URL'].strip()
443
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300444 elif 'fuel' in cfg['clouds'] and \
445 'openstack_env' in cfg['clouds']['fuel']:
446 logger.info("Using fuel creds")
447 creds = ctx.fuel_openstack_creds
koder aka kdanilovcee43342015-04-14 22:52:53 +0300448 else:
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300449 logger.error("Can't found OS credentials")
450 raise utils.StopTestError("Can't found OS credentials", None)
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300451
koder aka kdanilovcee43342015-04-14 22:52:53 +0300452 if creds is None:
453 creds = {'name': user,
454 'passwd': passwd,
455 'tenant': tenant,
456 'auth_url': auth_url}
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300457
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300458 msg = "OS_CREDS: user={name} tenant={tenant} auth_url={auth_url}"
459 logger.debug(msg.format(**creds))
koder aka kdanilovcee43342015-04-14 22:52:53 +0300460 return creds
koder aka kdanilov4e9f3ed2015-04-14 11:26:12 +0300461
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300462
koder aka kdanilov168f6092015-04-19 02:33:38 +0300463@contextlib.contextmanager
koder aka kdanilovd5ed4da2015-05-07 23:33:23 +0300464def create_vms_ctx(ctx, cfg, config, already_has_count=0):
koder aka kdanilovc368eb62015-04-28 18:22:01 +0300465 params = cfg['vm_configs'][config['cfg_name']].copy()
koder aka kdanilov168f6092015-04-19 02:33:38 +0300466 os_nodes_ids = []
467
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300468 if not start_vms.is_connected():
469 os_creds = get_OS_credentials(cfg, ctx)
470 else:
471 os_creds = {}
koder aka kdanilov168f6092015-04-19 02:33:38 +0300472 start_vms.nova_connect(**os_creds)
473
koder aka kdanilovc368eb62015-04-28 18:22:01 +0300474 params.update(config)
475 params['keypair_file_private'] = params['keypair_name'] + ".pem"
476 params['group_name'] = cfg_dict['run_uuid']
477
koder aka kdanilovd5ed4da2015-05-07 23:33:23 +0300478 if not config.get('skip_preparation', False):
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300479 logger.info("Preparing openstack")
koder aka kdanilovd5ed4da2015-05-07 23:33:23 +0300480 start_vms.prepare_os_subpr(params=params, **os_creds)
koder aka kdanilov168f6092015-04-19 02:33:38 +0300481
482 new_nodes = []
483 try:
koder aka kdanilovd5ed4da2015-05-07 23:33:23 +0300484 for new_node, node_id in start_vms.launch_vms(params,
485 already_has_count):
koder aka kdanilov168f6092015-04-19 02:33:38 +0300486 new_node.roles.append('testnode')
487 ctx.nodes.append(new_node)
488 os_nodes_ids.append(node_id)
489 new_nodes.append(new_node)
490
491 store_nodes_in_log(cfg, os_nodes_ids)
492 ctx.openstack_nodes_ids = os_nodes_ids
493
494 yield new_nodes
495
496 finally:
497 if not cfg['keep_vm']:
498 shut_down_vms_stage(cfg, ctx)
499
500
koder aka kdanilovcee43342015-04-14 22:52:53 +0300501def run_tests_stage(cfg, ctx):
502 ctx.results = []
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300503
koder aka kdanilovcee43342015-04-14 22:52:53 +0300504 if 'tests' not in cfg:
505 return
gstepanov023c1e42015-04-08 15:50:19 +0300506
koder aka kdanilovcee43342015-04-14 22:52:53 +0300507 for group in cfg['tests']:
508
509 assert len(group.items()) == 1
510 key, config = group.items()[0]
511
512 if 'start_test_nodes' == key:
koder aka kdanilovc368eb62015-04-28 18:22:01 +0300513 if 'openstack' not in config:
514 msg = "No openstack block in config - can't spawn vm's"
515 logger.error(msg)
516 raise utils.StopTestError(msg)
517
koder aka kdanilovd5ed4da2015-05-07 23:33:23 +0300518 num_test_nodes = sum(1 for node in ctx.nodes
519 if 'testnode' in node.roles)
520
521 vm_ctx = create_vms_ctx(ctx, cfg, config['openstack'],
522 num_test_nodes)
523 with vm_ctx as new_nodes:
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300524 if len(new_nodes) != 0:
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300525 logger.debug("Connecting to new nodes")
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300526 connect_all(new_nodes, True)
koder aka kdanilovcee43342015-04-14 22:52:53 +0300527
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300528 for node in new_nodes:
529 if node.connection is None:
530 msg = "Failed to connect to vm {0}"
531 raise RuntimeError(msg.format(node.get_conn_id()))
koder aka kdanilovcee43342015-04-14 22:52:53 +0300532
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300533 with with_sensors_util(cfg_dict, ctx.nodes):
534 for test_group in config.get('tests', []):
535 ctx.results.extend(run_tests(cfg, test_group,
536 ctx.nodes))
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300537 else:
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300538 with with_sensors_util(cfg_dict, ctx.nodes):
539 ctx.results.extend(run_tests(cfg, group, ctx.nodes))
koder aka kdanilovda45e882015-04-06 02:24:42 +0300540
gstepanov023c1e42015-04-08 15:50:19 +0300541
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300542def shut_down_vms_stage(cfg, ctx):
koder aka kdanilov66839a92015-04-11 13:22:31 +0300543 vm_ids_fname = cfg_dict['vm_ids_fname']
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300544 if ctx.openstack_nodes_ids is None:
koder aka kdanilov66839a92015-04-11 13:22:31 +0300545 nodes_ids = open(vm_ids_fname).read().split()
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300546 else:
547 nodes_ids = ctx.openstack_nodes_ids
548
koder aka kdanilov652cd802015-04-13 12:21:07 +0300549 if len(nodes_ids) != 0:
550 logger.info("Removing nodes")
551 start_vms.clear_nodes(nodes_ids)
552 logger.info("Nodes has been removed")
gstepanov023c1e42015-04-08 15:50:19 +0300553
koder aka kdanilov66839a92015-04-11 13:22:31 +0300554 if os.path.exists(vm_ids_fname):
555 os.remove(vm_ids_fname)
gstepanov023c1e42015-04-08 15:50:19 +0300556
koder aka kdanilov66839a92015-04-11 13:22:31 +0300557
558def store_nodes_in_log(cfg, nodes_ids):
559 with open(cfg['vm_ids_fname'], 'w') as fd:
560 fd.write("\n".join(nodes_ids))
gstepanov023c1e42015-04-08 15:50:19 +0300561
562
563def clear_enviroment(cfg, ctx):
koder aka kdanilov66839a92015-04-11 13:22:31 +0300564 if os.path.exists(cfg_dict['vm_ids_fname']):
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300565 shut_down_vms_stage(cfg, ctx)
gstepanov023c1e42015-04-08 15:50:19 +0300566
567
koder aka kdanilovda45e882015-04-06 02:24:42 +0300568def disconnect_stage(cfg, ctx):
koder aka kdanilov652cd802015-04-13 12:21:07 +0300569 ssh_utils.close_all_sessions()
570
koder aka kdanilovda45e882015-04-06 02:24:42 +0300571 for node in ctx.nodes:
572 if node.connection is not None:
573 node.connection.close()
574
575
koder aka kdanilov66839a92015-04-11 13:22:31 +0300576def store_raw_results_stage(cfg, ctx):
577
578 raw_results = os.path.join(cfg_dict['var_dir'], 'raw_results.yaml')
579
580 if os.path.exists(raw_results):
581 cont = yaml.load(open(raw_results).read())
582 else:
583 cont = []
584
koder aka kdanilov168f6092015-04-19 02:33:38 +0300585 cont.extend(utils.yamable(ctx.results))
koder aka kdanilov66839a92015-04-11 13:22:31 +0300586 raw_data = pretty_yaml.dumps(cont)
587
588 with open(raw_results, "w") as fd:
589 fd.write(raw_data)
590
591
592def console_report_stage(cfg, ctx):
593 for tp, data in ctx.results:
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300594 if 'io' == tp and data is not None:
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300595 dinfo = report.process_disk_info(data)
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300596 text_rep_fname = cfg['text_report_file']
597 rep = IOPerfTest.format_for_console(data, dinfo)
598
599 with open(text_rep_fname, "w") as fd:
600 fd.write(rep)
601 fd.write("\n")
602 fd.flush()
603
604 logger.info("Text report were stored in " + text_rep_fname)
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300605 print("\n" + rep + "\n")
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300606
Yulia Portnova1f123962015-05-06 18:48:11 +0300607 if tp in ['mysql', 'pgbench'] and data is not None:
Yulia Portnovab1a15072015-05-06 14:59:25 +0300608 print("\n")
609 print(MysqlTest.format_for_console(data))
610 print("\n")
koder aka kdanilov66839a92015-04-11 13:22:31 +0300611
612
koder aka kdanilove87ae652015-04-20 02:14:35 +0300613def html_report_stage(cfg, ctx):
Yulia Portnova8ca20572015-04-14 14:09:39 +0300614 html_rep_fname = cfg['html_report_file']
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300615 found = False
616 for tp, data in ctx.results:
617 if 'io' == tp and data is not None:
618 if found:
619 logger.error("Making reports for more than one " +
620 "io block isn't supported! All " +
621 "report, except first are skipped")
622 continue
623 found = True
624 dinfo = report.process_disk_info(data)
625 report.make_io_report(dinfo, data, html_rep_fname,
626 lab_info=ctx.hw_info)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300627
koder aka kdanilovda45e882015-04-06 02:24:42 +0300628
629def complete_log_nodes_statistic(cfg, ctx):
630 nodes = ctx.nodes
631 for node in nodes:
632 logger.debug(str(node))
633
634
koder aka kdanilov66839a92015-04-11 13:22:31 +0300635def load_data_from(var_dir):
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300636 def load_data_from_file(_, ctx):
koder aka kdanilov66839a92015-04-11 13:22:31 +0300637 raw_results = os.path.join(var_dir, 'raw_results.yaml')
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300638 ctx.results = []
639 for tp, results in yaml.load(open(raw_results).read()):
640 cls = TOOL_TYPE_MAPPER[tp]
641 ctx.results.append((tp, map(cls.load, results)))
642
koder aka kdanilov4e9f3ed2015-04-14 11:26:12 +0300643 return load_data_from_file
gstepanovcd256d62015-04-07 17:47:32 +0300644
645
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300646def start_web_ui(cfg, ctx):
647 if webui is None:
648 logger.error("Can't start webui. Install cherrypy module")
649 ctx.web_thread = None
650 else:
651 th = threading.Thread(None, webui.web_main_thread, "webui", (None,))
652 th.daemon = True
653 th.start()
654 ctx.web_thread = th
655
656
657def stop_web_ui(cfg, ctx):
658 webui.web_main_stop()
659 time.sleep(1)
660
661
koder aka kdanilovcee43342015-04-14 22:52:53 +0300662def parse_args(argv):
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300663 descr = "Disk io performance test suite"
664 parser = argparse.ArgumentParser(prog='wally', description=descr)
koder aka kdanilovcee43342015-04-14 22:52:53 +0300665
666 parser.add_argument("-l", dest='extra_logs',
667 action='store_true', default=False,
668 help="print some extra log info")
koder aka kdanilovcee43342015-04-14 22:52:53 +0300669 parser.add_argument("-b", '--build_description',
670 type=str, default="Build info")
671 parser.add_argument("-i", '--build_id', type=str, default="id")
672 parser.add_argument("-t", '--build_type', type=str, default="GA")
673 parser.add_argument("-u", '--username', type=str, default="admin")
koder aka kdanilove87ae652015-04-20 02:14:35 +0300674 parser.add_argument("-n", '--no-tests', action='store_true',
675 help="Don't run tests", default=False)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300676 parser.add_argument("-p", '--post-process-only', metavar="VAR_DIR",
677 help="Only process data from previour run")
678 parser.add_argument("-k", '--keep-vm', action='store_true',
679 help="Don't remove test vm's", default=False)
koder aka kdanilove87ae652015-04-20 02:14:35 +0300680 parser.add_argument("-d", '--dont-discover-nodes', action='store_true',
681 help="Don't connect/discover fuel nodes",
682 default=False)
koder aka kdanilova047e1b2015-04-21 23:16:59 +0300683 parser.add_argument("-r", '--no-html-report', action='store_true',
684 help="Skip html report", default=False)
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300685 parser.add_argument("--params", metavar="testname.paramname",
koder aka kdanilov63ad2062015-04-27 13:11:40 +0300686 help="Test params", default=[])
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300687 parser.add_argument("config_file")
koder aka kdanilovcee43342015-04-14 22:52:53 +0300688
689 return parser.parse_args(argv[1:])
690
691
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300692def get_stage_name(func):
693 if func.__name__.endswith("stage"):
694 return func.__name__
695 else:
696 return func.__name__ + " stage"
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300697
698
koder aka kdanilov3f356262015-02-13 08:06:14 -0800699def main(argv):
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300700 faulthandler.register(signal.SIGUSR1, all_threads=True)
koder aka kdanilove06762a2015-03-22 23:32:09 +0200701 opts = parse_args(argv)
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300702 load_config(opts.config_file, opts.post_process_only)
koder aka kdanilov2c473092015-03-29 17:12:13 +0300703
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300704 if cfg_dict.get('logging', {}).get("extra_logs", False) or opts.extra_logs:
705 level = logging.DEBUG
706 else:
707 level = logging.WARNING
708
709 setup_loggers(level, cfg_dict['log_file'])
710
koder aka kdanilov66839a92015-04-11 13:22:31 +0300711 if opts.post_process_only is not None:
712 stages = [
koder aka kdanilove87ae652015-04-20 02:14:35 +0300713 load_data_from(opts.post_process_only)
koder aka kdanilov66839a92015-04-11 13:22:31 +0300714 ]
715 else:
716 stages = [
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300717 discover_stage
718 ]
719
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300720 stages.extend([
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300721 reuse_vms_stage,
koder aka kdanilov66839a92015-04-11 13:22:31 +0300722 log_nodes_statistic,
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300723 save_nodes_stage,
724 connect_stage])
725
726 if cfg_dict.get('collect_info', True):
727 stages.append(collect_hw_info_stage)
728
729 stages.extend([
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300730 # deploy_sensors_stage,
koder aka kdanilov66839a92015-04-11 13:22:31 +0300731 run_tests_stage,
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300732 store_raw_results_stage,
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300733 # gather_sensors_stage
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300734 ])
koder aka kdanilov66839a92015-04-11 13:22:31 +0300735
koder aka kdanilove87ae652015-04-20 02:14:35 +0300736 report_stages = [
737 console_report_stage,
koder aka kdanilove87ae652015-04-20 02:14:35 +0300738 ]
739
koder aka kdanilova047e1b2015-04-21 23:16:59 +0300740 if not opts.no_html_report:
741 report_stages.append(html_report_stage)
742
koder aka kdanilov652cd802015-04-13 12:21:07 +0300743 logger.info("All info would be stored into {0}".format(
744 cfg_dict['var_dir']))
gstepanovcd256d62015-04-07 17:47:32 +0300745
koder aka kdanilovda45e882015-04-06 02:24:42 +0300746 ctx = Context()
gstepanovaffcdb12015-04-07 17:18:29 +0300747 ctx.build_meta['build_id'] = opts.build_id
748 ctx.build_meta['build_descrption'] = opts.build_description
749 ctx.build_meta['build_type'] = opts.build_type
750 ctx.build_meta['username'] = opts.username
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300751 ctx.sensors_data = SensorDatastore()
koder aka kdanilove87ae652015-04-20 02:14:35 +0300752
koder aka kdanilov168f6092015-04-19 02:33:38 +0300753 cfg_dict['keep_vm'] = opts.keep_vm
koder aka kdanilove87ae652015-04-20 02:14:35 +0300754 cfg_dict['no_tests'] = opts.no_tests
755 cfg_dict['dont_discover_nodes'] = opts.dont_discover_nodes
koder aka kdanilov6c491062015-04-09 22:33:13 +0300756
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300757 if cfg_dict.get('run_web_ui', False):
758 start_web_ui(cfg_dict, ctx)
759
koder aka kdanilovc368eb62015-04-28 18:22:01 +0300760 msg_templ = "Exception during {0.__name__}: {1!s}"
761 msg_templ_no_exc = "During {0.__name__}"
762
koder aka kdanilovda45e882015-04-06 02:24:42 +0300763 try:
764 for stage in stages:
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300765 logger.info("Start " + get_stage_name(stage))
koder aka kdanilovda45e882015-04-06 02:24:42 +0300766 stage(cfg_dict, ctx)
koder aka kdanilovc368eb62015-04-28 18:22:01 +0300767 except utils.StopTestError as exc:
768 logger.error(msg_templ.format(stage, exc))
769 except Exception:
770 logger.exception(msg_templ_no_exc.format(stage))
koder aka kdanilovda45e882015-04-06 02:24:42 +0300771 finally:
772 exc, cls, tb = sys.exc_info()
773 for stage in ctx.clear_calls_stack[::-1]:
774 try:
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300775 logger.info("Start " + get_stage_name(stage))
koder aka kdanilovda45e882015-04-06 02:24:42 +0300776 stage(cfg_dict, ctx)
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300777 except utils.StopTestError as cleanup_exc:
778 logger.error(msg_templ.format(stage, cleanup_exc))
779 except Exception:
780 logger.exception(msg_templ_no_exc.format(stage))
781
782 logger.debug("Start utils.cleanup")
783 for clean_func, args, kwargs in utils.iter_clean_func():
784 try:
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300785 logger.info("Start " + get_stage_name(clean_func))
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300786 clean_func(*args, **kwargs)
787 except utils.StopTestError as cleanup_exc:
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300788 logger.error(msg_templ.format(clean_func, cleanup_exc))
koder aka kdanilovc368eb62015-04-28 18:22:01 +0300789 except Exception:
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300790 logger.exception(msg_templ_no_exc.format(clean_func))
koder aka kdanilov2c473092015-03-29 17:12:13 +0300791
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300792 if exc is None:
793 for report_stage in report_stages:
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300794 logger.info("Start " + get_stage_name(report_stage))
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300795 report_stage(cfg_dict, ctx)
koder aka kdanilove87ae652015-04-20 02:14:35 +0300796
797 logger.info("All info stored in {0} folder".format(cfg_dict['var_dir']))
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300798
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300799 if cfg_dict.get('run_web_ui', False):
800 stop_web_ui(cfg_dict, ctx)
801
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300802 if exc is None:
803 logger.info("Tests finished successfully")
804 return 0
805 else:
806 logger.error("Tests are failed. See detailed error above")
807 return 1