From ff5e7634daccad14bd39efbef1a132ecb6648574 Mon Sep 17 00:00:00 2001 From: Andrea Sorbini Date: Mon, 15 Apr 2024 12:27:01 -0700 Subject: [PATCH] Capture container logs --- uno/test/integration/experiment.py | 3 ++- uno/test/integration/host.py | 28 ++++++++++++++++++++-------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/uno/test/integration/experiment.py b/uno/test/integration/experiment.py index f374213..2928ddd 100644 --- a/uno/test/integration/experiment.py +++ b/uno/test/integration/experiment.py @@ -61,6 +61,7 @@ class Experiment: InsideTestRunner = bool(os.environ.get("UNO_TEST_RUNNER", False)) TestImage = os.environ.get("TEST_IMAGE", "mentalsmash/uno-test-runner:latest") RunnerScript = os.environ.get("TEST_RUNNER", "/uno/uno/test/integration/runner.py") + ExternalTestDir = os.environ.get("TEST_DIR") BuiltImages = set() UnoDir = _uno_dir # Load the selected uno middleware plugin @@ -101,7 +102,7 @@ def define( config = cls.load_config(config) # Check if the user specified a non-temporary test directory # Otherwise the experiment will allocate a temporary directory - test_dir = os.environ.get("TEST_DIR", test_dir) + test_dir = cls.ExternalTestDir or test_dir test_dir_tmp = None if test_dir is not None: test_dir = Path(test_dir) / name diff --git a/uno/test/integration/host.py b/uno/test/integration/host.py index 2e49b23..8cc768c 100644 --- a/uno/test/integration/host.py +++ b/uno/test/integration/host.py @@ -406,19 +406,31 @@ def start(self, wait: bool = False) -> subprocess.Popen: self.log.activity("started") return result - def print_logs(self, output_file: Path | None = None) -> None: + def print_logs(self) -> None: self.log.debug("generating host logs... (DEBUG={})", self.log.DEBUG) if self.log.DEBUG: import sys - print("{} {} [host logs] {}".format("=" * 20, self.container_name, "=" * 20), file=sys.stderr) - exec_command(["docker", "logs", self.container_name], debug=True) - print( - "{} //{} [host logs] {}".format("=" * 19, self.container_name, "=" * 19), file=sys.stderr - ) - if output_file is None: + def _print(name, out): + print( + "{} {} [host logs - {}] {}".format("=" * 20, self.container_name, name, "=" * 20), + file=sys.stderr, + ) + if out: + print(out.decode(), file=sys.stderr) + else: + print("", file=sys.stderr) + print( + "{} //{} [host logs - {}] {}".format("=" * 19, self.container_name, name, "=" * 19), + file=sys.stderr, + ) + + result = exec_command(["docker", "logs", self.container_name], capture_output=True) + _print("stdout", result.stdout) + _print("stderr", result.stderr) + if self.experiment.ExternalTestDir: output_file = self.test_dir / "container.log" - exec_command(["docker", "logs", self.container_name], output_file=output_file) + exec_command(["docker", "logs", self.container_name], output_file=output_file) self.log.debug("generated host logs: {}", output_file) def stop(self) -> subprocess.Popen: