Skip to content

Commit

Permalink
Add function to read the container logs
Browse files Browse the repository at this point in the history
  • Loading branch information
dcermak committed Aug 16, 2024
1 parent 70de2e0 commit 21d316d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
6 changes: 6 additions & 0 deletions pytest_container/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions tests/test_container_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 21d316d

Please sign in to comment.