Skip to content

Commit

Permalink
Merge pull request #215 from dcermak/add-inspect-workdir
Browse files Browse the repository at this point in the history
Expose the WorkingDir via inspect.config.workingdir
  • Loading branch information
dcermak authored Jul 9, 2024
2 parents c02cf44 + a918449 commit a872ff9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ Breaking changes:

Improvements and new features:

- Expose the working directory via
:py:attr:`~pytest_container.inspect.Config.workingdir`

- Don't use non-FIPS hashes for generating the lockfile (`gh#213
<https://github.com/dcermak/pytest_container/issues/213>`_)

Expand Down
3 changes: 3 additions & 0 deletions pytest_container/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ class Config:
#:
stop_signal: Union[int, str]

#: The working directory of the container
workingdir: str

#: optional healthcheck defined for the underlying container image
healthcheck: Optional[HealthCheck] = None

Expand Down
4 changes: 4 additions & 0 deletions pytest_container/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ def inspect_container(self, container_id: str) -> ContainerInspect:
image=config["Image"],
entrypoint=entrypoint,
labels=config["Labels"],
workingdir=config["WorkingDir"],
env=dict([env.split("=", maxsplit=1) for env in config["Env"]]),
stop_signal=self._stop_signal_from_inspect_conf(config),
healthcheck=healthcheck,
Expand Down Expand Up @@ -606,6 +607,9 @@ def inspect_container(self, container_id: str) -> ContainerInspect:
image=config["Image"],
entrypoint=config["Entrypoint"],
labels=config["Labels"],
# docker sometimes omits the working directory,
# then it defaults to
workingdir=config["WorkingDir"] or "/",
stop_signal=self._stop_signal_from_inspect_conf(config),
env=env,
healthcheck=healthcheck,
Expand Down
13 changes: 12 additions & 1 deletion tests/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
USER opensuse
ENTRYPOINT ["/bin/bash", "-e"]
ENV HOME=/src/
WORKDIR /foobar/
ENV MY_VAR=
ENV SUFFIX_NAME=dc=example,dc=com
CMD ["/bin/sh"]
Expand Down Expand Up @@ -66,6 +67,7 @@ def test_inspect(

assert inspect.config.image == expected_img
assert inspect.config.cmd == ["/bin/sh"]
assert inspect.config.workingdir == "/foobar/"

assert (
not inspect.state.paused
Expand All @@ -82,10 +84,19 @@ def test_inspect(

assert inspect.network.ip_address or "" == host.check_output(
f"{container_runtime.runner_binary} inspect --format "
f'"{{{{ .NetworkSettings.IPAddress }}}}" {_CTR_NAME}'
'"{{ .NetworkSettings.IPAddress }}" ' + _CTR_NAME
)


@pytest.mark.parametrize("container", [LEAP], indirect=True)
def test_inspect_unset_workdir(container: ContainerData) -> None:
"""If the container has no workdir set, check that it defaults to ``/`` as
docker sometimes omits the workingdir setting.
"""
assert container.inspect.config.workingdir == "/"


@pytest.mark.parametrize(
"container", [IMAGE_WITH_STRING_CMD_AND_ENTRYPOINT], indirect=True
)
Expand Down

0 comments on commit a872ff9

Please sign in to comment.