blob: c4581d3b9cda17dc4f335e3565f470a4635487b7 [file] [log] [blame]
koder aka kdanilov962ee5f2016-12-19 02:40:08 +02001import time
koder aka kdanilov23e6bdf2016-12-24 02:18:54 +02002import json
kdanylov aka koder736e5c12017-05-07 17:27:14 +03003import copy
koder aka kdanilove21d7472015-02-14 19:02:04 -08004import logging
koder aka kdanilov39e449e2016-12-17 15:15:26 +02005from concurrent.futures import Future
6from typing import List, Dict, Tuple, Optional, Union, cast
koder aka kdanilov88407ff2015-05-26 15:35:57 +03007
koder aka kdanilov39e449e2016-12-17 15:15:26 +02008from . import utils, ssh_utils, hw_info
9from .config import ConfigBlock
koder aka kdanilov73084622016-11-16 21:51:08 +020010from .node import setup_rpc, connect
koder aka kdanilov7f59d562016-12-26 01:34:23 +020011from .node_interfaces import NodeInfo, IRPCNode
koder aka kdanilov39e449e2016-12-17 15:15:26 +020012from .stage import Stage, StepOrder
koder aka kdanilov7f59d562016-12-26 01:34:23 +020013from .sensors import collect_sensors_data
koder aka kdanilov108ac362017-01-19 20:17:16 +020014from .suits.all_suits import all_suits
koder aka kdanilov39e449e2016-12-17 15:15:26 +020015from .test_run_class import TestRun
koder aka kdanilov7f59d562016-12-26 01:34:23 +020016from .utils import StopTestError
koder aka kdanilova732a602017-02-01 20:29:56 +020017from .result_classes import SuiteConfig
koder aka kdanilov108ac362017-01-19 20:17:16 +020018from .hlstorage import ResultStorage
koder aka kdanilov63ad2062015-04-27 13:11:40 +030019
koder aka kdanilov57ce4db2015-04-25 21:25:51 +030020
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030021logger = logging.getLogger("wally")
koder aka kdanilovcee43342015-04-14 22:52:53 +030022
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080023
koder aka kdanilov39e449e2016-12-17 15:15:26 +020024class ConnectStage(Stage):
25 """Connect to nodes stage"""
koder aka kdanilove21d7472015-02-14 19:02:04 -080026
koder aka kdanilov39e449e2016-12-17 15:15:26 +020027 priority = StepOrder.CONNECT
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030028
koder aka kdanilov39e449e2016-12-17 15:15:26 +020029 def run(self, ctx: TestRun) -> None:
koder aka kdanilov73084622016-11-16 21:51:08 +020030 with ctx.get_pool() as pool:
koder aka kdanilov39e449e2016-12-17 15:15:26 +020031 logger.info("Connecting to %s nodes", len(ctx.nodes_info))
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030032
koder aka kdanilov39e449e2016-12-17 15:15:26 +020033 def connect_ext(node_info: NodeInfo) -> Tuple[bool, Union[IRPCNode, NodeInfo]]:
34 try:
35 ssh_node = connect(node_info, conn_timeout=ctx.config.connect_timeout)
koder aka kdanilov23e6bdf2016-12-24 02:18:54 +020036
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020037 return True, setup_rpc(ssh_node,
38 ctx.rpc_code,
39 ctx.default_rpc_plugins,
40 log_level=ctx.config.rpc_log_level)
koder aka kdanilov39e449e2016-12-17 15:15:26 +020041 except Exception as exc:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020042 logger.exception("During connect to %s: %s", node_info, exc)
koder aka kdanilov39e449e2016-12-17 15:15:26 +020043 return False, node_info
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030044
koder aka kdanilov39e449e2016-12-17 15:15:26 +020045 failed_testnodes = [] # type: List[NodeInfo]
46 failed_nodes = [] # type: List[NodeInfo]
47 ctx.nodes = []
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030048
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020049 for ok, node in pool.map(connect_ext, ctx.nodes_info.values()):
koder aka kdanilov39e449e2016-12-17 15:15:26 +020050 if not ok:
51 node = cast(NodeInfo, node)
52 if 'testnode' in node.roles:
53 failed_testnodes.append(node)
54 else:
55 failed_nodes.append(node)
56 else:
57 ctx.nodes.append(cast(IRPCNode, node))
koder aka kdanilov22d134e2016-11-08 11:33:19 +020058
koder aka kdanilov39e449e2016-12-17 15:15:26 +020059 if failed_nodes:
60 msg = "Node(s) {} would be excluded - can't connect"
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020061 logger.warning(msg.format(", ".join(map(str, failed_nodes))))
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030062
koder aka kdanilov39e449e2016-12-17 15:15:26 +020063 if failed_testnodes:
koder aka kdanilov23e6bdf2016-12-24 02:18:54 +020064 msg = "Can't start RPC on testnode(s) " + ",".join(map(str, failed_testnodes))
koder aka kdanilovc368eb62015-04-28 18:22:01 +030065 logger.error(msg)
66 raise utils.StopTestError(msg)
67
koder aka kdanilov39e449e2016-12-17 15:15:26 +020068 if not failed_nodes:
69 logger.info("All nodes connected successfully")
koder aka kdanilovcee43342015-04-14 22:52:53 +030070
koder aka kdanilov39e449e2016-12-17 15:15:26 +020071 def cleanup(self, ctx: TestRun) -> None:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020072 if ctx.config.get("download_rpc_logs", False):
73 for node in ctx.nodes:
74 if node.rpc_log_file is not None:
koder aka kdanilov108ac362017-01-19 20:17:16 +020075 nid = node.node_id
koder aka kdanilova732a602017-02-01 20:29:56 +020076 path = "rpc_logs/{}.txt".format(nid)
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020077 node.conn.server.flush_logs()
78 log = node.get_file_content(node.rpc_log_file)
koder aka kdanilov7f59d562016-12-26 01:34:23 +020079 if path in ctx.storage:
koder aka kdanilovffaf48d2016-12-27 02:25:29 +020080 ctx.storage.append_raw(log, path)
koder aka kdanilov7f59d562016-12-26 01:34:23 +020081 else:
koder aka kdanilovffaf48d2016-12-27 02:25:29 +020082 ctx.storage.put_raw(log, path)
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020083 logger.debug("RPC log from node {} stored into storage::{}".format(nid, path))
84
85 with ctx.get_pool() as pool:
86 list(pool.map(lambda node: node.disconnect(stop=True), ctx.nodes))
koder aka kdanilovcee43342015-04-14 22:52:53 +030087
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030088
koder aka kdanilov39e449e2016-12-17 15:15:26 +020089class CollectInfoStage(Stage):
90 """Collect node info"""
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +020091
koder aka kdanilov23e6bdf2016-12-24 02:18:54 +020092 priority = StepOrder.START_SENSORS - 2
koder aka kdanilov39e449e2016-12-17 15:15:26 +020093 config_block = 'collect_info'
94
95 def run(self, ctx: TestRun) -> None:
96 if not ctx.config.collect_info:
97 return
98
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020099 futures = {} # type: Dict[Tuple[str, str], Future]
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200100
101 with ctx.get_pool() as pool:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200102 # can't make next RPC request until finish with previous
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200103 for node in ctx.nodes:
koder aka kdanilov108ac362017-01-19 20:17:16 +0200104 nid = node.node_id
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200105 hw_info_path = "hw_info/{}".format(nid)
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200106 if hw_info_path not in ctx.storage:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200107 futures[(hw_info_path, nid)] = pool.submit(hw_info.get_hw_info, node)
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200108
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200109 for (path, nid), future in futures.items():
110 try:
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200111 ctx.storage.put(future.result(), path)
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200112 except Exception:
113 logger.exception("During collecting hardware info from %s", nid)
114 raise utils.StopTestError()
115
116 futures.clear()
117 for node in ctx.nodes:
koder aka kdanilov108ac362017-01-19 20:17:16 +0200118 nid = node.node_id
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200119 sw_info_path = "sw_info/{}".format(nid)
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200120 if sw_info_path not in ctx.storage:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200121 futures[(sw_info_path, nid)] = pool.submit(hw_info.get_sw_info, node)
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200122
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200123 for (path, nid), future in futures.items():
124 try:
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200125 ctx.storage.put(future.result(), path)
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200126 except Exception:
127 logger.exception("During collecting software info from %s", nid)
128 raise utils.StopTestError()
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200129
130
131class ExplicitNodesStage(Stage):
132 """add explicit nodes"""
133
134 priority = StepOrder.DISCOVER
135 config_block = 'nodes'
136
137 def run(self, ctx: TestRun) -> None:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200138 if 'all_nodes' in ctx.storage:
139 logger.info("Skip explicid nodes filling, as all_nodes all ready in storage")
140 return
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200141
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +0200142 for url, roles in ctx.config.get('nodes', {}).raw().items():
kdanylov aka koder150b2192017-04-01 16:53:01 +0300143 ctx.merge_node(ssh_utils.parse_ssh_uri(url), set(role.strip() for role in roles.split(",")))
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +0200144 logger.debug("Add node %s with roles %s", url, roles)
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200145
146
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200147class SleepStage(Stage):
148 """Save nodes list to file"""
149
150 priority = StepOrder.TEST
151 config_block = 'sleep'
152
153 def run(self, ctx: TestRun) -> None:
154 logger.debug("Will sleep for %r seconds", ctx.config.sleep)
155 time.sleep(ctx.config.sleep)
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200156
157
koder aka kdanilov23e6bdf2016-12-24 02:18:54 +0200158class PrepareNodes(Stage):
159 priority = StepOrder.START_SENSORS - 1
160
161 def __init__(self):
162 Stage.__init__(self)
163 self.nodeepscrub_updated = False
164 self.noscrub_updated = False
165
166 def run(self, ctx: TestRun) -> None:
167 ceph_sett = ctx.config.get('ceph_settings', "").split()
168 if ceph_sett:
169 for node in ctx.nodes:
170 if "ceph-mon" in node.info.roles or "ceph-osd" in node.info.roles:
171 state = json.loads(node.run("ceph health --format json"))["summary"]["summary"]
172 if 'noscrub' in ceph_sett:
173 if 'noscrub' in state:
174 logger.debug("noscrub already set on cluster")
175 else:
176 logger.info("Applying noscrub settings to ceph cluster")
177 node.run("ceph osd set noscrub")
178 self.noscrub_updated = True
179
180 if 'nodeepscrub' in ceph_sett:
181 if 'nodeepscrub' in state:
182 logger.debug("noscrub already set on cluster")
183 else:
184 logger.info("Applying noscrub settings to ceph cluster")
185 node.run("ceph osd set noscrub")
186 self.nodeepscrub_updated = True
187 break
188
189 def cleanup(self, ctx: TestRun) -> None:
190 if self.nodeepscrub_updated or self.noscrub_updated:
191 for node in ctx.nodes:
192 if "ceph-mon" in node.info.roles or "ceph-osd" in node.info.roles :
193 if self.noscrub_updated:
194 logger.info("Reverting noscrub setting for ceph cluster")
195 node.run("ceph osd unset noscrub")
196 self.noscrub_updated = False
197
198 if self.nodeepscrub_updated:
199 logger.info("Reverting noscrub setting for ceph cluster")
200 node.run("ceph osd unset nodeepscrub")
201 self.nodeepscrub_updated = False
202
203
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200204class RunTestsStage(Stage):
205
206 priority = StepOrder.TEST
207 config_block = 'tests'
208
209 def run(self, ctx: TestRun) -> None:
koder aka kdanilovf2865172016-12-30 03:35:11 +0200210 if ctx.config.no_tests:
211 logger.info("Skiping tests, as 'no_tests' config settings is True")
212 return
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +0200213
koder aka kdanilovf2865172016-12-30 03:35:11 +0200214 for suite_idx, test_suite in enumerate(ctx.config.get('tests', [])):
215 test_nodes = [node for node in ctx.nodes if 'testnode' in node.info.roles]
koder aka kdanilovda45e882015-04-06 02:24:42 +0300216
koder aka kdanilovf2865172016-12-30 03:35:11 +0200217 if not test_nodes:
218 logger.error("No test nodes found")
219 raise StopTestError()
gstepanov023c1e42015-04-08 15:50:19 +0300220
koder aka kdanilovf2865172016-12-30 03:35:11 +0200221 if len(test_suite) != 1:
222 logger.error("Test suite %s contain more than one test. Put each test in separated group", suite_idx)
223 raise StopTestError()
koder aka kdanilov70227062016-11-26 23:23:21 +0200224
koder aka kdanilovf2865172016-12-30 03:35:11 +0200225 name, params = list(test_suite.items())[0]
226 vm_count = params.get('node_limit', None) # type: Optional[int]
koder aka kdanilov70227062016-11-26 23:23:21 +0200227
koder aka kdanilovf2865172016-12-30 03:35:11 +0200228 # select test nodes
229 if vm_count is None:
230 curr_test_nodes = test_nodes
231 else:
232 curr_test_nodes = test_nodes[:vm_count]
koder aka kdanilov70227062016-11-26 23:23:21 +0200233
koder aka kdanilovf2865172016-12-30 03:35:11 +0200234 if not curr_test_nodes:
235 logger.error("No nodes found for test, skipping it.")
236 continue
237
kdanylov aka koder150b2192017-04-01 16:53:01 +0300238 if name not in all_suits:
239 logger.error("Test suite %r not found. Only suits [%s] available", name, ", ".join(all_suits))
240 raise StopTestError()
241
koder aka kdanilov108ac362017-01-19 20:17:16 +0200242 test_cls = all_suits[name]
koder aka kdanilovf2865172016-12-30 03:35:11 +0200243 remote_dir = ctx.config.default_test_local_folder.format(name=name, uuid=ctx.config.run_uuid)
koder aka kdanilova732a602017-02-01 20:29:56 +0200244 suite = SuiteConfig(test_cls.name,
245 params=params,
246 run_uuid=ctx.config.run_uuid,
247 nodes=test_nodes,
248 remote_dir=remote_dir,
249 idx=suite_idx,
250 keep_raw_files=ctx.config.keep_raw_files)
koder aka kdanilovf2865172016-12-30 03:35:11 +0200251
koder aka kdanilov108ac362017-01-19 20:17:16 +0200252 test_cls(storage=ResultStorage(ctx.storage),
253 suite=suite,
koder aka kdanilovf2865172016-12-30 03:35:11 +0200254 on_idle=lambda: collect_sensors_data(ctx, False)).run()
gstepanov023c1e42015-04-08 15:50:19 +0300255
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200256 @classmethod
257 def validate_config(cls, cfg: ConfigBlock) -> None:
258 pass
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200259
260
kdanylov aka koder736e5c12017-05-07 17:27:14 +0300261class SaveNodesStage(Stage):
262 """Save nodes list to file"""
263 nodes_path = 'all_nodes'
264 params_path = 'all_nodes_params.js'
265 priority = StepOrder.UPDATE_NODES_INFO + 1
266
267 def run(self, ctx: TestRun) -> None:
268 infos = list(ctx.nodes_info.values())
269 params = {node.node_id: node.params for node in infos}
270 ninfos = [copy.copy(node) for node in infos]
271 for node in ninfos:
272 node.params = "in {!r} file".format(self.params_path)
273 ctx.storage.put_list(ninfos, self.nodes_path)
274 ctx.storage.put_raw(json.dumps(params).encode('utf8'), self.params_path)
275
276
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200277class LoadStoredNodesStage(Stage):
278 priority = StepOrder.DISCOVER
279
280 def run(self, ctx: TestRun) -> None:
kdanylov aka koder736e5c12017-05-07 17:27:14 +0300281 if SaveNodesStage.nodes_path in ctx.storage:
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200282 if ctx.nodes_info:
283 logger.error("Internal error: Some nodes already stored in " +
284 "nodes_info before LoadStoredNodesStage stage")
285 raise StopTestError()
kdanylov aka koder736e5c12017-05-07 17:27:14 +0300286
287 nodes = {node.node_id: node for node in ctx.storage.load_list(NodeInfo, SaveNodesStage.nodes_path)}
288
289 if SaveNodesStage.params_path in ctx.storage:
290 params = json.loads(ctx.storage.get_raw(SaveNodesStage.params_path).decode('utf8'))
291 for node_id, node in nodes.items():
292 node.params = params.get(node_id, {})
293
294 ctx.nodes_info = nodes
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200295 logger.info("%s nodes loaded from database", len(ctx.nodes_info))