Skip to content

Commit f3e45d0

Browse files
mvo5achilleas-k
authored andcommitted
test: move bib tests from bootc-image-builder
Move the integration tests from bootc-image-builder into image-builder. With bootc-image-builder being merged into image-builder we still want to run the integration tests for bib because our goal is 100% compatibility. Note that this does not preserve history. There is a PR [0] that does that and while I'm a big fan in general I am not sure its worth the trouble because GH will not allow us to merge the repos and we need to manually force push it. The history of the tests is still fully available in the bootc-image-builder repo in cases it is needed. [0] osbuild#398
1 parent e03bb22 commit f3e45d0

13 files changed

+2905
-0
lines changed

test/bib/conftest.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import pytest
2+
3+
# pylint: disable=wrong-import-order
4+
from testcases import TestCase
5+
from vmtest.util import get_free_port
6+
7+
8+
def pytest_addoption(parser):
9+
parser.addoption("--force-aws-upload", action="store_true", default=False,
10+
help=("Force AWS upload when building AMI, failing if credentials are not set. "
11+
"If not set, the upload will be performed only when credentials are available."))
12+
13+
14+
@pytest.fixture(name="force_aws_upload", scope="session")
15+
def force_aws_upload_fixture(request):
16+
return request.config.getoption("--force-aws-upload")
17+
18+
19+
# see https://hackebrot.github.io/pytest-tricks/param_id_func/ and
20+
# https://docs.pytest.org/en/7.1.x/reference/reference.html#pytest.hookspec.pytest_make_parametrize_id
21+
def pytest_make_parametrize_id(config, val): # pylint: disable=W0613
22+
if isinstance(val, TestCase):
23+
return f"{val}"
24+
return None
25+
26+
27+
@pytest.fixture(name="free_port")
28+
def free_port_fixture():
29+
return get_free_port()

test/bib/containerbuild.py

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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

test/bib/requirements.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pytest==7.4.3
2+
flake8==6.1.0
3+
boto3==1.33.13
4+
qmp==1.1.0
5+
pylint==3.2.5
6+
vmtest @ git+https://github.com/osbuild/images.git

test/bib/test_build_cross.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import platform
2+
3+
import pytest
4+
5+
from testcases import gen_testcases
6+
7+
from test_build_disk import ( # pylint: disable=unused-import
8+
assert_disk_image_boots,
9+
build_container_fixture,
10+
gpg_conf_fixture,
11+
image_type_fixture,
12+
registry_conf_fixture,
13+
shared_tmpdir_fixture,
14+
)
15+
16+
17+
# This testcase is not part of "test_build_disk.py:test_image_boots"
18+
# because it takes ~30min on the GH runners so moving it into a
19+
# separate file ensures it is run in parallel on GH.
20+
@pytest.mark.skipif(platform.system() != "Linux", reason="boot test only runs on linux right now")
21+
@pytest.mark.parametrize("image_type", gen_testcases("qemu-cross"), indirect=["image_type"])
22+
def test_image_boots_cross(image_type):
23+
assert_disk_image_boots(image_type)

0 commit comments

Comments
 (0)