Skip to content

Commit

Permalink
Merge pull request #219 from dcermak/get-container-logs
Browse files Browse the repository at this point in the history
Add function to read the container logs
  • Loading branch information
dcermak authored Aug 19, 2024
2 parents 8b8b5a1 + e8034b5 commit 659cee0
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 @@ -316,3 +316,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 659cee0

Please sign in to comment.