koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 1 | import time |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 2 | import logging |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 3 | from concurrent.futures import Future |
| 4 | from typing import List, Dict, Tuple, Optional, Union, cast |
koder aka kdanilov | 88407ff | 2015-05-26 15:35:57 +0300 | [diff] [blame] | 5 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 6 | from . import utils, ssh_utils, hw_info |
| 7 | from .config import ConfigBlock |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 8 | from .node import setup_rpc, connect |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 9 | from .node_interfaces import NodeInfo, IRPCNode |
| 10 | from .stage import Stage, StepOrder |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 11 | from .suits.io.fio import IOPerfTest |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 12 | from .suits.itest import TestInputConfig |
| 13 | from .suits.mysql import MysqlTest |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 14 | from .suits.omgbench import OmgTest |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 15 | from .suits.postgres import PgBenchTest |
| 16 | from .test_run_class import TestRun |
koder aka kdanilov | bc2c898 | 2015-06-13 02:50:43 +0300 | [diff] [blame] | 17 | |
| 18 | |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 19 | TOOL_TYPE_MAPPER = { |
| 20 | "io": IOPerfTest, |
| 21 | "pgbench": PgBenchTest, |
| 22 | "mysql": MysqlTest, |
Yulia Portnova | b0c977c | 2015-12-11 19:23:28 +0200 | [diff] [blame] | 23 | "omg": OmgTest, |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 24 | } |
koder aka kdanilov | 63ad206 | 2015-04-27 13:11:40 +0300 | [diff] [blame] | 25 | |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 26 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 27 | logger = logging.getLogger("wally") |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 28 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 29 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 30 | class ConnectStage(Stage): |
| 31 | """Connect to nodes stage""" |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 32 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 33 | priority = StepOrder.CONNECT |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 34 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 35 | def run(self, ctx: TestRun) -> None: |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 36 | with ctx.get_pool() as pool: |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 37 | logger.info("Connecting to %s nodes", len(ctx.nodes_info)) |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 38 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 39 | def connect_ext(node_info: NodeInfo) -> Tuple[bool, Union[IRPCNode, NodeInfo]]: |
| 40 | try: |
| 41 | ssh_node = connect(node_info, conn_timeout=ctx.config.connect_timeout) |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 42 | return True, setup_rpc(ssh_node, |
| 43 | ctx.rpc_code, |
| 44 | ctx.default_rpc_plugins, |
| 45 | log_level=ctx.config.rpc_log_level) |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 46 | except Exception as exc: |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 47 | logger.exception("During connect to %s: %s", node_info, exc) |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 48 | return False, node_info |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 49 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 50 | failed_testnodes = [] # type: List[NodeInfo] |
| 51 | failed_nodes = [] # type: List[NodeInfo] |
| 52 | ctx.nodes = [] |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 53 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 54 | for ok, node in pool.map(connect_ext, ctx.nodes_info.values()): |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 55 | if not ok: |
| 56 | node = cast(NodeInfo, node) |
| 57 | if 'testnode' in node.roles: |
| 58 | failed_testnodes.append(node) |
| 59 | else: |
| 60 | failed_nodes.append(node) |
| 61 | else: |
| 62 | ctx.nodes.append(cast(IRPCNode, node)) |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 63 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 64 | if failed_nodes: |
| 65 | msg = "Node(s) {} would be excluded - can't connect" |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 66 | logger.warning(msg.format(", ".join(map(str, failed_nodes)))) |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 67 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 68 | if failed_testnodes: |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 69 | msg = "Can't connect to testnode(s) " + ",".join(map(str, failed_testnodes)) |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 70 | logger.error(msg) |
| 71 | raise utils.StopTestError(msg) |
| 72 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 73 | if not failed_nodes: |
| 74 | logger.info("All nodes connected successfully") |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 75 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 76 | def cleanup(self, ctx: TestRun) -> None: |
| 77 | # TODO(koder): what next line was for? |
| 78 | # ssh_utils.close_all_sessions() |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 79 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 80 | if ctx.config.get("download_rpc_logs", False): |
| 81 | for node in ctx.nodes: |
| 82 | if node.rpc_log_file is not None: |
| 83 | nid = node.info.node_id() |
| 84 | path = "rpc_logs/" + nid |
| 85 | node.conn.server.flush_logs() |
| 86 | log = node.get_file_content(node.rpc_log_file) |
koder aka kdanilov | 3af3c33 | 2016-12-19 17:12:34 +0200 | [diff] [blame] | 87 | ctx.storage.store_raw(log, path) |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 88 | logger.debug("RPC log from node {} stored into storage::{}".format(nid, path)) |
| 89 | |
| 90 | with ctx.get_pool() as pool: |
| 91 | list(pool.map(lambda node: node.disconnect(stop=True), ctx.nodes)) |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 92 | |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 93 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 94 | class CollectInfoStage(Stage): |
| 95 | """Collect node info""" |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 96 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 97 | priority = StepOrder.START_SENSORS - 1 |
| 98 | config_block = 'collect_info' |
| 99 | |
| 100 | def run(self, ctx: TestRun) -> None: |
| 101 | if not ctx.config.collect_info: |
| 102 | return |
| 103 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 104 | futures = {} # type: Dict[Tuple[str, str], Future] |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 105 | |
| 106 | with ctx.get_pool() as pool: |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 107 | # can't make next RPC request until finish with previous |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 108 | for node in ctx.nodes: |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 109 | nid = node.info.node_id() |
| 110 | hw_info_path = "hw_info/{}".format(nid) |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 111 | if hw_info_path not in ctx.storage: |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 112 | futures[(hw_info_path, nid)] = pool.submit(hw_info.get_hw_info, node) |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 113 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 114 | for (path, nid), future in futures.items(): |
| 115 | try: |
| 116 | ctx.storage[path] = future.result() |
| 117 | except Exception: |
| 118 | logger.exception("During collecting hardware info from %s", nid) |
| 119 | raise utils.StopTestError() |
| 120 | |
| 121 | futures.clear() |
| 122 | for node in ctx.nodes: |
| 123 | nid = node.info.node_id() |
| 124 | sw_info_path = "sw_info/{}".format(nid) |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 125 | if sw_info_path not in ctx.storage: |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 126 | futures[(sw_info_path, nid)] = pool.submit(hw_info.get_sw_info, node) |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 127 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 128 | for (path, nid), future in futures.items(): |
| 129 | try: |
| 130 | ctx.storage[path] = future.result() |
| 131 | except Exception: |
| 132 | logger.exception("During collecting software info from %s", nid) |
| 133 | raise utils.StopTestError() |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 134 | |
| 135 | |
| 136 | class ExplicitNodesStage(Stage): |
| 137 | """add explicit nodes""" |
| 138 | |
| 139 | priority = StepOrder.DISCOVER |
| 140 | config_block = 'nodes' |
| 141 | |
| 142 | def run(self, ctx: TestRun) -> None: |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 143 | if 'all_nodes' in ctx.storage: |
| 144 | logger.info("Skip explicid nodes filling, as all_nodes all ready in storage") |
| 145 | return |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 146 | |
koder aka kdanilov | bbbe1dc | 2016-12-20 01:19:56 +0200 | [diff] [blame^] | 147 | for url, roles in ctx.config.get('nodes', {}).raw().items(): |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 148 | ctx.merge_node(ssh_utils.parse_ssh_uri(url), set(roles.split(","))) |
koder aka kdanilov | bbbe1dc | 2016-12-20 01:19:56 +0200 | [diff] [blame^] | 149 | logger.debug("Add node %s with roles %s", url, roles) |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 150 | |
| 151 | |
| 152 | class SaveNodesStage(Stage): |
| 153 | """Save nodes list to file""" |
| 154 | |
| 155 | priority = StepOrder.CONNECT |
| 156 | |
| 157 | def run(self, ctx: TestRun) -> None: |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 158 | ctx.storage['all_nodes'] = list(ctx.nodes_info.values()) # type: ignore |
| 159 | |
| 160 | |
| 161 | class SleepStage(Stage): |
| 162 | """Save nodes list to file""" |
| 163 | |
| 164 | priority = StepOrder.TEST |
| 165 | config_block = 'sleep' |
| 166 | |
| 167 | def run(self, ctx: TestRun) -> None: |
| 168 | logger.debug("Will sleep for %r seconds", ctx.config.sleep) |
| 169 | time.sleep(ctx.config.sleep) |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 170 | |
| 171 | |
| 172 | class RunTestsStage(Stage): |
| 173 | |
| 174 | priority = StepOrder.TEST |
| 175 | config_block = 'tests' |
| 176 | |
| 177 | def run(self, ctx: TestRun) -> None: |
| 178 | for test_group in ctx.config.get('tests', []): |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 179 | if not ctx.config.no_tests: |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 180 | test_nodes = [node for node in ctx.nodes if 'testnode' in node.info.roles] |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 181 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 182 | if not test_nodes: |
| 183 | logger.error("No test nodes found") |
| 184 | return |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 185 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 186 | for name, params in test_group.items(): |
| 187 | vm_count = params.get('node_limit', None) # type: Optional[int] |
gstepanov | 023c1e4 | 2015-04-08 15:50:19 +0300 | [diff] [blame] | 188 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 189 | # select test nodes |
| 190 | if vm_count is None: |
| 191 | curr_test_nodes = test_nodes |
| 192 | else: |
| 193 | curr_test_nodes = test_nodes[:vm_count] |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 194 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 195 | if not curr_test_nodes: |
| 196 | logger.error("No nodes found for test, skipping it.") |
| 197 | continue |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 198 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 199 | test_cls = TOOL_TYPE_MAPPER[name] |
| 200 | remote_dir = ctx.config.default_test_local_folder.format(name=name, uuid=ctx.config.run_uuid) |
| 201 | test_cfg = TestInputConfig(test_cls.__name__, |
| 202 | params=params, |
| 203 | run_uuid=ctx.config.run_uuid, |
| 204 | nodes=test_nodes, |
| 205 | storage=ctx.storage, |
| 206 | remote_dir=remote_dir) |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 207 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 208 | test_cls(test_cfg).run() |
gstepanov | 023c1e4 | 2015-04-08 15:50:19 +0300 | [diff] [blame] | 209 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 210 | @classmethod |
| 211 | def validate_config(cls, cfg: ConfigBlock) -> None: |
| 212 | pass |