Logo AND Algorithmique Numérique Distribuée

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