|
8 | 8 | import time
|
9 | 9 | from typing import Iterable, TYPE_CHECKING
|
10 | 10 |
|
| 11 | +import podman |
11 | 12 | import docker.client
|
12 | 13 |
|
13 | 14 | import testcontainers.core.container
|
14 | 15 |
|
| 16 | +import tests.containers.pydantic_schemas |
| 17 | + |
15 | 18 | if TYPE_CHECKING:
|
16 | 19 | from docker.models.containers import Container
|
17 | 20 |
|
@@ -158,13 +161,29 @@ def communicate(self, line_prefix=b"") -> int:
|
158 | 161 | raise RuntimeError("Hm could that really happen?")
|
159 | 162 | return self.poll()
|
160 | 163 |
|
| 164 | + |
161 | 165 | def get_socket_path(client: docker.client.DockerClient) -> str:
|
162 | 166 | """Determine the local socket path.
|
163 | 167 | This works even when `podman machine` with its own host-mounts is involved
|
164 | 168 | NOTE: this will not work for remote docker, but we will cross the bridge when we come to it"""
|
165 | 169 | socket_path = _the_one(adapter.socket_path for adapter in client.api.adapters.values())
|
166 | 170 | return socket_path
|
167 | 171 |
|
| 172 | + |
| 173 | +def get_podman_machine_socket_path(docker_client: docker.client.DockerClient) -> str: |
| 174 | + """Determine the podman socket path that's valid from inside Podman Machine. |
| 175 | + * rootful podman: both the host (`ls`) and podman machine (`podman machine ssh ls`) have it at `/var/run/docker.sock`. |
| 176 | + * rootless podman: the location on host is still the same while podman machine has it in `/var/run/user/${PID}/podman/podman.sock`. |
| 177 | + """ |
| 178 | + socket_path = get_socket_path(docker_client) |
| 179 | + podman_client = podman.PodmanClient(base_url="http+unix://" + socket_path) |
| 180 | + info = tests.containers.pydantic_schemas.PodmanInfo.model_validate(podman_client.info()) |
| 181 | + assert info.host.remoteSocket.exists, "Failed to determine the podman remote socket" |
| 182 | + assert info.host.remoteSocket.path.startswith("unix://"), "Unexpected remote socket path" |
| 183 | + machine_socket_path = info.host.remoteSocket.path[len("unix://"):] |
| 184 | + return machine_socket_path |
| 185 | + |
| 186 | + |
168 | 187 | def get_container_pid(container: Container) -> int | None:
|
169 | 188 | """Get the network namespace of a Docker container."""
|
170 | 189 | container.reload()
|
|
0 commit comments