From b69995c292a993e4daf1abca256740521589b5da Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Thu, 5 Dec 2024 11:51:31 +0100 Subject: [PATCH] test: add integration test for the new `swap` customization This commit adds an integration test for the new swap disk customization that got added to the `images` library in PR https://github.com/osbuild/images/pull/1072 Also extends the LVM test to include swap on lvm. --- test/test_build.py | 7 ++++ test/test_manifest.py | 92 +++++++++++++++++++++++++++++++++++++++++++ test/testcases.py | 15 +++---- test/testutil.py | 13 ++++++ 4 files changed, 118 insertions(+), 9 deletions(-) diff --git a/test/test_build.py b/test/test_build.py index 7c8ffd13..02e9da0f 100644 --- a/test/test_build.py +++ b/test/test_build.py @@ -694,12 +694,17 @@ def assert_disk_customizations(image_type, test_vm): keyfile=image_type.ssh_keyfile_private_path) assert exit_status == 0 findmnt = json.loads(output) + exit_status, swapon_output = test_vm.run("swapon --show", user="root", + keyfile=image_type.ssh_keyfile_private_path) + assert exit_status == 0 if dc := image_type.disk_config: if dc == "lvm": mnts = [mnt for mnt in findmnt["filesystems"][0]["children"] if mnt["target"] == "/sysroot"] assert len(mnts) == 1 assert "/dev/mapper/vg00-rootlv" == mnts[0]["source"] + # check swap too + assert "7G" in swapon_output elif dc == "btrfs": mnts = [mnt for mnt in findmnt["filesystems"][0]["children"] if mnt["target"] == "/sysroot"] @@ -707,3 +712,5 @@ def assert_disk_customizations(image_type, test_vm): assert "btrfs" == mnts[0]["fstype"] # ensure sysroot comes from the "root" subvolume assert mnts[0]["source"].endswith("[/root]") + elif dc == "swap": + assert "123M" in swapon_output diff --git a/test/test_manifest.py b/test/test_manifest.py index c869bdb4..b64cf5e8 100644 --- a/test/test_manifest.py +++ b/test/test_manifest.py @@ -633,3 +633,95 @@ def test_manifest_disk_customization_btrfs(tmp_path, build_container): st = find_bootc_install_to_fs_stage_from(output) assert st["mounts"][0]["type"] == "org.osbuild.btrfs" assert st["mounts"][0]["target"] == "/" + + +def find_mkswap_stage_from(manifest_str): + manifest = json.loads(manifest_str) + for pipeline in manifest["pipelines"]: + if pipeline["name"] == "image": + for st in pipeline["stages"]: + if st["type"] == "org.osbuild.mkswap": + return st + raise ValueError(f"cannot find mkswap stage in manifest:\n{manifest_str}") + + +def test_manifest_disk_customization_swap(tmp_path, build_container): + container_ref = "quay.io/centos-bootc/centos-bootc:stream9" + + config = { + "customizations": { + "disk": { + "partitions": [ + { + "minsize": "2 GiB", + "fs_type": "swap", + } + ] + } + } + } + config_path = tmp_path / "config.json" + with config_path.open("w") as config_file: + json.dump(config, config_file) + + output = subprocess.check_output([ + *testutil.podman_run_common, + "-v", f"{config_path}:/config.json:ro", + build_container, + "manifest", f"{container_ref}", + ]) + mkswap_stage = find_mkswap_stage_from(output) + assert mkswap_stage["options"].get("uuid") + swap_uuid = mkswap_stage["options"]["uuid"] + fstab_stage = find_fstab_stage_from(output) + filesystems = fstab_stage["options"]["filesystems"] + assert { + 'uuid': swap_uuid, + "vfs_type": "swap", + "path": "none", + "options": "defaults", + } in filesystems + + +def test_manifest_disk_customization_lvm_swap(tmp_path, build_container): + container_ref = "quay.io/centos-bootc/centos-bootc:stream9" + + config = { + "customizations": { + "disk": { + "partitions": [ + { + "type": "lvm", + "minsize": "10 GiB", + "logical_volumes": [ + { + "minsize": "2 GiB", + "fs_type": "swap", + } + ] + } + ] + } + } + } + config_path = tmp_path / "config.json" + with config_path.open("w") as config_file: + json.dump(config, config_file) + + output = subprocess.check_output([ + *testutil.podman_run_common, + "-v", f"{config_path}:/config.json:ro", + build_container, + "manifest", f"{container_ref}", + ]) + mkswap_stage = find_mkswap_stage_from(output) + assert mkswap_stage["options"].get("uuid") + swap_uuid = mkswap_stage["options"]["uuid"] + fstab_stage = find_fstab_stage_from(output) + filesystems = fstab_stage["options"]["filesystems"] + assert { + 'uuid': swap_uuid, + "vfs_type": "swap", + "path": "none", + "options": "defaults", + } in filesystems diff --git a/test/testcases.py b/test/testcases.py index ed0ee45e..ea10622f 100644 --- a/test/testcases.py +++ b/test/testcases.py @@ -84,17 +84,14 @@ def gen_testcases(what): # pylint: disable=too-many-return-statements TestCaseCentos(image="anaconda-iso"), ] if what == "qemu-boot": - # test partition defaults with qcow2 test_cases = [ - klass(image="qcow2") - for klass in (TestCaseCentos, TestCaseFedora) + # test default partitioning + TestCaseFedora(image="qcow2"), + # test with custom disk configs + TestCaseCentos(image="qcow2", disk_config="swap"), + TestCaseFedora(image="raw", disk_config="btrfs"), + TestCaseCentos(image="raw", disk_config="lvm"), ] - # and custom with raw (this is arbitrary, we could do it the - # other way around too - test_cases.append( - TestCaseCentos(image="raw", disk_config="lvm")) - test_cases.append( - TestCaseFedora(image="raw", disk_config="btrfs")) # do a cross arch test too if platform.machine() == "x86_64": # TODO: re-enable once diff --git a/test/testutil.py b/test/testutil.py index 8ee8682d..aa69c35d 100644 --- a/test/testutil.py +++ b/test/testutil.py @@ -159,6 +159,10 @@ def maybe_create_disk_customizations(cfg, tc): "minsize": "10 GiB", "fs_type": "xfs", "mountpoint": "/", + }, + { + "minsize": "7 GiB", + "fs_type": "swap", } ] } @@ -179,6 +183,15 @@ def maybe_create_disk_customizations(cfg, tc): } ] } + elif tc.disk_config == "swap": + cfg["customizations"]["disk"] = { + "partitions": [ + { + "minsize": "123 MiB", + "fs_type": "swap", + } + ] + } else: raise ValueError(f"unsupported disk_config {tc.disk_config}")