Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of framagit.org:simgrid/simgrid
[simgrid.git] / tools / tesh / tesh.py
1 #! @PYTHON_EXECUTABLE@
2 # -*- coding: utf-8 -*-
3 """
4
5 tesh -- testing shell
6 ========================
7
8 Copyright (c) 2012-2019. The SimGrid Team. All rights reserved.
9
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.
12
13
14 #TODO: child of child of child that printfs. Does it work?
15 #TODO: a child dies after its parent. What happen?
16
17 #TODO: regular expression in output
18 #ex: >> Time taken: [0-9]+s
19 #TODO: linked regular expression in output
20 #ex:
21 # >> Bytes sent: ([0-9]+)
22 # >> Bytes recv: \1
23 # then, even better:
24 # ! expect (\1 > 500)
25
26 """
27
28
29 import sys
30 import os
31 import shlex
32 import re
33 import difflib
34 import signal
35 import argparse
36
37 if sys.version_info[0] == 3:
38     import subprocess
39     import _thread
40 else:
41     raise "This program is expected to run with Python3 only"
42
43 ##############
44 #
45 # Utilities
46 #
47 #
48
49
50 def isWindows():
51     return sys.platform.startswith('win')
52
53 # Singleton metaclass that works in Python 2 & 3
54 # http://stackoverflow.com/questions/6760685/creating-a-singleton-in-python
55
56
57 class _Singleton(type):
58     """ A metaclass that creates a Singleton base class when called. """
59     _instances = {}
60
61     def __call__(cls, *args, **kwargs):
62         if cls not in cls._instances:
63             cls._instances[cls] = super(_Singleton, cls).__call__(*args, **kwargs)
64         return cls._instances[cls]
65
66
67 class Singleton(_Singleton('SingletonMeta', (object,), {})):
68     pass
69
70
71 SIGNALS_TO_NAMES_DICT = dict((getattr(signal, n), n)
72                              for n in dir(signal) if n.startswith('SIG') and '_' not in n)
73
74 return_code = 0
75
76 # exit correctly
77 def tesh_exit(errcode):
78     # If you do not flush some prints are skipped
79     sys.stdout.flush()
80     # os._exit exit even when executed within a thread
81     os._exit(errcode)
82
83
84 def fatal_error(msg):
85     print("[Tesh/CRITICAL] " + str(msg))
86     tesh_exit(1)
87
88
89 # Set an environment variable.
90 # arg must be a string with the format "variable=value"
91 def setenv(arg):
92     print("[Tesh/INFO] setenv " + arg)
93     t = arg.split("=", 1)
94     os.environ[t[0]] = t[1]
95     # os.putenv(t[0], t[1]) does not work
96     # see http://stackoverflow.com/questions/17705419/python-os-environ-os-putenv-usr-bin-env
97
98
99 # http://stackoverflow.com/questions/30734967/how-to-expand-environment-variables-in-python-as-bash-does
100 def expandvars2(path):
101     return re.sub(r'(?<!\\)\$[A-Za-z_][A-Za-z0-9_]*', '', os.path.expandvars(path))
102
103
104 # https://github.com/Cadair/jupyter_environment_kernels/issues/10
105 try:
106     FileNotFoundError
107 except NameError:
108     # py2
109     FileNotFoundError = OSError
110
111 ##############
112 #
113 # Cleanup on signal
114 #
115 #
116
117 # Global variable. Stores which process group should be killed (or None otherwise)
118 running_pgids = list()
119
120 def kill_process_group(pgid):
121     if pgid 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
122         return
123
124     # print("Kill process group {}".format(pgid))
125     try:
126         os.killpg(pgid, signal.SIGTERM)
127     except OSError:
128         # os.killpg failed. OK. Some subprocesses may still be running.
129         pass
130
131
132 def signal_handler(signal, frame):
133     print("Caught signal {}".format(SIGNALS_TO_NAMES_DICT[signal]))
134     global running_pgids
135     running_pgids_copy = running_pgids # Just in case of interthread conflicts.
136     for pgid in running_pgids_copy:
137         kill_process_group(pgid)
138     running_pgids.clear()
139     tesh_exit(5)
140
141
142 ##############
143 #
144 # Classes
145 #
146 #
147
148
149 # read file line per line (and concat line that ends with "\")
150 class FileReader(Singleton):
151     def __init__(self, filename=None):
152         if filename is None:
153             self.filename = "(stdin)"
154             self.f = sys.stdin
155         else:
156             self.filename_raw = filename
157             self.filename = os.path.basename(filename)
158             self.abspath = os.path.abspath(filename)
159             self.f = open(self.filename_raw)
160
161         self.linenumber = 0
162
163     def __repr__(self):
164         return self.filename + ":" + str(self.linenumber)
165
166     def readfullline(self):
167         try:
168             line = next(self.f)
169             self.linenumber += 1
170         except StopIteration:
171             return None
172         if line[-1] == "\n":
173             txt = line[0:-1]
174         else:
175             txt = line
176         while len(line) > 1 and line[-2] == "\\":
177             txt = txt[0:-1]
178             line = next(self.f)
179             self.linenumber += 1
180             txt += line[0:-1]
181         return txt
182
183
184 # keep the state of tesh (mostly configuration values)
185 class TeshState(Singleton):
186     def __init__(self):
187         self.threads = []
188         self.args_suffix = ""
189         self.ignore_regexps_common = []
190         self.jenkins = False  # not a Jenkins run by default
191         self.timeout = 10  # default value: 10 sec
192         self.wrapper = None
193         self.keep = False
194
195     def add_thread(self, thread):
196         self.threads.append(thread)
197
198     def join_all_threads(self):
199         for t in self.threads:
200             t.acquire()
201             t.release()
202
203 # Command line object
204
205
206 class Cmd(object):
207     def __init__(self):
208         self.input_pipe = []
209         self.output_pipe_stdout = []
210         self.output_pipe_stderr = []
211         self.timeout = TeshState().timeout
212         self.args = None
213         self.linenumber = -1
214
215         self.background = False
216         # Python threads loose the cwd
217         self.cwd = os.getcwd()
218
219         self.ignore_output = False
220         self.expect_return = 0
221
222         self.output_display = False
223
224         self.sort = -1
225
226         self.ignore_regexps = TeshState().ignore_regexps_common
227
228     def add_input_pipe(self, l):
229         self.input_pipe.append(l)
230
231     def add_output_pipe_stdout(self, l):
232         self.output_pipe_stdout.append(l)
233
234     def add_output_pipe_stderr(self, l):
235         self.output_pipe_stderr.append(l)
236
237     def set_cmd(self, args, linenumber):
238         self.args = args
239         self.linenumber = linenumber
240
241     def add_ignore(self, txt):
242         self.ignore_regexps.append(re.compile(txt))
243
244     def remove_ignored_lines(self, lines):
245         for ign in self.ignore_regexps:
246             lines = [l for l in lines if not ign.match(l)]
247         return lines
248
249     def _cmd_mkfile(self, argline):
250         filename = argline[len("mkfile "):]
251         file = open(filename, "w")
252         if file is None:
253             fatal_error("Unable to create file " + filename)
254         file.write("\n".join(self.input_pipe))
255         file.write("\n")
256         file.close()
257
258     def _cmd_cd(self, argline):
259         args = shlex.split(argline)
260         if len(args) != 2:
261             fatal_error("Too many arguments to cd")
262         try:
263             os.chdir(args[1])
264             print("[Tesh/INFO] change directory to " + args[1])
265         except FileNotFoundError:
266             print("Chdir to " + args[1] + " failed: No such file or directory")
267             print("Test suite `" + FileReader().filename + "': NOK (system error)")
268             tesh_exit(4)
269
270     # Run the Cmd if possible.
271     # Return False if nothing has been ran.
272
273     def run_if_possible(self):
274         if self.can_run():
275             if self.background:
276                 lock = _thread.allocate_lock()
277                 lock.acquire()
278                 TeshState().add_thread(lock)
279                 _thread.start_new_thread(Cmd._run, (self, lock))
280             else:
281                 self._run()
282             return True
283         else:
284             return False
285
286     def _run(self, lock=None):
287         # Python threads loose the cwd
288         os.chdir(self.cwd)
289
290         # retrocompatibility: support ${aaa:=.} variable format
291         def replace_perl_variables(m):
292             vname = m.group(1)
293             vdefault = m.group(2)
294             if vname in os.environ:
295                 return "$" + vname
296             else:
297                 return vdefault
298         self.args = re.sub(r"\${(\w+):=([^}]*)}", replace_perl_variables, self.args)
299
300         # replace bash environment variables ($THINGS) to their values
301         self.args = expandvars2(self.args)
302
303         if re.match("^mkfile ", self.args) is not None:
304             self._cmd_mkfile(self.args)
305             if lock is not None:
306                 lock.release()
307             return
308
309         if re.match("^cd ", self.args) is not None:
310             self._cmd_cd(self.args)
311             if lock is not None:
312                 lock.release()
313             return
314
315         if TeshState().wrapper is not None:
316             self.timeout *= 20
317             self.args = TeshState().wrapper + self.args
318         elif re.match(".*smpirun.*", self.args) is not None:
319             self.args = "sh " + self.args
320         if TeshState().jenkins and self.timeout is not None:
321             self.timeout *= 10
322
323         self.args += TeshState().args_suffix
324
325         logs = list()
326         logs.append("[{file}:{number}] {args}".format(file=FileReader().filename,
327             number=self.linenumber, args=self.args))
328
329         args = shlex.split(self.args)
330
331         global running_pgids
332         local_pgid = None
333         global return_code
334
335         try:
336             preexec_function = None
337             if not isWindows():
338                 preexec_function = lambda: os.setpgid(0, 0)
339             proc = subprocess.Popen(
340                 args,
341                 bufsize=1,
342                 stdin=subprocess.PIPE,
343                 stdout=subprocess.PIPE,
344                 stderr=subprocess.STDOUT,
345                 universal_newlines=True,
346                 preexec_fn=preexec_function)
347             try:
348                 if not isWindows():
349                     local_pgid = os.getpgid(proc.pid)
350                     running_pgids.append(local_pgid)
351             except OSError:
352                 # os.getpgid failed. OK. No cleanup.
353                 pass
354         except PermissionError:
355             logs.append("[{file}:{number}] Cannot start '{cmd}': The binary is not executable.".format(
356                 file=FileReader().filename, number=self.linenumber, cmd=args[0]))
357             logs.append("[{file}:{number}] Current dir: {dir}".format(file=FileReader().filename,
358                 number=self.linenumber, dir=os.getcwd()))
359             return_code = max(3, return_code)
360             print('\n'.join(logs))
361             return
362         except NotADirectoryError:
363             logs.append("[{file}:{number}] Cannot start '{cmd}': The path to binary does not exist.".format(
364                 file=FileReader().filename, number=self.linenumber, cmd=args[0]))
365             logs.append("[{file}:{number}] Current dir: {dir}".format(file=FileReader().filename,
366                 number=self.linenumber, dir=os.getcwd()))
367             return_code = max(3, return_code)
368             print('\n'.join(logs))
369             return
370         except FileNotFoundError:
371             logs.append("[{file}:{number}] Cannot start '{cmd}': File not found.".format(
372                 file=FileReader().filename, number=self.linenumber, cmd=args[0]))
373             return_code = max(3, return_code)
374             print('\n'.join(logs))
375             return
376         except OSError as osE:
377             if osE.errno == 8:
378                 osE.strerror += "\nOSError: [Errno 8] Executed scripts should start with shebang line (like #!/usr/bin/env sh)"
379             raise osE
380
381         cmdName = FileReader().filename + ":" + str(self.linenumber)
382         try:
383             (stdout_data, stderr_data) = proc.communicate("\n".join(self.input_pipe), self.timeout)
384             local_pgid = None
385             timeout_reached = False
386         except subprocess.TimeoutExpired:
387             timeout_reached = True
388             logs.append("Test suite `{file}': NOK (<{cmd}> timeout after {timeout} sec)".format(
389                 file=FileReader().filename, cmd=cmdName, timeout=self.timeout))
390             running_pgids.remove(local_pgid)
391             kill_process_group(local_pgid)
392             # Try to get the output of the timeout process, to help in debugging.
393             try:
394                 (stdout_data, stderr_data) = proc.communicate(timeout=1)
395             except subprocess.TimeoutExpired:
396                 logs.append("[{file}:{number}] Could not retrieve output. Killing the process group failed?".format(
397                     file=FileReader().filename, number=self.linenumber))
398                 return_code = max(3, return_code)
399                 print('\n'.join(logs))
400                 return
401
402         if self.output_display:
403             logs.append(str(stdout_data))
404
405         # remove text colors
406         ansi_escape = re.compile(r'\x1b[^m]*m')
407         stdout_data = ansi_escape.sub('', stdout_data)
408
409         if self.ignore_output:
410             logs.append("(ignoring the output of <{cmd}> as requested)".format(cmd=cmdName))
411         else:
412             stdouta = stdout_data.split("\n")
413             while len(stdouta) > 0 and stdouta[-1] == "":
414                 del stdouta[-1]
415             stdouta = self.remove_ignored_lines(stdouta)
416             stdcpy = stdouta[:]
417
418             # Mimic the "sort" bash command, which is case unsensitive.
419             if self.sort == 0:
420                 stdouta.sort(key=lambda x: x.lower())
421                 self.output_pipe_stdout.sort(key=lambda x: x.lower())
422             elif self.sort > 0:
423                 stdouta.sort(key=lambda x: x[:self.sort].lower())
424                 self.output_pipe_stdout.sort(key=lambda x: x[:self.sort].lower())
425
426             diff = list(
427                 difflib.unified_diff(
428                     self.output_pipe_stdout,
429                     stdouta,
430                     lineterm="",
431                     fromfile='expected',
432                     tofile='obtained'))
433             if len(diff) > 0:
434                 logs.append("Output of <{cmd}> mismatch:".format(cmd=cmdName))
435                 if self.sort >= 0:  # If sorted, truncate the diff output and show the unsorted version
436                     difflen = 0
437                     for line in diff:
438                         if difflen < 50:
439                             print(line)
440                         difflen += 1
441                     if difflen > 50:
442                         logs.append("(diff truncated after 50 lines)")
443                     logs.append("Unsorted observed output:\n")
444                     for line in stdcpy:
445                         logs.append(line)
446                 else:  # If not sorted, just display the diff
447                     for line in diff:
448                         logs.append(line)
449
450                 logs.append("Test suite `{file}': NOK (<{cmd}> output mismatch)".format(
451                     file=FileReader().filename, cmd=cmdName))
452                 if lock is not None:
453                     lock.release()
454                 if TeshState().keep:
455                     f = open('obtained', 'w')
456                     obtained = stdout_data.split("\n")
457                     while len(obtained) > 0 and obtained[-1] == "":
458                         del obtained[-1]
459                     obtained = self.remove_ignored_lines(obtained)
460                     for line in obtained:
461                         f.write("> " + line + "\n")
462                     f.close()
463                     logs.append("Obtained output kept as requested: {path}".format(path=os.path.abspath("obtained")))
464                 return_code = max(2, return_code)
465                 print('\n'.join(logs))
466                 return
467
468         if timeout_reached:
469             return_code = max(3, return_code)
470             print('\n'.join(logs))
471             return
472
473         if proc.returncode != self.expect_return:
474             if proc.returncode >= 0:
475                 logs.append("Test suite `{file}': NOK (<{cmd}> returned code {code})".format(
476                     file=FileReader().filename, cmd=cmdName, code=proc.returncode))
477                 if lock is not None:
478                     lock.release()
479                 return_code = max(2, return_code)
480                 print('\n'.join(logs))
481                 return
482             else:
483                 logs.append("Test suite `{file}': NOK (<{cmd}> got signal {sig})".format(
484                     file=FileReader().filename, cmd=cmdName,
485                     sig=SIGNALS_TO_NAMES_DICT[-proc.returncode]))
486                 if lock is not None:
487                     lock.release()
488                 return_code = max(max(-proc.returncode, 1), return_code)
489                 print('\n'.join(logs))
490                 return
491
492         if lock is not None:
493             lock.release()
494
495         print('\n'.join(logs))
496
497     def can_run(self):
498         return self.args is not None
499
500
501 ##############
502 #
503 # Main
504 #
505 #
506
507
508 if __name__ == '__main__':
509     signal.signal(signal.SIGINT, signal_handler)
510     signal.signal(signal.SIGTERM, signal_handler)
511
512     parser = argparse.ArgumentParser(description='tesh -- testing shell')
513     group1 = parser.add_argument_group('Options')
514     group1.add_argument('teshfile', nargs='?', help='Name of teshfile, stdin if omitted')
515     group1.add_argument(
516         '--cd',
517         metavar='some/directory',
518         help='ask tesh to switch the working directory before launching the tests')
519     group1.add_argument('--setenv', metavar='var=value', action='append', help='set a specific environment variable')
520     group1.add_argument('--cfg', metavar='arg', action='append', help='add parameter --cfg=arg to each command line')
521     group1.add_argument('--log', metavar='arg', action='append', help='add parameter --log=arg to each command line')
522     group1.add_argument(
523         '--ignore-jenkins',
524         action='store_true',
525         help='ignore all cruft generated on SimGrid continous integration servers')
526     group1.add_argument('--wrapper', metavar='arg', help='Run each command in the provided wrapper (eg valgrind)')
527     group1.add_argument(
528         '--keep',
529         action='store_true',
530         help='Keep the obtained output when it does not match the expected one')
531
532     options = parser.parse_args()
533
534     if options.cd is not None:
535         print("[Tesh/INFO] change directory to " + options.cd)
536         os.chdir(options.cd)
537
538     if options.ignore_jenkins:
539         print("Ignore all cruft seen on SimGrid's continous integration servers")
540         # Note: regexps should match at the beginning of lines
541         TeshState().ignore_regexps_common = [
542             re.compile(r"profiling:"),
543             re.compile(r"Unable to clean temporary file C:"),
544             re.compile(r".*Configuration change: Set 'contexts/"),
545             re.compile(r"Picked up JAVA_TOOL_OPTIONS: "),
546             re.compile(r"Picked up _JAVA_OPTIONS: "),
547             re.compile(r"==[0-9]+== ?WARNING: ASan doesn't fully support"),
548             re.compile(r"==[0-9]+== ?WARNING: ASan is ignoring requested __asan_handle_no_return: stack top:"),
549             re.compile(r"False positive error reports may follow"),
550             re.compile(r"For details see http://code.google.com/p/address-sanitizer/issues/detail\?id=189"),
551             re.compile(r"For details see https://github.com/google/sanitizers/issues/189"),
552             re.compile(r"Python runtime initialized with LC_CTYPE=C .*"),
553             # Seen on CircleCI
554             re.compile(r"cmake: /usr/local/lib/libcurl\.so\.4: no version information available \(required by cmake\)"),
555             re.compile(r".*mmap broken on FreeBSD, but dlopen\+thread broken too. Switching to dlopen\+raw contexts\."),
556             re.compile(r".*dlopen\+thread broken on Apple and BSD\. Switching to raw contexts\."),
557         ]
558         TeshState().jenkins = True  # This is a Jenkins build
559
560     if options.teshfile is None:
561         f = FileReader(None)
562         print("Test suite from stdin")
563     else:
564         if not os.path.isfile(options.teshfile):
565             print("Cannot open teshfile '" + options.teshfile + "': File not found")
566             tesh_exit(3)
567         f = FileReader(options.teshfile)
568         print("Test suite '" + f.abspath + "'")
569
570     if options.setenv is not None:
571         for e in options.setenv:
572             setenv(e)
573
574     if options.cfg is not None:
575         for c in options.cfg:
576             TeshState().args_suffix += " --cfg=" + c
577     if options.log is not None:
578         for l in options.log:
579             TeshState().args_suffix += " --log=" + l
580
581     if options.wrapper is not None:
582         TeshState().wrapper = options.wrapper
583
584     if options.keep:
585         TeshState().keep = True
586
587     # cmd holds the current command line
588     # tech commands will add some parameters to it
589     # when ready, we execute it.
590     cmd = Cmd()
591
592     line = f.readfullline()
593     while line is not None:
594         # print(">>============="+line+"==<<")
595         if len(line) == 0:
596             #print ("END CMD block")
597             if cmd.run_if_possible():
598                 cmd = Cmd()
599
600         elif line[0] == "#":
601             pass
602
603         elif line[0:2] == "p ":
604             print("[" + str(FileReader()) + "] " + line[2:])
605
606         elif line[0:2] == "< ":
607             cmd.add_input_pipe(line[2:])
608         elif line[0:1] == "<":
609             cmd.add_input_pipe(line[1:])
610
611         elif line[0:2] == "> ":
612             cmd.add_output_pipe_stdout(line[2:])
613         elif line[0:1] == ">":
614             cmd.add_output_pipe_stdout(line[1:])
615
616         elif line[0:2] == "$ ":
617             if cmd.run_if_possible():
618                 cmd = Cmd()
619             cmd.set_cmd(line[2:], f.linenumber)
620
621         elif line[0:2] == "& ":
622             if cmd.run_if_possible():
623                 cmd = Cmd()
624             cmd.set_cmd(line[2:], f.linenumber)
625             cmd.background = True
626
627         elif line[0:15] == "! output ignore":
628             cmd.ignore_output = True
629             #print("cmd.ignore_output = True")
630         elif line[0:16] == "! output display":
631             cmd.output_display = True
632             cmd.ignore_output = True
633         elif line[0:15] == "! expect return":
634             cmd.expect_return = int(line[16:])
635             #print("expect return "+str(int(line[16:])))
636         elif line[0:15] == "! expect signal":
637             sig = line[16:]
638             # get the signal integer value from the signal module
639             if sig not in signal.__dict__:
640                 fatal_error("unrecognized signal '" + sig + "'")
641             sig = int(signal.__dict__[sig])
642             # popen return -signal when a process ends with a signal
643             cmd.expect_return = -sig
644         elif line[0:len("! timeout ")] == "! timeout ":
645             if "no" in line[len("! timeout "):]:
646                 cmd.timeout = None
647             else:
648                 cmd.timeout = int(line[len("! timeout "):])
649
650         elif line[0:len("! output sort")] == "! output sort":
651             if len(line) >= len("! output sort "):
652                 sort = int(line[len("! output sort "):])
653             else:
654                 sort = 0
655             cmd.sort = sort
656         elif line[0:len("! setenv ")] == "! setenv ":
657             setenv(line[len("! setenv "):])
658
659         elif line[0:len("! ignore ")] == "! ignore ":
660             cmd.add_ignore(line[len("! ignore "):])
661
662         else:
663             fatal_error("UNRECOGNIZED OPTION")
664
665         line = f.readfullline()
666
667     cmd.run_if_possible()
668
669     TeshState().join_all_threads()
670
671     if return_code == 0:
672         if f.filename == "(stdin)":
673             print("Test suite from stdin OK")
674         else:
675             print("Test suite `" + f.filename + "' OK")
676     else:
677         tesh_exit(return_code)