Skip to content

Commit

Permalink
Capture container logs
Browse files Browse the repository at this point in the history
  • Loading branch information
asorbini committed Apr 15, 2024
1 parent a09a5db commit ff5e763
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
3 changes: 2 additions & 1 deletion uno/test/integration/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
28 changes: 20 additions & 8 deletions uno/test/integration/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("<none>", 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:
Expand Down

0 comments on commit ff5e763

Please sign in to comment.