Skip to content

Commit 3c45d7a

Browse files
committed
NO-JIRA: chore(tests/containers): detect socket for ryuk's use with rootless podman on macOS
# Conflicts: # poetry.lock
1 parent 5888b6c commit 3c45d7a

File tree

9 files changed

+607
-49
lines changed

9 files changed

+607
-49
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ DOCKER_HOST=unix:///run/user/$UID/podman/podman.sock poetry run pytest tests/con
110110
# Mac OS
111111
brew install podman
112112
podman machine init
113-
podman machine set --rootful
113+
podman machine set --rootful=false
114114
sudo podman-mac-helper install
115115
podman machine start
116116
poetry run pytest tests/containers --image quay.io/opendatahub/workbench-images@sha256:e98d19df346e7abb1fa3053f6d41f0d1fa9bab39e49b4cb90b510ca33452c2e4

poetry.lock

Lines changed: 20 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pytest-subtests = "^0.14.1"
1717
pyfakefs = "^5.7.4"
1818
testcontainers = "^4.9.1"
1919
docker = "^7.1.0"
20+
podman = "^5.2.0"
2021
pydantic = "^2.10.6"
2122
requests = "^2.32.3"
2223

tests/containers/docker_utils.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@
33
import io
44
import logging
55
import os.path
6+
import platform
67
import sys
78
import tarfile
89
import time
910
from typing import Iterable, TYPE_CHECKING
1011

12+
import podman
1113
import docker.client
1214

1315
import testcontainers.core.container
1416

17+
import tests.containers.pydantic_schemas
18+
1519
if TYPE_CHECKING:
1620
from docker.models.containers import Container
1721

@@ -158,12 +162,24 @@ def communicate(self, line_prefix=b"") -> int:
158162
raise RuntimeError("Hm could that really happen?")
159163
return self.poll()
160164

161-
def get_socket_path(client: docker.client.DockerClient) -> str:
165+
def get_socket_path(docker_client: docker.client.DockerClient) -> str:
162166
"""Determine the local socket path.
163167
This works even when `podman machine` with its own host-mounts is involved
164168
NOTE: this will not work for remote docker, but we will cross the bridge when we come to it"""
165-
socket_path = _the_one(adapter.socket_path for adapter in client.api.adapters.values())
166-
return socket_path
169+
socket_path = _the_one(adapter.socket_path for adapter in docker_client.api.adapters.values())
170+
if platform.system().lower() == 'linux':
171+
return socket_path
172+
if platform.system().lower() == 'darwin':
173+
# we want to return the socket path that's valid from inside Podman Machine
174+
# with rootful podman, both the host (ls) and podman machine (podman machine ssh ls) have it at /var/run/docker.sock
175+
# but with rootless podman, the location on host is still the same while podman machine has it in /var/run/user/${PID}/podman/podman.sock
176+
podman_client = podman.PodmanClient(base_url="http+unix://" + socket_path)
177+
info = tests.containers.pydantic_schemas.PodmanInfo.model_validate(podman_client.info())
178+
assert info.host.remoteSocket.exists, "Failed to determine the podman remote socket"
179+
assert info.host.remoteSocket.path.startswith("unix://"), "Unexpected remote socket path"
180+
return info.host.remoteSocket.path[len("unix://"):]
181+
182+
raise RuntimeError(f"Unsupported platform: {platform.system()=}")
167183

168184
def get_container_pid(container: Container) -> int | None:
169185
"""Get the network namespace of a Docker container."""

tests/containers/podman_machine_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
import subprocess
55
from typing import Callable
66

7-
import tests.containers.schemas
7+
import tests.containers.pydantic_schemas
88

99
logging.basicConfig(level=logging.DEBUG)
1010

11-
def open_ssh_tunnel(machine_predicate: Callable[[tests.containers.schemas.PodmanMachine], bool],
11+
def open_ssh_tunnel(machine_predicate: Callable[[tests.containers.pydantic_schemas.PodmanMachine], bool],
1212
local_port: int, remote_port: int, remote_interface: str = "localhost") -> subprocess.Popen:
1313
# Load and parse the Podman machine data
1414
machine_names = subprocess.check_output(["podman", "machine", "list", "--quiet"], text=True).splitlines()
1515
json_data = subprocess.check_output(["podman", "machine", "inspect", *machine_names], text=True)
16-
inspect = tests.containers.schemas.PodmanMachineInspect(machines=json.loads(json_data))
16+
inspect = tests.containers.pydantic_schemas.PodmanMachineInspect(machines=json.loads(json_data))
1717
machines = inspect.machines
1818

1919
machine = next((m for m in machines if machine_predicate(m)), None)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from .podman_info import PodmanInfo
2+
from .podman_machine_inspect import PodmanMachineInspect, PodmanMachine
3+
4+
__all__ = [
5+
PodmanInfo,
6+
PodmanMachineInspect,
7+
PodmanMachine,
8+
]

0 commit comments

Comments
 (0)