blob: 4e02a755e90f1712aef2654cf7e2e51c79e11442 [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 kdanilova732a602017-02-01 20:29:56 +020016from .result_classes import SuiteConfig
koder aka kdanilov63ad2062015-04-27 13:11:40 +030017
koder aka kdanilov57ce4db2015-04-25 21:25:51 +030018
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030019logger = logging.getLogger("wally")
koder aka kdanilovcee43342015-04-14 22:52:53 +030020
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080021
koder aka kdanilov39e449e2016-12-17 15:15:26 +020022class ConnectStage(Stage):
23 """Connect to nodes stage"""
koder aka kdanilove21d7472015-02-14 19:02:04 -080024
koder aka kdanilov39e449e2016-12-17 15:15:26 +020025 priority = StepOrder.CONNECT
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030026
koder aka kdanilov39e449e2016-12-17 15:15:26 +020027 def run(self, ctx: TestRun) -> None:
koder aka kdanilov73084622016-11-16 21:51:08 +020028 with ctx.get_pool() as pool:
koder aka kdanilov39e449e2016-12-17 15:15:26 +020029 logger.info("Connecting to %s nodes", len(ctx.nodes_info))
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030030
koder aka kdanilov39e449e2016-12-17 15:15:26 +020031 def connect_ext(node_info: NodeInfo) -> Tuple[bool, Union[IRPCNode, NodeInfo]]:
32 try:
33 ssh_node = connect(node_info, conn_timeout=ctx.config.connect_timeout)
koder aka kdanilov23e6bdf2016-12-24 02:18:54 +020034
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020035 return True, setup_rpc(ssh_node,
36 ctx.rpc_code,
37 ctx.default_rpc_plugins,
38 log_level=ctx.config.rpc_log_level)
koder aka kdanilov39e449e2016-12-17 15:15:26 +020039 except Exception as exc:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020040 logger.exception("During connect to %s: %s", node_info, exc)
koder aka kdanilov39e449e2016-12-17 15:15:26 +020041 return False, node_info
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030042
koder aka kdanilov39e449e2016-12-17 15:15:26 +020043 failed_testnodes = [] # type: List[NodeInfo]
44 failed_nodes = [] # type: List[NodeInfo]
45 ctx.nodes = []
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +030046
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020047 for ok, node in pool.map(connect_ext, ctx.nodes_info.values()):
koder aka kdanilov39e449e2016-12-17 15:15:26 +020048 if not ok:
49 node = cast(NodeInfo, node)
50 if 'testnode' in node.roles:
51 failed_testnodes.append(node)
52 else:
53 failed_nodes.append(node)
54 else:
55 ctx.nodes.append(cast(IRPCNode, node))
koder aka kdanilov22d134e2016-11-08 11:33:19 +020056
koder aka kdanilov39e449e2016-12-17 15:15:26 +020057 if failed_nodes:
58 msg = "Node(s) {} would be excluded - can't connect"
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020059 logger.warning(msg.format(", ".join(map(str, failed_nodes))))
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030060
koder aka kdanilov39e449e2016-12-17 15:15:26 +020061 if failed_testnodes:
koder aka kdanilov23e6bdf2016-12-24 02:18:54 +020062 msg = "Can't start RPC on testnode(s) " + ",".join(map(str, failed_testnodes))
koder aka kdanilovc368eb62015-04-28 18:22:01 +030063 logger.error(msg)
64 raise utils.StopTestError(msg)
65
koder aka kdanilov39e449e2016-12-17 15:15:26 +020066 if not failed_nodes:
67 logger.info("All nodes connected successfully")
koder aka kdanilovcee43342015-04-14 22:52:53 +030068
kdanylov aka koder3a9e5db2017-05-09 20:00:44 +030069 def get_time(node):
70 return node.conn.sys.time()
71
72 t_start = time.time()
73 tms = pool.map(get_time, ctx.nodes)
74 t_end = time.time()
75
76 for node, val in zip(ctx.nodes, tms):
kdanylov aka koderb0833332017-05-13 20:39:17 +030077 delta = 0
78 if val > t_end:
79 delta = val - t_end
80 elif t_start > val:
81 delta = t_start - val
82
83 if delta > ctx.config.max_time_diff_ms:
kdanylov aka koder3a9e5db2017-05-09 20:00:44 +030084 msg = ("Too large time shift {}ms on node {}. Stopping test." +
85 " Fix time on cluster nodes and restart test, or change " +
kdanylov aka koderb0833332017-05-13 20:39:17 +030086 "max_time_diff_ms(={}ms) setting in config").format(delta,
kdanylov aka koder3a9e5db2017-05-09 20:00:44 +030087 str(node),
88 ctx.config.max_time_diff_ms)
89 logger.error(msg)
kdanylov aka koderb0833332017-05-13 20:39:17 +030090 raise utils.StopTestError(msg)
91 if delta > 0:
92 logger.warning("Node %s has time shift at least %s ms", node, delta)
kdanylov aka koder3a9e5db2017-05-09 20:00:44 +030093
94
koder aka kdanilov39e449e2016-12-17 15:15:26 +020095 def cleanup(self, ctx: TestRun) -> None:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020096 if ctx.config.get("download_rpc_logs", False):
kdanylov aka koder3a9e5db2017-05-09 20:00:44 +030097 logger.info("Killing all outstanding processes")
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020098 for node in ctx.nodes:
kdanylov aka koder3a9e5db2017-05-09 20:00:44 +030099 node.conn.cli.killall()
100
101 logger.info("Downloading RPC servers logs")
102 for node in ctx.nodes:
103 node.conn.cli.killall()
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200104 if node.rpc_log_file is not None:
koder aka kdanilov108ac362017-01-19 20:17:16 +0200105 nid = node.node_id
koder aka kdanilova732a602017-02-01 20:29:56 +0200106 path = "rpc_logs/{}.txt".format(nid)
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200107 node.conn.server.flush_logs()
108 log = node.get_file_content(node.rpc_log_file)
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200109 if path in ctx.storage:
koder aka kdanilovffaf48d2016-12-27 02:25:29 +0200110 ctx.storage.append_raw(log, path)
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200111 else:
koder aka kdanilovffaf48d2016-12-27 02:25:29 +0200112 ctx.storage.put_raw(log, path)
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200113 logger.debug("RPC log from node {} stored into storage::{}".format(nid, path))
114
kdanylov aka koder3a9e5db2017-05-09 20:00:44 +0300115 logger.info("Disconnecting")
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200116 with ctx.get_pool() as pool:
117 list(pool.map(lambda node: node.disconnect(stop=True), ctx.nodes))
koder aka kdanilovcee43342015-04-14 22:52:53 +0300118
koder aka kdanilov0fdaaee2015-06-30 11:10:48 +0300119
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200120class CollectInfoStage(Stage):
121 """Collect node info"""
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +0200122
koder aka kdanilov23e6bdf2016-12-24 02:18:54 +0200123 priority = StepOrder.START_SENSORS - 2
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200124 config_block = 'collect_info'
125
126 def run(self, ctx: TestRun) -> None:
127 if not ctx.config.collect_info:
128 return
129
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200130 futures = {} # type: Dict[Tuple[str, str], Future]
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200131
132 with ctx.get_pool() as pool:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200133 # can't make next RPC request until finish with previous
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200134 for node in ctx.nodes:
koder aka kdanilov108ac362017-01-19 20:17:16 +0200135 nid = node.node_id
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200136 hw_info_path = "hw_info/{}".format(nid)
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200137 if hw_info_path not in ctx.storage:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200138 futures[(hw_info_path, nid)] = pool.submit(hw_info.get_hw_info, node)
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200139
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200140 for (path, nid), future in futures.items():
141 try:
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200142 ctx.storage.put(future.result(), path)
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200143 except Exception:
144 logger.exception("During collecting hardware info from %s", nid)
145 raise utils.StopTestError()
146
147 futures.clear()
148 for node in ctx.nodes:
koder aka kdanilov108ac362017-01-19 20:17:16 +0200149 nid = node.node_id
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200150 sw_info_path = "sw_info/{}".format(nid)
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200151 if sw_info_path not in ctx.storage:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200152 futures[(sw_info_path, nid)] = pool.submit(hw_info.get_sw_info, node)
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200153
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200154 for (path, nid), future in futures.items():
155 try:
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200156 ctx.storage.put(future.result(), path)
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200157 except Exception:
158 logger.exception("During collecting software info from %s", nid)
159 raise utils.StopTestError()
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200160
161
162class ExplicitNodesStage(Stage):
163 """add explicit nodes"""
164
165 priority = StepOrder.DISCOVER
166 config_block = 'nodes'
167
168 def run(self, ctx: TestRun) -> None:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200169 if 'all_nodes' in ctx.storage:
170 logger.info("Skip explicid nodes filling, as all_nodes all ready in storage")
171 return
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200172
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +0200173 for url, roles in ctx.config.get('nodes', {}).raw().items():
kdanylov aka koder150b2192017-04-01 16:53:01 +0300174 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 +0200175 logger.debug("Add node %s with roles %s", url, roles)
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200176
177
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200178class SleepStage(Stage):
179 """Save nodes list to file"""
180
181 priority = StepOrder.TEST
182 config_block = 'sleep'
183
184 def run(self, ctx: TestRun) -> None:
185 logger.debug("Will sleep for %r seconds", ctx.config.sleep)
kdanylov aka koder3a9e5db2017-05-09 20:00:44 +0300186 stime = time.time()
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200187 time.sleep(ctx.config.sleep)
kdanylov aka koder3a9e5db2017-05-09 20:00:44 +0300188 ctx.storage.put([int(stime), int(time.time())], 'idle')
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200189
190
koder aka kdanilov23e6bdf2016-12-24 02:18:54 +0200191class PrepareNodes(Stage):
192 priority = StepOrder.START_SENSORS - 1
193
194 def __init__(self):
195 Stage.__init__(self)
196 self.nodeepscrub_updated = False
197 self.noscrub_updated = False
198
199 def run(self, ctx: TestRun) -> None:
200 ceph_sett = ctx.config.get('ceph_settings', "").split()
201 if ceph_sett:
202 for node in ctx.nodes:
203 if "ceph-mon" in node.info.roles or "ceph-osd" in node.info.roles:
204 state = json.loads(node.run("ceph health --format json"))["summary"]["summary"]
205 if 'noscrub' in ceph_sett:
206 if 'noscrub' in state:
207 logger.debug("noscrub already set on cluster")
208 else:
209 logger.info("Applying noscrub settings to ceph cluster")
210 node.run("ceph osd set noscrub")
211 self.noscrub_updated = True
212
213 if 'nodeepscrub' in ceph_sett:
214 if 'nodeepscrub' in state:
215 logger.debug("noscrub already set on cluster")
216 else:
217 logger.info("Applying noscrub settings to ceph cluster")
218 node.run("ceph osd set noscrub")
219 self.nodeepscrub_updated = True
220 break
221
222 def cleanup(self, ctx: TestRun) -> None:
223 if self.nodeepscrub_updated or self.noscrub_updated:
224 for node in ctx.nodes:
225 if "ceph-mon" in node.info.roles or "ceph-osd" in node.info.roles :
226 if self.noscrub_updated:
227 logger.info("Reverting noscrub setting for ceph cluster")
228 node.run("ceph osd unset noscrub")
229 self.noscrub_updated = False
230
231 if self.nodeepscrub_updated:
232 logger.info("Reverting noscrub setting for ceph cluster")
233 node.run("ceph osd unset nodeepscrub")
234 self.nodeepscrub_updated = False
235
236
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200237class RunTestsStage(Stage):
238
239 priority = StepOrder.TEST
240 config_block = 'tests'
241
242 def run(self, ctx: TestRun) -> None:
koder aka kdanilovf2865172016-12-30 03:35:11 +0200243 if ctx.config.no_tests:
244 logger.info("Skiping tests, as 'no_tests' config settings is True")
245 return
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +0200246
koder aka kdanilovf2865172016-12-30 03:35:11 +0200247 for suite_idx, test_suite in enumerate(ctx.config.get('tests', [])):
248 test_nodes = [node for node in ctx.nodes if 'testnode' in node.info.roles]
koder aka kdanilovda45e882015-04-06 02:24:42 +0300249
koder aka kdanilovf2865172016-12-30 03:35:11 +0200250 if not test_nodes:
251 logger.error("No test nodes found")
kdanylov aka koderb0833332017-05-13 20:39:17 +0300252 raise utils.StopTestError()
gstepanov023c1e42015-04-08 15:50:19 +0300253
koder aka kdanilovf2865172016-12-30 03:35:11 +0200254 if len(test_suite) != 1:
255 logger.error("Test suite %s contain more than one test. Put each test in separated group", suite_idx)
kdanylov aka koderb0833332017-05-13 20:39:17 +0300256 raise utils.StopTestError()
koder aka kdanilov70227062016-11-26 23:23:21 +0200257
koder aka kdanilovf2865172016-12-30 03:35:11 +0200258 name, params = list(test_suite.items())[0]
259 vm_count = params.get('node_limit', None) # type: Optional[int]
koder aka kdanilov70227062016-11-26 23:23:21 +0200260
koder aka kdanilovf2865172016-12-30 03:35:11 +0200261 # select test nodes
262 if vm_count is None:
263 curr_test_nodes = test_nodes
264 else:
265 curr_test_nodes = test_nodes[:vm_count]
koder aka kdanilov70227062016-11-26 23:23:21 +0200266
koder aka kdanilovf2865172016-12-30 03:35:11 +0200267 if not curr_test_nodes:
268 logger.error("No nodes found for test, skipping it.")
269 continue
270
kdanylov aka koder150b2192017-04-01 16:53:01 +0300271 if name not in all_suits:
272 logger.error("Test suite %r not found. Only suits [%s] available", name, ", ".join(all_suits))
kdanylov aka koderb0833332017-05-13 20:39:17 +0300273 raise utils.StopTestError()
kdanylov aka koder150b2192017-04-01 16:53:01 +0300274
koder aka kdanilov108ac362017-01-19 20:17:16 +0200275 test_cls = all_suits[name]
koder aka kdanilovf2865172016-12-30 03:35:11 +0200276 remote_dir = ctx.config.default_test_local_folder.format(name=name, uuid=ctx.config.run_uuid)
koder aka kdanilova732a602017-02-01 20:29:56 +0200277 suite = SuiteConfig(test_cls.name,
278 params=params,
279 run_uuid=ctx.config.run_uuid,
280 nodes=test_nodes,
281 remote_dir=remote_dir,
282 idx=suite_idx,
283 keep_raw_files=ctx.config.keep_raw_files)
koder aka kdanilovf2865172016-12-30 03:35:11 +0200284
kdanylov aka koderb0833332017-05-13 20:39:17 +0300285 test_cls(storage=ctx.rstorage,
koder aka kdanilov108ac362017-01-19 20:17:16 +0200286 suite=suite,
koder aka kdanilovf2865172016-12-30 03:35:11 +0200287 on_idle=lambda: collect_sensors_data(ctx, False)).run()
gstepanov023c1e42015-04-08 15:50:19 +0300288
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200289 @classmethod
290 def validate_config(cls, cfg: ConfigBlock) -> None:
291 pass
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200292
293
kdanylov aka koder736e5c12017-05-07 17:27:14 +0300294class SaveNodesStage(Stage):
295 """Save nodes list to file"""
296 nodes_path = 'all_nodes'
297 params_path = 'all_nodes_params.js'
298 priority = StepOrder.UPDATE_NODES_INFO + 1
299
300 def run(self, ctx: TestRun) -> None:
301 infos = list(ctx.nodes_info.values())
302 params = {node.node_id: node.params for node in infos}
303 ninfos = [copy.copy(node) for node in infos]
304 for node in ninfos:
305 node.params = "in {!r} file".format(self.params_path)
306 ctx.storage.put_list(ninfos, self.nodes_path)
307 ctx.storage.put_raw(json.dumps(params).encode('utf8'), self.params_path)
308
309
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200310class LoadStoredNodesStage(Stage):
311 priority = StepOrder.DISCOVER
312
313 def run(self, ctx: TestRun) -> None:
kdanylov aka koder736e5c12017-05-07 17:27:14 +0300314 if SaveNodesStage.nodes_path in ctx.storage:
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200315 if ctx.nodes_info:
316 logger.error("Internal error: Some nodes already stored in " +
317 "nodes_info before LoadStoredNodesStage stage")
kdanylov aka koderb0833332017-05-13 20:39:17 +0300318 raise utils.StopTestError()
kdanylov aka koder736e5c12017-05-07 17:27:14 +0300319
320 nodes = {node.node_id: node for node in ctx.storage.load_list(NodeInfo, SaveNodesStage.nodes_path)}
321
322 if SaveNodesStage.params_path in ctx.storage:
323 params = json.loads(ctx.storage.get_raw(SaveNodesStage.params_path).decode('utf8'))
324 for node_id, node in nodes.items():
325 node.params = params.get(node_id, {})
326
327 ctx.nodes_info = nodes
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200328 logger.info("%s nodes loaded from database", len(ctx.nodes_info))