blob: 9cea103c90c3daf196503dce7838a52ac9e1597d [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
20from wally.discover import discover, Node, undiscover
21from wally import utils, report, ssh_utils, start_vms
22from wally.suits.itest import IOPerfTest, PgBenchTest
23from wally.config import cfg_dict, load_config, setup_loggers
koder aka kdanilov57ce4db2015-04-25 21:25:51 +030024from wally.sensors_utils import deploy_sensors_stage, SensorDatastore
25
26try:
27 from wally import webui
28except ImportError:
29 webui = None
koder aka kdanilov2c473092015-03-29 17:12:13 +030030
31
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030032logger = logging.getLogger("wally")
koder aka kdanilovcee43342015-04-14 22:52:53 +030033
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080034
Yulia Portnova7ddfa732015-02-24 17:32:58 +020035def format_result(res, formatter):
koder aka kdanilove21d7472015-02-14 19:02:04 -080036 data = "\n{0}\n".format("=" * 80)
37 data += pprint.pformat(res) + "\n"
38 data += "{0}\n".format("=" * 80)
koder aka kdanilovfe056622015-02-19 08:46:15 -080039 templ = "{0}\n\n====> {1}\n\n{2}\n\n"
Yulia Portnova7ddfa732015-02-24 17:32:58 +020040 return templ.format(data, formatter(res), "=" * 80)
koder aka kdanilove21d7472015-02-14 19:02:04 -080041
42
koder aka kdanilov1c2b5112015-04-10 16:53:51 +030043class Context(object):
44 def __init__(self):
45 self.build_meta = {}
46 self.nodes = []
47 self.clear_calls_stack = []
48 self.openstack_nodes_ids = []
koder aka kdanilov168f6092015-04-19 02:33:38 +030049 self.sensors_mon_q = None
koder aka kdanilov1c2b5112015-04-10 16:53:51 +030050
51
koder aka kdanilov168f6092015-04-19 02:33:38 +030052def connect_one(node, vm=False):
koder aka kdanilov0c598a12015-04-21 03:01:40 +030053 if node.conn_url == 'local':
54 node.connection = ssh_utils.connect(node.conn_url)
55 return
56
koder aka kdanilov5d589b42015-03-26 12:25:51 +020057 try:
koder aka kdanilov2c473092015-03-29 17:12:13 +030058 ssh_pref = "ssh://"
59 if node.conn_url.startswith(ssh_pref):
60 url = node.conn_url[len(ssh_pref):]
koder aka kdanilov168f6092015-04-19 02:33:38 +030061
62 if vm:
koder aka kdanilov6b1341a2015-04-21 22:44:21 +030063 conn_timeout = 240
koder aka kdanilov168f6092015-04-19 02:33:38 +030064 else:
koder aka kdanilov6b1341a2015-04-21 22:44:21 +030065 conn_timeout = 30
koder aka kdanilov168f6092015-04-19 02:33:38 +030066
67 node.connection = ssh_utils.connect(url,
koder aka kdanilov6b1341a2015-04-21 22:44:21 +030068 conn_timeout=conn_timeout)
koder aka kdanilov2c473092015-03-29 17:12:13 +030069 else:
70 raise ValueError("Unknown url type {0}".format(node.conn_url))
koder aka kdanilove87ae652015-04-20 02:14:35 +030071 except Exception as exc:
72 # logger.exception("During connect to " + node.get_conn_id())
koder aka kdanilovec1b9732015-04-23 20:43:29 +030073 msg = "During connect to {0}: {1!s}".format(node.get_conn_id(),
74 exc)
koder aka kdanilove87ae652015-04-20 02:14:35 +030075 logger.error(msg)
koder aka kdanilov168f6092015-04-19 02:33:38 +030076 node.connection = None
koder aka kdanilov5d589b42015-03-26 12:25:51 +020077
78
koder aka kdanilov168f6092015-04-19 02:33:38 +030079def connect_all(nodes, vm=False):
koder aka kdanilov2c473092015-03-29 17:12:13 +030080 logger.info("Connecting to nodes")
81 with ThreadPoolExecutor(32) as pool:
koder aka kdanilov168f6092015-04-19 02:33:38 +030082 connect_one_f = functools.partial(connect_one, vm=vm)
83 list(pool.map(connect_one_f, nodes))
koder aka kdanilov2c473092015-03-29 17:12:13 +030084
85
koder aka kdanilov652cd802015-04-13 12:21:07 +030086def test_thread(test, node, barrier, res_q):
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030087 exc = None
koder aka kdanilov2c473092015-03-29 17:12:13 +030088 try:
koder aka kdanilova047e1b2015-04-21 23:16:59 +030089 logger.debug("Run preparation for {0}".format(node.get_conn_id()))
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030090 test.pre_run()
koder aka kdanilova047e1b2015-04-21 23:16:59 +030091 logger.debug("Run test for {0}".format(node.get_conn_id()))
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030092 test.run(barrier)
koder aka kdanilove2de58c2015-04-24 22:59:36 +030093 except utils.StopTestError as exc:
94 pass
koder aka kdanilov652cd802015-04-13 12:21:07 +030095 except Exception as exc:
koder aka kdanilove2de58c2015-04-24 22:59:36 +030096 msg = "In test {0} for node {1}"
97 msg = msg.format(test, node.get_conn_id())
98 logger.exception(msg)
99 exc = utils.StopTestError(msg, exc)
koder aka kdanilov2c473092015-03-29 17:12:13 +0300100
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300101 try:
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300102 test.cleanup()
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300103 except utils.StopTestError as exc1:
104 if exc is None:
105 exc = exc1
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300106 except:
107 msg = "Duringf cleanup - in test {0} for node {1}"
108 logger.exception(msg.format(test, node))
109
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300110 if exc is not None:
111 res_q.put(exc)
koder aka kdanilov2c473092015-03-29 17:12:13 +0300112
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300113
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300114def run_tests(cfg, test_block, nodes):
koder aka kdanilov2c473092015-03-29 17:12:13 +0300115 tool_type_mapper = {
116 "io": IOPerfTest,
117 "pgbench": PgBenchTest,
118 }
119
120 test_nodes = [node for node in nodes
121 if 'testnode' in node.roles]
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300122 test_number_per_type = {}
koder aka kdanilov2c473092015-03-29 17:12:13 +0300123 res_q = Queue.Queue()
124
koder aka kdanilovcee43342015-04-14 22:52:53 +0300125 for name, params in test_block.items():
126 logger.info("Starting {0} tests".format(name))
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300127 test_num = test_number_per_type.get(name, 0)
128 test_number_per_type[name] = test_num + 1
koder aka kdanilovcee43342015-04-14 22:52:53 +0300129 threads = []
130 barrier = utils.Barrier(len(test_nodes))
koder aka kdanilovec1b9732015-04-23 20:43:29 +0300131 coord_q = Queue.Queue()
132 test_cls = tool_type_mapper[name]
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300133 rem_folder = cfg['default_test_local_folder'].format(name=name)
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300134
koder aka kdanilovabd6ead2015-04-24 02:03:07 +0300135 for idx, node in enumerate(test_nodes):
koder aka kdanilovcee43342015-04-14 22:52:53 +0300136 msg = "Starting {0} test on {1} node"
137 logger.debug(msg.format(name, node.conn_url))
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300138
139 dr = os.path.join(
140 cfg_dict['test_log_directory'],
141 "{0}_{1}_{2}".format(name, test_num, node.get_ip())
142 )
143
144 if not os.path.exists(dr):
145 os.makedirs(dr)
146
koder aka kdanilovabd6ead2015-04-24 02:03:07 +0300147 test = test_cls(options=params,
148 is_primary=(idx == 0),
149 on_result_cb=res_q.put,
150 test_uuid=cfg['run_uuid'],
151 node=node,
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300152 remote_dir=rem_folder,
koder aka kdanilovec1b9732015-04-23 20:43:29 +0300153 log_directory=dr,
154 coordination_queue=coord_q)
koder aka kdanilovcee43342015-04-14 22:52:53 +0300155 th = threading.Thread(None, test_thread, None,
156 (test, node, barrier, res_q))
157 threads.append(th)
158 th.daemon = True
159 th.start()
koder aka kdanilov2c473092015-03-29 17:12:13 +0300160
koder aka kdanilovec1b9732015-04-23 20:43:29 +0300161 th = threading.Thread(None, test_cls.coordination_th, None,
162 (coord_q, barrier, len(threads)))
163 threads.append(th)
164 th.daemon = True
165 th.start()
166
koder aka kdanilovcee43342015-04-14 22:52:53 +0300167 def gather_results(res_q, results):
168 while not res_q.empty():
169 val = res_q.get()
koder aka kdanilov66839a92015-04-11 13:22:31 +0300170
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300171 if isinstance(val, utils.StopTestError):
172 raise val
173
koder aka kdanilovcee43342015-04-14 22:52:53 +0300174 if isinstance(val, Exception):
koder aka kdanilovec1b9732015-04-23 20:43:29 +0300175 msg = "Exception during test execution: {0!s}"
176 raise ValueError(msg.format(val))
koder aka kdanilov652cd802015-04-13 12:21:07 +0300177
koder aka kdanilovcee43342015-04-14 22:52:53 +0300178 results.append(val)
koder aka kdanilov652cd802015-04-13 12:21:07 +0300179
koder aka kdanilovcee43342015-04-14 22:52:53 +0300180 results = []
koder aka kdanilov652cd802015-04-13 12:21:07 +0300181
koder aka kdanilove87ae652015-04-20 02:14:35 +0300182 # MAX_WAIT_TIME = 10
183 # end_time = time.time() + MAX_WAIT_TIME
184
185 # while time.time() < end_time:
koder aka kdanilovcee43342015-04-14 22:52:53 +0300186 while True:
187 for th in threads:
188 th.join(1)
189 gather_results(res_q, results)
koder aka kdanilove87ae652015-04-20 02:14:35 +0300190 # if time.time() > end_time:
191 # break
koder aka kdanilov652cd802015-04-13 12:21:07 +0300192
koder aka kdanilovcee43342015-04-14 22:52:53 +0300193 if all(not th.is_alive() for th in threads):
194 break
koder aka kdanilov652cd802015-04-13 12:21:07 +0300195
koder aka kdanilove87ae652015-04-20 02:14:35 +0300196 # if any(th.is_alive() for th in threads):
197 # logger.warning("Some test threads still running")
198
koder aka kdanilovcee43342015-04-14 22:52:53 +0300199 gather_results(res_q, results)
200 yield name, test.merge_results(results)
koder aka kdanilov2c473092015-03-29 17:12:13 +0300201
202
koder aka kdanilovda45e882015-04-06 02:24:42 +0300203def log_nodes_statistic(_, ctx):
204 nodes = ctx.nodes
koder aka kdanilov2c473092015-03-29 17:12:13 +0300205 logger.info("Found {0} nodes total".format(len(nodes)))
206 per_role = collections.defaultdict(lambda: 0)
207 for node in nodes:
208 for role in node.roles:
209 per_role[role] += 1
210
211 for role, count in sorted(per_role.items()):
212 logger.debug("Found {0} nodes with role {1}".format(count, role))
213
214
koder aka kdanilovda45e882015-04-06 02:24:42 +0300215def connect_stage(cfg, ctx):
216 ctx.clear_calls_stack.append(disconnect_stage)
217 connect_all(ctx.nodes)
218
koder aka kdanilov168f6092015-04-19 02:33:38 +0300219 all_ok = True
koder aka kdanilovda45e882015-04-06 02:24:42 +0300220
koder aka kdanilov168f6092015-04-19 02:33:38 +0300221 for node in ctx.nodes:
222 if node.connection is None:
223 if 'testnode' in node.roles:
224 msg = "Can't connect to testnode {0}"
225 raise RuntimeError(msg.format(node.get_conn_id()))
226 else:
227 msg = "Node {0} would be excluded - can't connect"
228 logger.warning(msg.format(node.get_conn_id()))
229 all_ok = False
230
231 if all_ok:
232 logger.info("All nodes connected successfully")
233
234 ctx.nodes = [node for node in ctx.nodes
235 if node.connection is not None]
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300236
237
koder aka kdanilovda45e882015-04-06 02:24:42 +0300238def discover_stage(cfg, ctx):
koder aka kdanilov652cd802015-04-13 12:21:07 +0300239 if cfg.get('discover') is not None:
koder aka kdanilovda45e882015-04-06 02:24:42 +0300240 discover_objs = [i.strip() for i in cfg['discover'].strip().split(",")]
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300241
koder aka kdanilove87ae652015-04-20 02:14:35 +0300242 nodes, clean_data = discover(ctx,
243 discover_objs,
244 cfg['clouds'],
245 cfg['var_dir'],
246 not cfg['dont_discover_nodes'])
koder aka kdanilov168f6092015-04-19 02:33:38 +0300247
248 def undiscover_stage(cfg, ctx):
249 undiscover(clean_data)
250
251 ctx.clear_calls_stack.append(undiscover_stage)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300252 ctx.nodes.extend(nodes)
koder aka kdanilovda45e882015-04-06 02:24:42 +0300253
254 for url, roles in cfg.get('explicit_nodes', {}).items():
255 ctx.nodes.append(Node(url, roles.split(",")))
256
257
koder aka kdanilove87ae652015-04-20 02:14:35 +0300258def get_OS_credentials(cfg, ctx, creds_type):
koder aka kdanilovcee43342015-04-14 22:52:53 +0300259 creds = None
koder aka kdanilovda45e882015-04-06 02:24:42 +0300260
koder aka kdanilovcee43342015-04-14 22:52:53 +0300261 if creds_type == 'clouds':
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300262 logger.info("Using OS credentials from 'cloud' section")
koder aka kdanilovcee43342015-04-14 22:52:53 +0300263 if 'openstack' in cfg['clouds']:
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300264 os_cfg = cfg['clouds']['openstack']
koder aka kdanilovcee43342015-04-14 22:52:53 +0300265
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300266 tenant = os_cfg['OS_TENANT_NAME'].strip()
267 user = os_cfg['OS_USERNAME'].strip()
268 passwd = os_cfg['OS_PASSWORD'].strip()
269 auth_url = os_cfg['OS_AUTH_URL'].strip()
270
koder aka kdanilovcee43342015-04-14 22:52:53 +0300271 elif 'fuel' in cfg['clouds'] and \
272 'openstack_env' in cfg['clouds']['fuel']:
273 creds = ctx.fuel_openstack_creds
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300274
koder aka kdanilovcee43342015-04-14 22:52:53 +0300275 elif creds_type == 'ENV':
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300276 logger.info("Using OS credentials from shell environment")
koder aka kdanilovcee43342015-04-14 22:52:53 +0300277 user, passwd, tenant, auth_url = start_vms.ostack_get_creds()
278 elif os.path.isfile(creds_type):
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300279 logger.info("Using OS credentials from " + creds_type)
koder aka kdanilov7306c642015-04-23 15:29:45 +0300280 fc = open(creds_type).read()
281
282 echo = 'echo "$OS_TENANT_NAME:$OS_USERNAME:$OS_PASSWORD@$OS_AUTH_URL"'
283
284 p = subprocess.Popen(['/bin/bash'], shell=False,
285 stdout=subprocess.PIPE,
286 stdin=subprocess.PIPE,
287 stderr=subprocess.STDOUT)
288 p.stdin.write(fc + "\n" + echo)
289 p.stdin.close()
290 code = p.wait()
291 data = p.stdout.read().strip()
292
293 if code != 0:
294 msg = "Failed to get creads from openrc file: " + data
295 logger.error(msg)
296 raise RuntimeError(msg)
297
298 try:
299 user, tenant, passwd_auth_url = data.split(':', 2)
300 passwd, auth_url = passwd_auth_url.rsplit("@", 1)
301 assert (auth_url.startswith("https://") or
302 auth_url.startswith("http://"))
303 except Exception:
304 msg = "Failed to get creads from openrc file: " + data
305 logger.exception(msg)
306 raise
307
koder aka kdanilovcee43342015-04-14 22:52:53 +0300308 else:
309 msg = "Creds {0!r} isn't supported".format(creds_type)
310 raise ValueError(msg)
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300311
koder aka kdanilovcee43342015-04-14 22:52:53 +0300312 if creds is None:
313 creds = {'name': user,
314 'passwd': passwd,
315 'tenant': tenant,
316 'auth_url': auth_url}
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300317
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300318 msg = "OS_CREDS: user={name} tenant={tenant} auth_url={auth_url}"
319 logger.debug(msg.format(**creds))
koder aka kdanilovcee43342015-04-14 22:52:53 +0300320 return creds
koder aka kdanilov4e9f3ed2015-04-14 11:26:12 +0300321
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300322
koder aka kdanilov168f6092015-04-19 02:33:38 +0300323@contextlib.contextmanager
324def create_vms_ctx(ctx, cfg, config):
325 params = config['vm_params'].copy()
326 os_nodes_ids = []
327
328 os_creds_type = config['creds']
koder aka kdanilove87ae652015-04-20 02:14:35 +0300329 os_creds = get_OS_credentials(cfg, ctx, os_creds_type)
koder aka kdanilov168f6092015-04-19 02:33:38 +0300330
331 start_vms.nova_connect(**os_creds)
332
333 logger.info("Preparing openstack")
334 start_vms.prepare_os_subpr(**os_creds)
335
336 new_nodes = []
337 try:
338 params['group_name'] = cfg_dict['run_uuid']
339 for new_node, node_id in start_vms.launch_vms(params):
340 new_node.roles.append('testnode')
341 ctx.nodes.append(new_node)
342 os_nodes_ids.append(node_id)
343 new_nodes.append(new_node)
344
345 store_nodes_in_log(cfg, os_nodes_ids)
346 ctx.openstack_nodes_ids = os_nodes_ids
347
348 yield new_nodes
349
350 finally:
351 if not cfg['keep_vm']:
352 shut_down_vms_stage(cfg, ctx)
353
354
koder aka kdanilovcee43342015-04-14 22:52:53 +0300355def run_tests_stage(cfg, ctx):
356 ctx.results = []
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300357
koder aka kdanilovcee43342015-04-14 22:52:53 +0300358 if 'tests' not in cfg:
359 return
gstepanov023c1e42015-04-08 15:50:19 +0300360
koder aka kdanilovcee43342015-04-14 22:52:53 +0300361 for group in cfg['tests']:
362
363 assert len(group.items()) == 1
364 key, config = group.items()[0]
365
366 if 'start_test_nodes' == key:
koder aka kdanilov168f6092015-04-19 02:33:38 +0300367 with create_vms_ctx(ctx, cfg, config) as new_nodes:
368 connect_all(new_nodes, True)
koder aka kdanilovcee43342015-04-14 22:52:53 +0300369
koder aka kdanilov168f6092015-04-19 02:33:38 +0300370 for node in new_nodes:
371 if node.connection is None:
372 msg = "Failed to connect to vm {0}"
373 raise RuntimeError(msg.format(node.get_conn_id()))
koder aka kdanilovcee43342015-04-14 22:52:53 +0300374
koder aka kdanilov168f6092015-04-19 02:33:38 +0300375 deploy_sensors_stage(cfg_dict,
376 ctx,
377 nodes=new_nodes,
378 undeploy=False)
koder aka kdanilov12ae0632015-04-15 01:13:43 +0300379
koder aka kdanilove87ae652015-04-20 02:14:35 +0300380 if not cfg['no_tests']:
381 for test_group in config.get('tests', []):
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300382 test_res = run_tests(cfg, test_group, ctx.nodes)
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300383 ctx.results.extend(test_res)
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300384 else:
koder aka kdanilove87ae652015-04-20 02:14:35 +0300385 if not cfg['no_tests']:
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300386 test_res = run_tests(cfg, group, ctx.nodes)
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300387 ctx.results.extend(test_res)
koder aka kdanilovda45e882015-04-06 02:24:42 +0300388
gstepanov023c1e42015-04-08 15:50:19 +0300389
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300390def shut_down_vms_stage(cfg, ctx):
koder aka kdanilov66839a92015-04-11 13:22:31 +0300391 vm_ids_fname = cfg_dict['vm_ids_fname']
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300392 if ctx.openstack_nodes_ids is None:
koder aka kdanilov66839a92015-04-11 13:22:31 +0300393 nodes_ids = open(vm_ids_fname).read().split()
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300394 else:
395 nodes_ids = ctx.openstack_nodes_ids
396
koder aka kdanilov652cd802015-04-13 12:21:07 +0300397 if len(nodes_ids) != 0:
398 logger.info("Removing nodes")
399 start_vms.clear_nodes(nodes_ids)
400 logger.info("Nodes has been removed")
gstepanov023c1e42015-04-08 15:50:19 +0300401
koder aka kdanilov66839a92015-04-11 13:22:31 +0300402 if os.path.exists(vm_ids_fname):
403 os.remove(vm_ids_fname)
gstepanov023c1e42015-04-08 15:50:19 +0300404
koder aka kdanilov66839a92015-04-11 13:22:31 +0300405
406def store_nodes_in_log(cfg, nodes_ids):
407 with open(cfg['vm_ids_fname'], 'w') as fd:
408 fd.write("\n".join(nodes_ids))
gstepanov023c1e42015-04-08 15:50:19 +0300409
410
411def clear_enviroment(cfg, ctx):
koder aka kdanilov66839a92015-04-11 13:22:31 +0300412 if os.path.exists(cfg_dict['vm_ids_fname']):
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300413 shut_down_vms_stage(cfg, ctx)
gstepanov023c1e42015-04-08 15:50:19 +0300414
415
koder aka kdanilovda45e882015-04-06 02:24:42 +0300416def disconnect_stage(cfg, ctx):
koder aka kdanilov652cd802015-04-13 12:21:07 +0300417 ssh_utils.close_all_sessions()
418
koder aka kdanilovda45e882015-04-06 02:24:42 +0300419 for node in ctx.nodes:
420 if node.connection is not None:
421 node.connection.close()
422
423
koder aka kdanilov66839a92015-04-11 13:22:31 +0300424def store_raw_results_stage(cfg, ctx):
425
426 raw_results = os.path.join(cfg_dict['var_dir'], 'raw_results.yaml')
427
428 if os.path.exists(raw_results):
429 cont = yaml.load(open(raw_results).read())
430 else:
431 cont = []
432
koder aka kdanilov168f6092015-04-19 02:33:38 +0300433 cont.extend(utils.yamable(ctx.results))
koder aka kdanilov66839a92015-04-11 13:22:31 +0300434 raw_data = pretty_yaml.dumps(cont)
435
436 with open(raw_results, "w") as fd:
437 fd.write(raw_data)
438
439
440def console_report_stage(cfg, ctx):
441 for tp, data in ctx.results:
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300442 if 'io' == tp and data is not None:
koder aka kdanilove87ae652015-04-20 02:14:35 +0300443 print("\n")
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300444 print(IOPerfTest.format_for_console(data))
koder aka kdanilove87ae652015-04-20 02:14:35 +0300445 print("\n")
koder aka kdanilov66839a92015-04-11 13:22:31 +0300446
447
koder aka kdanilove87ae652015-04-20 02:14:35 +0300448def html_report_stage(cfg, ctx):
Yulia Portnova8ca20572015-04-14 14:09:39 +0300449 html_rep_fname = cfg['html_report_file']
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300450
451 try:
452 fuel_url = cfg['clouds']['fuel']['url']
453 except KeyError:
454 fuel_url = None
455
456 try:
457 creds = cfg['clouds']['fuel']['creds']
458 except KeyError:
459 creds = None
460
gstepanov69339ac2015-04-16 20:09:33 +0300461 report.make_io_report(ctx.results, html_rep_fname, fuel_url, creds=creds)
koder aka kdanilov652cd802015-04-13 12:21:07 +0300462
koder aka kdanilov652cd802015-04-13 12:21:07 +0300463 text_rep_fname = cfg_dict['text_report_file']
464 with open(text_rep_fname, "w") as fd:
465 for tp, data in ctx.results:
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300466 if 'io' == tp and data is not None:
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300467 fd.write(IOPerfTest.format_for_console(data))
koder aka kdanilov652cd802015-04-13 12:21:07 +0300468 fd.write("\n")
469 fd.flush()
470
471 logger.info("Text report were stored in " + text_rep_fname)
koder aka kdanilovda45e882015-04-06 02:24:42 +0300472
473
474def complete_log_nodes_statistic(cfg, ctx):
475 nodes = ctx.nodes
476 for node in nodes:
477 logger.debug(str(node))
478
479
koder aka kdanilov66839a92015-04-11 13:22:31 +0300480def load_data_from(var_dir):
koder aka kdanilov4e9f3ed2015-04-14 11:26:12 +0300481 def load_data_from_file(cfg, ctx):
koder aka kdanilov66839a92015-04-11 13:22:31 +0300482 raw_results = os.path.join(var_dir, 'raw_results.yaml')
483 ctx.results = yaml.load(open(raw_results).read())
koder aka kdanilov4e9f3ed2015-04-14 11:26:12 +0300484 return load_data_from_file
gstepanovcd256d62015-04-07 17:47:32 +0300485
486
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300487def start_web_ui(cfg, ctx):
488 if webui is None:
489 logger.error("Can't start webui. Install cherrypy module")
490 ctx.web_thread = None
491 else:
492 th = threading.Thread(None, webui.web_main_thread, "webui", (None,))
493 th.daemon = True
494 th.start()
495 ctx.web_thread = th
496
497
498def stop_web_ui(cfg, ctx):
499 webui.web_main_stop()
500 time.sleep(1)
501
502
koder aka kdanilovcee43342015-04-14 22:52:53 +0300503def parse_args(argv):
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300504 descr = "Disk io performance test suite"
505 parser = argparse.ArgumentParser(prog='wally', description=descr)
koder aka kdanilovcee43342015-04-14 22:52:53 +0300506
507 parser.add_argument("-l", dest='extra_logs',
508 action='store_true', default=False,
509 help="print some extra log info")
koder aka kdanilovcee43342015-04-14 22:52:53 +0300510 parser.add_argument("-b", '--build_description',
511 type=str, default="Build info")
512 parser.add_argument("-i", '--build_id', type=str, default="id")
513 parser.add_argument("-t", '--build_type', type=str, default="GA")
514 parser.add_argument("-u", '--username', type=str, default="admin")
koder aka kdanilove87ae652015-04-20 02:14:35 +0300515 parser.add_argument("-n", '--no-tests', action='store_true',
516 help="Don't run tests", default=False)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300517 parser.add_argument("-p", '--post-process-only', metavar="VAR_DIR",
518 help="Only process data from previour run")
519 parser.add_argument("-k", '--keep-vm', action='store_true',
520 help="Don't remove test vm's", default=False)
koder aka kdanilove87ae652015-04-20 02:14:35 +0300521 parser.add_argument("-d", '--dont-discover-nodes', action='store_true',
522 help="Don't connect/discover fuel nodes",
523 default=False)
koder aka kdanilova047e1b2015-04-21 23:16:59 +0300524 parser.add_argument("-r", '--no-html-report', action='store_true',
525 help="Skip html report", default=False)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300526 parser.add_argument("config_file")
koder aka kdanilovcee43342015-04-14 22:52:53 +0300527
528 return parser.parse_args(argv[1:])
529
530
koder aka kdanilov3f356262015-02-13 08:06:14 -0800531def main(argv):
koder aka kdanilove06762a2015-03-22 23:32:09 +0200532 opts = parse_args(argv)
koder aka kdanilov2c473092015-03-29 17:12:13 +0300533
koder aka kdanilov66839a92015-04-11 13:22:31 +0300534 if opts.post_process_only is not None:
535 stages = [
koder aka kdanilove87ae652015-04-20 02:14:35 +0300536 load_data_from(opts.post_process_only)
koder aka kdanilov66839a92015-04-11 13:22:31 +0300537 ]
538 else:
539 stages = [
540 discover_stage,
541 log_nodes_statistic,
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300542 connect_stage,
koder aka kdanilov66839a92015-04-11 13:22:31 +0300543 deploy_sensors_stage,
544 run_tests_stage,
koder aka kdanilove87ae652015-04-20 02:14:35 +0300545 store_raw_results_stage
koder aka kdanilov66839a92015-04-11 13:22:31 +0300546 ]
547
koder aka kdanilove87ae652015-04-20 02:14:35 +0300548 report_stages = [
549 console_report_stage,
koder aka kdanilove87ae652015-04-20 02:14:35 +0300550 ]
551
koder aka kdanilova047e1b2015-04-21 23:16:59 +0300552 if not opts.no_html_report:
553 report_stages.append(html_report_stage)
554
koder aka kdanilovcee43342015-04-14 22:52:53 +0300555 load_config(opts.config_file, opts.post_process_only)
koder aka kdanilovf4b82c22015-04-11 13:35:25 +0300556
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300557 if cfg_dict.get('logging', {}).get("extra_logs", False) or opts.extra_logs:
558 level = logging.DEBUG
559 else:
560 level = logging.WARNING
561
562 setup_loggers(level, cfg_dict['log_file'])
koder aka kdanilovf4b82c22015-04-11 13:35:25 +0300563
koder aka kdanilov652cd802015-04-13 12:21:07 +0300564 logger.info("All info would be stored into {0}".format(
565 cfg_dict['var_dir']))
gstepanovcd256d62015-04-07 17:47:32 +0300566
koder aka kdanilovda45e882015-04-06 02:24:42 +0300567 ctx = Context()
gstepanovaffcdb12015-04-07 17:18:29 +0300568 ctx.build_meta['build_id'] = opts.build_id
569 ctx.build_meta['build_descrption'] = opts.build_description
570 ctx.build_meta['build_type'] = opts.build_type
571 ctx.build_meta['username'] = opts.username
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300572 ctx.sensors_data = SensorDatastore()
koder aka kdanilove87ae652015-04-20 02:14:35 +0300573
koder aka kdanilov168f6092015-04-19 02:33:38 +0300574 cfg_dict['keep_vm'] = opts.keep_vm
koder aka kdanilove87ae652015-04-20 02:14:35 +0300575 cfg_dict['no_tests'] = opts.no_tests
576 cfg_dict['dont_discover_nodes'] = opts.dont_discover_nodes
koder aka kdanilov6c491062015-04-09 22:33:13 +0300577
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300578 if cfg_dict.get('run_web_ui', False):
579 start_web_ui(cfg_dict, ctx)
580
koder aka kdanilovda45e882015-04-06 02:24:42 +0300581 try:
582 for stage in stages:
583 logger.info("Start {0.__name__} stage".format(stage))
584 stage(cfg_dict, ctx)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300585 except Exception as exc:
koder aka kdanilovec1b9732015-04-23 20:43:29 +0300586 msg = "Exception during {0.__name__}: {1!s}".format(stage, exc)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300587 logger.error(msg)
koder aka kdanilovda45e882015-04-06 02:24:42 +0300588 finally:
589 exc, cls, tb = sys.exc_info()
590 for stage in ctx.clear_calls_stack[::-1]:
591 try:
592 logger.info("Start {0.__name__} stage".format(stage))
593 stage(cfg_dict, ctx)
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300594 except utils.StopTestError as exc:
595 msg = "During {0.__name__} stage: {1}".format(stage, exc)
596 logger.error(msg)
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300597 except Exception as exc:
598 logger.exception("During {0.__name__} stage".format(stage))
koder aka kdanilov2c473092015-03-29 17:12:13 +0300599
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300600 # if exc is not None:
601 # raise exc, cls, tb
koder aka kdanilov2c473092015-03-29 17:12:13 +0300602
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300603 if exc is None:
604 for report_stage in report_stages:
605 report_stage(cfg_dict, ctx)
koder aka kdanilove87ae652015-04-20 02:14:35 +0300606
607 logger.info("All info stored in {0} folder".format(cfg_dict['var_dir']))
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300608
koder aka kdanilov57ce4db2015-04-25 21:25:51 +0300609 if cfg_dict.get('run_web_ui', False):
610 stop_web_ui(cfg_dict, ctx)
611
koder aka kdanilove2de58c2015-04-24 22:59:36 +0300612 if exc is None:
613 logger.info("Tests finished successfully")
614 return 0
615 else:
616 logger.error("Tests are failed. See detailed error above")
617 return 1