blob: 8c1fecede24e4665b43a7c7f787ebcf356984f6c [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 kdanilove21d7472015-02-14 19:02:04 -08008import logging
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -08009import argparse
koder aka kdanilov168f6092015-04-19 02:33:38 +030010import functools
koder aka kdanilov2c473092015-03-29 17:12:13 +030011import threading
koder aka kdanilov168f6092015-04-19 02:33:38 +030012import contextlib
koder aka kdanilov7306c642015-04-23 15:29:45 +030013import subprocess
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 kdanilov2c473092015-03-29 17:12:13 +030017from concurrent.futures import ThreadPoolExecutor
koder aka kdanilov6c491062015-04-09 22:33:13 +030018
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030019from wally import pretty_yaml
koder aka kdanilov63ad2062015-04-27 13:11:40 +030020from wally.timeseries import SensorDatastore
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030021from wally.discover import discover, Node, undiscover
22from wally import utils, report, ssh_utils, start_vms
Yulia Portnovab1a15072015-05-06 14:59:25 +030023from wally.suits.itest import IOPerfTest, PgBenchTest, MysqlTest
koder aka kdanilov63ad2062015-04-27 13:11:40 +030024from wally.sensors_utils import deploy_sensors_stage
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030025from wally.config import cfg_dict, load_config, setup_loggers
koder aka kdanilov63ad2062015-04-27 13:11:40 +030026
koder aka kdanilov57ce4db2015-04-25 21:25:51 +030027
28try:
29 from wally import webui
30except ImportError:
31 webui = None
koder aka kdanilov2c473092015-03-29 17:12:13 +030032
33
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030034logger = logging.getLogger("wally")
koder aka kdanilovcee43342015-04-14 22:52:53 +030035
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080036
Yulia Portnova7ddfa732015-02-24 17:32:58 +020037def format_result(res, formatter):
koder aka kdanilove21d7472015-02-14 19:02:04 -080038 data = "\n{0}\n".format("=" * 80)
39 data += pprint.pformat(res) + "\n"
40 data += "{0}\n".format("=" * 80)
koder aka kdanilovfe056622015-02-19 08:46:15 -080041 templ = "{0}\n\n====> {1}\n\n{2}\n\n"
Yulia Portnova7ddfa732015-02-24 17:32:58 +020042 return templ.format(data, formatter(res), "=" * 80)
koder aka kdanilove21d7472015-02-14 19:02:04 -080043
44
koder aka kdanilov1c2b5112015-04-10 16:53:51 +030045class Context(object):
46 def __init__(self):
47 self.build_meta = {}
48 self.nodes = []
49 self.clear_calls_stack = []
50 self.openstack_nodes_ids = []
koder aka kdanilov168f6092015-04-19 02:33:38 +030051 self.sensors_mon_q = None
koder aka kdanilov1c2b5112015-04-10 16:53:51 +030052
53
koder aka kdanilov168f6092015-04-19 02:33:38 +030054def connect_one(node, vm=False):
koder aka kdanilov0c598a12015-04-21 03:01:40 +030055 if node.conn_url == 'local':
56 node.connection = ssh_utils.connect(node.conn_url)
57 return
58
koder aka kdanilov5d589b42015-03-26 12:25:51 +020059 try:
koder aka kdanilov2c473092015-03-29 17:12:13 +030060 ssh_pref = "ssh://"
61 if node.conn_url.startswith(ssh_pref):
62 url = node.conn_url[len(ssh_pref):]
koder aka kdanilov168f6092015-04-19 02:33:38 +030063
64 if vm:
koder aka kdanilov6b1341a2015-04-21 22:44:21 +030065 conn_timeout = 240
koder aka kdanilov168f6092015-04-19 02:33:38 +030066 else:
koder aka kdanilov6b1341a2015-04-21 22:44:21 +030067 conn_timeout = 30
koder aka kdanilov168f6092015-04-19 02:33:38 +030068
69 node.connection = ssh_utils.connect(url,
koder aka kdanilov6b1341a2015-04-21 22:44:21 +030070 conn_timeout=conn_timeout)
koder aka kdanilov2c473092015-03-29 17:12:13 +030071 else:
72 raise ValueError("Unknown url type {0}".format(node.conn_url))
koder aka kdanilove87ae652015-04-20 02:14:35 +030073 except Exception as exc:
74 # logger.exception("During connect to " + node.get_conn_id())
koder aka kdanilovec1b9732015-04-23 20:43:29 +030075 msg = "During connect to {0}: {1!s}".format(node.get_conn_id(),
76 exc)
koder aka kdanilove87ae652015-04-20 02:14:35 +030077 logger.error(msg)
koder aka kdanilov168f6092015-04-19 02:33:38 +030078 node.connection = None
koder aka kdanilov5d589b42015-03-26 12:25:51 +020079
80
koder aka kdanilov168f6092015-04-19 02:33:38 +030081def connect_all(nodes, vm=False):
koder aka kdanilov2c473092015-03-29 17:12:13 +030082 logger.info("Connecting to nodes")
83 with ThreadPoolExecutor(32) as pool:
koder aka kdanilov168f6092015-04-19 02:33:38 +030084 connect_one_f = functools.partial(connect_one, vm=vm)
85 list(pool.map(connect_one_f, nodes))
koder aka kdanilov2c473092015-03-29 17:12:13 +030086
87
koder aka kdanilov652cd802015-04-13 12:21:07 +030088def test_thread(test, node, barrier, res_q):
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030089 exc = None
koder aka kdanilov2c473092015-03-29 17:12:13 +030090 try:
koder aka kdanilova047e1b2015-04-21 23:16:59 +030091 logger.debug("Run preparation for {0}".format(node.get_conn_id()))
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030092 test.pre_run()
koder aka kdanilova047e1b2015-04-21 23:16:59 +030093 logger.debug("Run test for {0}".format(node.get_conn_id()))
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030094 test.run(barrier)
koder aka kdanilove2de58c2015-04-24 22:59:36 +030095 except utils.StopTestError as exc:
96 pass
koder aka kdanilov652cd802015-04-13 12:21:07 +030097 except Exception as exc:
koder aka kdanilove2de58c2015-04-24 22:59:36 +030098 msg = "In test {0} for node {1}"
99 msg = msg.format(test, node.get_conn_id())
100 logger.exception(msg)
101 exc = utils.StopTestError(msg, exc)
koder aka kdanilov2c473092015-03-29 17:12:13 +0300102
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300103 try:
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300104 test.cleanup()
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300105 except utils.StopTestError as exc1:
106 if exc is None:
107 exc = exc1
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300108 except:
109 msg = "Duringf cleanup - in test {0} for node {1}"
110 logger.exception(msg.format(test, node))
111
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300112 if exc is not None:
113 res_q.put(exc)
koder aka kdanilov2c473092015-03-29 17:12:13 +0300114
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300115
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300116def run_tests(cfg, test_block, nodes):
koder aka kdanilov2c473092015-03-29 17:12:13 +0300117 tool_type_mapper = {
118 "io": IOPerfTest,
119 "pgbench": PgBenchTest,
Yulia Portnovab1a15072015-05-06 14:59:25 +0300120 "mysql": MysqlTest,
koder aka kdanilov2c473092015-03-29 17:12:13 +0300121 }
122
123 test_nodes = [node for node in nodes
124 if 'testnode' in node.roles]
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300125 test_number_per_type = {}
koder aka kdanilov2c473092015-03-29 17:12:13 +0300126 res_q = Queue.Queue()
127
koder aka kdanilovcee43342015-04-14 22:52:53 +0300128 for name, params in test_block.items():
129 logger.info("Starting {0} tests".format(name))
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300130 test_num = test_number_per_type.get(name, 0)
131 test_number_per_type[name] = test_num + 1
koder aka kdanilovcee43342015-04-14 22:52:53 +0300132 threads = []
133 barrier = utils.Barrier(len(test_nodes))
koder aka kdanilovec1b9732015-04-23 20:43:29 +0300134 coord_q = Queue.Queue()
135 test_cls = tool_type_mapper[name]
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300136 rem_folder = cfg['default_test_local_folder'].format(name=name)
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300137
koder aka kdanilovabd6ead2015-04-24 02:03:07 +0300138 for idx, node in enumerate(test_nodes):
koder aka kdanilovcee43342015-04-14 22:52:53 +0300139 msg = "Starting {0} test on {1} node"
140 logger.debug(msg.format(name, node.conn_url))
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300141
142 dr = os.path.join(
143 cfg_dict['test_log_directory'],
144 "{0}_{1}_{2}".format(name, test_num, node.get_ip())
145 )
146
147 if not os.path.exists(dr):
148 os.makedirs(dr)
149
koder aka kdanilovabd6ead2015-04-24 02:03:07 +0300150 test = test_cls(options=params,
151 is_primary=(idx == 0),
152 on_result_cb=res_q.put,
153 test_uuid=cfg['run_uuid'],
154 node=node,
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300155 remote_dir=rem_folder,
koder aka kdanilovec1b9732015-04-23 20:43:29 +0300156 log_directory=dr,
157 coordination_queue=coord_q)
koder aka kdanilovcee43342015-04-14 22:52:53 +0300158 th = threading.Thread(None, test_thread, None,
159 (test, node, barrier, res_q))
160 threads.append(th)
161 th.daemon = True
162 th.start()
koder aka kdanilov2c473092015-03-29 17:12:13 +0300163
koder aka kdanilovec1b9732015-04-23 20:43:29 +0300164 th = threading.Thread(None, test_cls.coordination_th, None,
165 (coord_q, barrier, len(threads)))
166 threads.append(th)
167 th.daemon = True
168 th.start()
169
koder aka kdanilovcee43342015-04-14 22:52:53 +0300170 def gather_results(res_q, results):
171 while not res_q.empty():
172 val = res_q.get()
koder aka kdanilov66839a92015-04-11 13:22:31 +0300173
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300174 if isinstance(val, utils.StopTestError):
175 raise val
176
koder aka kdanilovcee43342015-04-14 22:52:53 +0300177 if isinstance(val, Exception):
koder aka kdanilovec1b9732015-04-23 20:43:29 +0300178 msg = "Exception during test execution: {0!s}"
179 raise ValueError(msg.format(val))
koder aka kdanilov652cd802015-04-13 12:21:07 +0300180
koder aka kdanilovcee43342015-04-14 22:52:53 +0300181 results.append(val)
koder aka kdanilov652cd802015-04-13 12:21:07 +0300182
koder aka kdanilovcee43342015-04-14 22:52:53 +0300183 results = []
koder aka kdanilov652cd802015-04-13 12:21:07 +0300184
koder aka kdanilove87ae652015-04-20 02:14:35 +0300185 # MAX_WAIT_TIME = 10
186 # end_time = time.time() + MAX_WAIT_TIME
187
188 # while time.time() < end_time:
koder aka kdanilovcee43342015-04-14 22:52:53 +0300189 while True:
190 for th in threads:
191 th.join(1)
192 gather_results(res_q, results)
koder aka kdanilove87ae652015-04-20 02:14:35 +0300193 # if time.time() > end_time:
194 # break
koder aka kdanilov652cd802015-04-13 12:21:07 +0300195
koder aka kdanilovcee43342015-04-14 22:52:53 +0300196 if all(not th.is_alive() for th in threads):
197 break
koder aka kdanilov652cd802015-04-13 12:21:07 +0300198
koder aka kdanilove87ae652015-04-20 02:14:35 +0300199 # if any(th.is_alive() for th in threads):
200 # logger.warning("Some test threads still running")
201
koder aka kdanilovcee43342015-04-14 22:52:53 +0300202 gather_results(res_q, results)
koder aka kdanilov63ad2062015-04-27 13:11:40 +0300203 result = test.merge_results(results)
204 result['__test_meta__'] = {'testnodes_count': len(test_nodes)}
205 yield name, result
koder aka kdanilov2c473092015-03-29 17:12:13 +0300206
207
koder aka kdanilovda45e882015-04-06 02:24:42 +0300208def log_nodes_statistic(_, ctx):
209 nodes = ctx.nodes
koder aka kdanilov2c473092015-03-29 17:12:13 +0300210 logger.info("Found {0} nodes total".format(len(nodes)))
211 per_role = collections.defaultdict(lambda: 0)
212 for node in nodes:
213 for role in node.roles:
214 per_role[role] += 1
215
216 for role, count in sorted(per_role.items()):
217 logger.debug("Found {0} nodes with role {1}".format(count, role))
218
219
koder aka kdanilovda45e882015-04-06 02:24:42 +0300220def connect_stage(cfg, ctx):
221 ctx.clear_calls_stack.append(disconnect_stage)
222 connect_all(ctx.nodes)
223
koder aka kdanilov168f6092015-04-19 02:33:38 +0300224 all_ok = True
koder aka kdanilovda45e882015-04-06 02:24:42 +0300225
koder aka kdanilov168f6092015-04-19 02:33:38 +0300226 for node in ctx.nodes:
227 if node.connection is None:
228 if 'testnode' in node.roles:
229 msg = "Can't connect to testnode {0}"
230 raise RuntimeError(msg.format(node.get_conn_id()))
231 else:
232 msg = "Node {0} would be excluded - can't connect"
233 logger.warning(msg.format(node.get_conn_id()))
234 all_ok = False
235
236 if all_ok:
237 logger.info("All nodes connected successfully")
238
239 ctx.nodes = [node for node in ctx.nodes
240 if node.connection is not None]
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300241
242
koder aka kdanilovda45e882015-04-06 02:24:42 +0300243def discover_stage(cfg, ctx):
koder aka kdanilov652cd802015-04-13 12:21:07 +0300244 if cfg.get('discover') is not None:
koder aka kdanilovda45e882015-04-06 02:24:42 +0300245 discover_objs = [i.strip() for i in cfg['discover'].strip().split(",")]
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300246
koder aka kdanilove87ae652015-04-20 02:14:35 +0300247 nodes, clean_data = discover(ctx,
248 discover_objs,
249 cfg['clouds'],
250 cfg['var_dir'],
251 not cfg['dont_discover_nodes'])
koder aka kdanilov168f6092015-04-19 02:33:38 +0300252
253 def undiscover_stage(cfg, ctx):
254 undiscover(clean_data)
255
256 ctx.clear_calls_stack.append(undiscover_stage)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300257 ctx.nodes.extend(nodes)
koder aka kdanilovda45e882015-04-06 02:24:42 +0300258
259 for url, roles in cfg.get('explicit_nodes', {}).items():
260 ctx.nodes.append(Node(url, roles.split(",")))
261
262
koder aka kdanilove87ae652015-04-20 02:14:35 +0300263def get_OS_credentials(cfg, ctx, creds_type):
koder aka kdanilovcee43342015-04-14 22:52:53 +0300264 creds = None
koder aka kdanilovda45e882015-04-06 02:24:42 +0300265
koder aka kdanilovcee43342015-04-14 22:52:53 +0300266 if creds_type == 'clouds':
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300267 logger.info("Using OS credentials from 'cloud' section")
koder aka kdanilovcee43342015-04-14 22:52:53 +0300268 if 'openstack' in cfg['clouds']:
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300269 os_cfg = cfg['clouds']['openstack']
koder aka kdanilovcee43342015-04-14 22:52:53 +0300270
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300271 tenant = os_cfg['OS_TENANT_NAME'].strip()
272 user = os_cfg['OS_USERNAME'].strip()
273 passwd = os_cfg['OS_PASSWORD'].strip()
274 auth_url = os_cfg['OS_AUTH_URL'].strip()
275
koder aka kdanilovcee43342015-04-14 22:52:53 +0300276 elif 'fuel' in cfg['clouds'] and \
277 'openstack_env' in cfg['clouds']['fuel']:
278 creds = ctx.fuel_openstack_creds
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300279
koder aka kdanilovcee43342015-04-14 22:52:53 +0300280 elif creds_type == 'ENV':
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300281 logger.info("Using OS credentials from shell environment")
koder aka kdanilovcee43342015-04-14 22:52:53 +0300282 user, passwd, tenant, auth_url = start_vms.ostack_get_creds()
283 elif os.path.isfile(creds_type):
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300284 logger.info("Using OS credentials from " + creds_type)
koder aka kdanilov7306c642015-04-23 15:29:45 +0300285 fc = open(creds_type).read()
286
287 echo = 'echo "$OS_TENANT_NAME:$OS_USERNAME:$OS_PASSWORD@$OS_AUTH_URL"'
288
289 p = subprocess.Popen(['/bin/bash'], shell=False,
290 stdout=subprocess.PIPE,
291 stdin=subprocess.PIPE,
292 stderr=subprocess.STDOUT)
293 p.stdin.write(fc + "\n" + echo)
294 p.stdin.close()
295 code = p.wait()
296 data = p.stdout.read().strip()
297
298 if code != 0:
299 msg = "Failed to get creads from openrc file: " + data
300 logger.error(msg)
301 raise RuntimeError(msg)
302
303 try:
304 user, tenant, passwd_auth_url = data.split(':', 2)
305 passwd, auth_url = passwd_auth_url.rsplit("@", 1)
306 assert (auth_url.startswith("https://") or
307 auth_url.startswith("http://"))
308 except Exception:
309 msg = "Failed to get creads from openrc file: " + data
310 logger.exception(msg)
311 raise
312
koder aka kdanilovcee43342015-04-14 22:52:53 +0300313 else:
314 msg = "Creds {0!r} isn't supported".format(creds_type)
315 raise ValueError(msg)
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300316
koder aka kdanilovcee43342015-04-14 22:52:53 +0300317 if creds is None:
318 creds = {'name': user,
319 'passwd': passwd,
320 'tenant': tenant,
321 'auth_url': auth_url}
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300322
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300323 msg = "OS_CREDS: user={name} tenant={tenant} auth_url={auth_url}"
324 logger.debug(msg.format(**creds))
koder aka kdanilovcee43342015-04-14 22:52:53 +0300325 return creds
koder aka kdanilov4e9f3ed2015-04-14 11:26:12 +0300326
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300327
koder aka kdanilov168f6092015-04-19 02:33:38 +0300328@contextlib.contextmanager
329def create_vms_ctx(ctx, cfg, config):
koder aka kdanilovc368eb62015-04-28 18:22:01 +0300330 params = cfg['vm_configs'][config['cfg_name']].copy()
koder aka kdanilov168f6092015-04-19 02:33:38 +0300331 os_nodes_ids = []
332
333 os_creds_type = config['creds']
koder aka kdanilove87ae652015-04-20 02:14:35 +0300334 os_creds = get_OS_credentials(cfg, ctx, os_creds_type)
koder aka kdanilov168f6092015-04-19 02:33:38 +0300335
336 start_vms.nova_connect(**os_creds)
337
338 logger.info("Preparing openstack")
koder aka kdanilovc368eb62015-04-28 18:22:01 +0300339 params.update(config)
340 params['keypair_file_private'] = params['keypair_name'] + ".pem"
341 params['group_name'] = cfg_dict['run_uuid']
342
343 start_vms.prepare_os_subpr(params=params, **os_creds)
koder aka kdanilov168f6092015-04-19 02:33:38 +0300344
345 new_nodes = []
346 try:
koder aka kdanilov168f6092015-04-19 02:33:38 +0300347 for new_node, node_id in start_vms.launch_vms(params):
348 new_node.roles.append('testnode')
349 ctx.nodes.append(new_node)
350 os_nodes_ids.append(node_id)
351 new_nodes.append(new_node)
352
353 store_nodes_in_log(cfg, os_nodes_ids)
354 ctx.openstack_nodes_ids = os_nodes_ids
355
356 yield new_nodes
357
358 finally:
359 if not cfg['keep_vm']:
360 shut_down_vms_stage(cfg, ctx)
361
362
koder aka kdanilovcee43342015-04-14 22:52:53 +0300363def run_tests_stage(cfg, ctx):
364 ctx.results = []
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300365
koder aka kdanilovcee43342015-04-14 22:52:53 +0300366 if 'tests' not in cfg:
367 return
gstepanov023c1e42015-04-08 15:50:19 +0300368
koder aka kdanilovcee43342015-04-14 22:52:53 +0300369 for group in cfg['tests']:
370
371 assert len(group.items()) == 1
372 key, config = group.items()[0]
373
374 if 'start_test_nodes' == key:
koder aka kdanilovc368eb62015-04-28 18:22:01 +0300375 if 'openstack' not in config:
376 msg = "No openstack block in config - can't spawn vm's"
377 logger.error(msg)
378 raise utils.StopTestError(msg)
379
380 with create_vms_ctx(ctx, cfg, config['openstack']) as new_nodes:
koder aka kdanilov168f6092015-04-19 02:33:38 +0300381 connect_all(new_nodes, True)
koder aka kdanilovcee43342015-04-14 22:52:53 +0300382
koder aka kdanilov168f6092015-04-19 02:33:38 +0300383 for node in new_nodes:
384 if node.connection is None:
385 msg = "Failed to connect to vm {0}"
386 raise RuntimeError(msg.format(node.get_conn_id()))
koder aka kdanilovcee43342015-04-14 22:52:53 +0300387
koder aka kdanilov168f6092015-04-19 02:33:38 +0300388 deploy_sensors_stage(cfg_dict,
389 ctx,
390 nodes=new_nodes,
391 undeploy=False)
koder aka kdanilov12ae0632015-04-15 01:13:43 +0300392
koder aka kdanilove87ae652015-04-20 02:14:35 +0300393 if not cfg['no_tests']:
394 for test_group in config.get('tests', []):
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300395 test_res = run_tests(cfg, test_group, ctx.nodes)
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300396 ctx.results.extend(test_res)
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300397 else:
koder aka kdanilove87ae652015-04-20 02:14:35 +0300398 if not cfg['no_tests']:
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300399 test_res = run_tests(cfg, group, ctx.nodes)
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300400 ctx.results.extend(test_res)
koder aka kdanilovda45e882015-04-06 02:24:42 +0300401
gstepanov023c1e42015-04-08 15:50:19 +0300402
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300403def shut_down_vms_stage(cfg, ctx):
koder aka kdanilov66839a92015-04-11 13:22:31 +0300404 vm_ids_fname = cfg_dict['vm_ids_fname']
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300405 if ctx.openstack_nodes_ids is None:
koder aka kdanilov66839a92015-04-11 13:22:31 +0300406 nodes_ids = open(vm_ids_fname).read().split()
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300407 else:
408 nodes_ids = ctx.openstack_nodes_ids
409
koder aka kdanilov652cd802015-04-13 12:21:07 +0300410 if len(nodes_ids) != 0:
411 logger.info("Removing nodes")
412 start_vms.clear_nodes(nodes_ids)
413 logger.info("Nodes has been removed")
gstepanov023c1e42015-04-08 15:50:19 +0300414
koder aka kdanilov66839a92015-04-11 13:22:31 +0300415 if os.path.exists(vm_ids_fname):
416 os.remove(vm_ids_fname)
gstepanov023c1e42015-04-08 15:50:19 +0300417
koder aka kdanilov66839a92015-04-11 13:22:31 +0300418
419def store_nodes_in_log(cfg, nodes_ids):
420 with open(cfg['vm_ids_fname'], 'w') as fd:
421 fd.write("\n".join(nodes_ids))
gstepanov023c1e42015-04-08 15:50:19 +0300422
423
424def clear_enviroment(cfg, ctx):
koder aka kdanilov66839a92015-04-11 13:22:31 +0300425 if os.path.exists(cfg_dict['vm_ids_fname']):
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300426 shut_down_vms_stage(cfg, ctx)
gstepanov023c1e42015-04-08 15:50:19 +0300427
428
koder aka kdanilovda45e882015-04-06 02:24:42 +0300429def disconnect_stage(cfg, ctx):
koder aka kdanilov652cd802015-04-13 12:21:07 +0300430 ssh_utils.close_all_sessions()
431
koder aka kdanilovda45e882015-04-06 02:24:42 +0300432 for node in ctx.nodes:
433 if node.connection is not None:
434 node.connection.close()
435
436
koder aka kdanilov66839a92015-04-11 13:22:31 +0300437def store_raw_results_stage(cfg, ctx):
438
439 raw_results = os.path.join(cfg_dict['var_dir'], 'raw_results.yaml')
440
441 if os.path.exists(raw_results):
442 cont = yaml.load(open(raw_results).read())
443 else:
444 cont = []
445
koder aka kdanilov168f6092015-04-19 02:33:38 +0300446 cont.extend(utils.yamable(ctx.results))
koder aka kdanilov66839a92015-04-11 13:22:31 +0300447 raw_data = pretty_yaml.dumps(cont)
448
449 with open(raw_results, "w") as fd:
450 fd.write(raw_data)
451
452
453def console_report_stage(cfg, ctx):
454 for tp, data in ctx.results:
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300455 if 'io' == tp and data is not None:
koder aka kdanilove87ae652015-04-20 02:14:35 +0300456 print("\n")
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300457 print(IOPerfTest.format_for_console(data))
koder aka kdanilove87ae652015-04-20 02:14:35 +0300458 print("\n")
Yulia Portnova1f123962015-05-06 18:48:11 +0300459 if tp in ['mysql', 'pgbench'] and data is not None:
Yulia Portnovab1a15072015-05-06 14:59:25 +0300460 print("\n")
461 print(MysqlTest.format_for_console(data))
462 print("\n")
koder aka kdanilov66839a92015-04-11 13:22:31 +0300463
464
koder aka kdanilove87ae652015-04-20 02:14:35 +0300465def html_report_stage(cfg, ctx):
Yulia Portnova8ca20572015-04-14 14:09:39 +0300466 html_rep_fname = cfg['html_report_file']
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300467
468 try:
469 fuel_url = cfg['clouds']['fuel']['url']
470 except KeyError:
471 fuel_url = None
472
473 try:
474 creds = cfg['clouds']['fuel']['creds']
475 except KeyError:
476 creds = None
477
gstepanov69339ac2015-04-16 20:09:33 +0300478 report.make_io_report(ctx.results, html_rep_fname, fuel_url, creds=creds)
koder aka kdanilov652cd802015-04-13 12:21:07 +0300479
koder aka kdanilov652cd802015-04-13 12:21:07 +0300480 text_rep_fname = cfg_dict['text_report_file']
481 with open(text_rep_fname, "w") as fd:
482 for tp, data in ctx.results:
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300483 if 'io' == tp and data is not None:
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300484 fd.write(IOPerfTest.format_for_console(data))
koder aka kdanilov652cd802015-04-13 12:21:07 +0300485 fd.write("\n")
486 fd.flush()
487
488 logger.info("Text report were stored in " + text_rep_fname)
koder aka kdanilovda45e882015-04-06 02:24:42 +0300489
490
491def complete_log_nodes_statistic(cfg, ctx):
492 nodes = ctx.nodes
493 for node in nodes:
494 logger.debug(str(node))
495
496
koder aka kdanilov66839a92015-04-11 13:22:31 +0300497def load_data_from(var_dir):
koder aka kdanilov4e9f3ed2015-04-14 11:26:12 +0300498 def load_data_from_file(cfg, ctx):
koder aka kdanilov66839a92015-04-11 13:22:31 +0300499 raw_results = os.path.join(var_dir, 'raw_results.yaml')
500 ctx.results = yaml.load(open(raw_results).read())
koder aka kdanilov4e9f3ed2015-04-14 11:26:12 +0300501 return load_data_from_file
gstepanovcd256d62015-04-07 17:47:32 +0300502
503
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300504def start_web_ui(cfg, ctx):
505 if webui is None:
506 logger.error("Can't start webui. Install cherrypy module")
507 ctx.web_thread = None
508 else:
509 th = threading.Thread(None, webui.web_main_thread, "webui", (None,))
510 th.daemon = True
511 th.start()
512 ctx.web_thread = th
513
514
515def stop_web_ui(cfg, ctx):
516 webui.web_main_stop()
517 time.sleep(1)
518
519
koder aka kdanilovcee43342015-04-14 22:52:53 +0300520def parse_args(argv):
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300521 descr = "Disk io performance test suite"
522 parser = argparse.ArgumentParser(prog='wally', description=descr)
koder aka kdanilovcee43342015-04-14 22:52:53 +0300523
524 parser.add_argument("-l", dest='extra_logs',
525 action='store_true', default=False,
526 help="print some extra log info")
koder aka kdanilovcee43342015-04-14 22:52:53 +0300527 parser.add_argument("-b", '--build_description',
528 type=str, default="Build info")
529 parser.add_argument("-i", '--build_id', type=str, default="id")
530 parser.add_argument("-t", '--build_type', type=str, default="GA")
531 parser.add_argument("-u", '--username', type=str, default="admin")
koder aka kdanilove87ae652015-04-20 02:14:35 +0300532 parser.add_argument("-n", '--no-tests', action='store_true',
533 help="Don't run tests", default=False)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300534 parser.add_argument("-p", '--post-process-only', metavar="VAR_DIR",
535 help="Only process data from previour run")
536 parser.add_argument("-k", '--keep-vm', action='store_true',
537 help="Don't remove test vm's", default=False)
koder aka kdanilove87ae652015-04-20 02:14:35 +0300538 parser.add_argument("-d", '--dont-discover-nodes', action='store_true',
539 help="Don't connect/discover fuel nodes",
540 default=False)
koder aka kdanilova047e1b2015-04-21 23:16:59 +0300541 parser.add_argument("-r", '--no-html-report', action='store_true',
542 help="Skip html report", default=False)
koder aka kdanilov63ad2062015-04-27 13:11:40 +0300543 parser.add_argument("--params", nargs="*", metavar="testname.paramname",
544 help="Test params", default=[])
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300545 parser.add_argument("config_file")
koder aka kdanilovcee43342015-04-14 22:52:53 +0300546
547 return parser.parse_args(argv[1:])
548
549
koder aka kdanilov3f356262015-02-13 08:06:14 -0800550def main(argv):
koder aka kdanilove06762a2015-03-22 23:32:09 +0200551 opts = parse_args(argv)
koder aka kdanilov2c473092015-03-29 17:12:13 +0300552
koder aka kdanilov66839a92015-04-11 13:22:31 +0300553 if opts.post_process_only is not None:
554 stages = [
koder aka kdanilove87ae652015-04-20 02:14:35 +0300555 load_data_from(opts.post_process_only)
koder aka kdanilov66839a92015-04-11 13:22:31 +0300556 ]
557 else:
558 stages = [
559 discover_stage,
560 log_nodes_statistic,
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300561 connect_stage,
koder aka kdanilov66839a92015-04-11 13:22:31 +0300562 deploy_sensors_stage,
563 run_tests_stage,
koder aka kdanilove87ae652015-04-20 02:14:35 +0300564 store_raw_results_stage
koder aka kdanilov66839a92015-04-11 13:22:31 +0300565 ]
566
koder aka kdanilove87ae652015-04-20 02:14:35 +0300567 report_stages = [
568 console_report_stage,
koder aka kdanilove87ae652015-04-20 02:14:35 +0300569 ]
570
koder aka kdanilova047e1b2015-04-21 23:16:59 +0300571 if not opts.no_html_report:
572 report_stages.append(html_report_stage)
573
koder aka kdanilovcee43342015-04-14 22:52:53 +0300574 load_config(opts.config_file, opts.post_process_only)
koder aka kdanilovf4b82c22015-04-11 13:35:25 +0300575
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300576 if cfg_dict.get('logging', {}).get("extra_logs", False) or opts.extra_logs:
577 level = logging.DEBUG
578 else:
579 level = logging.WARNING
580
581 setup_loggers(level, cfg_dict['log_file'])
koder aka kdanilovf4b82c22015-04-11 13:35:25 +0300582
koder aka kdanilov652cd802015-04-13 12:21:07 +0300583 logger.info("All info would be stored into {0}".format(
584 cfg_dict['var_dir']))
gstepanovcd256d62015-04-07 17:47:32 +0300585
koder aka kdanilovda45e882015-04-06 02:24:42 +0300586 ctx = Context()
gstepanovaffcdb12015-04-07 17:18:29 +0300587 ctx.build_meta['build_id'] = opts.build_id
588 ctx.build_meta['build_descrption'] = opts.build_description
589 ctx.build_meta['build_type'] = opts.build_type
590 ctx.build_meta['username'] = opts.username
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300591 ctx.sensors_data = SensorDatastore()
koder aka kdanilove87ae652015-04-20 02:14:35 +0300592
koder aka kdanilov168f6092015-04-19 02:33:38 +0300593 cfg_dict['keep_vm'] = opts.keep_vm
koder aka kdanilove87ae652015-04-20 02:14:35 +0300594 cfg_dict['no_tests'] = opts.no_tests
595 cfg_dict['dont_discover_nodes'] = opts.dont_discover_nodes
koder aka kdanilov6c491062015-04-09 22:33:13 +0300596
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300597 if cfg_dict.get('run_web_ui', False):
598 start_web_ui(cfg_dict, ctx)
599
koder aka kdanilovc368eb62015-04-28 18:22:01 +0300600 msg_templ = "Exception during {0.__name__}: {1!s}"
601 msg_templ_no_exc = "During {0.__name__}"
602
koder aka kdanilovda45e882015-04-06 02:24:42 +0300603 try:
604 for stage in stages:
605 logger.info("Start {0.__name__} stage".format(stage))
606 stage(cfg_dict, ctx)
koder aka kdanilovc368eb62015-04-28 18:22:01 +0300607 except utils.StopTestError as exc:
608 logger.error(msg_templ.format(stage, exc))
609 except Exception:
610 logger.exception(msg_templ_no_exc.format(stage))
koder aka kdanilovda45e882015-04-06 02:24:42 +0300611 finally:
612 exc, cls, tb = sys.exc_info()
613 for stage in ctx.clear_calls_stack[::-1]:
614 try:
615 logger.info("Start {0.__name__} stage".format(stage))
616 stage(cfg_dict, ctx)
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300617 except utils.StopTestError as exc:
koder aka kdanilovc368eb62015-04-28 18:22:01 +0300618 logger.error(msg_templ.format(stage, exc))
619 except Exception:
620 logger.exception(msg_templ_no_exc.format(stage))
koder aka kdanilov2c473092015-03-29 17:12:13 +0300621
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300622 if exc is None:
623 for report_stage in report_stages:
624 report_stage(cfg_dict, ctx)
koder aka kdanilove87ae652015-04-20 02:14:35 +0300625
626 logger.info("All info stored in {0} folder".format(cfg_dict['var_dir']))
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300627
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300628 if cfg_dict.get('run_web_ui', False):
629 stop_web_ui(cfg_dict, ctx)
630
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300631 if exc is None:
632 logger.info("Tests finished successfully")
633 return 0
634 else:
635 logger.error("Tests are failed. See detailed error above")
636 return 1