blob: 4551952a6982ad580584fb6e0c1359b2f7da8fbe [file] [log] [blame]
koder aka kdanilov652cd802015-04-13 12:21:07 +03001import re
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +03002import os
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +03003import sys
koder aka kdanilovffaf48d2016-12-27 02:25:29 +02004import math
koder aka kdanilov22d134e2016-11-08 11:33:19 +02005import time
6import uuid
koder aka kdanilovafd98742015-04-24 01:27:22 +03007import socket
koder aka kdanilove21d7472015-02-14 19:02:04 -08008import logging
koder aka kdanilovf2865172016-12-30 03:35:11 +02009import datetime
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030010import ipaddress
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080011import threading
12import contextlib
koder aka kdanilov652cd802015-04-13 12:21:07 +030013import subprocess
koder aka kdanilovf90de852017-01-20 18:12:27 +020014from fractions import Fraction
15
koder aka kdanilov4643fd62015-02-10 16:20:13 -080016
koder aka kdanilov108ac362017-01-19 20:17:16 +020017from typing import Any, Tuple, Union, List, Iterator, Iterable, Optional, IO, cast, TypeVar, Callable
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030018
koder aka kdanilovbb5fe072015-05-21 02:50:23 +030019try:
20 import psutil
21except ImportError:
22 psutil = None
23
koder aka kdanilov22d134e2016-11-08 11:33:19 +020024try:
25 from petname import Generate as pet_generate
26except ImportError:
27 def pet_generate(x: str, y: str) -> str:
28 return str(uuid.uuid4())
29
koder aka kdanilove21d7472015-02-14 19:02:04 -080030
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030031logger = logging.getLogger("wally")
koder aka kdanilovffaf48d2016-12-27 02:25:29 +020032TNumber = TypeVar('TNumber', int, float)
33Number = Union[int, float]
koder aka kdanilov209e85d2015-04-27 23:11:05 +030034
35
koder aka kdanilovf86d7af2015-05-06 04:01:54 +030036class StopTestError(RuntimeError):
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030037 pass
koder aka kdanilovf86d7af2015-05-06 04:01:54 +030038
39
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030040class LogError:
koder aka kdanilov22d134e2016-11-08 11:33:19 +020041 def __init__(self, message: str, exc_logger: logging.Logger = None) -> None:
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030042 self.message = message
43 self.exc_logger = exc_logger
44
koder aka kdanilov22d134e2016-11-08 11:33:19 +020045 def __enter__(self) -> 'LogError':
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030046 return self
47
koder aka kdanilov22d134e2016-11-08 11:33:19 +020048 def __exit__(self, tp: type, value: Exception, traceback: Any) -> bool:
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030049 if value is None or isinstance(value, StopTestError):
koder aka kdanilov22d134e2016-11-08 11:33:19 +020050 return False
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030051
52 if self.exc_logger is None:
53 exc_logger = sys._getframe(1).f_globals.get('logger', logger)
54 else:
55 exc_logger = self.exc_logger
56
57 exc_logger.exception(self.message, exc_info=(tp, value, traceback))
koder aka kdanilov22d134e2016-11-08 11:33:19 +020058 raise StopTestError(self.message) from value
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030059
60
koder aka kdanilov22d134e2016-11-08 11:33:19 +020061class TaskFinished(Exception):
koder aka kdanilov2c473092015-03-29 17:12:13 +030062 pass
koder aka kdanilov4643fd62015-02-10 16:20:13 -080063
koder aka kdanilov2c473092015-03-29 17:12:13 +030064
koder aka kdanilovffaf48d2016-12-27 02:25:29 +020065class Timeout(Iterable[float]):
66 def __init__(self, timeout: int, message: str = None, min_tick: int = 1, no_exc: bool = False) -> None:
67 self.end_time = time.time() + timeout
68 self.message = message
69 self.min_tick = min_tick
70 self.prev_tick_at = time.time()
71 self.no_exc = no_exc
72
73 def tick(self) -> bool:
74 current_time = time.time()
75
76 if current_time > self.end_time:
77 if self.message:
78 msg = "Timeout: {}".format(self.message)
79 else:
80 msg = "Timeout"
81
82 if self.no_exc:
83 return False
84
85 raise TimeoutError(msg)
86
87 sleep_time = self.min_tick - (current_time - self.prev_tick_at)
88 if sleep_time > 0:
89 time.sleep(sleep_time)
90 self.prev_tick_at = time.time()
91 else:
92 self.prev_tick_at = current_time
93
94 return True
95
96 def __iter__(self) -> Iterator[float]:
97 return cast(Iterator[float], self)
98
99 def __next__(self) -> float:
100 if not self.tick():
101 raise StopIteration()
102 return self.end_time - time.time()
103
104
105def greater_digit_pos(val: Number) -> int:
106 return int(math.floor(math.log10(val))) + 1
107
108
109def round_digits(val: TNumber, num_digits: int = 3) -> TNumber:
110 pow = 10 ** (greater_digit_pos(val) - num_digits)
111 return type(val)(int(val / pow) * pow)
112
113
114def is_ip(data: str) -> bool:
115 try:
116 ipaddress.ip_address(data)
117 return True
118 except ValueError:
119 return False
120
121
122def log_block(message: str, exc_logger:logging.Logger = None) -> LogError:
123 logger.debug("Starts : " + message)
124 return LogError(message, exc_logger)
125
126
127def check_input_param(is_ok: bool, message: str) -> None:
128 if not is_ok:
129 logger.error(message)
130 raise StopTestError(message)
131
132
133def parse_creds(creds: str) -> Tuple[str, str, str]:
134 """Parse simple credentials format user[:passwd]@host"""
135 user, passwd_host = creds.split(":", 1)
136
137 if '@' not in passwd_host:
138 passwd, host = passwd_host, None
139 else:
140 passwd, host = passwd_host.rsplit('@', 1)
141
142 return user, passwd, host
143
144
koder aka kdanilov2c473092015-03-29 17:12:13 +0300145SMAP = dict(k=1024, m=1024 ** 2, g=1024 ** 3, t=1024 ** 4)
koder aka kdanilov8ad6e812015-03-22 14:42:18 +0200146
147
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +0300148def ssize2b(ssize: Union[str, int]) -> int:
koder aka kdanilov8ad6e812015-03-22 14:42:18 +0200149 try:
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +0300150 if isinstance(ssize, int):
koder aka kdanilov63e9c5a2015-04-28 23:06:07 +0300151 return ssize
koder aka kdanilov8ad6e812015-03-22 14:42:18 +0200152
koder aka kdanilov63e9c5a2015-04-28 23:06:07 +0300153 ssize = ssize.lower()
koder aka kdanilov2c473092015-03-29 17:12:13 +0300154 if ssize[-1] in SMAP:
155 return int(ssize[:-1]) * SMAP[ssize[-1]]
koder aka kdanilov8ad6e812015-03-22 14:42:18 +0200156 return int(ssize)
157 except (ValueError, TypeError, AttributeError):
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +0300158 raise ValueError("Unknow size format {!r}".format(ssize))
koder aka kdanilov652cd802015-04-13 12:21:07 +0300159
160
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300161RSMAP = [('K', 1024),
162 ('M', 1024 ** 2),
163 ('G', 1024 ** 3),
164 ('T', 1024 ** 4)]
165
166
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +0300167def b2ssize(size: int) -> str:
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300168 if size < 1024:
169 return str(size)
170
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200171 # make mypy happy
172 scale = 1
173 name = ""
174
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300175 for name, scale in RSMAP:
176 if size < 1024 * scale:
177 if size % scale == 0:
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +0300178 return "{} {}i".format(size // scale, name)
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300179 else:
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +0300180 return "{:.1f} {}i".format(float(size) / scale, name)
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300181
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +0300182 return "{}{}i".format(size // scale, name)
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300183
184
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300185RSMAP_10 = [('k', 1000),
186 ('m', 1000 ** 2),
187 ('g', 1000 ** 3),
188 ('t', 1000 ** 4)]
189
190
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +0300191def b2ssize_10(size: int) -> str:
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300192 if size < 1000:
193 return str(size)
194
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200195 # make mypy happy
196 scale = 1
197 name = ""
198
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300199 for name, scale in RSMAP_10:
200 if size < 1000 * scale:
201 if size % scale == 0:
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +0300202 return "{} {}".format(size // scale, name)
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300203 else:
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +0300204 return "{:.1f} {}".format(float(size) / scale, name)
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300205
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +0300206 return "{}{}".format(size // scale, name)
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300207
208
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +0300209def run_locally(cmd: Union[str, List[str]], input_data: str="", timeout:int =20) -> str:
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200210 if isinstance(cmd, str):
211 shell = True
212 cmd_str = cmd
213 else:
koder aka kdanilov73084622016-11-16 21:51:08 +0200214 shell = False
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200215 cmd_str = " ".join(cmd)
216
koder aka kdanilovd5ed4da2015-05-07 23:33:23 +0300217 proc = subprocess.Popen(cmd,
218 shell=shell,
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300219 stdin=subprocess.PIPE,
koder aka kdanilovd5ed4da2015-05-07 23:33:23 +0300220 stdout=subprocess.PIPE,
221 stderr=subprocess.PIPE)
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200222 res = [] # type: List[Tuple[bytes, bytes]]
koder aka kdanilovd5ed4da2015-05-07 23:33:23 +0300223
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +0300224 def thread_func() -> None:
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200225 rr = proc.communicate(input_data.encode("utf8"))
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300226 res.extend(rr)
koder aka kdanilovd5ed4da2015-05-07 23:33:23 +0300227
koder aka kdanilovfd2cfa52015-05-20 03:17:42 +0300228 thread = threading.Thread(target=thread_func,
229 name="Local cmd execution")
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300230 thread.daemon = True
231 thread.start()
232 thread.join(timeout)
koder aka kdanilovd5ed4da2015-05-07 23:33:23 +0300233
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300234 if thread.is_alive():
koder aka kdanilovbb5fe072015-05-21 02:50:23 +0300235 if psutil is not None:
236 parent = psutil.Process(proc.pid)
237 for child in parent.children(recursive=True):
238 child.kill()
239 parent.kill()
240 else:
241 proc.kill()
koder aka kdanilovd5ed4da2015-05-07 23:33:23 +0300242
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300243 thread.join()
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200244 raise RuntimeError("Local process timeout: " + cmd_str)
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300245
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200246 stdout_data, stderr_data = zip(*res) # type: List[bytes], List[bytes]
247
248 out = b"".join(stdout_data).decode("utf8")
249 err = b"".join(stderr_data).decode("utf8")
250
koder aka kdanilovd5ed4da2015-05-07 23:33:23 +0300251 if 0 != proc.returncode:
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300252 raise subprocess.CalledProcessError(proc.returncode,
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200253 cmd_str, out + err)
koder aka kdanilovd5ed4da2015-05-07 23:33:23 +0300254
255 return out
256
257
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +0300258def get_ip_for_target(target_ip: str) -> str:
koder aka kdanilov209e85d2015-04-27 23:11:05 +0300259 if not is_ip(target_ip):
koder aka kdanilovafd98742015-04-24 01:27:22 +0300260 target_ip = socket.gethostbyname(target_ip)
261
koder aka kdanilov209e85d2015-04-27 23:11:05 +0300262 first_dig = map(int, target_ip.split("."))
263 if first_dig == 127:
koder aka kdanilovafd98742015-04-24 01:27:22 +0300264 return '127.0.0.1'
265
koder aka kdanilovd5ed4da2015-05-07 23:33:23 +0300266 data = run_locally('ip route get to'.split(" ") + [target_ip])
koder aka kdanilov652cd802015-04-13 12:21:07 +0300267
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300268 rr1 = r'{0} via [.0-9]+ dev (?P<dev>.*?) src (?P<ip>[.0-9]+)$'
269 rr1 = rr1.replace(" ", r'\s+')
270 rr1 = rr1.format(target_ip.replace('.', r'\.'))
271
272 rr2 = r'{0} dev (?P<dev>.*?) src (?P<ip>[.0-9]+)$'
273 rr2 = rr2.replace(" ", r'\s+')
274 rr2 = rr2.format(target_ip.replace('.', r'\.'))
koder aka kdanilov652cd802015-04-13 12:21:07 +0300275
276 data_line = data.split("\n")[0].strip()
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300277 res1 = re.match(rr1, data_line)
278 res2 = re.match(rr2, data_line)
koder aka kdanilov652cd802015-04-13 12:21:07 +0300279
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300280 if res1 is not None:
281 return res1.group('ip')
koder aka kdanilov652cd802015-04-13 12:21:07 +0300282
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300283 if res2 is not None:
284 return res2.group('ip')
285
286 raise OSError("Can't define interface for {0}".format(target_ip))
287
288
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200289def open_for_append_or_create(fname: str) -> IO[str]:
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300290 if not os.path.exists(fname):
291 return open(fname, "w")
292
293 fd = open(fname, 'r+')
294 fd.seek(0, os.SEEK_END)
295 return fd
296
297
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +0300298def sec_to_str(seconds: int) -> str:
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300299 h = seconds // 3600
300 m = (seconds % 3600) // 60
301 s = seconds % 60
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +0300302 return "{}:{:02d}:{:02d}".format(h, m, s)
koder aka kdanilov168f6092015-04-19 02:33:38 +0300303
304
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +0300305def yamable(data: Any) -> Any:
koder aka kdanilov168f6092015-04-19 02:33:38 +0300306 if isinstance(data, (tuple, list)):
307 return map(yamable, data)
308
koder aka kdanilov168f6092015-04-19 02:33:38 +0300309 if isinstance(data, dict):
310 res = {}
311 for k, v in data.items():
312 res[yamable(k)] = yamable(v)
313 return res
314
315 return data
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300316
317
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +0300318def flatten(data: Iterable[Any]) -> List[Any]:
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +0300319 res = []
320 for i in data:
321 if isinstance(i, (list, tuple, set)):
322 res.extend(flatten(i))
323 else:
324 res.append(i)
325 return res
koder aka kdanilov89fb6102015-06-13 02:58:08 +0300326
327
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200328def get_creds_openrc(path: str) -> Tuple[str, str, str, str, bool]:
koder aka kdanilov89fb6102015-06-13 02:58:08 +0300329 fc = open(path).read()
330
koder aka kdanilovb7197432015-07-15 00:40:43 +0300331 echo = 'echo "$OS_INSECURE:$OS_TENANT_NAME:$OS_USERNAME:$OS_PASSWORD@$OS_AUTH_URL"'
koder aka kdanilov89fb6102015-06-13 02:58:08 +0300332
333 msg = "Failed to get creads from openrc file"
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +0300334 with LogError(msg):
koder aka kdanilov89fb6102015-06-13 02:58:08 +0300335 data = run_locally(['/bin/bash'], input_data=fc + "\n" + echo)
336
337 msg = "Failed to get creads from openrc file: " + data
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +0300338 with LogError(msg):
koder aka kdanilov89fb6102015-06-13 02:58:08 +0300339 data = data.strip()
koder aka kdanilovb7197432015-07-15 00:40:43 +0300340 insecure_str, user, tenant, passwd_auth_url = data.split(':', 3)
341 insecure = (insecure_str in ('1', 'True', 'true'))
koder aka kdanilov89fb6102015-06-13 02:58:08 +0300342 passwd, auth_url = passwd_auth_url.rsplit("@", 1)
343 assert (auth_url.startswith("https://") or
344 auth_url.startswith("http://"))
345
koder aka kdanilovb7197432015-07-15 00:40:43 +0300346 return user, passwd, tenant, auth_url, insecure
koder aka kdanilov89fb6102015-06-13 02:58:08 +0300347
348
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +0300349def which(program: str) -> Optional[str]:
koder aka kdanilova94dfe12015-08-19 13:04:51 +0300350 def is_exe(fpath):
351 return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
352
353 for path in os.environ["PATH"].split(os.pathsep):
354 path = path.strip('"')
355 exe_file = os.path.join(path, program)
356 if is_exe(exe_file):
357 return exe_file
358
359 return None
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200360
361
koder aka kdanilov108ac362017-01-19 20:17:16 +0200362@contextlib.contextmanager
363def empty_ctx(val: Any = None) -> Iterator[Any]:
364 yield val
365
366
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200367def get_uniq_path_uuid(path: str, max_iter: int = 10) -> Tuple[str, str]:
368 for i in range(max_iter):
369 run_uuid = pet_generate(2, "_")
370 results_dir = os.path.join(path, run_uuid)
371 if not os.path.exists(results_dir):
372 break
373 else:
374 run_uuid = str(uuid.uuid4())
375 results_dir = os.path.join(path, run_uuid)
376
377 return results_dir, run_uuid
378
379
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +0200380def to_ip(host_or_ip: str) -> str:
381 # translate hostname to address
382 try:
383 ipaddress.ip_address(host_or_ip)
384 return host_or_ip
385 except ValueError:
386 ip_addr = socket.gethostbyname(host_or_ip)
387 logger.info("Will use ip_addr %r instead of hostname %r", ip_addr, host_or_ip)
388 return ip_addr
koder aka kdanilovf2865172016-12-30 03:35:11 +0200389
390
391def get_time_interval_printable_info(seconds: int) -> Tuple[str, str]:
392 exec_time_s = sec_to_str(seconds)
393 now_dt = datetime.datetime.now()
394 end_dt = now_dt + datetime.timedelta(0, seconds)
395 return exec_time_s, "{:%H:%M:%S}".format(end_dt)
koder aka kdanilov108ac362017-01-19 20:17:16 +0200396
397
398FM_FUNC_INPUT = TypeVar("FM_FUNC_INPUT")
399FM_FUNC_RES = TypeVar("FM_FUNC_RES")
400
401
402def flatmap(func: Callable[[FM_FUNC_INPUT], Iterable[FM_FUNC_RES]],
403 inp_iter: Iterable[FM_FUNC_INPUT]) -> Iterator[FM_FUNC_RES]:
404 for val in inp_iter:
405 for res in func(val):
406 yield res
407
408
koder aka kdanilovf90de852017-01-20 18:12:27 +0200409_coefs = {
410 'n': Fraction(1, 1000**3),
411 'u': Fraction(1, 1000**2),
412 'm': Fraction(1, 1000),
413 'K': 1000,
414 'M': 1000 ** 2,
415 'G': 1000 ** 3,
416 'Ki': 1024,
417 'Mi': 1024 ** 2,
418 'Gi': 1024 ** 3,
419}
420
421
422def split_unit(units: str) -> Tuple[Union[Fraction, int], str]:
423 if len(units) > 2 and units[:2] in _coefs:
424 return _coefs[units[:2]], units[2:]
425 if len(units) > 1 and units[0] in _coefs:
426 return _coefs[units[0]], units[1:]
427 else:
428 return Fraction(1), units
429
430
431def unit_conversion_coef(from_unit: str, to_unit: str) -> Union[Fraction, int]:
432 f1, u1 = split_unit(from_unit)
433 f2, u2 = split_unit(to_unit)
434
435 assert u1 == u2, "Can't convert {!r} to {!r}".format(from_unit, to_unit)
436
437 if isinstance(int, f1) and isinstance(int, f2) and f1 % f2 != 0:
438 return Fraction(f1, f2)
439
440 return f1 // f2