From a918449a439950216b0b0e93d2d5ff7c17f5e220 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dan=20=C4=8Cerm=C3=A1k?= Date: Tue, 9 Jul 2024 09:44:07 +0200 Subject: [PATCH] Expose the WorkingDir via inspect.config.workingdir --- CHANGELOG.rst | 3 +++ pytest_container/inspect.py | 3 +++ pytest_container/runtime.py | 4 ++++ tests/test_inspect.py | 13 ++++++++++++- 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 7f5e28a..c4dca2d 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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 `_) diff --git a/pytest_container/inspect.py b/pytest_container/inspect.py index b71fd68..2eed592 100644 --- a/pytest_container/inspect.py +++ b/pytest_container/inspect.py @@ -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 diff --git a/pytest_container/runtime.py b/pytest_container/runtime.py index 26578a7..517f712 100644 --- a/pytest_container/runtime.py +++ b/pytest_container/runtime.py @@ -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, @@ -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, diff --git a/tests/test_inspect.py b/tests/test_inspect.py index 827ac64..93aae6e 100644 --- a/tests/test_inspect.py +++ b/tests/test_inspect.py @@ -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"] @@ -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 @@ -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 )