|
| 1 | +import pytest |
| 2 | +import shutil |
| 3 | +import subprocess |
| 4 | +import testinfra |
| 5 | +import time |
| 6 | +from rich.console import Console |
| 7 | + |
| 8 | +console = Console() |
| 9 | + |
| 10 | + |
| 11 | +def pytest_addoption(parser): |
| 12 | + parser.addoption( |
| 13 | + "--image-name", |
| 14 | + action="store", |
| 15 | + help="Docker image and tag to use for testing", |
| 16 | + ) |
| 17 | + parser.addoption( |
| 18 | + "--image-path", |
| 19 | + action="store", |
| 20 | + help="Compressed Docker image to load for testing", |
| 21 | + ) |
| 22 | + |
| 23 | + parser.addoption( |
| 24 | + "--force-docker", |
| 25 | + action="store_true", |
| 26 | + help="Force using Docker instead of Podman for testing", |
| 27 | + ) |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture(scope="session") |
| 31 | +def host(request): |
| 32 | + force_docker = request.config.getoption("--force-docker") |
| 33 | + image_name = request.config.getoption("--image-name") |
| 34 | + image_path = request.config.getoption("--image-path") |
| 35 | + if not force_docker and shutil.which("podman"): |
| 36 | + console.log("Using Podman for testing") |
| 37 | + with console.status("Loading image..."): |
| 38 | + subprocess.check_output(["podman", "load", "-q", "-i", image_path]) |
| 39 | + podman_id = ( |
| 40 | + subprocess.check_output( |
| 41 | + [ |
| 42 | + "podman", |
| 43 | + "run", |
| 44 | + "--cap-add", |
| 45 | + "SYS_ADMIN", |
| 46 | + "-d", |
| 47 | + image_name, |
| 48 | + ] |
| 49 | + ) |
| 50 | + .decode() |
| 51 | + .strip() |
| 52 | + ) |
| 53 | + yield testinfra.get_host("podman://" + podman_id) |
| 54 | + with console.status("Cleaning up..."): |
| 55 | + subprocess.check_call( |
| 56 | + ["podman", "rm", "-f", podman_id], stdout=subprocess.DEVNULL |
| 57 | + ) |
| 58 | + else: |
| 59 | + console.log("Using Docker for testing") |
| 60 | + with console.status("Loading image..."): |
| 61 | + subprocess.check_output(["docker", "load", "-q", "-i", image_path]) |
| 62 | + docker_id = ( |
| 63 | + subprocess.check_output( |
| 64 | + [ |
| 65 | + "docker", |
| 66 | + "run", |
| 67 | + "--privileged", |
| 68 | + "--cap-add", |
| 69 | + "SYS_ADMIN", |
| 70 | + "--security-opt", |
| 71 | + "seccomp=unconfined", |
| 72 | + "--cgroup-parent=docker.slice", |
| 73 | + "--cgroupns", |
| 74 | + "private", |
| 75 | + "-d", |
| 76 | + image_name, |
| 77 | + ] |
| 78 | + ) |
| 79 | + .decode() |
| 80 | + .strip() |
| 81 | + ) |
| 82 | + yield testinfra.get_host("docker://" + docker_id) |
| 83 | + with console.status("Cleaning up..."): |
| 84 | + subprocess.check_call( |
| 85 | + ["docker", "rm", "-f", docker_id], stdout=subprocess.DEVNULL |
| 86 | + ) |
| 87 | + |
| 88 | + |
| 89 | +def wait_for_target(host, target, timeout=60): |
| 90 | + start_time = time.time() |
| 91 | + while time.time() - start_time < timeout: |
| 92 | + result = host.run(f"systemctl is-active {target}") |
| 93 | + if result.rc == 0: |
| 94 | + return True |
| 95 | + time.sleep(1) |
| 96 | + return False |
| 97 | + |
| 98 | + |
| 99 | +@pytest.fixture(scope="session", autouse=True) |
| 100 | +def activate_system_manager(host): |
| 101 | + with console.status("Waiting systemd to be ready..."): |
| 102 | + assert wait_for_target(host, "multi-user.target") |
| 103 | + result = host.run("activate") |
| 104 | + console.log(result.stdout) |
| 105 | + console.log(result.stderr) |
| 106 | + if result.failed: |
| 107 | + raise pytest.fail( |
| 108 | + "System manager activation failed with return code {}".format(result.rc) |
| 109 | + ) |
0 commit comments