Skip to content

Commit 63b396e

Browse files
Merge pull request #597 from Honny1/fix-client
fix: use active service connection when no explicit connection specified
2 parents 599b17a + eb36f56 commit 63b396e

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

podman/client.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,15 @@ def __init__(self, **kwargs) -> None:
6969
# Override configured identity, if provided in arguments
7070
api_kwargs["identity"] = kwargs.get("identity", str(connection.identity))
7171
elif "base_url" not in api_kwargs:
72-
path = str(Path(get_runtime_dir()) / "podman" / "podman.sock")
73-
api_kwargs["base_url"] = "http+unix://" + path
72+
# Check if there's an active service configured and is a podman machine
73+
active_service = config.active_service
74+
if active_service and active_service.is_machine:
75+
api_kwargs["base_url"] = active_service.url.geturl()
76+
api_kwargs["identity"] = kwargs.get("identity", str(active_service.identity))
77+
else:
78+
# Fall back to local Unix socket
79+
path = str(Path(get_runtime_dir()) / "podman" / "podman.sock")
80+
api_kwargs["base_url"] = "http+unix://" + path
7481
self.api = APIClient(**api_kwargs)
7582

7683
def __enter__(self) -> "PodmanClient":

podman/domain/config.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ def identity(self):
5959
return Path(self.attrs.get("identity"))
6060
return Path(self.attrs.get("Identity"))
6161

62+
@cached_property
63+
def is_machine(self) -> bool:
64+
"""bool: Returns True if connection is to a Podman machine."""
65+
return self.attrs.get("IsMachine", False)
66+
6267

6368
class PodmanConfig:
6469
"""PodmanConfig provides a representation of the containers.conf file."""

podman/tests/unit/test_podmanclient.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import requests_mock
88

9+
from podman.domain.config import PodmanConfig, ServiceConnection
910
from podman import PodmanClient, tests
1011
from podman.api.path_utils import get_runtime_dir, get_xdg_config_home
1112

@@ -108,6 +109,23 @@ def test_connect_default(self):
108109
expected = Path(get_xdg_config_home()) / "containers" / "containers.conf"
109110
PodmanClientTestCase.opener.assert_called_with(expected, encoding="utf-8")
110111

112+
def test_connect_with_connection_file(self):
113+
mock_config = MagicMock(spec=PodmanConfig)
114+
mock_service = MagicMock(spec=ServiceConnection)
115+
mock_service.url.geturl.return_value = (
116+
"http+ssh://[email protected]:58468/run/user/501/podman/podman.sock"
117+
)
118+
mock_service.identity = "/Users/test/.local/share/containers/podman/machine/machine"
119+
mock_service.is_machine = True
120+
mock_config.active_service = mock_service
121+
122+
with mock.patch('podman.client.PodmanConfig', return_value=mock_config):
123+
# Mock pathlib.Path.exists to return True for the identity file
124+
with mock.patch('pathlib.Path.exists', return_value=True):
125+
with PodmanClient() as client:
126+
expected = "http+ssh://[email protected]:58468/run/user/501/podman/podman.sock"
127+
self.assertEqual(client.api.base_url.geturl(), expected)
128+
111129

112130
if __name__ == '__main__':
113131
unittest.main()

0 commit comments

Comments
 (0)