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