tesh -- testing shell
========================
-Copyright (c) 2012-2019. The SimGrid Team. All rights reserved.
+Copyright (c) 2012-2021. 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 dies after its parent. What happen?
"""
-
import sys
import os
import shlex
import difflib
import signal
import argparse
+import time
if sys.version_info[0] == 3:
import subprocess
#
#
-
def isWindows():
return sys.platform.startswith('win')
# Singleton metaclass that works in Python 2 & 3
# http://stackoverflow.com/questions/6760685/creating-a-singleton-in-python
-
class _Singleton(type):
""" A metaclass that creates a Singleton base class when called. """
_instances = {}
cls._instances[cls] = super(_Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
-
class Singleton(_Singleton('SingletonMeta', (object,), {})):
pass
-
SIGNALS_TO_NAMES_DICT = dict((getattr(signal, n), n)
for n in dir(signal) if n.startswith('SIG') and '_' not in n)
def expandvars2(path):
return re.sub(r'(?<!\\)\$[A-Za-z_][A-Za-z0-9_]*', '', os.path.expandvars(path))
-
-# https://github.com/Cadair/jupyter_environment_kernels/issues/10
-try:
- FileNotFoundError
-except NameError:
- # py2
- FileNotFoundError = OSError
-
##############
#
# Cleanup on signal
#
# Global variable. Stores which process group should be killed (or None otherwise)
-running_pgids = list()
+running_pids = list()
+
+# Tests whether the process is dead already
+def process_is_dead(pid):
+ try:
+ os.kill(pid, 0)
+ except ProcessLookupError:
+ return True
+ except OSError as err:
+ if err.errno == errno.ESRCH: # ESRCH == No such process. The process is now dead
+ return True
+ return False
-def kill_process_group(pgid):
- 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
+# This function send TERM signal + KILL signal after 0.2s to the group of the specified process
+def kill_process_group(pid):
+ 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
return
- # print("Kill process group {}".format(pgid))
+ try:
+ pgid = os.getpgid(pid)
+ except:
+ # os.getpgid failed. Ok, don't cleanup.
+ return
+
try:
os.killpg(pgid, signal.SIGTERM)
+ if process_is_dead(pid):
+ return
+ time.sleep(0.2)
+ os.killpg(pgid, signal.SIGKILL)
except OSError:
# os.killpg failed. OK. Some subprocesses may still be running.
pass
-
def signal_handler(signal, frame):
print("Caught signal {}".format(SIGNALS_TO_NAMES_DICT[signal]))
- global running_pgids
- running_pgids_copy = running_pgids # Just in case of interthread conflicts.
- for pgid in running_pgids_copy:
- kill_process_group(pgid)
- running_pgids.clear()
+ global running_pids
+ running_pids_copy = running_pids # Just in case of interthread conflicts.
+ for pid in running_pids_copy:
+ kill_process_group(pid)
+ running_pids.clear()
tesh_exit(5)
self.cwd = os.getcwd()
self.ignore_output = False
- self.expect_return = 0
+ self.expect_return = [0]
self.output_display = False
# Return False if nothing has been ran.
def run_if_possible(self):
- if self.can_run():
- if self.background:
- lock = _thread.allocate_lock()
- lock.acquire()
- TeshState().add_thread(lock)
- _thread.start_new_thread(Cmd._run, (self, lock))
- else:
- self._run()
- return True
- else:
+ if not self.can_run():
return False
+ if self.background:
+ lock = _thread.allocate_lock()
+ lock.acquire()
+ TeshState().add_thread(lock)
+ _thread.start_new_thread(Cmd._run, (self, lock))
+ else:
+ self._run()
+ return True
def _run(self, lock=None):
# Python threads loose the cwd
vdefault = m.group(2)
if vname in os.environ:
return "$" + vname
- else:
- return vdefault
+ return vdefault
+
self.args = re.sub(r"\${(\w+):=([^}]*)}", replace_perl_variables, self.args)
# replace bash environment variables ($THINGS) to their values
args = shlex.split(self.args)
- global running_pgids
- local_pgid = None
+ global running_pids
+ local_pid = None
global return_code
try:
stderr=subprocess.STDOUT,
universal_newlines=True,
preexec_fn=preexec_function)
- try:
- if not isWindows():
- local_pgid = os.getpgid(proc.pid)
- running_pgids.append(local_pgid)
- except OSError:
- # os.getpgid failed. OK. No cleanup.
- pass
+ if not isWindows():
+ local_pid = proc.pid
+ running_pids.append(local_pid)
except PermissionError:
logs.append("[{file}:{number}] Cannot start '{cmd}': The binary is not executable.".format(
file=FileReader().filename, number=self.linenumber, cmd=args[0]))
cmdName = FileReader().filename + ":" + str(self.linenumber)
try:
(stdout_data, stderr_data) = proc.communicate("\n".join(self.input_pipe), self.timeout)
- local_pgid = None
+ local_pid = None
timeout_reached = False
except subprocess.TimeoutExpired:
timeout_reached = True
logs.append("Test suite `{file}': NOK (<{cmd}> timeout after {timeout} sec)".format(
file=FileReader().filename, cmd=cmdName, timeout=self.timeout))
- running_pgids.remove(local_pgid)
- kill_process_group(local_pgid)
+ running_pids.remove(local_pid)
+ kill_process_group(local_pid)
# Try to get the output of the timeout process, to help in debugging.
try:
(stdout_data, stderr_data) = proc.communicate(timeout=1)
logs.append("(ignoring the output of <{cmd}> as requested)".format(cmd=cmdName))
else:
stdouta = stdout_data.split("\n")
- while len(stdouta) > 0 and stdouta[-1] == "":
+ while stdouta and stdouta[-1] == "":
del stdouta[-1]
stdouta = self.remove_ignored_lines(stdouta)
stdcpy = stdouta[:]
lineterm="",
fromfile='expected',
tofile='obtained'))
- if len(diff) > 0:
+ if diff:
logs.append("Output of <{cmd}> mismatch:".format(cmd=cmdName))
if self.sort >= 0: # If sorted, truncate the diff output and show the unsorted version
difflen = 0
if TeshState().keep:
f = open('obtained', 'w')
obtained = stdout_data.split("\n")
- while len(obtained) > 0 and obtained[-1] == "":
+ while obtained and obtained[-1] == "":
del obtained[-1]
obtained = self.remove_ignored_lines(obtained)
for line in obtained:
print('\n'.join(logs))
return
- if proc.returncode != self.expect_return:
+ if not proc.returncode in self.expect_return:
if proc.returncode >= 0:
logs.append("Test suite `{file}': NOK (<{cmd}> returned code {code})".format(
file=FileReader().filename, cmd=cmdName, code=proc.returncode))
return_code = max(2, return_code)
print('\n'.join(logs))
return
- else:
- logs.append("Test suite `{file}': NOK (<{cmd}> got signal {sig})".format(
- file=FileReader().filename, cmd=cmdName,
- sig=SIGNALS_TO_NAMES_DICT[-proc.returncode]))
- if lock is not None:
- lock.release()
- return_code = max(max(-proc.returncode, 1), return_code)
- print('\n'.join(logs))
- return
+
+ logs.append("Test suite `{file}': NOK (<{cmd}> got signal {sig})".format(
+ file=FileReader().filename, cmd=cmdName,
+ sig=SIGNALS_TO_NAMES_DICT[-proc.returncode]))
+ if lock is not None:
+ lock.release()
+ return_code = max(max(-proc.returncode, 1), return_code)
+ print('\n'.join(logs))
+ return
if lock is not None:
lock.release()
def can_run(self):
return self.args is not None
-
##############
#
# Main
#
#
-
if __name__ == '__main__':
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
group1.add_argument(
'--ignore-jenkins',
action='store_true',
- help='ignore all cruft generated on SimGrid continous integration servers')
+ help='ignore all cruft generated on SimGrid continuous integration servers')
group1.add_argument('--wrapper', metavar='arg', help='Run each command in the provided wrapper (eg valgrind)')
group1.add_argument(
'--keep',
os.chdir(options.cd)
if options.ignore_jenkins:
- print("Ignore all cruft seen on SimGrid's continous integration servers")
+ print("Ignore all cruft seen on SimGrid's continuous integration servers")
# Note: regexps should match at the beginning of lines
TeshState().ignore_regexps_common = [
re.compile(r"profiling:"),
re.compile(r"Picked up JAVA_TOOL_OPTIONS: "),
re.compile(r"Picked up _JAVA_OPTIONS: "),
re.compile(r"==[0-9]+== ?WARNING: ASan doesn't fully support"),
- re.compile(r"==[0-9]+== ?WARNING: ASan is ignoring requested __asan_handle_no_return: stack top:"),
+ re.compile(r"==[0-9]+== ?WARNING: ASan is ignoring requested __asan_handle_no_return: stack "),
re.compile(r"False positive error reports may follow"),
- re.compile(r"For details see http://code.google.com/p/address-sanitizer/issues/detail\?id=189"),
- re.compile(r"For details see https://github.com/google/sanitizers/issues/189"),
+ re.compile(r"For details see http://code\.google\.com/p/address-sanitizer/issues/detail\?id=189"),
+ re.compile(r"For details see https://github\.com/google/sanitizers/issues/189"),
re.compile(r"Python runtime initialized with LC_CTYPE=C .*"),
# Seen on CircleCI
re.compile(r"cmake: /usr/local/lib/libcurl\.so\.4: no version information available \(required by cmake\)"),
- re.compile(r".*mmap broken on FreeBSD, but dlopen\+thread broken too. Switching to dlopen\+raw contexts\."),
+ re.compile(r".*mmap broken on FreeBSD, but dlopen\+thread broken too\. Switching to dlopen\+raw contexts\."),
re.compile(r".*dlopen\+thread broken on Apple and BSD\. Switching to raw contexts\."),
]
TeshState().jenkins = True # This is a Jenkins build
line = f.readfullline()
while line is not None:
# print(">>============="+line+"==<<")
- if len(line) == 0:
+ if not line:
#print ("END CMD block")
if cmd.run_if_possible():
cmd = Cmd()
cmd.output_display = True
cmd.ignore_output = True
elif line[0:15] == "! expect return":
- cmd.expect_return = int(line[16:])
+ cmd.expect_return = [int(line[16:])]
#print("expect return "+str(int(line[16:])))
elif line[0:15] == "! expect signal":
- sig = line[16:]
- # get the signal integer value from the signal module
- if sig not in signal.__dict__:
- fatal_error("unrecognized signal '" + sig + "'")
- sig = int(signal.__dict__[sig])
- # popen return -signal when a process ends with a signal
- cmd.expect_return = -sig
+ cmd.expect_return = []
+ for sig in (line[16:]).split("|"):
+ # get the signal integer value from the signal module
+ if sig not in signal.__dict__:
+ fatal_error("unrecognized signal '" + sig + "'")
+ sig = int(signal.__dict__[sig])
+ # popen return -signal when a process ends with a signal
+ cmd.expect_return.append(-sig)
elif line[0:len("! timeout ")] == "! timeout ":
if "no" in line[len("! timeout "):]:
cmd.timeout = None