From f7d4afeb2f09f444dd1d36f9bec88ceb6919e4b7 Mon Sep 17 00:00:00 2001 From: mayeut Date: Fri, 8 Sep 2023 11:02:04 +0200 Subject: [PATCH] use fixture in oci_container_test.py to clean-up images after tests --- test/conftest.py | 8 ++------ unit_test/oci_container_test.py | 35 ++++++++++++++++++++++++--------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/test/conftest.py b/test/conftest.py index 161148680..06a4f2424 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -34,6 +34,8 @@ def build_frontend_env(request) -> dict[str, str]: @pytest.fixture() def docker_cleanup() -> Generator[None, None, None]: def get_images() -> set[str]: + if detect_ci_provider() is None or platform != "linux": + return set() images = subprocess.run( ["docker", "image", "ls", "--format", "{{json .ID}}"], text=True, @@ -42,12 +44,6 @@ def get_images() -> set[str]: ).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 diff --git a/unit_test/oci_container_test.py b/unit_test/oci_container_test.py index fb61b8b88..c1c86a7fc 100644 --- a/unit_test/oci_container_test.py +++ b/unit_test/oci_container_test.py @@ -14,6 +14,7 @@ from cibuildwheel.environment import EnvironmentAssignmentBash from cibuildwheel.oci_container import OCIContainer, OCIContainerEngineConfig +from cibuildwheel.util import detect_ci_provider # Test utilities @@ -31,14 +32,31 @@ PODMAN = OCIContainerEngineConfig(name="podman") -@pytest.fixture(params=["docker", "podman"]) +@pytest.fixture(params=["docker", "podman"], scope="module") def container_engine(request): if request.param == "docker" and not request.config.getoption("--run-docker"): pytest.skip("need --run-docker option to run") if request.param == "podman" and not request.config.getoption("--run-podman"): pytest.skip("need --run-podman option to run") - return OCIContainerEngineConfig(name=request.param) + def get_images() -> set[str]: + if detect_ci_provider() is None or platform != "linux": + return set() + images = subprocess.run( + [request.param, "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()} + + images_before = get_images() + try: + yield OCIContainerEngineConfig(name=request.param) + finally: + images_after = get_images() + for image in images_after - images_before: + subprocess.run([request.param, "rmi", image], check=False) # Tests @@ -219,10 +237,9 @@ def test_environment_executor(container_engine): assert assignment.evaluated_value({}, container.environment_executor) == "42" -def test_podman_vfs(tmp_path: Path, monkeypatch, request): - # Tests podman VFS, for the podman in docker use-case - if not request.config.getoption("--run-podman"): - pytest.skip("need --run-podman option to run") +def test_podman_vfs(tmp_path: Path, monkeypatch, container_engine): + if container_engine.name != "podman": + pytest.skip("only runs with podman") # create the VFS configuration vfs_path = tmp_path / "podman_vfs" @@ -298,9 +315,9 @@ def test_podman_vfs(tmp_path: Path, monkeypatch, request): subprocess.run(["podman", "unshare", "rm", "-rf", vfs_path], check=True) -def test_create_args_volume(tmp_path: Path, request): - if not request.config.getoption("--run-docker"): - pytest.skip("need --run-docker option to run") +def test_create_args_volume(tmp_path: Path, container_engine): + if container_engine.name != "docker": + pytest.skip("only runs with docker") if "CIRCLECI" in os.environ or "GITLAB_CI" in os.environ: pytest.skip(