Skip to content

Commit ba11212

Browse files
committed
use fixture in oci_container_test.py to clean-up images after tests
1 parent 6d0890e commit ba11212

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

test/conftest.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ def build_frontend_env(request) -> dict[str, str]:
3434
@pytest.fixture()
3535
def docker_cleanup() -> Generator[None, None, None]:
3636
def get_images() -> set[str]:
37+
if detect_ci_provider() is None or platform != "linux":
38+
return set()
3739
images = subprocess.run(
3840
["docker", "image", "ls", "--format", "{{json .ID}}"],
3941
text=True,
@@ -42,12 +44,6 @@ def get_images() -> set[str]:
4244
).stdout
4345
return {json.loads(image.strip()) for image in images.splitlines() if image.strip()}
4446

45-
if detect_ci_provider() is None or platform != "linux":
46-
try:
47-
yield
48-
finally:
49-
pass
50-
return
5147
images_before = get_images()
5248
try:
5349
yield

unit_test/oci_container_test.py

+27-9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from cibuildwheel.environment import EnvironmentAssignmentBash
1616
from cibuildwheel.oci_container import OCIContainer, OCIContainerEngineConfig
17+
from cibuildwheel.util import detect_ci_provider
1718

1819
# Test utilities
1920

@@ -31,13 +32,31 @@
3132
PODMAN = OCIContainerEngineConfig(name="podman")
3233

3334

34-
@pytest.fixture(params=["docker", "podman"])
35+
@pytest.fixture(params=["docker", "podman"], scope="module")
3536
def container_engine(request):
3637
if request.param == "docker" and not request.config.getoption("--run-docker"):
3738
pytest.skip("need --run-docker option to run")
3839
if request.param == "podman" and not request.config.getoption("--run-podman"):
3940
pytest.skip("need --run-podman option to run")
40-
return OCIContainerEngineConfig(name=request.param)
41+
42+
def get_images() -> set[str]:
43+
if detect_ci_provider() is None:
44+
return set()
45+
images = subprocess.run(
46+
[request.param, "image", "ls", "--format", "{{json .ID}}"],
47+
text=True,
48+
check=True,
49+
stdout=subprocess.PIPE,
50+
).stdout
51+
return {json.loads(image.strip()) for image in images.splitlines() if image.strip()}
52+
53+
images_before = get_images()
54+
try:
55+
yield OCIContainerEngineConfig(name=request.param)
56+
finally:
57+
images_after = get_images()
58+
for image in images_after - images_before:
59+
subprocess.run([request.param, "rmi", image], check=False)
4160

4261

4362
# Tests
@@ -230,10 +249,9 @@ def test_environment_executor(container_engine):
230249
assert assignment.evaluated_value({}, container.environment_executor) == "42"
231250

232251

233-
def test_podman_vfs(tmp_path: Path, monkeypatch, request):
234-
# Tests podman VFS, for the podman in docker use-case
235-
if not request.config.getoption("--run-podman"):
236-
pytest.skip("need --run-podman option to run")
252+
def test_podman_vfs(tmp_path: Path, monkeypatch, container_engine):
253+
if container_engine.name != "podman":
254+
pytest.skip("only runs with podman")
237255

238256
# create the VFS configuration
239257
vfs_path = tmp_path / "podman_vfs"
@@ -309,9 +327,9 @@ def test_podman_vfs(tmp_path: Path, monkeypatch, request):
309327
subprocess.run(["podman", "unshare", "rm", "-rf", vfs_path], check=True)
310328

311329

312-
def test_create_args_volume(tmp_path: Path, request):
313-
if not request.config.getoption("--run-docker"):
314-
pytest.skip("need --run-docker option to run")
330+
def test_create_args_volume(tmp_path: Path, container_engine):
331+
if container_engine.name != "docker":
332+
pytest.skip("only runs with docker")
315333

316334
if "CIRCLECI" in os.environ or "GITLAB_CI" in os.environ:
317335
pytest.skip(

0 commit comments

Comments
 (0)