diff --git a/CHANGELOG.rst b/CHANGELOG.rst index c4dca2d..41c4bca 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -6,6 +6,10 @@ Breaking changes: Improvements and new features: +- Add the function + :py:func:`~pytest_container.container.ContainerData.read_container_logs` to + get access to the logs of the running container + - Expose the working directory via :py:attr:`~pytest_container.inspect.Config.workingdir` diff --git a/pytest_container/container.py b/pytest_container/container.py index 4419a11..1c4c664 100644 --- a/pytest_container/container.py +++ b/pytest_container/container.py @@ -898,6 +898,12 @@ def inspect(self) -> ContainerInspect: """ return self._container_runtime.inspect_container(self.container_id) + def read_container_logs(self) -> str: + """Returns the logs from the running container.""" + return check_output( + [self._container_runtime.runner_binary, "logs", self.container_id] + ).decode() + def container_to_pytest_param( container: ContainerBase, diff --git a/tests/test_container_build.py b/tests/test_container_build.py index 6ecbe17..5499ac4 100644 --- a/tests/test_container_build.py +++ b/tests/test_container_build.py @@ -276,3 +276,13 @@ def test_multistage_build_target( "cat /etc/os-release", ).stdout.strip() ) + + +LEAP_THAT_ECHOES_STUFF = DerivedContainer( + base=LEAP, containerfile="""CMD ["echo", "foobar"]""" +) + + +@pytest.mark.parametrize("container", [LEAP_THAT_ECHOES_STUFF], indirect=True) +def test_container_logs(container: ContainerData) -> None: + assert "foobar" in container.read_container_logs()