Skip to content

Commit

Permalink
fix[test]: clean-up docker images in test_*linux*_only.py
Browse files Browse the repository at this point in the history
We are reaching disk space limits (14 GB) when running tests.
Add a fixture to clean-up docker images after tests.
This fixture is applied on tests that pull a specific image for one test only in order not to take too much time pulling that image many times.
  • Loading branch information
mayeut committed Jul 30, 2023
1 parent 144b852 commit e32ed3d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
34 changes: 34 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
from __future__ import annotations

import json
import subprocess
from typing import Generator

import pytest

from cibuildwheel.util import detect_ci_provider

from .utils import platform


def pytest_addoption(parser) -> None:
parser.addoption(
Expand All @@ -21,3 +29,29 @@ def pytest_addoption(parser) -> None:
)
def build_frontend_env(request) -> dict[str, str]:
return request.param # type: ignore[no-any-return]


@pytest.fixture()
def docker_cleanup() -> Generator[None, None, None]:
def get_images() -> set[str]:
images = subprocess.run(
["docker", "image", "ls", "--format", "{{json .ID}}"],
text=True,
check=True,
stdout=subprocess.PIPE,
).stdout
return {json.loads(image.strip()) for image in images.splitlines() if image.strip()}

if detect_ci_provider() is None or platform != "linux":
try:
yield
finally:
pass
return
images_before = get_images()
try:
yield
finally:
images_after = get_images()
for image in images_after - images_before:
subprocess.run(["docker", "rmi", image], check=False)
1 change: 1 addition & 0 deletions test/test_container_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
)


@pytest.mark.usefixtures("docker_cleanup")
def test(tmp_path):
if utils.platform != "linux":
pytest.skip("the test is only relevant to the linux build")
Expand Down
1 change: 1 addition & 0 deletions test/test_manylinuxXXXX_only.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"manylinux_image",
["manylinux1", "manylinux2010", "manylinux2014", "manylinux_2_24", "manylinux_2_28"],
)
@pytest.mark.usefixtures("docker_cleanup")
def test(manylinux_image, tmp_path):
if utils.platform != "linux":
pytest.skip("the container image test is only relevant to the linux build")
Expand Down
1 change: 1 addition & 0 deletions test/test_musllinux_X_Y_only.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"musllinux_image",
["musllinux_1_1", "musllinux_1_2"],
)
@pytest.mark.usefixtures("docker_cleanup")
def test(musllinux_image, tmp_path):
if utils.platform != "linux":
pytest.skip("the container image test is only relevant to the linux build")
Expand Down

0 comments on commit e32ed3d

Please sign in to comment.