2 # -*- coding: utf-8 -*-
6 ========================
8 Copyright (c) 2012-2019. The SimGrid Team. All rights reserved.
10 This program is free software; you can redistribute it and/or modify it
11 under the terms of the license (GNU LGPL) which comes with this package.
14 #TODO: child of child of child that printfs. Does it work?
15 #TODO: a child dies after its parent. What happen?
17 #TODO: regular expression in output
18 #ex: >> Time taken: [0-9]+s
19 #TODO: linked regular expression in output
21 # >> Bytes sent: ([0-9]+)
38 if sys.version_info[0] == 3:
42 raise "This program is expected to run with Python3 only"
52 return sys.platform.startswith('win')
54 # Singleton metaclass that works in Python 2 & 3
55 # http://stackoverflow.com/questions/6760685/creating-a-singleton-in-python
58 class _Singleton(type):
59 """ A metaclass that creates a Singleton base class when called. """
62 def __call__(cls, *args, **kwargs):
63 if cls not in cls._instances:
64 cls._instances[cls] = super(_Singleton, cls).__call__(*args, **kwargs)
65 return cls._instances[cls]
68 class Singleton(_Singleton('SingletonMeta', (object,), {})):
72 SIGNALS_TO_NAMES_DICT = dict((getattr(signal, n), n)
73 for n in dir(signal) if n.startswith('SIG') and '_' not in n)
78 def tesh_exit(errcode):
79 # If you do not flush some prints are skipped
81 # os._exit exit even when executed within a thread
86 print("[Tesh/CRITICAL] " + str(msg))
90 # Set an environment variable.
91 # arg must be a string with the format "variable=value"
93 print("[Tesh/INFO] setenv " + arg)
95 os.environ[t[0]] = t[1]
96 # os.putenv(t[0], t[1]) does not work
97 # see http://stackoverflow.com/questions/17705419/python-os-environ-os-putenv-usr-bin-env
100 # http://stackoverflow.com/questions/30734967/how-to-expand-environment-variables-in-python-as-bash-does
101 def expandvars2(path):
102 return re.sub(r'(?<!\\)\$[A-Za-z_][A-Za-z0-9_]*', '', os.path.expandvars(path))
105 # https://github.com/Cadair/jupyter_environment_kernels/issues/10
110 FileNotFoundError = OSError
118 # Global variable. Stores which process group should be killed (or None otherwise)
119 running_pids = list()
121 # Tests whether the process is dead already
122 def process_is_dead(pid):
125 except ProcessLookupError:
127 except OSError as err:
128 if err.errno == errno.ESRCH: # ESRCH == No such process. The process is now dead
132 # This function send TERM signal + KILL signal after 0.2s to the group of the specified process
133 def kill_process_group(pid):
134 if pid is None: # Nobody to kill. We don't know who to kill on windows, or we don't have anyone to kill on signal handler
138 pgid = os.getpgid(pid)
140 # os.getpgid failed. Ok, don't cleanup.
144 os.killpg(pgid, signal.SIGTERM)
145 if process_is_dead(pid):
148 os.killpg(pgid, signal.SIGKILL)
150 # os.killpg failed. OK. Some subprocesses may still be running.
153 def signal_handler(signal, frame):
154 print("Caught signal {}".format(SIGNALS_TO_NAMES_DICT[signal]))
156 running_pids_copy = running_pids # Just in case of interthread conflicts.
157 for pid in running_pids_copy:
158 kill_process_group(pid)
170 # read file line per line (and concat line that ends with "\")
171 class FileReader(Singleton):
172 def __init__(self, filename=None):
174 self.filename = "(stdin)"
177 self.filename_raw = filename
178 self.filename = os.path.basename(filename)
179 self.abspath = os.path.abspath(filename)
180 self.f = open(self.filename_raw)
185 return self.filename + ":" + str(self.linenumber)
187 def readfullline(self):
191 except StopIteration:
197 while len(line) > 1 and line[-2] == "\\":
205 # keep the state of tesh (mostly configuration values)
206 class TeshState(Singleton):
209 self.args_suffix = ""
210 self.ignore_regexps_common = []
211 self.jenkins = False # not a Jenkins run by default
212 self.timeout = 10 # default value: 10 sec
216 def add_thread(self, thread):
217 self.threads.append(thread)
219 def join_all_threads(self):
220 for t in self.threads:
224 # Command line object
230 self.output_pipe_stdout = []
231 self.output_pipe_stderr = []
232 self.timeout = TeshState().timeout
236 self.background = False
237 # Python threads loose the cwd
238 self.cwd = os.getcwd()
240 self.ignore_output = False
241 self.expect_return = 0
243 self.output_display = False
247 self.ignore_regexps = TeshState().ignore_regexps_common
249 def add_input_pipe(self, l):
250 self.input_pipe.append(l)
252 def add_output_pipe_stdout(self, l):
253 self.output_pipe_stdout.append(l)
255 def add_output_pipe_stderr(self, l):
256 self.output_pipe_stderr.append(l)
258 def set_cmd(self, args, linenumber):
260 self.linenumber = linenumber
262 def add_ignore(self, txt):
263 self.ignore_regexps.append(re.compile(txt))
265 def remove_ignored_lines(self, lines):
266 for ign in self.ignore_regexps:
267 lines = [l for l in lines if not ign.match(l)]
270 def _cmd_mkfile(self, argline):
271 filename = argline[len("mkfile "):]
272 file = open(filename, "w")
274 fatal_error("Unable to create file " + filename)
275 file.write("\n".join(self.input_pipe))
279 def _cmd_cd(self, argline):
280 args = shlex.split(argline)
282 fatal_error("Too many arguments to cd")
285 print("[Tesh/INFO] change directory to " + args[1])
286 except FileNotFoundError:
287 print("Chdir to " + args[1] + " failed: No such file or directory")
288 print("Test suite `" + FileReader().filename + "': NOK (system error)")
291 # Run the Cmd if possible.
292 # Return False if nothing has been ran.
294 def run_if_possible(self):
297 lock = _thread.allocate_lock()
299 TeshState().add_thread(lock)
300 _thread.start_new_thread(Cmd._run, (self, lock))
307 def _run(self, lock=None):
308 # Python threads loose the cwd
311 # retrocompatibility: support ${aaa:=.} variable format
312 def replace_perl_variables(m):
314 vdefault = m.group(2)
315 if vname in os.environ:
319 self.args = re.sub(r"\${(\w+):=([^}]*)}", replace_perl_variables, self.args)
321 # replace bash environment variables ($THINGS) to their values
322 self.args = expandvars2(self.args)
324 if re.match("^mkfile ", self.args) is not None:
325 self._cmd_mkfile(self.args)
330 if re.match("^cd ", self.args) is not None:
331 self._cmd_cd(self.args)
336 if TeshState().wrapper is not None:
338 self.args = TeshState().wrapper + self.args
339 elif re.match(".*smpirun.*", self.args) is not None:
340 self.args = "sh " + self.args
341 if TeshState().jenkins and self.timeout is not None:
344 self.args += TeshState().args_suffix
347 logs.append("[{file}:{number}] {args}".format(file=FileReader().filename,
348 number=self.linenumber, args=self.args))
350 args = shlex.split(self.args)
357 preexec_function = None
359 preexec_function = lambda: os.setpgid(0, 0)
360 proc = subprocess.Popen(
363 stdin=subprocess.PIPE,
364 stdout=subprocess.PIPE,
365 stderr=subprocess.STDOUT,
366 universal_newlines=True,
367 preexec_fn=preexec_function)
370 running_pids.append(local_pid)
371 except PermissionError:
372 logs.append("[{file}:{number}] Cannot start '{cmd}': The binary is not executable.".format(
373 file=FileReader().filename, number=self.linenumber, cmd=args[0]))
374 logs.append("[{file}:{number}] Current dir: {dir}".format(file=FileReader().filename,
375 number=self.linenumber, dir=os.getcwd()))
376 return_code = max(3, return_code)
377 print('\n'.join(logs))
379 except NotADirectoryError:
380 logs.append("[{file}:{number}] Cannot start '{cmd}': The path to binary does not exist.".format(
381 file=FileReader().filename, number=self.linenumber, cmd=args[0]))
382 logs.append("[{file}:{number}] Current dir: {dir}".format(file=FileReader().filename,
383 number=self.linenumber, dir=os.getcwd()))
384 return_code = max(3, return_code)
385 print('\n'.join(logs))
387 except FileNotFoundError:
388 logs.append("[{file}:{number}] Cannot start '{cmd}': File not found.".format(
389 file=FileReader().filename, number=self.linenumber, cmd=args[0]))
390 return_code = max(3, return_code)
391 print('\n'.join(logs))
393 except OSError as osE:
395 osE.strerror += "\nOSError: [Errno 8] Executed scripts should start with shebang line (like #!/usr/bin/env sh)"
398 cmdName = FileReader().filename + ":" + str(self.linenumber)
400 (stdout_data, stderr_data) = proc.communicate("\n".join(self.input_pipe), self.timeout)
402 timeout_reached = False
403 except subprocess.TimeoutExpired:
404 timeout_reached = True
405 logs.append("Test suite `{file}': NOK (<{cmd}> timeout after {timeout} sec)".format(
406 file=FileReader().filename, cmd=cmdName, timeout=self.timeout))
407 running_pids.remove(local_pid)
408 kill_process_group(local_pid)
409 # Try to get the output of the timeout process, to help in debugging.
411 (stdout_data, stderr_data) = proc.communicate(timeout=1)
412 except subprocess.TimeoutExpired:
413 logs.append("[{file}:{number}] Could not retrieve output. Killing the process group failed?".format(
414 file=FileReader().filename, number=self.linenumber))
415 return_code = max(3, return_code)
416 print('\n'.join(logs))
419 if self.output_display:
420 logs.append(str(stdout_data))
423 ansi_escape = re.compile(r'\x1b[^m]*m')
424 stdout_data = ansi_escape.sub('', stdout_data)
426 if self.ignore_output:
427 logs.append("(ignoring the output of <{cmd}> as requested)".format(cmd=cmdName))
429 stdouta = stdout_data.split("\n")
430 while len(stdouta) > 0 and stdouta[-1] == "":
432 stdouta = self.remove_ignored_lines(stdouta)
435 # Mimic the "sort" bash command, which is case unsensitive.
437 stdouta.sort(key=lambda x: x.lower())
438 self.output_pipe_stdout.sort(key=lambda x: x.lower())
440 stdouta.sort(key=lambda x: x[:self.sort].lower())
441 self.output_pipe_stdout.sort(key=lambda x: x[:self.sort].lower())
444 difflib.unified_diff(
445 self.output_pipe_stdout,
451 logs.append("Output of <{cmd}> mismatch:".format(cmd=cmdName))
452 if self.sort >= 0: # If sorted, truncate the diff output and show the unsorted version
459 logs.append("(diff truncated after 50 lines)")
460 logs.append("Unsorted observed output:\n")
463 else: # If not sorted, just display the diff
467 logs.append("Test suite `{file}': NOK (<{cmd}> output mismatch)".format(
468 file=FileReader().filename, cmd=cmdName))
472 f = open('obtained', 'w')
473 obtained = stdout_data.split("\n")
474 while len(obtained) > 0 and obtained[-1] == "":
476 obtained = self.remove_ignored_lines(obtained)
477 for line in obtained:
478 f.write("> " + line + "\n")
480 logs.append("Obtained output kept as requested: {path}".format(path=os.path.abspath("obtained")))
481 return_code = max(2, return_code)
482 print('\n'.join(logs))
486 return_code = max(3, return_code)
487 print('\n'.join(logs))
490 if proc.returncode != self.expect_return:
491 if proc.returncode >= 0:
492 logs.append("Test suite `{file}': NOK (<{cmd}> returned code {code})".format(
493 file=FileReader().filename, cmd=cmdName, code=proc.returncode))
496 return_code = max(2, return_code)
497 print('\n'.join(logs))
500 logs.append("Test suite `{file}': NOK (<{cmd}> got signal {sig})".format(
501 file=FileReader().filename, cmd=cmdName,
502 sig=SIGNALS_TO_NAMES_DICT[-proc.returncode]))
505 return_code = max(max(-proc.returncode, 1), return_code)
506 print('\n'.join(logs))
512 print('\n'.join(logs))
515 return self.args is not None
525 if __name__ == '__main__':
526 signal.signal(signal.SIGINT, signal_handler)
527 signal.signal(signal.SIGTERM, signal_handler)
529 parser = argparse.ArgumentParser(description='tesh -- testing shell')
530 group1 = parser.add_argument_group('Options')
531 group1.add_argument('teshfile', nargs='?', help='Name of teshfile, stdin if omitted')
534 metavar='some/directory',
535 help='ask tesh to switch the working directory before launching the tests')
536 group1.add_argument('--setenv', metavar='var=value', action='append', help='set a specific environment variable')
537 group1.add_argument('--cfg', metavar='arg', action='append', help='add parameter --cfg=arg to each command line')
538 group1.add_argument('--log', metavar='arg', action='append', help='add parameter --log=arg to each command line')
542 help='ignore all cruft generated on SimGrid continous integration servers')
543 group1.add_argument('--wrapper', metavar='arg', help='Run each command in the provided wrapper (eg valgrind)')
547 help='Keep the obtained output when it does not match the expected one')
549 options = parser.parse_args()
551 if options.cd is not None:
552 print("[Tesh/INFO] change directory to " + options.cd)
555 if options.ignore_jenkins:
556 print("Ignore all cruft seen on SimGrid's continous integration servers")
557 # Note: regexps should match at the beginning of lines
558 TeshState().ignore_regexps_common = [
559 re.compile(r"profiling:"),
560 re.compile(r"Unable to clean temporary file C:"),
561 re.compile(r".*Configuration change: Set 'contexts/"),
562 re.compile(r"Picked up JAVA_TOOL_OPTIONS: "),
563 re.compile(r"Picked up _JAVA_OPTIONS: "),
564 re.compile(r"==[0-9]+== ?WARNING: ASan doesn't fully support"),
565 re.compile(r"==[0-9]+== ?WARNING: ASan is ignoring requested __asan_handle_no_return: stack top:"),
566 re.compile(r"False positive error reports may follow"),
567 re.compile(r"For details see http://code.google.com/p/address-sanitizer/issues/detail\?id=189"),
568 re.compile(r"For details see https://github.com/google/sanitizers/issues/189"),
569 re.compile(r"Python runtime initialized with LC_CTYPE=C .*"),
571 re.compile(r"cmake: /usr/local/lib/libcurl\.so\.4: no version information available \(required by cmake\)"),
572 re.compile(r".*mmap broken on FreeBSD, but dlopen\+thread broken too. Switching to dlopen\+raw contexts\."),
573 re.compile(r".*dlopen\+thread broken on Apple and BSD\. Switching to raw contexts\."),
575 TeshState().jenkins = True # This is a Jenkins build
577 if options.teshfile is None:
579 print("Test suite from stdin")
581 if not os.path.isfile(options.teshfile):
582 print("Cannot open teshfile '" + options.teshfile + "': File not found")
584 f = FileReader(options.teshfile)
585 print("Test suite '" + f.abspath + "'")
587 if options.setenv is not None:
588 for e in options.setenv:
591 if options.cfg is not None:
592 for c in options.cfg:
593 TeshState().args_suffix += " --cfg=" + c
594 if options.log is not None:
595 for l in options.log:
596 TeshState().args_suffix += " --log=" + l
598 if options.wrapper is not None:
599 TeshState().wrapper = options.wrapper
602 TeshState().keep = True
604 # cmd holds the current command line
605 # tech commands will add some parameters to it
606 # when ready, we execute it.
609 line = f.readfullline()
610 while line is not None:
611 # print(">>============="+line+"==<<")
613 #print ("END CMD block")
614 if cmd.run_if_possible():
620 elif line[0:2] == "p ":
621 print("[" + str(FileReader()) + "] " + line[2:])
623 elif line[0:2] == "< ":
624 cmd.add_input_pipe(line[2:])
625 elif line[0:1] == "<":
626 cmd.add_input_pipe(line[1:])
628 elif line[0:2] == "> ":
629 cmd.add_output_pipe_stdout(line[2:])
630 elif line[0:1] == ">":
631 cmd.add_output_pipe_stdout(line[1:])
633 elif line[0:2] == "$ ":
634 if cmd.run_if_possible():
636 cmd.set_cmd(line[2:], f.linenumber)
638 elif line[0:2] == "& ":
639 if cmd.run_if_possible():
641 cmd.set_cmd(line[2:], f.linenumber)
642 cmd.background = True
644 elif line[0:15] == "! output ignore":
645 cmd.ignore_output = True
646 #print("cmd.ignore_output = True")
647 elif line[0:16] == "! output display":
648 cmd.output_display = True
649 cmd.ignore_output = True
650 elif line[0:15] == "! expect return":
651 cmd.expect_return = int(line[16:])
652 #print("expect return "+str(int(line[16:])))
653 elif line[0:15] == "! expect signal":
655 # get the signal integer value from the signal module
656 if sig not in signal.__dict__:
657 fatal_error("unrecognized signal '" + sig + "'")
658 sig = int(signal.__dict__[sig])
659 # popen return -signal when a process ends with a signal
660 cmd.expect_return = -sig
661 elif line[0:len("! timeout ")] == "! timeout ":
662 if "no" in line[len("! timeout "):]:
665 cmd.timeout = int(line[len("! timeout "):])
667 elif line[0:len("! output sort")] == "! output sort":
668 if len(line) >= len("! output sort "):
669 sort = int(line[len("! output sort "):])
673 elif line[0:len("! setenv ")] == "! setenv ":
674 setenv(line[len("! setenv "):])
676 elif line[0:len("! ignore ")] == "! ignore ":
677 cmd.add_ignore(line[len("! ignore "):])
680 fatal_error("UNRECOGNIZED OPTION")
682 line = f.readfullline()
684 cmd.run_if_possible()
686 TeshState().join_all_threads()
689 if f.filename == "(stdin)":
690 print("Test suite from stdin OK")
692 print("Test suite `" + f.filename + "' OK")
694 tesh_exit(return_code)