FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix(core): make is_podman respect resolved docker host by Tranquility2 · Pull Request #1048 · testcontainers/testcontainers-python · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
17 changes: 14 additions & 3 deletions src/testcontainers/core/docker_client.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -535,9 +535,20 @@ def is_podman() -> bool:
``pytest.mark.skipif`` decorators.
"""
try:
# Use docker.from_env() directly rather than DockerClient() so we avoid
# the constructor's side effects (DOCKER_HOST mutation, registry login).
version = docker.from_env().version()
# Build the client directly rather than via DockerClient() so we avoid
# the constructor's side effects (registry login). We still resolve the
# host the same way DockerClient does so that hosts coming from the
# docker context (and SSH connections) are detected correctly.
docker_host = get_docker_host()
if docker_host:
client_kwargs: dict[str, Any] = {"base_url": docker_host}
if docker_host.startswith("ssh://"):
# Mirror DockerClient: use the shell SSH client to avoid paramiko
# failures under pytest stdin capture.
client_kwargs["use_ssh_client"] = True
version = docker.DockerClient(**client_kwargs).version()
else:
version = docker.from_env().version()
except Exception as e:
LOGGER.debug(f"is_podman: failed to query daemon version: {e}")
return False
Expand Down
25 changes: 25 additions & 0 deletions tests/core/test_docker_client.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,9 @@ def test_is_podman(monkeypatch: pytest.MonkeyPatch, version: object, expected: b
from testcontainers.core import docker_client as dc

dc.is_podman.cache_clear()
# Force the no-host branch so the test is deterministic regardless of the
# docker context configured on the machine running it.
monkeypatch.setattr("testcontainers.core.docker_client.get_docker_host", lambda: None)
mock_client = MagicMock()
if isinstance(version, Exception):
monkeypatch.setattr("testcontainers.core.docker_client.docker.from_env", MagicMock(side_effect=version))
Expand All @@ -389,6 +392,28 @@ def test_is_podman(monkeypatch: pytest.MonkeyPatch, version: object, expected: b
dc.is_podman.cache_clear()


def test_is_podman_uses_resolved_host(monkeypatch: pytest.MonkeyPatch) -> None:
"""When a host is resolved (e.g. from the docker context), is_podman must
query that host rather than falling back to docker.from_env defaults."""
from testcontainers.core import docker_client as dc

dc.is_podman.cache_clear()
monkeypatch.setattr("testcontainers.core.docker_client.get_docker_host", lambda: "ssh://root@remote-podman")
mock_client = MagicMock()
mock_client.version.return_value = {"Platform": {"Name": "Podman Engine"}}
mock_docker_client = MagicMock(return_value=mock_client)
monkeypatch.setattr("testcontainers.core.docker_client.docker.DockerClient", mock_docker_client)
monkeypatch.setattr(
"testcontainers.core.docker_client.docker.from_env",
MagicMock(side_effect=AssertionError("should not call from_env when host resolved")),
)
try:
assert dc.is_podman() is True
mock_docker_client.assert_called_once_with(base_url="ssh://root@remote-podman", use_ssh_client=True)
finally:
dc.is_podman.cache_clear()


def _mock_docker_context(name: str, host: str) -> MagicMock:
context = MagicMock()
context.Name = name
Expand Down
Loading

Back | FazBrowse Home | New Git URL