Skip to content

Commit

Permalink
test: fix the testcase strings to not include osinfo_template
Browse files Browse the repository at this point in the history
This commit fixes the testcase string representation to not include
the `osinfo_template` string and adds a test case to not accidentally
regress here. Currently our test case strings look like:
```
<Dir bootc-image-builder>
  <Dir test>
    <Module test_build.py>
      <Function test_container_builds>
      <Function test_image_is_generated[quay.io/centos-bootc/centos-bootc:stream9,qcow2+raw+vmdk+vhd+gce,CentOS Stream 9 ({arch})]>
      <Function test_image_boots[quay.io/centos-bootc/centos-bootc:stream9,raw,CentOS Stream 9 ({arch})]>
...
```
which is an accident. A testcase should ideally only contain what

Instead of putting the osinfo_template into the testcase itself,
have a function that generates it. It has downsides (now the
osinfo_template is more disconnected from the testcase) so we could
move it also back into testcase or we could exclude osinfo_template
from the string generation. Ideas welcome here, my current approach
feels okay but not like it's perfect yet.
  • Loading branch information
mvo5 committed Nov 7, 2024
1 parent 858734f commit 5ded3cb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
17 changes: 12 additions & 5 deletions test/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class ImageBuildResult(NamedTuple):
img_type: str
img_path: str
img_arch: str
osinfo_template: str
container_ref: str
rootfs: str
username: str
Expand Down Expand Up @@ -327,7 +326,7 @@ def build_images(shared_tmpdir, build_container, request, force_aws_upload, gpg_
journal_output = journal_log_path.read_text(encoding="utf8")
bib_output = bib_output_path.read_text(encoding="utf8")
results.append(ImageBuildResult(
image_type, generated_img, tc.target_arch, tc.osinfo_template,
image_type, generated_img, tc.target_arch,
container_ref, tc.rootfs, username, password,
ssh_keyfile_private_path, kargs, bib_output, journal_output))

Expand Down Expand Up @@ -473,7 +472,7 @@ def del_ami():
results = []
for image_type in image_types:
results.append(ImageBuildResult(
image_type, artifact[image_type], tc.target_arch, tc.osinfo_template,
image_type, artifact[image_type], tc.target_arch,
container_ref, tc.rootfs, username, password,
ssh_keyfile_private_path, kargs, bib_output, journal_output, metadata))
yield results
Expand Down Expand Up @@ -628,20 +627,28 @@ def test_iso_installs(image_type):
assert_kernel_args(vm, image_type)


def osinfo_for(it: ImageBuildResult, arch: str) -> str:
if it.container_ref == "quay.io/centos-bootc/centos-bootc:stream9":
return f"CentOS Stream 9 ({arch})"
if it.container_ref.startswith("quay.io/fedora/fedora-bootc:"):
ver = it.container_ref.split(":", maxsplit=2)[1]
return f"Fedora Server {ver} ({arch})"
raise ValueError(f"cannot find osinfo_template for '{it.container_ref}'")


@pytest.mark.skipif(platform.system() != "Linux", reason="osinfo detect test only runs on linux right now")
@pytest.mark.parametrize("image_type", gen_testcases("anaconda-iso"), indirect=["image_type"])
def test_iso_os_detection(image_type):
installer_iso_path = image_type.img_path
arch = image_type.img_arch
if not arch:
arch = platform.machine()
osinfo = image_type.osinfo_template.format(arch=arch)
result = subprocess.run([
"osinfo-detect",
installer_iso_path,
], capture_output=True, text=True, check=True)
osinfo_output = result.stdout
expected_output = f"Media is bootable.\nMedia is an installer for OS '{osinfo}'\n"
expected_output = f"Media is bootable.\nMedia is an installer for OS '{osinfo_for(image_type, arch)}'\n"
assert osinfo_output == expected_output


Expand Down
13 changes: 10 additions & 3 deletions test/testcases.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,29 @@ def __str__(self):
class TestCaseFedora(TestCase):
container_ref: str = "quay.io/fedora/fedora-bootc:40"
rootfs: str = "btrfs"
osinfo_template: str = "Fedora Server 40 ({arch})"


@dataclasses.dataclass
class TestCaseFedora42(TestCase):
container_ref: str = "quay.io/fedora/fedora-bootc:42"
rootfs: str = "btrfs"
osinfo_template: str = "Fedora Server 42 ({arch})"


@dataclasses.dataclass
class TestCaseCentos(TestCase):
container_ref: str = os.getenv(
"BIB_TEST_BOOTC_CONTAINER_TAG",
"quay.io/centos-bootc/centos-bootc:stream9")
osinfo_template: str = "CentOS Stream 9 ({arch})"


def test_testcase_nameing():
"""
Ensure the testcase naming does not change without us knowing as those
are visible when running "pytest --collect-only"
"""
tc = TestCaseFedora()
expected = "container_ref=quay.io/fedora/fedora-bootc:40,rootfs=btrfs"
assert f"{tc}" == expected, f"{tc} != {expected}"


def gen_testcases(what): # pylint: disable=too-many-return-statements
Expand Down

0 comments on commit 5ded3cb

Please sign in to comment.