Skip to content

Commit

Permalink
use fixture in oci_container_test.py to clean-up images after tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mayeut committed Sep 8, 2023
1 parent fa9dfd4 commit f7d4afe
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
8 changes: 2 additions & 6 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
35 changes: 26 additions & 9 deletions unit_test/oci_container_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit f7d4afe

Please sign in to comment.