use fixture in oci_container_test.py to clean-up images after tests
This commit is contained in:
+2
-6
@@ -34,6 +34,8 @@ def build_frontend_env(request) -> dict[str, str]:
|
|||||||
@pytest.fixture()
|
@pytest.fixture()
|
||||||
def docker_cleanup() -> Generator[None, None, None]:
|
def docker_cleanup() -> Generator[None, None, None]:
|
||||||
def get_images() -> set[str]:
|
def get_images() -> set[str]:
|
||||||
|
if detect_ci_provider() is None or platform != "linux":
|
||||||
|
return set()
|
||||||
images = subprocess.run(
|
images = subprocess.run(
|
||||||
["docker", "image", "ls", "--format", "{{json .ID}}"],
|
["docker", "image", "ls", "--format", "{{json .ID}}"],
|
||||||
text=True,
|
text=True,
|
||||||
@@ -42,12 +44,6 @@ def docker_cleanup() -> Generator[None, None, None]:
|
|||||||
).stdout
|
).stdout
|
||||||
return {json.loads(image.strip()) for image in images.splitlines() if image.strip()}
|
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()
|
images_before = get_images()
|
||||||
try:
|
try:
|
||||||
yield
|
yield
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import tomli_w
|
|||||||
|
|
||||||
from cibuildwheel.environment import EnvironmentAssignmentBash
|
from cibuildwheel.environment import EnvironmentAssignmentBash
|
||||||
from cibuildwheel.oci_container import OCIContainer, OCIContainerEngineConfig
|
from cibuildwheel.oci_container import OCIContainer, OCIContainerEngineConfig
|
||||||
|
from cibuildwheel.util import detect_ci_provider
|
||||||
|
|
||||||
# Test utilities
|
# Test utilities
|
||||||
|
|
||||||
@@ -31,13 +32,31 @@ else:
|
|||||||
PODMAN = OCIContainerEngineConfig(name="podman")
|
PODMAN = OCIContainerEngineConfig(name="podman")
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(params=["docker", "podman"])
|
@pytest.fixture(params=["docker", "podman"], scope="module")
|
||||||
def container_engine(request):
|
def container_engine(request):
|
||||||
if request.param == "docker" and not request.config.getoption("--run-docker"):
|
if request.param == "docker" and not request.config.getoption("--run-docker"):
|
||||||
pytest.skip("need --run-docker option to run")
|
pytest.skip("need --run-docker option to run")
|
||||||
if request.param == "podman" and not request.config.getoption("--run-podman"):
|
if request.param == "podman" and not request.config.getoption("--run-podman"):
|
||||||
pytest.skip("need --run-podman option to run")
|
pytest.skip("need --run-podman option to run")
|
||||||
return OCIContainerEngineConfig(name=request.param)
|
|
||||||
|
def get_images() -> set[str]:
|
||||||
|
if detect_ci_provider() is None:
|
||||||
|
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
|
# Tests
|
||||||
@@ -230,10 +249,9 @@ def test_environment_executor(container_engine):
|
|||||||
assert assignment.evaluated_value({}, container.environment_executor) == "42"
|
assert assignment.evaluated_value({}, container.environment_executor) == "42"
|
||||||
|
|
||||||
|
|
||||||
def test_podman_vfs(tmp_path: Path, monkeypatch, request):
|
def test_podman_vfs(tmp_path: Path, monkeypatch, container_engine):
|
||||||
# Tests podman VFS, for the podman in docker use-case
|
if container_engine.name != "podman":
|
||||||
if not request.config.getoption("--run-podman"):
|
pytest.skip("only runs with podman")
|
||||||
pytest.skip("need --run-podman option to run")
|
|
||||||
|
|
||||||
# create the VFS configuration
|
# create the VFS configuration
|
||||||
vfs_path = tmp_path / "podman_vfs"
|
vfs_path = tmp_path / "podman_vfs"
|
||||||
@@ -309,9 +327,9 @@ def test_podman_vfs(tmp_path: Path, monkeypatch, request):
|
|||||||
subprocess.run(["podman", "unshare", "rm", "-rf", vfs_path], check=True)
|
subprocess.run(["podman", "unshare", "rm", "-rf", vfs_path], check=True)
|
||||||
|
|
||||||
|
|
||||||
def test_create_args_volume(tmp_path: Path, request):
|
def test_create_args_volume(tmp_path: Path, container_engine):
|
||||||
if not request.config.getoption("--run-docker"):
|
if container_engine.name != "docker":
|
||||||
pytest.skip("need --run-docker option to run")
|
pytest.skip("only runs with docker")
|
||||||
|
|
||||||
if "CIRCLECI" in os.environ or "GITLAB_CI" in os.environ:
|
if "CIRCLECI" in os.environ or "GITLAB_CI" in os.environ:
|
||||||
pytest.skip(
|
pytest.skip(
|
||||||
|
|||||||
Reference in New Issue
Block a user