gstepanov | 023c1e4 | 2015-04-08 15:50:19 +0300 | [diff] [blame] | 1 | import os |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 2 | import logging |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 3 | import contextlib |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 4 | from typing import List, Dict, Iterable, Iterator, Tuple, Optional, Union, cast |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 5 | from concurrent.futures import ThreadPoolExecutor, Future |
koder aka kdanilov | 88407ff | 2015-05-26 15:35:57 +0300 | [diff] [blame] | 6 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 7 | from .node_interfaces import NodeInfo, IRPCNode |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 8 | from .test_run_class import TestRun |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 9 | from .discover import discover |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 10 | from . import pretty_yaml, utils, report, ssh_utils, start_vms, hw_info |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 11 | from .node import setup_rpc, connect |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 12 | from .config import ConfigBlock, Config |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 13 | |
| 14 | from .suits.mysql import MysqlTest |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame^] | 15 | from .suits.itest import TestInputConfig |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 16 | from .suits.io.fio import IOPerfTest |
| 17 | from .suits.postgres import PgBenchTest |
| 18 | from .suits.omgbench import OmgTest |
koder aka kdanilov | bc2c898 | 2015-06-13 02:50:43 +0300 | [diff] [blame] | 19 | |
| 20 | |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 21 | TOOL_TYPE_MAPPER = { |
| 22 | "io": IOPerfTest, |
| 23 | "pgbench": PgBenchTest, |
| 24 | "mysql": MysqlTest, |
Yulia Portnova | b0c977c | 2015-12-11 19:23:28 +0200 | [diff] [blame] | 25 | "omg": OmgTest, |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 26 | } |
koder aka kdanilov | 63ad206 | 2015-04-27 13:11:40 +0300 | [diff] [blame] | 27 | |
koder aka kdanilov | 57ce4db | 2015-04-25 21:25:51 +0300 | [diff] [blame] | 28 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 29 | logger = logging.getLogger("wally") |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 30 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 31 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 32 | def connect_all(nodes_info: List[NodeInfo], pool: ThreadPoolExecutor, conn_timeout: int = 30) -> List[IRPCNode]: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 33 | """Connect to all nodes, log errors""" |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 34 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 35 | logger.info("Connecting to %s nodes", len(nodes_info)) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 36 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 37 | def connect_ext(node_info: NodeInfo) -> Tuple[bool, Union[IRPCNode, NodeInfo]]: |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 38 | try: |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 39 | ssh_node = connect(node_info, conn_timeout=conn_timeout) |
| 40 | # TODO(koder): need to pass all required rpc bytes to this call |
| 41 | return True, setup_rpc(ssh_node, b"") |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 42 | except Exception as exc: |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 43 | logger.error("During connect to {}: {!s}".format(node, exc)) |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 44 | return False, node_info |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 45 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 46 | failed_testnodes = [] # type: List[NodeInfo] |
| 47 | failed_nodes = [] # type: List[NodeInfo] |
| 48 | ready = [] # type: List[IRPCNode] |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 49 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 50 | for ok, node in pool.map(connect_ext, nodes_info): |
| 51 | if not ok: |
| 52 | node = cast(NodeInfo, node) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 53 | if 'testnode' in node.roles: |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 54 | failed_testnodes.append(node) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 55 | else: |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 56 | failed_nodes.append(node) |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 57 | else: |
| 58 | ready.append(cast(IRPCNode, node)) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 59 | |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 60 | if failed_nodes: |
| 61 | msg = "Node(s) {} would be excluded - can't connect" |
| 62 | logger.warning(msg.format(",".join(map(str, failed_nodes)))) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 63 | |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 64 | if failed_testnodes: |
| 65 | msg = "Can't connect to testnode(s) " + \ |
| 66 | ",".join(map(str, failed_testnodes)) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 67 | logger.error(msg) |
| 68 | raise utils.StopTestError(msg) |
| 69 | |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 70 | if not failed_nodes: |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 71 | logger.info("All nodes connected successfully") |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 72 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 73 | return ready |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 74 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 75 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 76 | def collect_info_stage(ctx: TestRun) -> None: |
| 77 | futures = {} # type: Dict[str, Future] |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 78 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 79 | with ctx.get_pool() as pool: |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 80 | for node in ctx.nodes: |
| 81 | hw_info_path = "hw_info/{}".format(node.info.node_id()) |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 82 | if hw_info_path not in ctx.storage: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 83 | futures[hw_info_path] = pool.submit(hw_info.get_hw_info, node), node |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 84 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 85 | sw_info_path = "sw_info/{}".format(node.info.node_id()) |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 86 | if sw_info_path not in ctx.storage: |
| 87 | futures[sw_info_path] = pool.submit(hw_info.get_sw_info, node) |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 88 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 89 | for path, future in futures.items(): |
| 90 | ctx.storage[path] = future.result() |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 91 | |
| 92 | |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 93 | @contextlib.contextmanager |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 94 | def suspend_vm_nodes_ctx(ctx: TestRun, unused_nodes: List[IRPCNode]) -> Iterator[List[int]]: |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 95 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 96 | pausable_nodes_ids = [cast(int, node.info.os_vm_id) |
| 97 | for node in unused_nodes |
| 98 | if node.info.os_vm_id is not None] |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 99 | |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 100 | non_pausable = len(unused_nodes) - len(pausable_nodes_ids) |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 101 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 102 | if non_pausable: |
| 103 | logger.warning("Can't pause {} nodes".format(non_pausable)) |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 104 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 105 | if pausable_nodes_ids: |
| 106 | logger.debug("Try to pause {} unused nodes".format(len(pausable_nodes_ids))) |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 107 | with ctx.get_pool() as pool: |
| 108 | start_vms.pause(ctx.os_connection, pausable_nodes_ids, pool) |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 109 | |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 110 | try: |
| 111 | yield pausable_nodes_ids |
| 112 | finally: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 113 | if pausable_nodes_ids: |
| 114 | logger.debug("Unpausing {} nodes".format(len(pausable_nodes_ids))) |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 115 | with ctx.get_pool() as pool: |
| 116 | start_vms.unpause(ctx.os_connection, pausable_nodes_ids, pool) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 117 | |
| 118 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 119 | def run_tests(ctx: TestRun, test_block: ConfigBlock, nodes: List[IRPCNode]) -> None: |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 120 | """Run test from test block""" |
| 121 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 122 | test_nodes = [node for node in nodes if 'testnode' in node.info.roles] |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 123 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 124 | if not test_nodes: |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 125 | logger.error("No test nodes found") |
| 126 | return |
| 127 | |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 128 | for name, params in test_block.items(): |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 129 | vm_count = params.get('node_limit', None) # type: Optional[int] |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 130 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 131 | # select test nodes |
| 132 | if vm_count is None: |
| 133 | curr_test_nodes = test_nodes |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 134 | unused_nodes = [] # type: List[IRPCNode] |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 135 | else: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 136 | curr_test_nodes = test_nodes[:vm_count] |
| 137 | unused_nodes = test_nodes[vm_count:] |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 138 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 139 | if not curr_test_nodes: |
| 140 | logger.error("No nodes found for test, skipping it.") |
| 141 | continue |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 142 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 143 | # results_path = generate_result_dir_name(cfg.results_storage, name, params) |
| 144 | # utils.mkdirs_if_unxists(results_path) |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 145 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 146 | # suspend all unused virtual nodes |
| 147 | if ctx.config.get('suspend_unused_vms', True): |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 148 | suspend_ctx = suspend_vm_nodes_ctx(ctx, unused_nodes) |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 149 | else: |
| 150 | suspend_ctx = utils.empty_ctx() |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame] | 151 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 152 | resumable_nodes_ids = [cast(int, node.info.os_vm_id) |
| 153 | for node in curr_test_nodes |
| 154 | if node.info.os_vm_id is not None] |
| 155 | |
| 156 | if resumable_nodes_ids: |
| 157 | logger.debug("Check and unpause {} nodes".format(len(resumable_nodes_ids))) |
| 158 | |
| 159 | with ctx.get_pool() as pool: |
| 160 | start_vms.unpause(ctx.os_connection, resumable_nodes_ids, pool) |
| 161 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 162 | with suspend_ctx: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 163 | test_cls = TOOL_TYPE_MAPPER[name] |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 164 | remote_dir = ctx.config.default_test_local_folder.format(name=name, uuid=ctx.config.run_uuid) |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame^] | 165 | test_cfg = TestInputConfig(test_cls.__name__, |
| 166 | params=params, |
| 167 | run_uuid=ctx.config.run_uuid, |
| 168 | nodes=test_nodes, |
| 169 | storage=ctx.storage, |
| 170 | remote_dir=remote_dir) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 171 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 172 | test_cls(test_cfg).run() |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 173 | |
| 174 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 175 | def connect_stage(ctx: TestRun) -> None: |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 176 | ctx.clear_calls_stack.append(disconnect_stage) |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 177 | |
| 178 | with ctx.get_pool() as pool: |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 179 | ctx.nodes = connect_all(ctx.nodes_info, pool) |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 180 | |
| 181 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 182 | def discover_stage(ctx: TestRun) -> None: |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 183 | """discover clusters and nodes stage""" |
| 184 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 185 | # TODO(koder): Properly store discovery info and check if it available to skip phase |
| 186 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 187 | discover_info = ctx.config.get('discover') |
| 188 | if discover_info: |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 189 | if "discovered_nodes" in ctx.storage: |
| 190 | nodes = ctx.storage.load_list("discovered_nodes", NodeInfo) |
| 191 | ctx.fuel_openstack_creds = ctx.storage.load("fuel_openstack_creds", start_vms.OSCreds) |
| 192 | else: |
| 193 | discover_objs = [i.strip() for i in discover_info.strip().split(",")] |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 194 | |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame^] | 195 | ctx.fuel_openstack_creds, nodes = discover.discover(ctx, |
| 196 | discover_objs, |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 197 | ctx.config.clouds, |
| 198 | not ctx.config.dont_discover_nodes) |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 199 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 200 | ctx.storage["fuel_openstack_creds"] = ctx.fuel_openstack_creds # type: ignore |
| 201 | ctx.storage["discovered_nodes"] = nodes # type: ignore |
| 202 | ctx.nodes_info.extend(nodes) |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 203 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 204 | for url, roles in ctx.config.get('explicit_nodes', {}).items(): |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 205 | creds = ssh_utils.parse_ssh_uri(url) |
| 206 | roles = set(roles.split(",")) |
| 207 | ctx.nodes_info.append(NodeInfo(creds, roles)) |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 208 | |
| 209 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 210 | def save_nodes_stage(ctx: TestRun) -> None: |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 211 | """Save nodes list to file""" |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 212 | ctx.storage['nodes'] = ctx.nodes_info # type: ignore |
| 213 | |
| 214 | |
| 215 | def ensure_connected_to_openstack(ctx: TestRun) -> None: |
| 216 | if not ctx.os_connection is None: |
| 217 | if ctx.os_creds is None: |
| 218 | ctx.os_creds = get_OS_credentials(ctx) |
| 219 | ctx.os_connection = start_vms.os_connect(ctx.os_creds) |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 220 | |
| 221 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 222 | def reuse_vms_stage(ctx: TestRun) -> None: |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 223 | if "reused_nodes" in ctx.storage: |
| 224 | ctx.nodes_info.extend(ctx.storage.load_list("reused_nodes", NodeInfo)) |
| 225 | else: |
| 226 | reused_nodes = [] |
| 227 | vms_patterns = ctx.config.get('clouds/openstack/vms', []) |
| 228 | private_key_path = get_vm_keypair_path(ctx.config)[0] |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 229 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 230 | for creds in vms_patterns: |
| 231 | user_name, vm_name_pattern = creds.split("@", 1) |
| 232 | msg = "Vm like {} lookup failed".format(vm_name_pattern) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 233 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 234 | with utils.LogError(msg): |
| 235 | msg = "Looking for vm with name like {0}".format(vm_name_pattern) |
| 236 | logger.debug(msg) |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 237 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 238 | ensure_connected_to_openstack(ctx) |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 239 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 240 | for ip, vm_id in start_vms.find_vms(ctx.os_connection, vm_name_pattern): |
| 241 | creds = ssh_utils.ConnCreds(host=ip, user=user_name, key_file=private_key_path) |
| 242 | node_info = NodeInfo(creds, {'testnode'}) |
| 243 | node_info.os_vm_id = vm_id |
| 244 | reused_nodes.append(node_info) |
| 245 | ctx.nodes_info.append(node_info) |
| 246 | |
| 247 | ctx.storage["reused_nodes"] = reused_nodes # type: ignore |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 248 | |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 249 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 250 | def get_OS_credentials(ctx: TestRun) -> start_vms.OSCreds: |
| 251 | |
| 252 | if "openstack_openrc" in ctx.storage: |
| 253 | return ctx.storage.load("openstack_openrc", start_vms.OSCreds) |
| 254 | |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 255 | creds = None |
koder aka kdanilov | b719743 | 2015-07-15 00:40:43 +0300 | [diff] [blame] | 256 | os_creds = None |
koder aka kdanilov | 8fbb27f | 2015-07-17 22:23:31 +0300 | [diff] [blame] | 257 | force_insecure = False |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 258 | cfg = ctx.config |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame] | 259 | |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 260 | if 'openstack' in cfg.clouds: |
| 261 | os_cfg = cfg.clouds['openstack'] |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 262 | if 'OPENRC' in os_cfg: |
| 263 | logger.info("Using OS credentials from " + os_cfg['OPENRC']) |
koder aka kdanilov | b719743 | 2015-07-15 00:40:43 +0300 | [diff] [blame] | 264 | creds_tuple = utils.get_creds_openrc(os_cfg['OPENRC']) |
| 265 | os_creds = start_vms.OSCreds(*creds_tuple) |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 266 | elif 'ENV' in os_cfg: |
| 267 | logger.info("Using OS credentials from shell environment") |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 268 | os_creds = start_vms.get_openstack_credentials() |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame] | 269 | elif 'OS_TENANT_NAME' in os_cfg: |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 270 | logger.info("Using predefined credentials") |
koder aka kdanilov | b719743 | 2015-07-15 00:40:43 +0300 | [diff] [blame] | 271 | os_creds = start_vms.OSCreds(os_cfg['OS_USERNAME'].strip(), |
| 272 | os_cfg['OS_PASSWORD'].strip(), |
| 273 | os_cfg['OS_TENANT_NAME'].strip(), |
| 274 | os_cfg['OS_AUTH_URL'].strip(), |
| 275 | os_cfg.get('OS_INSECURE', False)) |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 276 | |
koder aka kdanilov | 8fbb27f | 2015-07-17 22:23:31 +0300 | [diff] [blame] | 277 | elif 'OS_INSECURE' in os_cfg: |
| 278 | force_insecure = os_cfg.get('OS_INSECURE', False) |
| 279 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 280 | if os_creds is None and 'fuel' in cfg.clouds and 'openstack_env' in cfg.clouds['fuel'] and \ |
| 281 | ctx.fuel_openstack_creds is not None: |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 282 | logger.info("Using fuel creds") |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 283 | creds = ctx.fuel_openstack_creds |
koder aka kdanilov | b719743 | 2015-07-15 00:40:43 +0300 | [diff] [blame] | 284 | elif os_creds is None: |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 285 | logger.error("Can't found OS credentials") |
| 286 | raise utils.StopTestError("Can't found OS credentials", None) |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 287 | |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 288 | if creds is None: |
koder aka kdanilov | b719743 | 2015-07-15 00:40:43 +0300 | [diff] [blame] | 289 | creds = os_creds |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 290 | |
koder aka kdanilov | 8fbb27f | 2015-07-17 22:23:31 +0300 | [diff] [blame] | 291 | if force_insecure and not creds.insecure: |
| 292 | creds = start_vms.OSCreds(creds.name, |
| 293 | creds.passwd, |
| 294 | creds.tenant, |
| 295 | creds.auth_url, |
| 296 | True) |
| 297 | |
koder aka kdanilov | 05e15b9 | 2016-02-07 19:32:46 +0200 | [diff] [blame] | 298 | logger.debug(("OS_CREDS: user={0.name} tenant={0.tenant} " + |
koder aka kdanilov | b719743 | 2015-07-15 00:40:43 +0300 | [diff] [blame] | 299 | "auth_url={0.auth_url} insecure={0.insecure}").format(creds)) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 300 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 301 | ctx.storage["openstack_openrc"] = creds # type: ignore |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 302 | return creds |
koder aka kdanilov | 4e9f3ed | 2015-04-14 11:26:12 +0300 | [diff] [blame] | 303 | |
koder aka kdanilov | 1c2b511 | 2015-04-10 16:53:51 +0300 | [diff] [blame] | 304 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 305 | def get_vm_keypair_path(cfg: Config) -> Tuple[str, str]: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 306 | key_name = cfg.vm_configs['keypair_name'] |
| 307 | private_path = os.path.join(cfg.settings_dir, key_name + "_private.pem") |
| 308 | public_path = os.path.join(cfg.settings_dir, key_name + "_public.pub") |
| 309 | return (private_path, public_path) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 310 | |
| 311 | |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 312 | @contextlib.contextmanager |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 313 | def create_vms_ctx(ctx: TestRun, vm_config: ConfigBlock, already_has_count: int = 0) -> Iterator[List[NodeInfo]]: |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 314 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 315 | if 'spawned_vm_ids' in ctx.storage: |
| 316 | os_nodes_ids = ctx.storage.get('spawned_vm_ids', []) # type: List[int] |
| 317 | new_nodes = [] # type: List[NodeInfo] |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 318 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 319 | # TODO(koder): reconnect to old VM's |
| 320 | raise NotImplementedError("Reconnect to old vms is not implemented") |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 321 | else: |
| 322 | os_nodes_ids = [] |
| 323 | new_nodes = [] |
| 324 | no_spawn = False |
| 325 | if vm_config['count'].startswith('='): |
| 326 | count = int(vm_config['count'][1:]) |
| 327 | if count <= already_has_count: |
| 328 | logger.debug("Not need new vms") |
| 329 | no_spawn = True |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 330 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 331 | if not no_spawn: |
| 332 | ensure_connected_to_openstack(ctx) |
| 333 | params = ctx.config.vm_configs[vm_config['cfg_name']].copy() |
| 334 | params.update(vm_config) |
| 335 | params.update(get_vm_keypair_path(ctx.config)) |
| 336 | params['group_name'] = ctx.config.run_uuid |
| 337 | params['keypair_name'] = ctx.config.vm_configs['keypair_name'] |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 338 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 339 | if not vm_config.get('skip_preparation', False): |
| 340 | logger.info("Preparing openstack") |
| 341 | start_vms.prepare_os(ctx.os_connection, params) |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 342 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 343 | with ctx.get_pool() as pool: |
| 344 | for node_info in start_vms.launch_vms(ctx.os_connection, params, pool, already_has_count): |
| 345 | node_info.roles.add('testnode') |
| 346 | os_nodes_ids.append(node_info.os_vm_id) |
| 347 | new_nodes.append(node_info) |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 348 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 349 | ctx.storage['spawned_vm_ids'] = os_nodes_ids # type: ignore |
| 350 | yield new_nodes |
| 351 | |
| 352 | # keep nodes in case of error for future test restart |
| 353 | if not ctx.config.keep_vm: |
| 354 | shut_down_vms_stage(ctx, os_nodes_ids) |
| 355 | |
| 356 | del ctx.storage['spawned_vm_ids'] |
| 357 | |
| 358 | |
| 359 | @contextlib.contextmanager |
| 360 | def sensor_monitoring(ctx: TestRun, cfg: ConfigBlock, nodes: List[IRPCNode]) -> Iterator[None]: |
| 361 | yield |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 362 | |
| 363 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 364 | def run_tests_stage(ctx: TestRun) -> None: |
| 365 | for group in ctx.config.get('tests', []): |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 366 | gitems = list(group.items()) |
| 367 | if len(gitems) != 1: |
koder aka kdanilov | 170936a | 2015-06-27 22:51:17 +0300 | [diff] [blame] | 368 | msg = "Items in tests section should have len == 1" |
| 369 | logger.error(msg) |
| 370 | raise utils.StopTestError(msg) |
| 371 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 372 | key, config = gitems[0] |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 373 | |
| 374 | if 'start_test_nodes' == key: |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 375 | if 'openstack' not in config: |
| 376 | msg = "No openstack block in config - can't spawn vm's" |
| 377 | logger.error(msg) |
| 378 | raise utils.StopTestError(msg) |
| 379 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 380 | num_test_nodes = len([node for node in ctx.nodes if 'testnode' in node.info.roles]) |
| 381 | vm_ctx = create_vms_ctx(ctx, config['openstack'], num_test_nodes) |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 382 | tests = config.get('tests', []) |
| 383 | else: |
| 384 | vm_ctx = utils.empty_ctx([]) |
| 385 | tests = [group] |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 386 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 387 | # make mypy happy |
| 388 | new_nodes = [] # type: List[NodeInfo] |
| 389 | |
| 390 | with vm_ctx as new_nodes: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 391 | if new_nodes: |
| 392 | with ctx.get_pool() as pool: |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 393 | new_rpc_nodes = connect_all(new_nodes, pool) |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 394 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 395 | test_nodes = ctx.nodes + new_rpc_nodes |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 396 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 397 | if ctx.config.get('sensors'): |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 398 | sensor_ctx = sensor_monitoring(ctx, ctx.config.get('sensors'), test_nodes) |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 399 | else: |
| 400 | sensor_ctx = utils.empty_ctx([]) |
| 401 | |
| 402 | if not ctx.config.no_tests: |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 403 | for test_group in tests: |
| 404 | with sensor_ctx: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 405 | run_tests(ctx, test_group, test_nodes) |
| 406 | |
| 407 | for node in new_rpc_nodes: |
| 408 | node.disconnect() |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 409 | |
gstepanov | 023c1e4 | 2015-04-08 15:50:19 +0300 | [diff] [blame] | 410 | |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame^] | 411 | def clouds_connect_stage(ctx: TestRun) -> None: |
| 412 | # TODO(koder): need to use this to connect to openstack in upper code |
| 413 | # conn = ctx.config['clouds/openstack'] |
| 414 | # user, passwd, tenant = parse_creds(conn['creds']) |
| 415 | # auth_data = dict(auth_url=conn['auth_url'], |
| 416 | # username=user, |
| 417 | # api_key=passwd, |
| 418 | # project_id=tenant) # type: Dict[str, str] |
| 419 | # logger.debug("Discovering openstack nodes with connection details: %r", conn) |
| 420 | # connect to openstack, fuel |
| 421 | |
| 422 | # # parse FUEL REST credentials |
| 423 | # username, tenant_name, password = parse_creds(fuel_data['creds']) |
| 424 | # creds = {"username": username, |
| 425 | # "tenant_name": tenant_name, |
| 426 | # "password": password} |
| 427 | # |
| 428 | # # connect to FUEL |
| 429 | # conn = fuel_rest_api.KeystoneAuth(fuel_data['url'], creds, headers=None) |
| 430 | pass |
| 431 | |
| 432 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 433 | def shut_down_vms_stage(ctx: TestRun, nodes_ids: List[int]) -> None: |
| 434 | if nodes_ids: |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 435 | logger.info("Removing nodes") |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 436 | start_vms.clear_nodes(ctx.os_connection, nodes_ids) |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 437 | logger.info("Nodes has been removed") |
gstepanov | 023c1e4 | 2015-04-08 15:50:19 +0300 | [diff] [blame] | 438 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 439 | |
| 440 | def clear_enviroment(ctx: TestRun) -> None: |
| 441 | shut_down_vms_stage(ctx, ctx.storage.get('spawned_vm_ids', [])) |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 442 | ctx.storage['spawned_vm_ids'] = [] # type: ignore |
gstepanov | 023c1e4 | 2015-04-08 15:50:19 +0300 | [diff] [blame] | 443 | |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 444 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 445 | def disconnect_stage(ctx: TestRun) -> None: |
| 446 | # TODO(koder): what next line was for? |
| 447 | # ssh_utils.close_all_sessions() |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 448 | |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 449 | for node in ctx.nodes: |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 450 | node.disconnect() |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 451 | |
| 452 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 453 | def console_report_stage(ctx: TestRun) -> None: |
| 454 | # TODO(koder): load data from storage |
| 455 | raise NotImplementedError("...") |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 456 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 457 | def html_report_stage(ctx: TestRun) -> None: |
| 458 | # TODO(koder): load data from storage |
| 459 | raise NotImplementedError("...") |