Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f9ef1d4775467e002237da4723d8711e93bd9dca
[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 import time
37
38 if sys.version_info[0] == 3:
39     import subprocess
40     import _thread
41 else:
42     raise "This program is expected to run with Python3 only"
43
44 ##############
45 #
46 # Utilities
47 #
48 #
49
50
51 def isWindows():
52     return sys.platform.startswith('win')
53
54 # Singleton metaclass that works in Python 2 & 3
55 # http://stackoverflow.com/questions/6760685/creating-a-singleton-in-python
56
57
58 class _Singleton(type):
59     """ A metaclass that creates a Singleton base class when called. """
60     _instances = {}
61
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]
66
67
68 class Singleton(_Singleton('SingletonMeta', (object,), {})):
69     pass
70
71
72 SIGNALS_TO_NAMES_DICT = dict((getattr(signal, n), n)
73                              for n in dir(signal) if n.startswith('SIG') and '_' not in n)
74
75 return_code = 0
76
77 # exit correctly
78 def tesh_exit(errcode):
79     # If you do not flush some prints are skipped
80     sys.stdout.flush()
81     # os._exit exit even when executed within a thread
82     os._exit(errcode)
83
84
85 def fatal_error(msg):
86     print("[Tesh/CRITICAL] " + str(msg))
87     tesh_exit(1)
88
89
90 # Set an environment variable.
91 # arg must be a string with the format "variable=value"
92 def setenv(arg):
93     print("[Tesh/INFO] setenv " + arg)
94     t = arg.split("=", 1)
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
98
99
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))
103
104
105 # https://github.com/Cadair/jupyter_environment_kernels/issues/10
106 try:
107     FileNotFoundError
108 except NameError:
109     # py2
110     FileNotFoundError = OSError
111
112 ##############
113 #
114 # Cleanup on signal
115 #
116 #
117
118 # Global variable. Stores which process group should be killed (or None otherwise)
119 running_pids = list()
120
121 # Tests whether the process is dead already
122 def process_is_dead(pid):
123     try:
124         os.kill(pid, 0)
125     except ProcessLookupError:
126         return True
127     except OSError as err:
128         if err.errno == errno.ESRCH: # ESRCH == No such process. The process is now dead
129             return True
130     return False
131
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
135         return
136
137     try:
138         pgid = os.getpgid(pid)
139     except:
140         # os.getpgid failed. Ok, don't cleanup.
141         return
142     
143     try:
144         os.killpg(pgid, signal.SIGTERM)
145         if process_is_dead(pid):
146             return
147         time.sleep(0.2)
148         os.killpg(pgid, signal.SIGKILL)
149     except OSError:
150         # os.killpg failed. OK. Some subprocesses may still be running.
151         pass
152
153 def signal_handler(signal, frame):
154     print("Caught signal {}".format(SIGNALS_TO_NAMES_DICT[signal]))
155     global running_pids
156     running_pids_copy = running_pids # Just in case of interthread conflicts.
157     for pid in running_pids_copy:
158         kill_process_group(pid)
159     running_pids.clear()
160     tesh_exit(5)
161
162
163 ##############
164 #
165 # Classes
166 #
167 #
168
169
170 # read file line per line (and concat line that ends with "\")
171 class FileReader(Singleton):
172     def __init__(self, filename=None):
173         if filename is None:
174             self.filename = "(stdin)"
175             self.f = sys.stdin
176         else:
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)
181
182         self.linenumber = 0
183
184     def __repr__(self):
185         return self.filename + ":" + str(self.linenumber)
186
187     def readfullline(self):
188         try:
189             line = next(self.f)
190             self.linenumber += 1
191         except StopIteration:
192             return None
193         if line[-1] == "\n":
194             txt = line[0:-1]
195         else:
196             txt = line
197         while len(line) > 1 and line[-2] == "\\":
198             txt = txt[0:-1]
199             line = next(self.f)
200             self.linenumber += 1
201             txt += line[0:-1]
202         return txt
203
204
205 # keep the state of tesh (mostly configuration values)
206 class TeshState(Singleton):
207     def __init__(self):
208         self.threads = []
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
213         self.wrapper = None
214         self.keep = False
215
216     def add_thread(self, thread):
217         self.threads.append(thread)
218
219     def join_all_threads(self):
220         for t in self.threads:
221             t.acquire()
222             t.release()
223
224 # Command line object
225
226
227 class Cmd(object):
228     def __init__(self):
229         self.input_pipe = []
230         self.output_pipe_stdout = []
231         self.output_pipe_stderr = []
232         self.timeout = TeshState().timeout
233         self.args = None
234         self.linenumber = -1
235
236         self.background = False
237         # Python threads loose the cwd
238         self.cwd = os.getcwd()
239
240         self.ignore_output = False
241         self.expect_return = 0
242
243         self.output_display = False
244
245         self.sort = -1
246
247         self.ignore_regexps = TeshState().ignore_regexps_common
248
249     def add_input_pipe(self, l):
250         self.input_pipe.append(l)
251
252     def add_output_pipe_stdout(self, l):
253         self.output_pipe_stdout.append(l)
254
255     def add_output_pipe_stderr(self, l):
256         self.output_pipe_stderr.append(l)
257
258     def set_cmd(self, args, linenumber):
259         self.args = args
260         self.linenumber = linenumber
261
262     def add_ignore(self, txt):
263         self.ignore_regexps.append(re.compile(txt))
264
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)]
268         return lines
269
270     def _cmd_mkfile(self, argline):
271         filename = argline[len("mkfile "):]
272         file = open(filename, "w")
273         if file is None:
274             fatal_error("Unable to create file " + filename)
275         file.write("\n".join(self.input_pipe))
276         file.write("\n")
277         file.close()
278
279     def _cmd_cd(self, argline):
280         args = shlex.split(argline)
281         if len(args) != 2:
282             fatal_error("Too many arguments to cd")
283         try:
284             os.chdir(args[1])
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)")
289             tesh_exit(4)
290
291     # Run the Cmd if possible.
292     # Return False if nothing has been ran.
293
294     def run_if_possible(self):
295         if self.can_run():
296             if self.background:
297                 lock = _thread.allocate_lock()
298                 lock.acquire()
299                 TeshState().add_thread(lock)
300                 _thread.start_new_thread(Cmd._run, (self, lock))
301             else:
302                 self._run()
303             return True
304         else:
305             return False
306
307     def _run(self, lock=None):
308         # Python threads loose the cwd
309         os.chdir(self.cwd)
310
311         # retrocompatibility: support ${aaa:=.} variable format
312         def replace_perl_variables(m):
313             vname = m.group(1)
314             vdefault = m.group(2)
315             if vname in os.environ:
316                 return "$" + vname
317             else:
318                 return vdefault
319         self.args = re.sub(r"\${(\w+):=([^}]*)}", replace_perl_variables, self.args)
320
321         # replace bash environment variables ($THINGS) to their values
322         self.args = expandvars2(self.args)
323
324         if re.match("^mkfile ", self.args) is not None:
325             self._cmd_mkfile(self.args)
326             if lock is not None:
327                 lock.release()
328             return
329
330         if re.match("^cd ", self.args) is not None:
331             self._cmd_cd(self.args)
332             if lock is not None:
333                 lock.release()
334             return
335
336         if TeshState().wrapper is not None:
337             self.timeout *= 20
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:
342             self.timeout *= 10
343
344         self.args += TeshState().args_suffix
345
346         logs = list()
347         logs.append("[{file}:{number}] {args}".format(file=FileReader().filename,
348             number=self.linenumber, args=self.args))
349
350         args = shlex.split(self.args)
351
352         global running_pids
353         local_pid = None
354         global return_code
355
356         try:
357             preexec_function = None
358             if not isWindows():
359                 preexec_function = lambda: os.setpgid(0, 0)
360             proc = subprocess.Popen(
361                 args,
362                 bufsize=1,
363                 stdin=subprocess.PIPE,
364                 stdout=subprocess.PIPE,
365                 stderr=subprocess.STDOUT,
366                 universal_newlines=True,
367                 preexec_fn=preexec_function)
368             if not isWindows():
369                 local_pid = proc.pid
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))
378             return
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))
386             return
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))
392             return
393         except OSError as osE:
394             if osE.errno == 8:
395                 osE.strerror += "\nOSError: [Errno 8] Executed scripts should start with shebang line (like #!/usr/bin/env sh)"
396             raise osE
397
398         cmdName = FileReader().filename + ":" + str(self.linenumber)
399         try:
400             (stdout_data, stderr_data) = proc.communicate("\n".join(self.input_pipe), self.timeout)
401             local_pid = None
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.
410             try:
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))
417                 return
418
419         if self.output_display:
420             logs.append(str(stdout_data))
421
422         # remove text colors
423         ansi_escape = re.compile(r'\x1b[^m]*m')
424         stdout_data = ansi_escape.sub('', stdout_data)
425
426         if self.ignore_output:
427             logs.append("(ignoring the output of <{cmd}> as requested)".format(cmd=cmdName))
428         else:
429             stdouta = stdout_data.split("\n")
430             while len(stdouta) > 0 and stdouta[-1] == "":
431                 del stdouta[-1]
432             stdouta = self.remove_ignored_lines(stdouta)
433             stdcpy = stdouta[:]
434
435             # Mimic the "sort" bash command, which is case unsensitive.
436             if self.sort == 0:
437                 stdouta.sort(key=lambda x: x.lower())
438                 self.output_pipe_stdout.sort(key=lambda x: x.lower())
439             elif self.sort > 0:
440                 stdouta.sort(key=lambda x: x[:self.sort].lower())
441                 self.output_pipe_stdout.sort(key=lambda x: x[:self.sort].lower())
442
443             diff = list(
444                 difflib.unified_diff(
445                     self.output_pipe_stdout,
446                     stdouta,
447                     lineterm="",
448                     fromfile='expected',
449                     tofile='obtained'))
450             if len(diff) > 0:
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
453                     difflen = 0
454                     for line in diff:
455                         if difflen < 50:
456                             print(line)
457                         difflen += 1
458                     if difflen > 50:
459                         logs.append("(diff truncated after 50 lines)")
460                     logs.append("Unsorted observed output:\n")
461                     for line in stdcpy:
462                         logs.append(line)
463                 else:  # If not sorted, just display the diff
464                     for line in diff:
465                         logs.append(line)
466
467                 logs.append("Test suite `{file}': NOK (<{cmd}> output mismatch)".format(
468                     file=FileReader().filename, cmd=cmdName))
469                 if lock is not None:
470                     lock.release()
471                 if TeshState().keep:
472                     f = open('obtained', 'w')
473                     obtained = stdout_data.split("\n")
474                     while len(obtained) > 0 and obtained[-1] == "":
475                         del obtained[-1]
476                     obtained = self.remove_ignored_lines(obtained)
477                     for line in obtained:
478                         f.write("> " + line + "\n")
479                     f.close()
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))
483                 return
484
485         if timeout_reached:
486             return_code = max(3, return_code)
487             print('\n'.join(logs))
488             return
489
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))
494                 if lock is not None:
495                     lock.release()
496                 return_code = max(2, return_code)
497                 print('\n'.join(logs))
498                 return
499             else:
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]))
503                 if lock is not None:
504                     lock.release()
505                 return_code = max(max(-proc.returncode, 1), return_code)
506                 print('\n'.join(logs))
507                 return
508
509         if lock is not None:
510             lock.release()
511
512         print('\n'.join(logs))
513
514     def can_run(self):
515         return self.args is not None
516
517
518 ##############
519 #
520 # Main
521 #
522 #
523
524
525 if __name__ == '__main__':
526     signal.signal(signal.SIGINT, signal_handler)
527     signal.signal(signal.SIGTERM, signal_handler)
528
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')
532     group1.add_argument(
533         '--cd',
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')
539     group1.add_argument(
540         '--ignore-jenkins',
541         action='store_true',
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)')
544     group1.add_argument(
545         '--keep',
546         action='store_true',
547         help='Keep the obtained output when it does not match the expected one')
548
549     options = parser.parse_args()
550
551     if options.cd is not None:
552         print("[Tesh/INFO] change directory to " + options.cd)
553         os.chdir(options.cd)
554
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 .*"),
570             # Seen on CircleCI
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\."),
574         ]
575         TeshState().jenkins = True  # This is a Jenkins build
576
577     if options.teshfile is None:
578         f = FileReader(None)
579         print("Test suite from stdin")
580     else:
581         if not os.path.isfile(options.teshfile):
582             print("Cannot open teshfile '" + options.teshfile + "': File not found")
583             tesh_exit(3)
584         f = FileReader(options.teshfile)
585         print("Test suite '" + f.abspath + "'")
586
587     if options.setenv is not None:
588         for e in options.setenv:
589             setenv(e)
590
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
597
598     if options.wrapper is not None:
599         TeshState().wrapper = options.wrapper
600
601     if options.keep:
602         TeshState().keep = True
603
604     # cmd holds the current command line
605     # tech commands will add some parameters to it
606     # when ready, we execute it.
607     cmd = Cmd()
608
609     line = f.readfullline()
610     while line is not None:
611         # print(">>============="+line+"==<<")
612         if len(line) == 0:
613             #print ("END CMD block")
614             if cmd.run_if_possible():
615                 cmd = Cmd()
616
617         elif line[0] == "#":
618             pass
619
620         elif line[0:2] == "p ":
621             print("[" + str(FileReader()) + "] " + line[2:])
622
623         elif line[0:2] == "< ":
624             cmd.add_input_pipe(line[2:])
625         elif line[0:1] == "<":
626             cmd.add_input_pipe(line[1:])
627
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:])
632
633         elif line[0:2] == "$ ":
634             if cmd.run_if_possible():
635                 cmd = Cmd()
636             cmd.set_cmd(line[2:], f.linenumber)
637
638         elif line[0:2] == "& ":
639             if cmd.run_if_possible():
640                 cmd = Cmd()
641             cmd.set_cmd(line[2:], f.linenumber)
642             cmd.background = True
643
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":
654             sig = line[16:]
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 "):]:
663                 cmd.timeout = None
664             else:
665                 cmd.timeout = int(line[len("! timeout "):])
666
667         elif line[0:len("! output sort")] == "! output sort":
668             if len(line) >= len("! output sort "):
669                 sort = int(line[len("! output sort "):])
670             else:
671                 sort = 0
672             cmd.sort = sort
673         elif line[0:len("! setenv ")] == "! setenv ":
674             setenv(line[len("! setenv "):])
675
676         elif line[0:len("! ignore ")] == "! ignore ":
677             cmd.add_ignore(line[len("! ignore "):])
678
679         else:
680             fatal_error("UNRECOGNIZED OPTION")
681
682         line = f.readfullline()
683
684     cmd.run_if_possible()
685
686     TeshState().join_all_threads()
687
688     if return_code == 0:
689         if f.filename == "(stdin)":
690             print("Test suite from stdin OK")
691         else:
692             print("Test suite `" + f.filename + "' OK")
693     else:
694         tesh_exit(return_code)