Logo AND Algorithmique Numérique Distribuée

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