Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics to please codacy
[simgrid.git] / tools / tesh / tesh.py
index 984e23d..be40773 100755 (executable)
@@ -1,19 +1,18 @@
-#!/usr/bin/env python
+#! @PYTHON_EXECUTABLE@
 # -*- coding: utf-8 -*-
 """
 
 tesh -- testing shell
 ========================
 
-Copyright (c) 2012-2015. The SimGrid Team.
-All rights reserved.
+Copyright (c) 2012-2017. The SimGrid Team. All rights reserved.
 
 This program is free software; you can redistribute it and/or modify it
 under the terms of the license (GNU LGPL) which comes with this package.
 
 
 #TODO: child of child of child that printfs. Does it work?
-#TODO: a child die after its parent. What happen?
+#TODO: a child dies after its parent. What happen?
 
 #TODO: regular expression in output
 #ex: >> Time taken: [0-9]+s
@@ -24,6 +23,17 @@ under the terms of the license (GNU LGPL) which comes with this package.
 # then, even better:
 # ! expect (\1 > 500)
 
+# TODO: If the output is sorted, we should report it to the users. Corresponding perl chunk
+# print "WARNING: Both the observed output and expected output were sorted as requested.\n";
+# print "WARNING: Output were only sorted using the $sort_prefix first chars.\n"
+#    if ( $sort_prefix > 0 );
+# print "WARNING: Use <! output sort 19> to sort by simulated date and process ID only.\n";
+#
+# print "----8<---------------  Begin of unprocessed observed output (as it should appear in file):\n";
+# map {print "> $_\n"} @{$cmd{'unsorted got'}};
+# print "--------------->8----  End of the unprocessed observed output.\n";
+
+
 """
 
 
@@ -37,11 +47,8 @@ import argparse
 if sys.version_info[0] == 3:
     import subprocess
     import _thread
-elif sys.version_info[0] < 3:
-    import subprocess32 as subprocess
-    import thread as _thread
 else:
-    raise "This program has not been made to exist this long"
+    raise "This program is expected to run with Python3 only"
 
 
 
@@ -120,6 +127,7 @@ class FileReader(Singleton):
         else:
             self.filename_raw = filename
             self.filename = os.path.basename(filename)
+            self.abspath = os.path.abspath(filename)
             self.f = open(self.filename_raw)
         
         self.linenumber = 0
@@ -154,6 +162,10 @@ class TeshState(Singleton):
         self.threads = []
         self.args_suffix = ""
         self.ignore_regexps_common = []
+        self.jenkins = False # not a Jenkins run by default
+        self.timeout = 10 # default value: 10 sec
+        self.wrapper = None
+        self.keep = False
     
     def add_thread(self, thread):
         self.threads.append(thread)
@@ -163,16 +175,13 @@ class TeshState(Singleton):
             t.acquire()
             t.release()
 
-
-
-
 #Command line object
 class Cmd(object):
     def __init__(self):
         self.input_pipe = []
         self.output_pipe_stdout = []
         self.output_pipe_stderr = []
-        self.timeout = 5
+        self.timeout = TeshState().timeout
         self.args = None
         self.linenumber = -1
         
@@ -216,6 +225,7 @@ class Cmd(object):
         if file is None:
             fatal_error("Unable to create file "+filename)
         file.write("\n".join(self.input_pipe))
+        file.write("\n")
         file.close()
 
     def _cmd_cd(self, argline):
@@ -278,23 +288,37 @@ class Cmd(object):
             if lock is not None: lock.release()
             return
         
+        if TeshState().wrapper is not None:
+            self.timeout *= 20
+            self.args = TeshState().wrapper + self.args
+        elif re.match(".*smpirun.*", self.args) is not None:
+            self.args = "sh " + self.args
+        if TeshState().jenkins and self.timeout != None:
+            self.timeout *= 10
+
         self.args += TeshState().args_suffix
         
         print("["+FileReader().filename+":"+str(self.linenumber)+"] "+self.args)
-        
+                
         args = shlex.split(self.args)
         #print (args)
+
         try:
             proc = subprocess.Popen(args, bufsize=1, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
         except OSError as e:
             if e.errno == 8:
                 e.strerror += "\nOSError: [Errno 8] Executed scripts should start with shebang line (like #!/bin/sh)"
             raise e
+        except FileNotFoundError:
+            print("["+FileReader().filename+":"+str(self.linenumber)+"] Cannot start '"+args[0]+"': File not found")
+            exit(3)
 
+        cmdName = FileReader().filename+":"+str(self.linenumber)
         try:
             (stdout_data, stderr_data) = proc.communicate("\n".join(self.input_pipe), self.timeout)
         except subprocess.TimeoutExpired:
-            print("Test suite `"+FileReader().filename+"': NOK (<"+FileReader().filename+":"+str(self.linenumber)+"> timeout after "+str(self.timeout)+" sec)")
+            print("Test suite `"+FileReader().filename+"': NOK (<"+cmdName+"> timeout after "+str(self.timeout)+" sec)")
+            proc.kill()
             exit(3)
 
         if self.output_display:
@@ -307,15 +331,15 @@ class Cmd(object):
         #print ((stdout_data, stderr_data))
         
         if self.ignore_output:
-            print("(ignoring the output of <"+FileReader().filename+":"+str(self.linenumber)+"> as requested)")
+            print("(ignoring the output of <"+cmdName+"> as requested)")
         else:
             stdouta = stdout_data.split("\n")
             while len(stdouta) > 0 and stdouta[-1] == "":
                 del stdouta[-1]
             stdouta = self.remove_ignored_lines(stdouta)
+            stdcpy = stdouta[:]
 
-            #the "sort" bash command is case unsensitive,
-            # we mimic its behaviour
+            # Mimic the "sort" bash command, which is case unsensitive.
             if self.sort == 0:
                 stdouta.sort(key=lambda x: x.lower())
                 self.output_pipe_stdout.sort(key=lambda x: x.lower())
@@ -325,22 +349,45 @@ class Cmd(object):
             
             diff = list(difflib.unified_diff(self.output_pipe_stdout, stdouta,lineterm="",fromfile='expected', tofile='obtained'))
             if len(diff) > 0: 
-                print("Output of <"+FileReader().filename+":"+str(self.linenumber)+"> mismatch:")
-                for line in diff:
-                    print(line)
-                print("Test suite `"+FileReader().filename+"': NOK (<"+str(FileReader())+"> output mismatch)")
+                print("Output of <"+cmdName+"> mismatch:")
+                if self.sort >= 0: # If sorted, truncate the diff output and show the unsorted version
+                    difflen = 0;
+                    for line in diff:
+                        if difflen<250:
+                            print(line)
+                        difflen += 1
+                    if difflen > 100:
+                        print("(diff truncated after 250 lines)")
+                    print("Unsorted observed output:\n")
+                    for line in stdcpy:
+                        print(line)
+                else: # If not sorted, just display the diff
+                    for line in diff:
+                        print(line)
+                        
+                print("Test suite `"+FileReader().filename+"': NOK (<"+cmdName+"> output mismatch)")
                 if lock is not None: lock.release()
+                if TeshState().keep:
+                    f = open('obtained','w')
+                    obtained = stdout_data.split("\n")
+                    while len(obtained) > 0 and obtained[-1] == "":
+                        del obtained[-1]
+                    obtained = self.remove_ignored_lines(obtained)
+                    for line in obtained:
+                        f.write("> "+line+"\n")
+                    f.close()
+                    print("Obtained output kept as requested: "+os.path.abspath("obtained"))
                 exit(2)
         
         #print ((proc.returncode, self.expect_return))
         
         if proc.returncode != self.expect_return:
             if proc.returncode >= 0:
-                print("Test suite `"+FileReader().filename+"': NOK (<"+str(FileReader())+"> returned code "+str(proc.returncode)+")")
+                print("Test suite `"+FileReader().filename+"': NOK (<"+cmdName+"> returned code "+str(proc.returncode)+")")
                 if lock is not None: lock.release()
                 exit(2)
             else:
-                print("Test suite `"+FileReader().filename+"': NOK (<"+str(FileReader())+"> got signal "+SIGNALS_TO_NAMES_DICT[-proc.returncode]+")")
+                print("Test suite `"+FileReader().filename+"': NOK (<"+cmdName+"> got signal "+SIGNALS_TO_NAMES_DICT[-proc.returncode]+")")
                 if lock is not None: lock.release()
                 exit(-proc.returncode)
             
@@ -354,12 +401,6 @@ class Cmd(object):
 
 
 
-
-
-
-
-
-
 ##############
 #
 # Main
@@ -377,7 +418,9 @@ if __name__ == '__main__':
     group1.add_argument('--setenv', metavar='var=value', action='append', help='set a specific environment variable')
     group1.add_argument('--cfg', metavar='arg', help='add parameter --cfg=arg to each command line')
     group1.add_argument('--log', metavar='arg', help='add parameter --log=arg to each command line')
-    group1.add_argument('--enable-coverage', action='store_true', help='ignore output lines starting with "profiling:"')
+    group1.add_argument('--ignore-jenkins', action='store_true', help='ignore all cruft generated on SimGrid continous integration servers')
+    group1.add_argument('--wrapper', metavar='arg', help='Run each command in the provided wrapper (eg valgrind)')
+    group1.add_argument('--keep', action='store_true', help='Keep the obtained output when it does not match the expected one')
 
     try:
         options = parser.parse_args()
@@ -386,17 +429,33 @@ if __name__ == '__main__':
 
     if options.cd is not None:
         os.chdir(options.cd)
-
-    if options.enable_coverage:
-        print("Enable coverage")
-        TeshState().ignore_regexps_common = [re.compile("^profiling:")]
+    
+    if options.ignore_jenkins:
+        print("Ignore all cruft seen on SimGrid's continous integration servers")
+        TeshState().ignore_regexps_common = [
+           re.compile("^profiling:"),
+           re.compile(".*WARNING: ASan doesn\'t fully support"),
+           re.compile("Unable to clean temporary file C:.*"),
+           re.compile(".*Configuration change: Set \'contexts/"),
+           re.compile(".*Picked up JAVA_TOOL_OPTIONS.*"),
+
+           re.compile("==WARNING: ASan is ignoring requested __asan_handle_no_return: stack top:"),
+           re.compile("False positive error reports may follow"),
+           re.compile("For details see http://code.google.com/p/address-sanitizer/issues/detail?id=189"),
+           
+           re.compile("Python runtime initialized with LC_CTYPE=C .*"),
+           ]
+        TeshState().jenkins = True # This is a Jenkins build
     
     if options.teshfile is None:
         f = FileReader(None)
         print("Test suite from stdin")
     else:
+        if not os.path.isfile(options.teshfile):
+            print("Cannot open teshfile '"+options.teshfile+"': File not found")
+            exit(3)
         f = FileReader(options.teshfile)
-        print("Test suite '"+f.filename+"'")
+        print("Test suite '"+f.abspath+"'")
     
     if options.setenv is not None:
         for e in options.setenv:
@@ -406,7 +465,12 @@ if __name__ == '__main__':
         TeshState().args_suffix += " --cfg="+options.cfg
     if options.log is not None:
         TeshState().args_suffix += " --log="+options.log
-    
+
+    if options.wrapper is not None:
+        TeshState().wrapper = options.wrapper
+        
+    if options.keep:
+        TeshState().keep = True
     
     #cmd holds the current command line
     # tech commands will add some parameters to it
@@ -497,8 +561,3 @@ if __name__ == '__main__':
         print("Test suite from stdin OK")
     else:
         print("Test suite `"+f.filename+"' OK")
-
-
-
-
-