|
| 1 | +import os |
| 2 | +import platform |
| 3 | +import random |
| 4 | +import string |
| 5 | +import subprocess |
| 6 | +import textwrap |
| 7 | +from contextlib import contextmanager |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | + |
| 12 | +@contextmanager |
| 13 | +def make_container(container_path, arch=None): |
| 14 | + # BIB only supports container tags, not hashes |
| 15 | + container_tag = "bib-test-" + "".join(random.choices(string.digits, k=12)) |
| 16 | + |
| 17 | + if not arch: |
| 18 | + # Always provide an architecture here because without that the default |
| 19 | + # behavior is to pull whatever arch was pulled for this image ref |
| 20 | + # last but we want "native" if nothing else is specified. |
| 21 | + # |
| 22 | + # Note: podman seems to translate kernel arch to go arches |
| 23 | + # automatically it seems. |
| 24 | + arch = platform.uname().machine |
| 25 | + |
| 26 | + subprocess.check_call([ |
| 27 | + "podman", "build", |
| 28 | + "--cache-ttl=1h", |
| 29 | + "-t", container_tag, |
| 30 | + "--arch", arch, |
| 31 | + container_path], encoding="utf8") |
| 32 | + yield container_tag |
| 33 | + subprocess.check_call(["podman", "rmi", container_tag]) |
| 34 | + |
| 35 | + |
| 36 | +@pytest.fixture(name="build_container", scope="session") |
| 37 | +def build_container_fixture(): |
| 38 | + """Build a container from the Containerfile and returns the name""" |
| 39 | + if tag_from_env := os.getenv("BIB_TEST_BUILD_CONTAINER_TAG"): |
| 40 | + return tag_from_env |
| 41 | + |
| 42 | + container_tag = "bootc-image-builder-test" |
| 43 | + subprocess.check_call([ |
| 44 | + "podman", "build", |
| 45 | + "--cache-ttl=1h", |
| 46 | + "-f", "Containerfile", |
| 47 | + "-t", container_tag, |
| 48 | + ]) |
| 49 | + return container_tag |
| 50 | + |
| 51 | + |
| 52 | +@pytest.fixture(name="build_fake_container", scope="session") |
| 53 | +def build_fake_container_fixture(tmpdir_factory, build_container): |
| 54 | + """Build a container with a fake osbuild and returns the name""" |
| 55 | + tmp_path = tmpdir_factory.mktemp("build-fake-container") |
| 56 | + |
| 57 | + # see https://github.com/osbuild/osbuild/blob/main/osbuild/testutil/__init__.py#L91 |
| 58 | + tracing_podman_path = tmp_path / "tracing-podman" |
| 59 | + tracing_podman_path.write_text(textwrap.dedent("""\ |
| 60 | + #!/bin/sh -e |
| 61 | +
|
| 62 | + TRACE_PATH=/output/"$(basename $0)".log |
| 63 | + for arg in "$@"; do |
| 64 | + echo "$arg" >> "$TRACE_PATH" |
| 65 | + done |
| 66 | + # extra separator to differenciate between calls |
| 67 | + echo >> "$TRACE_PATH" |
| 68 | + exec "$0".real "$@" |
| 69 | + """), encoding="utf8") |
| 70 | + |
| 71 | + fake_osbuild_path = tmp_path / "fake-osbuild" |
| 72 | + fake_osbuild_path.write_text(textwrap.dedent("""\ |
| 73 | + #!/bin/bash -e |
| 74 | +
|
| 75 | + # injest generated manifest from the images library, if we do not |
| 76 | + # do this images may fail with "broken" pipe errors |
| 77 | + cat - >/dev/null |
| 78 | +
|
| 79 | + mkdir -p /output/qcow2 |
| 80 | + echo "fake-disk.qcow2" > /output/qcow2/disk.qcow2 |
| 81 | +
|
| 82 | + """), encoding="utf8") |
| 83 | + |
| 84 | + cntf_path = tmp_path / "Containerfile" |
| 85 | + |
| 86 | + cntf_path.write_text(textwrap.dedent(f"""\n |
| 87 | + FROM {build_container} |
| 88 | + COPY fake-osbuild /usr/bin/osbuild |
| 89 | + RUN chmod 755 /usr/bin/osbuild |
| 90 | + COPY --from={build_container} /usr/bin/podman /usr/bin/podman.real |
| 91 | + COPY tracing-podman /usr/bin/podman |
| 92 | + RUN chmod 755 /usr/bin/podman |
| 93 | + """), encoding="utf8") |
| 94 | + |
| 95 | + container_tag = "bootc-image-builder-test-faked-osbuild" |
| 96 | + subprocess.check_call([ |
| 97 | + "podman", "build", |
| 98 | + "-t", container_tag, |
| 99 | + tmp_path, |
| 100 | + ]) |
| 101 | + return container_tag |
| 102 | + |
| 103 | + |
| 104 | +@pytest.fixture(name="build_erroring_container", scope="session") |
| 105 | +def build_erroring_container_fixture(tmpdir_factory, build_container): |
| 106 | + """Build a container with a erroring osbuild and returns the name""" |
| 107 | + tmp_path = tmpdir_factory.mktemp("build-fake-container") |
| 108 | + |
| 109 | + # this ensures there are messages from osbuild itself that |
| 110 | + # we can reliably test for |
| 111 | + wrapping_osbuild_path = tmp_path / "wrapping-osbuild" |
| 112 | + wrapping_osbuild_path.write_text(textwrap.dedent("""\ |
| 113 | + #!/bin/sh -e |
| 114 | + echo "output-from-osbuild-stdout" |
| 115 | + >&2 echo "output-from-osbuild-stderr" |
| 116 | +
|
| 117 | + exec /usr/bin/osbuild.real "$@" |
| 118 | + """), encoding="utf8") |
| 119 | + |
| 120 | + # this ensures we have a failing stage and failure messages |
| 121 | + bad_stage_path = tmp_path / "bad-stage" |
| 122 | + bad_stage_path.write_text(textwrap.dedent("""\ |
| 123 | + #!/bin/sh -e |
| 124 | + echo osbuild-stage-stdout-output |
| 125 | + >&2 echo osbuild-stage-stderr-output |
| 126 | + exit 112 |
| 127 | + """), encoding="utf8") |
| 128 | + |
| 129 | + cntf_path = tmp_path / "Containerfile" |
| 130 | + cntf_path.write_text(textwrap.dedent(f"""\n |
| 131 | + FROM {build_container} |
| 132 | + # ensure there is osbuild output |
| 133 | + COPY --from={build_container} /usr/bin/osbuild /usr/bin/osbuild.real |
| 134 | + COPY wrapping-osbuild /usr/bin/osbuild |
| 135 | + RUN chmod 755 /usr/bin/osbuild |
| 136 | +
|
| 137 | + # we break org.osbuild.selinux as runs early and is used everywhere |
| 138 | + COPY bad-stage /usr/lib/osbuild/stages/org.osbuild.selinux |
| 139 | + RUN chmod +x /usr/lib/osbuild/stages/org.osbuild.selinux |
| 140 | + """), encoding="utf8") |
| 141 | + |
| 142 | + container_tag = "bootc-image-builder-test--osbuild" |
| 143 | + subprocess.check_call([ |
| 144 | + "podman", "build", |
| 145 | + "-t", container_tag, |
| 146 | + tmp_path, |
| 147 | + ]) |
| 148 | + return container_tag |
0 commit comments