koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 1 | from __future__ import print_function |
| 2 | |
gstepanov | 023c1e4 | 2015-04-08 15:50:19 +0300 | [diff] [blame] | 3 | import os |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 4 | import sys |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 5 | import time |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 6 | import Queue |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 7 | import pprint |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 8 | import signal |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 9 | import logging |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 10 | import argparse |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 11 | import functools |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 12 | import threading |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 13 | import contextlib |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 14 | import collections |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 15 | |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 16 | import yaml |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame^] | 17 | import texttable |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 18 | import faulthandler |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 19 | from concurrent.futures import ThreadPoolExecutor |
koder aka kdanilov | 6c49106 | 2015-04-09 22:33:13 +0300 | [diff] [blame] | 20 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 21 | from wally import pretty_yaml |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 22 | from wally.hw_info import get_hw_info |
| 23 | from wally.discover import discover, Node |
koder aka kdanilov | 63ad206 | 2015-04-27 13:11:40 +0300 | [diff] [blame] | 24 | from wally.timeseries import SensorDatastore |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 25 | from wally import utils, report, ssh_utils, start_vms |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 26 | from wally.suits import IOPerfTest, PgBenchTest, MysqlTest |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame^] | 27 | from wally.config import (cfg_dict, load_config, setup_loggers, |
| 28 | get_test_files) |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 29 | from wally.sensors_utils import with_sensors_util, sensors_info_util |
| 30 | |
| 31 | TOOL_TYPE_MAPPER = { |
| 32 | "io": IOPerfTest, |
| 33 | "pgbench": PgBenchTest, |
| 34 | "mysql": MysqlTest, |
| 35 | } |
koder aka kdanilov | 63ad206 | 2015-04-27 13:11:40 +0300 | [diff] [blame] | 36 | |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 37 | |
| 38 | try: |
| 39 | from wally import webui |
| 40 | except ImportError: |
| 41 | webui = None |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 42 | |
| 43 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 44 | logger = logging.getLogger("wally") |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 45 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 46 | |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 47 | def format_result(res, formatter): |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 48 | data = "\n{0}\n".format("=" * 80) |
| 49 | data += pprint.pformat(res) + "\n" |
| 50 | data += "{0}\n".format("=" * 80) |
koder aka kdanilov | fe05662 | 2015-02-19 08:46:15 -0800 | [diff] [blame] | 51 | templ = "{0}\n\n====> {1}\n\n{2}\n\n" |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 52 | return templ.format(data, formatter(res), "=" * 80) |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 53 | |
| 54 | |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 55 | class Context(object): |
| 56 | def __init__(self): |
| 57 | self.build_meta = {} |
| 58 | self.nodes = [] |
| 59 | self.clear_calls_stack = [] |
| 60 | self.openstack_nodes_ids = [] |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 61 | self.sensors_mon_q = None |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 62 | self.hw_info = [] |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 63 | |
| 64 | |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 65 | def connect_one(node, vm=False): |
koder aka kdanilov | 0c598a1 | 2015-04-21 03:01:40 +0300 | [diff] [blame] | 66 | if node.conn_url == 'local': |
| 67 | node.connection = ssh_utils.connect(node.conn_url) |
| 68 | return |
| 69 | |
koder aka kdanilov | 5d589b4 | 2015-03-26 12:25:51 +0200 | [diff] [blame] | 70 | try: |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 71 | ssh_pref = "ssh://" |
| 72 | if node.conn_url.startswith(ssh_pref): |
| 73 | url = node.conn_url[len(ssh_pref):] |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 74 | |
| 75 | if vm: |
koder aka kdanilov | 6b1341a | 2015-04-21 22:44:21 +0300 | [diff] [blame] | 76 | conn_timeout = 240 |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 77 | else: |
koder aka kdanilov | 6b1341a | 2015-04-21 22:44:21 +0300 | [diff] [blame] | 78 | conn_timeout = 30 |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 79 | |
| 80 | node.connection = ssh_utils.connect(url, |
koder aka kdanilov | 6b1341a | 2015-04-21 22:44:21 +0300 | [diff] [blame] | 81 | conn_timeout=conn_timeout) |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 82 | else: |
| 83 | raise ValueError("Unknown url type {0}".format(node.conn_url)) |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 84 | except Exception as exc: |
| 85 | # logger.exception("During connect to " + node.get_conn_id()) |
koder aka kdanilov | ec1b973 | 2015-04-23 20:43:29 +0300 | [diff] [blame] | 86 | msg = "During connect to {0}: {1!s}".format(node.get_conn_id(), |
| 87 | exc) |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 88 | logger.error(msg) |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 89 | node.connection = None |
koder aka kdanilov | 5d589b4 | 2015-03-26 12:25:51 +0200 | [diff] [blame] | 90 | |
| 91 | |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 92 | def connect_all(nodes, vm=False): |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 93 | logger.info("Connecting to nodes") |
| 94 | with ThreadPoolExecutor(32) as pool: |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 95 | connect_one_f = functools.partial(connect_one, vm=vm) |
| 96 | list(pool.map(connect_one_f, nodes)) |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 97 | |
| 98 | |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 99 | def collect_hw_info_stage(cfg, ctx): |
| 100 | if os.path.exists(cfg['hwreport_fname']): |
| 101 | msg = "{0} already exists. Skip hw info" |
| 102 | logger.info(msg.format(cfg['hwreport_fname'])) |
| 103 | return |
| 104 | |
| 105 | with ThreadPoolExecutor(32) as pool: |
| 106 | connections = (node.connection for node in ctx.nodes) |
| 107 | ctx.hw_info.extend(pool.map(get_hw_info, connections)) |
| 108 | |
| 109 | with open(cfg['hwreport_fname'], 'w') as hwfd: |
| 110 | for node, info in zip(ctx.nodes, ctx.hw_info): |
| 111 | hwfd.write("-" * 60 + "\n") |
| 112 | hwfd.write("Roles : " + ", ".join(node.roles) + "\n") |
| 113 | hwfd.write(str(info) + "\n") |
| 114 | hwfd.write("-" * 60 + "\n\n") |
| 115 | |
| 116 | if info.hostname is not None: |
| 117 | fname = os.path.join( |
| 118 | cfg_dict['hwinfo_directory'], |
| 119 | info.hostname + "_lshw.xml") |
| 120 | |
| 121 | with open(fname, "w") as fd: |
| 122 | fd.write(info.raw) |
| 123 | logger.info("Hardware report stored in " + cfg['hwreport_fname']) |
| 124 | logger.debug("Raw hardware info in " + cfg['hwinfo_directory'] + " folder") |
| 125 | |
| 126 | |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 127 | def test_thread(test, node, barrier, res_q): |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 128 | exc = None |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 129 | try: |
koder aka kdanilov | a047e1b | 2015-04-21 23:16:59 +0300 | [diff] [blame] | 130 | logger.debug("Run preparation for {0}".format(node.get_conn_id())) |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 131 | test.pre_run() |
koder aka kdanilov | a047e1b | 2015-04-21 23:16:59 +0300 | [diff] [blame] | 132 | logger.debug("Run test for {0}".format(node.get_conn_id())) |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 133 | test.run(barrier) |
koder aka kdanilov | e2de58c | 2015-04-24 22:59:36 +0300 | [diff] [blame] | 134 | except utils.StopTestError as exc: |
| 135 | pass |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 136 | except Exception as exc: |
koder aka kdanilov | e2de58c | 2015-04-24 22:59:36 +0300 | [diff] [blame] | 137 | msg = "In test {0} for node {1}" |
| 138 | msg = msg.format(test, node.get_conn_id()) |
| 139 | logger.exception(msg) |
| 140 | exc = utils.StopTestError(msg, exc) |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 141 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 142 | try: |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 143 | test.cleanup() |
koder aka kdanilov | e2de58c | 2015-04-24 22:59:36 +0300 | [diff] [blame] | 144 | except utils.StopTestError as exc1: |
| 145 | if exc is None: |
| 146 | exc = exc1 |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 147 | except: |
| 148 | msg = "Duringf cleanup - in test {0} for node {1}" |
| 149 | logger.exception(msg.format(test, node)) |
| 150 | |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 151 | if exc is not None: |
| 152 | res_q.put(exc) |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 153 | |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 154 | |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame^] | 155 | def run_single_test(test_nodes, name, test_cls, params, log_directory, |
| 156 | test_local_folder, run_uuid): |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 157 | logger.info("Starting {0} tests".format(name)) |
| 158 | res_q = Queue.Queue() |
| 159 | threads = [] |
| 160 | coord_q = Queue.Queue() |
| 161 | rem_folder = test_local_folder.format(name=name) |
| 162 | |
| 163 | barrier = utils.Barrier(len(test_nodes)) |
| 164 | for idx, node in enumerate(test_nodes): |
| 165 | msg = "Starting {0} test on {1} node" |
| 166 | logger.debug(msg.format(name, node.conn_url)) |
| 167 | |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 168 | params = params.copy() |
| 169 | params['testnodes_count'] = len(test_nodes) |
| 170 | test = test_cls(options=params, |
| 171 | is_primary=(idx == 0), |
| 172 | on_result_cb=res_q.put, |
| 173 | test_uuid=run_uuid, |
| 174 | node=node, |
| 175 | remote_dir=rem_folder, |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame^] | 176 | log_directory=log_directory, |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 177 | coordination_queue=coord_q, |
| 178 | total_nodes_count=len(test_nodes)) |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame^] | 179 | th = threading.Thread(None, test_thread, |
| 180 | "Test:" + node.get_conn_id(), |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 181 | (test, node, barrier, res_q)) |
| 182 | threads.append(th) |
| 183 | th.daemon = True |
| 184 | th.start() |
| 185 | |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame^] | 186 | th = threading.Thread(None, test_cls.coordination_th, |
| 187 | "Coordination thread", |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 188 | (coord_q, barrier, len(threads))) |
| 189 | threads.append(th) |
| 190 | th.daemon = True |
| 191 | th.start() |
| 192 | |
| 193 | results = [] |
| 194 | coord_q.put(None) |
| 195 | |
| 196 | while len(threads) != 0: |
| 197 | nthreads = [] |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame^] | 198 | time.sleep(0.1) |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 199 | |
| 200 | for th in threads: |
| 201 | if not th.is_alive(): |
| 202 | th.join() |
| 203 | else: |
| 204 | nthreads.append(th) |
| 205 | |
| 206 | threads = nthreads |
| 207 | |
| 208 | while not res_q.empty(): |
| 209 | val = res_q.get() |
| 210 | |
| 211 | if isinstance(val, utils.StopTestError): |
| 212 | raise val |
| 213 | |
| 214 | if isinstance(val, Exception): |
| 215 | msg = "Exception during test execution: {0!s}" |
| 216 | raise ValueError(msg.format(val)) |
| 217 | |
| 218 | results.append(val) |
| 219 | |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 220 | return results |
| 221 | |
| 222 | |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 223 | def suspend_vm_nodes(unused_nodes): |
| 224 | pausable_nodes_ids = [node.os_vm_id for node in unused_nodes |
| 225 | if node.os_vm_id is not None] |
| 226 | non_pausable = len(unused_nodes) - len(pausable_nodes_ids) |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 227 | |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 228 | if 0 != non_pausable: |
| 229 | logger.warning("Can't pause {0} nodes".format( |
| 230 | non_pausable)) |
| 231 | |
| 232 | if len(pausable_nodes_ids) != 0: |
| 233 | logger.debug("Try to pause {0} unused nodes".format( |
| 234 | len(pausable_nodes_ids))) |
| 235 | start_vms.pause(pausable_nodes_ids) |
| 236 | |
| 237 | return pausable_nodes_ids |
| 238 | |
| 239 | |
| 240 | def run_tests(cfg, test_block, nodes): |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 241 | test_nodes = [node for node in nodes |
| 242 | if 'testnode' in node.roles] |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 243 | |
| 244 | not_test_nodes = [node for node in nodes |
| 245 | if 'testnode' not in node.roles] |
| 246 | |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 247 | if len(test_nodes) == 0: |
| 248 | logger.error("No test nodes found") |
| 249 | return |
| 250 | |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 251 | for name, params in test_block.items(): |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 252 | results = [] |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 253 | limit = params.get('node_limit') |
| 254 | if isinstance(limit, (int, long)): |
| 255 | vm_limits = [limit] |
| 256 | elif limit is None: |
| 257 | vm_limits = [len(test_nodes)] |
| 258 | else: |
| 259 | vm_limits = limit |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 260 | |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 261 | for vm_count in vm_limits: |
| 262 | if vm_count == 'all': |
| 263 | curr_test_nodes = test_nodes |
| 264 | unused_nodes = [] |
| 265 | else: |
| 266 | curr_test_nodes = test_nodes[:vm_count] |
| 267 | unused_nodes = test_nodes[vm_count:] |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 268 | |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 269 | if 0 == len(curr_test_nodes): |
| 270 | continue |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 271 | |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame^] | 272 | # make a directory for results |
| 273 | all_tests_dirs = os.listdir(cfg_dict['results']) |
| 274 | |
| 275 | if 'name' in params: |
| 276 | dir_name = "{0}_{1}".format(name, params['name']) |
| 277 | else: |
| 278 | for idx in range(len(all_tests_dirs) + 1): |
| 279 | dir_name = "{0}_{1}".format(name, idx) |
| 280 | if dir_name not in all_tests_dirs: |
| 281 | break |
| 282 | else: |
| 283 | raise utils.StopTestError( |
| 284 | "Can't select directory for test results") |
| 285 | |
| 286 | dir_path = os.path.join(cfg_dict['results'], dir_name) |
| 287 | if not os.path.exists(dir_path): |
| 288 | os.mkdir(dir_path) |
| 289 | |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 290 | if cfg.get('suspend_unused_vms', True): |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 291 | pausable_nodes_ids = suspend_vm_nodes(unused_nodes) |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 292 | |
| 293 | resumable_nodes_ids = [node.os_vm_id for node in curr_test_nodes |
| 294 | if node.os_vm_id is not None] |
| 295 | |
| 296 | if len(resumable_nodes_ids) != 0: |
| 297 | logger.debug("Check and unpause {0} nodes".format( |
| 298 | len(resumable_nodes_ids))) |
| 299 | start_vms.unpause(resumable_nodes_ids) |
| 300 | |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 301 | test_cls = TOOL_TYPE_MAPPER[name] |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 302 | try: |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 303 | sens_nodes = curr_test_nodes + not_test_nodes |
| 304 | with sensors_info_util(cfg, sens_nodes) as sensor_data: |
| 305 | t_start = time.time() |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame^] | 306 | res = run_single_test(curr_test_nodes, |
| 307 | name, |
| 308 | test_cls, |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 309 | params, |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame^] | 310 | dir_path, |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 311 | cfg['default_test_local_folder'], |
| 312 | cfg['run_uuid']) |
| 313 | t_end = time.time() |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 314 | finally: |
| 315 | if cfg.get('suspend_unused_vms', True): |
| 316 | if len(pausable_nodes_ids) != 0: |
| 317 | logger.debug("Unpausing {0} nodes".format( |
| 318 | len(pausable_nodes_ids))) |
| 319 | start_vms.unpause(pausable_nodes_ids) |
| 320 | |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 321 | if sensor_data is not None: |
| 322 | fname = "{0}_{1}.csv".format(int(t_start), int(t_end)) |
| 323 | fpath = os.path.join(cfg['sensor_storage'], fname) |
| 324 | |
| 325 | with open(fpath, "w") as fd: |
| 326 | fd.write("\n\n".join(sensor_data)) |
| 327 | |
| 328 | results.extend(res) |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 329 | |
| 330 | yield name, results |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 331 | |
| 332 | |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 333 | def log_nodes_statistic(_, ctx): |
| 334 | nodes = ctx.nodes |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 335 | logger.info("Found {0} nodes total".format(len(nodes))) |
| 336 | per_role = collections.defaultdict(lambda: 0) |
| 337 | for node in nodes: |
| 338 | for role in node.roles: |
| 339 | per_role[role] += 1 |
| 340 | |
| 341 | for role, count in sorted(per_role.items()): |
| 342 | logger.debug("Found {0} nodes with role {1}".format(count, role)) |
| 343 | |
| 344 | |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 345 | def connect_stage(cfg, ctx): |
| 346 | ctx.clear_calls_stack.append(disconnect_stage) |
| 347 | connect_all(ctx.nodes) |
| 348 | |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 349 | all_ok = True |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 350 | |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 351 | for node in ctx.nodes: |
| 352 | if node.connection is None: |
| 353 | if 'testnode' in node.roles: |
| 354 | msg = "Can't connect to testnode {0}" |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 355 | msg = msg.format(node.get_conn_id()) |
| 356 | logger.error(msg) |
| 357 | raise utils.StopTestError(msg) |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 358 | else: |
| 359 | msg = "Node {0} would be excluded - can't connect" |
| 360 | logger.warning(msg.format(node.get_conn_id())) |
| 361 | all_ok = False |
| 362 | |
| 363 | if all_ok: |
| 364 | logger.info("All nodes connected successfully") |
| 365 | |
| 366 | ctx.nodes = [node for node in ctx.nodes |
| 367 | if node.connection is not None] |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 368 | |
| 369 | |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 370 | def discover_stage(cfg, ctx): |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 371 | if cfg.get('discover') is not None: |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 372 | discover_objs = [i.strip() for i in cfg['discover'].strip().split(",")] |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 373 | |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 374 | nodes = discover(ctx, |
| 375 | discover_objs, |
| 376 | cfg['clouds'], |
| 377 | cfg['var_dir'], |
| 378 | not cfg['dont_discover_nodes']) |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 379 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 380 | ctx.nodes.extend(nodes) |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 381 | |
| 382 | for url, roles in cfg.get('explicit_nodes', {}).items(): |
| 383 | ctx.nodes.append(Node(url, roles.split(","))) |
| 384 | |
| 385 | |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 386 | def save_nodes_stage(cfg, ctx): |
| 387 | cluster = {} |
| 388 | for node in ctx.nodes: |
| 389 | roles = node.roles[:] |
| 390 | if 'testnode' in roles: |
| 391 | roles.remove('testnode') |
| 392 | |
| 393 | if len(roles) != 0: |
| 394 | cluster[node.conn_url] = roles |
| 395 | |
| 396 | with open(cfg['nodes_report_file'], "w") as fd: |
| 397 | fd.write(pretty_yaml.dumps(cluster)) |
| 398 | |
| 399 | |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 400 | def reuse_vms_stage(cfg, ctx): |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame^] | 401 | p = cfg.get('clouds', {}).get('openstack', {}).get('vms', []) |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 402 | |
| 403 | for creds in p: |
| 404 | vm_name_pattern, conn_pattern = creds.split(",") |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 405 | msg = "Vm like {0} lookup failed".format(vm_name_pattern) |
| 406 | with utils.log_error(msg): |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 407 | msg = "Looking for vm with name like {0}".format(vm_name_pattern) |
| 408 | logger.debug(msg) |
| 409 | |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 410 | if not start_vms.is_connected(): |
| 411 | os_creds = get_OS_credentials(cfg, ctx) |
| 412 | else: |
| 413 | os_creds = {} |
| 414 | |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 415 | conn = start_vms.nova_connect(**os_creds) |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 416 | for ip, vm_id in start_vms.find_vms(conn, vm_name_pattern): |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 417 | node = Node(conn_pattern.format(ip=ip), ['testnode']) |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 418 | node.os_vm_id = vm_id |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 419 | ctx.nodes.append(node) |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 420 | |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 421 | |
| 422 | def get_creds_openrc(path): |
| 423 | fc = open(path).read() |
| 424 | |
| 425 | echo = 'echo "$OS_TENANT_NAME:$OS_USERNAME:$OS_PASSWORD@$OS_AUTH_URL"' |
| 426 | |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 427 | msg = "Failed to get creads from openrc file" |
| 428 | with utils.log_error(msg): |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 429 | data = utils.run_locally(['/bin/bash'], |
| 430 | input_data=fc + "\n" + echo) |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 431 | |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 432 | msg = "Failed to get creads from openrc file: " + data |
| 433 | with utils.log_error(msg): |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 434 | data = data.strip() |
| 435 | user, tenant, passwd_auth_url = data.split(':', 2) |
| 436 | passwd, auth_url = passwd_auth_url.rsplit("@", 1) |
| 437 | assert (auth_url.startswith("https://") or |
| 438 | auth_url.startswith("http://")) |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 439 | |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 440 | return user, passwd, tenant, auth_url |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 441 | |
| 442 | |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 443 | def get_OS_credentials(cfg, ctx): |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 444 | creds = None |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame^] | 445 | tenant = None |
| 446 | |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 447 | if 'openstack' in cfg['clouds']: |
| 448 | os_cfg = cfg['clouds']['openstack'] |
| 449 | if 'OPENRC' in os_cfg: |
| 450 | logger.info("Using OS credentials from " + os_cfg['OPENRC']) |
| 451 | user, passwd, tenant, auth_url = \ |
| 452 | get_creds_openrc(os_cfg['OPENRC']) |
| 453 | elif 'ENV' in os_cfg: |
| 454 | logger.info("Using OS credentials from shell environment") |
| 455 | user, passwd, tenant, auth_url = start_vms.ostack_get_creds() |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame^] | 456 | elif 'OS_TENANT_NAME' in os_cfg: |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 457 | logger.info("Using predefined credentials") |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 458 | tenant = os_cfg['OS_TENANT_NAME'].strip() |
| 459 | user = os_cfg['OS_USERNAME'].strip() |
| 460 | passwd = os_cfg['OS_PASSWORD'].strip() |
| 461 | auth_url = os_cfg['OS_AUTH_URL'].strip() |
| 462 | |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame^] | 463 | if tenant is None and 'fuel' in cfg['clouds'] and \ |
| 464 | 'openstack_env' in cfg['clouds']['fuel']: |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 465 | logger.info("Using fuel creds") |
| 466 | creds = ctx.fuel_openstack_creds |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame^] | 467 | elif tenant is None: |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 468 | logger.error("Can't found OS credentials") |
| 469 | raise utils.StopTestError("Can't found OS credentials", None) |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 470 | |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 471 | if creds is None: |
| 472 | creds = {'name': user, |
| 473 | 'passwd': passwd, |
| 474 | 'tenant': tenant, |
| 475 | 'auth_url': auth_url} |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 476 | |
koder aka kdanilov | 46d4f39 | 2015-04-24 11:35:00 +0300 | [diff] [blame] | 477 | msg = "OS_CREDS: user={name} tenant={tenant} auth_url={auth_url}" |
| 478 | logger.debug(msg.format(**creds)) |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 479 | return creds |
koder aka kdanilov | 4e9f3ed | 2015-04-14 11:26:12 +0300 | [diff] [blame] | 480 | |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 481 | |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 482 | @contextlib.contextmanager |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 483 | def create_vms_ctx(ctx, cfg, config, already_has_count=0): |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 484 | params = cfg['vm_configs'][config['cfg_name']].copy() |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 485 | os_nodes_ids = [] |
| 486 | |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 487 | if not start_vms.is_connected(): |
| 488 | os_creds = get_OS_credentials(cfg, ctx) |
| 489 | else: |
| 490 | os_creds = {} |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 491 | start_vms.nova_connect(**os_creds) |
| 492 | |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 493 | params.update(config) |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame^] | 494 | if 'keypair_file_private' not in params: |
| 495 | params['keypair_file_private'] = params['keypair_name'] + ".pem" |
| 496 | |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 497 | params['group_name'] = cfg_dict['run_uuid'] |
| 498 | |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 499 | if not config.get('skip_preparation', False): |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 500 | logger.info("Preparing openstack") |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 501 | start_vms.prepare_os_subpr(params=params, **os_creds) |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 502 | |
| 503 | new_nodes = [] |
| 504 | try: |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 505 | for new_node, node_id in start_vms.launch_vms(params, |
| 506 | already_has_count): |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 507 | new_node.roles.append('testnode') |
| 508 | ctx.nodes.append(new_node) |
| 509 | os_nodes_ids.append(node_id) |
| 510 | new_nodes.append(new_node) |
| 511 | |
| 512 | store_nodes_in_log(cfg, os_nodes_ids) |
| 513 | ctx.openstack_nodes_ids = os_nodes_ids |
| 514 | |
| 515 | yield new_nodes |
| 516 | |
| 517 | finally: |
| 518 | if not cfg['keep_vm']: |
| 519 | shut_down_vms_stage(cfg, ctx) |
| 520 | |
| 521 | |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 522 | def run_tests_stage(cfg, ctx): |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame^] | 523 | ctx.results = collections.defaultdict(lambda: []) |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 524 | |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 525 | if 'tests' not in cfg: |
| 526 | return |
gstepanov | 023c1e4 | 2015-04-08 15:50:19 +0300 | [diff] [blame] | 527 | |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 528 | for group in cfg['tests']: |
| 529 | |
| 530 | assert len(group.items()) == 1 |
| 531 | key, config = group.items()[0] |
| 532 | |
| 533 | if 'start_test_nodes' == key: |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 534 | if 'openstack' not in config: |
| 535 | msg = "No openstack block in config - can't spawn vm's" |
| 536 | logger.error(msg) |
| 537 | raise utils.StopTestError(msg) |
| 538 | |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 539 | num_test_nodes = sum(1 for node in ctx.nodes |
| 540 | if 'testnode' in node.roles) |
| 541 | |
| 542 | vm_ctx = create_vms_ctx(ctx, cfg, config['openstack'], |
| 543 | num_test_nodes) |
| 544 | with vm_ctx as new_nodes: |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 545 | if len(new_nodes) != 0: |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 546 | logger.debug("Connecting to new nodes") |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 547 | connect_all(new_nodes, True) |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 548 | |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 549 | for node in new_nodes: |
| 550 | if node.connection is None: |
| 551 | msg = "Failed to connect to vm {0}" |
| 552 | raise RuntimeError(msg.format(node.get_conn_id())) |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 553 | |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 554 | with with_sensors_util(cfg_dict, ctx.nodes): |
| 555 | for test_group in config.get('tests', []): |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame^] | 556 | for tp, res in run_tests(cfg, test_group, ctx.nodes): |
| 557 | ctx.results[tp].extend(res) |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 558 | else: |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 559 | with with_sensors_util(cfg_dict, ctx.nodes): |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame^] | 560 | for tp, res in run_tests(cfg, group, ctx.nodes): |
| 561 | ctx.results[tp].extend(res) |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 562 | |
gstepanov | 023c1e4 | 2015-04-08 15:50:19 +0300 | [diff] [blame] | 563 | |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 564 | def shut_down_vms_stage(cfg, ctx): |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 565 | vm_ids_fname = cfg_dict['vm_ids_fname'] |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 566 | if ctx.openstack_nodes_ids is None: |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 567 | nodes_ids = open(vm_ids_fname).read().split() |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 568 | else: |
| 569 | nodes_ids = ctx.openstack_nodes_ids |
| 570 | |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 571 | if len(nodes_ids) != 0: |
| 572 | logger.info("Removing nodes") |
| 573 | start_vms.clear_nodes(nodes_ids) |
| 574 | logger.info("Nodes has been removed") |
gstepanov | 023c1e4 | 2015-04-08 15:50:19 +0300 | [diff] [blame] | 575 | |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 576 | if os.path.exists(vm_ids_fname): |
| 577 | os.remove(vm_ids_fname) |
gstepanov | 023c1e4 | 2015-04-08 15:50:19 +0300 | [diff] [blame] | 578 | |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 579 | |
| 580 | def store_nodes_in_log(cfg, nodes_ids): |
| 581 | with open(cfg['vm_ids_fname'], 'w') as fd: |
| 582 | fd.write("\n".join(nodes_ids)) |
gstepanov | 023c1e4 | 2015-04-08 15:50:19 +0300 | [diff] [blame] | 583 | |
| 584 | |
| 585 | def clear_enviroment(cfg, ctx): |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 586 | if os.path.exists(cfg_dict['vm_ids_fname']): |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 587 | shut_down_vms_stage(cfg, ctx) |
gstepanov | 023c1e4 | 2015-04-08 15:50:19 +0300 | [diff] [blame] | 588 | |
| 589 | |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 590 | def disconnect_stage(cfg, ctx): |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 591 | ssh_utils.close_all_sessions() |
| 592 | |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 593 | for node in ctx.nodes: |
| 594 | if node.connection is not None: |
| 595 | node.connection.close() |
| 596 | |
| 597 | |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 598 | def store_raw_results_stage(cfg, ctx): |
| 599 | |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame^] | 600 | raw_results = cfg_dict['raw_results'] |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 601 | |
| 602 | if os.path.exists(raw_results): |
| 603 | cont = yaml.load(open(raw_results).read()) |
| 604 | else: |
| 605 | cont = [] |
| 606 | |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame^] | 607 | cont.extend(utils.yamable(ctx.results).items()) |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 608 | raw_data = pretty_yaml.dumps(cont) |
| 609 | |
| 610 | with open(raw_results, "w") as fd: |
| 611 | fd.write(raw_data) |
| 612 | |
| 613 | |
| 614 | def console_report_stage(cfg, ctx): |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame^] | 615 | first_report = True |
| 616 | text_rep_fname = cfg['text_report_file'] |
| 617 | with open(text_rep_fname, "w") as fd: |
| 618 | for tp, data in ctx.results.items(): |
| 619 | if 'io' == tp and data is not None: |
| 620 | dinfo = report.process_disk_info(data) |
| 621 | rep = IOPerfTest.format_for_console(data, dinfo) |
| 622 | elif tp in ['mysql', 'pgbench'] and data is not None: |
| 623 | rep = MysqlTest.format_for_console(data) |
| 624 | else: |
| 625 | logger.warning("Can't generate text report for " + tp) |
| 626 | continue |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 627 | |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame^] | 628 | fd.write(rep) |
| 629 | fd.write("\n") |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 630 | |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame^] | 631 | if first_report: |
| 632 | logger.info("Text report were stored in " + text_rep_fname) |
| 633 | first_report = False |
| 634 | |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 635 | print("\n" + rep + "\n") |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 636 | |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 637 | |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 638 | def html_report_stage(cfg, ctx): |
Yulia Portnova | 8ca2057 | 2015-04-14 14:09:39 +0300 | [diff] [blame] | 639 | html_rep_fname = cfg['html_report_file'] |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 640 | found = False |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame^] | 641 | for tp, data in ctx.results.items(): |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 642 | if 'io' == tp and data is not None: |
| 643 | if found: |
| 644 | logger.error("Making reports for more than one " + |
| 645 | "io block isn't supported! All " + |
| 646 | "report, except first are skipped") |
| 647 | continue |
| 648 | found = True |
| 649 | dinfo = report.process_disk_info(data) |
| 650 | report.make_io_report(dinfo, data, html_rep_fname, |
| 651 | lab_info=ctx.hw_info) |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 652 | |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 653 | |
| 654 | def complete_log_nodes_statistic(cfg, ctx): |
| 655 | nodes = ctx.nodes |
| 656 | for node in nodes: |
| 657 | logger.debug(str(node)) |
| 658 | |
| 659 | |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 660 | def load_data_from(var_dir): |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 661 | def load_data_from_file(_, ctx): |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 662 | raw_results = os.path.join(var_dir, 'raw_results.yaml') |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame^] | 663 | ctx.results = {} |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 664 | for tp, results in yaml.load(open(raw_results).read()): |
| 665 | cls = TOOL_TYPE_MAPPER[tp] |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame^] | 666 | ctx.results[tp] = map(cls.load, results) |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 667 | |
koder aka kdanilov | 4e9f3ed | 2015-04-14 11:26:12 +0300 | [diff] [blame] | 668 | return load_data_from_file |
gstepanov | cd256d6 | 2015-04-07 17:47:32 +0300 | [diff] [blame] | 669 | |
| 670 | |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 671 | def start_web_ui(cfg, ctx): |
| 672 | if webui is None: |
| 673 | logger.error("Can't start webui. Install cherrypy module") |
| 674 | ctx.web_thread = None |
| 675 | else: |
| 676 | th = threading.Thread(None, webui.web_main_thread, "webui", (None,)) |
| 677 | th.daemon = True |
| 678 | th.start() |
| 679 | ctx.web_thread = th |
| 680 | |
| 681 | |
| 682 | def stop_web_ui(cfg, ctx): |
| 683 | webui.web_main_stop() |
| 684 | time.sleep(1) |
| 685 | |
| 686 | |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 687 | def parse_args(argv): |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 688 | descr = "Disk io performance test suite" |
| 689 | parser = argparse.ArgumentParser(prog='wally', description=descr) |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 690 | |
| 691 | parser.add_argument("-l", dest='extra_logs', |
| 692 | action='store_true', default=False, |
| 693 | help="print some extra log info") |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 694 | parser.add_argument("-b", '--build_description', |
| 695 | type=str, default="Build info") |
| 696 | parser.add_argument("-i", '--build_id', type=str, default="id") |
| 697 | parser.add_argument("-t", '--build_type', type=str, default="GA") |
| 698 | parser.add_argument("-u", '--username', type=str, default="admin") |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 699 | parser.add_argument("-n", '--no-tests', action='store_true', |
| 700 | help="Don't run tests", default=False) |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 701 | parser.add_argument("-p", '--post-process-only', metavar="VAR_DIR", |
| 702 | help="Only process data from previour run") |
| 703 | parser.add_argument("-k", '--keep-vm', action='store_true', |
| 704 | help="Don't remove test vm's", default=False) |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 705 | parser.add_argument("-d", '--dont-discover-nodes', action='store_true', |
| 706 | help="Don't connect/discover fuel nodes", |
| 707 | default=False) |
koder aka kdanilov | a047e1b | 2015-04-21 23:16:59 +0300 | [diff] [blame] | 708 | parser.add_argument("-r", '--no-html-report', action='store_true', |
| 709 | help="Skip html report", default=False) |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 710 | parser.add_argument("--params", metavar="testname.paramname", |
koder aka kdanilov | 63ad206 | 2015-04-27 13:11:40 +0300 | [diff] [blame] | 711 | help="Test params", default=[]) |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame^] | 712 | parser.add_argument("--ls", action='store_true', default=False) |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 713 | parser.add_argument("config_file") |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 714 | |
| 715 | return parser.parse_args(argv[1:]) |
| 716 | |
| 717 | |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 718 | def get_stage_name(func): |
| 719 | if func.__name__.endswith("stage"): |
| 720 | return func.__name__ |
| 721 | else: |
| 722 | return func.__name__ + " stage" |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 723 | |
| 724 | |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame^] | 725 | def list_results(path): |
| 726 | results = [] |
| 727 | |
| 728 | for dname in os.listdir(path): |
| 729 | |
| 730 | cfg = get_test_files(os.path.join(path, dname)) |
| 731 | |
| 732 | if not os.path.isfile(cfg['raw_results']): |
| 733 | continue |
| 734 | |
| 735 | res_mtime = time.ctime(os.path.getmtime(cfg['raw_results'])) |
| 736 | cfg = yaml.load(open(cfg['saved_config_file']).read()) |
| 737 | |
| 738 | test_names = [] |
| 739 | |
| 740 | for block in cfg['tests']: |
| 741 | assert len(block.items()) == 1 |
| 742 | name, data = block.items()[0] |
| 743 | if name == 'start_test_nodes': |
| 744 | for in_blk in data['tests']: |
| 745 | assert len(in_blk.items()) == 1 |
| 746 | in_name, _ = in_blk.items()[0] |
| 747 | test_names.append(in_name) |
| 748 | else: |
| 749 | test_names.append(name) |
| 750 | results.append((dname, test_names, res_mtime)) |
| 751 | |
| 752 | tab = texttable.Texttable(max_width=120) |
| 753 | tab.set_deco(tab.HEADER | tab.VLINES | tab.BORDER) |
| 754 | tab.set_cols_align(["l", "l", "l"]) |
| 755 | results.sort(key=lambda x: x[2]) |
| 756 | |
| 757 | for data in results: |
| 758 | dname, tests, mtime = data |
| 759 | tab.add_row([dname, ', '.join(tests), mtime]) |
| 760 | |
| 761 | tab.header(["Name", "Tests", "etime"]) |
| 762 | |
| 763 | print(tab.draw()) |
| 764 | |
| 765 | |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 766 | def main(argv): |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame^] | 767 | if '--ls' in argv: |
| 768 | list_results(argv[-1]) |
| 769 | exit(0) |
| 770 | |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 771 | faulthandler.register(signal.SIGUSR1, all_threads=True) |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 772 | opts = parse_args(argv) |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 773 | load_config(opts.config_file, opts.post_process_only) |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 774 | |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 775 | if cfg_dict.get('logging', {}).get("extra_logs", False) or opts.extra_logs: |
| 776 | level = logging.DEBUG |
| 777 | else: |
| 778 | level = logging.WARNING |
| 779 | |
| 780 | setup_loggers(level, cfg_dict['log_file']) |
| 781 | |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame^] | 782 | if not os.path.exists(cfg_dict['saved_config_file']): |
| 783 | with open(cfg_dict['saved_config_file'], 'w') as fd: |
| 784 | fd.write(open(opts.config_file).read()) |
| 785 | |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 786 | if opts.post_process_only is not None: |
| 787 | stages = [ |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 788 | load_data_from(opts.post_process_only) |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 789 | ] |
| 790 | else: |
| 791 | stages = [ |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 792 | discover_stage |
| 793 | ] |
| 794 | |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 795 | stages.extend([ |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 796 | reuse_vms_stage, |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 797 | log_nodes_statistic, |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 798 | save_nodes_stage, |
| 799 | connect_stage]) |
| 800 | |
| 801 | if cfg_dict.get('collect_info', True): |
| 802 | stages.append(collect_hw_info_stage) |
| 803 | |
| 804 | stages.extend([ |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 805 | # deploy_sensors_stage, |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 806 | run_tests_stage, |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 807 | store_raw_results_stage, |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 808 | # gather_sensors_stage |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 809 | ]) |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 810 | |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 811 | report_stages = [ |
| 812 | console_report_stage, |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 813 | ] |
| 814 | |
koder aka kdanilov | a047e1b | 2015-04-21 23:16:59 +0300 | [diff] [blame] | 815 | if not opts.no_html_report: |
| 816 | report_stages.append(html_report_stage) |
| 817 | |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 818 | logger.info("All info would be stored into {0}".format( |
| 819 | cfg_dict['var_dir'])) |
gstepanov | cd256d6 | 2015-04-07 17:47:32 +0300 | [diff] [blame] | 820 | |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 821 | ctx = Context() |
gstepanov | affcdb1 | 2015-04-07 17:18:29 +0300 | [diff] [blame] | 822 | ctx.build_meta['build_id'] = opts.build_id |
| 823 | ctx.build_meta['build_descrption'] = opts.build_description |
| 824 | ctx.build_meta['build_type'] = opts.build_type |
| 825 | ctx.build_meta['username'] = opts.username |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 826 | ctx.sensors_data = SensorDatastore() |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 827 | |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 828 | cfg_dict['keep_vm'] = opts.keep_vm |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 829 | cfg_dict['no_tests'] = opts.no_tests |
| 830 | cfg_dict['dont_discover_nodes'] = opts.dont_discover_nodes |
koder aka kdanilov | 6c49106 | 2015-04-09 22:33:13 +0300 | [diff] [blame] | 831 | |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 832 | if cfg_dict.get('run_web_ui', False): |
| 833 | start_web_ui(cfg_dict, ctx) |
| 834 | |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 835 | msg_templ = "Exception during {0.__name__}: {1!s}" |
| 836 | msg_templ_no_exc = "During {0.__name__}" |
| 837 | |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 838 | try: |
| 839 | for stage in stages: |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 840 | logger.info("Start " + get_stage_name(stage)) |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 841 | stage(cfg_dict, ctx) |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 842 | except utils.StopTestError as exc: |
| 843 | logger.error(msg_templ.format(stage, exc)) |
| 844 | except Exception: |
| 845 | logger.exception(msg_templ_no_exc.format(stage)) |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 846 | finally: |
| 847 | exc, cls, tb = sys.exc_info() |
| 848 | for stage in ctx.clear_calls_stack[::-1]: |
| 849 | try: |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 850 | logger.info("Start " + get_stage_name(stage)) |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 851 | stage(cfg_dict, ctx) |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 852 | except utils.StopTestError as cleanup_exc: |
| 853 | logger.error(msg_templ.format(stage, cleanup_exc)) |
| 854 | except Exception: |
| 855 | logger.exception(msg_templ_no_exc.format(stage)) |
| 856 | |
| 857 | logger.debug("Start utils.cleanup") |
| 858 | for clean_func, args, kwargs in utils.iter_clean_func(): |
| 859 | try: |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 860 | logger.info("Start " + get_stage_name(clean_func)) |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 861 | clean_func(*args, **kwargs) |
| 862 | except utils.StopTestError as cleanup_exc: |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 863 | logger.error(msg_templ.format(clean_func, cleanup_exc)) |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 864 | except Exception: |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 865 | logger.exception(msg_templ_no_exc.format(clean_func)) |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 866 | |
koder aka kdanilov | e2de58c | 2015-04-24 22:59:36 +0300 | [diff] [blame] | 867 | if exc is None: |
| 868 | for report_stage in report_stages: |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 869 | logger.info("Start " + get_stage_name(report_stage)) |
koder aka kdanilov | e2de58c | 2015-04-24 22:59:36 +0300 | [diff] [blame] | 870 | report_stage(cfg_dict, ctx) |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 871 | |
| 872 | logger.info("All info stored in {0} folder".format(cfg_dict['var_dir'])) |
koder aka kdanilov | e2de58c | 2015-04-24 22:59:36 +0300 | [diff] [blame] | 873 | |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 874 | if cfg_dict.get('run_web_ui', False): |
| 875 | stop_web_ui(cfg_dict, ctx) |
| 876 | |
koder aka kdanilov | e2de58c | 2015-04-24 22:59:36 +0300 | [diff] [blame] | 877 | if exc is None: |
| 878 | logger.info("Tests finished successfully") |
| 879 | return 0 |
| 880 | else: |
| 881 | logger.error("Tests are failed. See detailed error above") |
| 882 | return 1 |