From 224f6234941e9ccc299111d52e194b5437a270c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20B=C3=BChler?= Date: Fri, 3 May 2013 13:56:24 +0200 Subject: [PATCH] [tests] new features for running tests: log to console, kill services again after timeout --- tests/runtests.py | 4 ++++ tests/service.py | 26 +++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/tests/runtests.py b/tests/runtests.py index 0b1b44d..da07519 100755 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -52,6 +52,8 @@ parser.add_option("--strace", help = "Strace services", action = "store_true", d parser.add_option("--truss", help = "Truss services", action = "store_true", default = False) parser.add_option("--debug-requests", help = "Dump requests", action = "store_true", default = False) parser.add_option("--no-angel", help = "Spawn lighttpd worker directly", action = "store_true", default = False) +parser.add_option("--debug", help = "Show service logs on console", action = "store_true", default = False) +parser.add_option("--wait", help = "Wait for services to exit on first signal", action = "store_true", default = False) (options, args) = parser.parse_args() @@ -73,6 +75,8 @@ Env.debugRequests = options.debug_requests Env.strace = options.strace Env.truss = options.truss Env.no_angel = options.no_angel +Env.debug = options.debug +Env.wait = options.wait Env.color = sys.stdin.isatty() Env.COLOR_RESET = Env.color and "\033[0m" or "" diff --git a/tests/service.py b/tests/service.py index fc746e6..3b60bf0 100644 --- a/tests/service.py +++ b/tests/service.py @@ -6,6 +6,7 @@ import subprocess import socket import select import signal +import time import base @@ -29,6 +30,16 @@ trussargs = [ 'truss', '-d', '-f', '-s', '4096', '-o' ] def preexec(): os.setsid() +def procwait(proc, timeout = 3): + ts = time.time() + while True: + if proc.poll() is not None: return True + seconds_passed = time.time() - ts + if seconds_passed > timeout: + return False + time.sleep(0.1) + + class Service(object): name = None @@ -48,7 +59,10 @@ class Service(object): if None == self.name: raise ServiceException("Service needs a name!") - logfile = open(self.log, "w") + if base.Env.debug: + logfile = None + else: + logfile = open(self.log, "w") if base.Env.strace: slog = self.tests.PrepareFile("log/strace-%s.log" % self.name, "") @@ -60,7 +74,7 @@ class Service(object): print >> base.Env.log, "Spawning '%s': %s" % (self.name, ' '.join(args)) proc = subprocess.Popen(args, stdin = inp, stdout = logfile, stderr = logfile, close_fds = True, preexec_fn = preexec) if None != inp: inp.close() - logfile.close() + if None != logfile: logfile.close() self.proc = proc atexit.register(self.kill) @@ -76,7 +90,13 @@ class Service(object): except: pass print >> base.Env.log, "Waiting for service '%s'" % (self.name) - proc.wait() + if base.Env.wait: proc.wait() + while not procwait(proc): + try: + os.killpg(proc.pid, signal.SIGINT) + proc.terminate() + except: + pass def portfree(self, port): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)