Logo AND Algorithmique Numérique Distribuée

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