From 5d0ea943d2d5827b4de6543f7538b2826b0ef27d Mon Sep 17 00:00:00 2001 From: Vyacheslav Yurkov Date: Thu, 31 Aug 2023 20:41:31 +0200 Subject: [PATCH] test: helper: Print stdout on timeout Signed-off-by: Vyacheslav Yurkov --- test/helper.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/test/helper.py b/test/helper.py index b067d6e3..bfabbd6d 100644 --- a/test/helper.py +++ b/test/helper.py @@ -66,15 +66,24 @@ def run(command, *, timeout=30): logger = logger_from_command(command) logger.info('running: %s', command) - proc = subprocess.run(shlex.split(command), capture_output=True, text=True, check=False, - timeout=timeout) - - for line in proc.stdout.splitlines(): - if line: - logger.info('stdout: %s', line) - for line in proc.stderr.splitlines(): - if line: - logger.warning('stderr: %s', line) + def stdout_print_helper(logger, prefix, stdout): + if stdout is None: + return + + for line in stdout.splitlines(): + if line: + logger.info(f'{prefix}: %s', line) + + try: + proc = subprocess.run(shlex.split(command), capture_output=True, text=True, check=False, + timeout=timeout) + except subprocess.TimeoutExpired as e: + stdout_print_helper(logger, "stdout", e.stdout) + stdout_print_helper(logger, "stderr", e.stderr) + raise + + stdout_print_helper(logger, "stdout", proc.stdout) + stdout_print_helper(logger, "stderr", proc.stderr) logger.info('exitcode: %d', proc.returncode)