From 2c3f528488cef38a192d273bf76b93f8f0dc0f46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Hozza?= Date: Tue, 31 Dec 2024 06:50:39 +0100 Subject: [PATCH] Test: add unit test for osbuild-image-info's read_boot_entries() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add unit test for osbuild-image-info's `read_boot_entries()` function, to ensure that it can handle various situations that can happen in the real world. Signed-off-by: Tomáš Hozza --- tools/test/test_osbuild_image_info.py | 137 ++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 tools/test/test_osbuild_image_info.py diff --git a/tools/test/test_osbuild_image_info.py b/tools/test/test_osbuild_image_info.py new file mode 100644 index 00000000..1386217a --- /dev/null +++ b/tools/test/test_osbuild_image_info.py @@ -0,0 +1,137 @@ +import pytest + +from osbuild.testutil import make_fake_tree +from osbuild.testutil.imports import import_module_from_path + +osbuild_image_info = import_module_from_path("osbuild_image_info", "tools/osbuild-image-info") + + +@pytest.mark.parametrize("fake_tree,entries", ( + # no entries + ({}, []), + # one entry + ( + { + "/boot/loader/entries/0649288e52434223afde4c36460a375e-6.11.9-100.fc39.x86_64.conf": """title Fedora Linux (6.11.9-100.fc39.x86_64) 39 (Thirty Nine) +version 6.11.9-100.fc39.x86_64 +linux /boot/vmlinuz-6.11.9-100.fc39.x86_64 +initrd /boot/initramfs-6.11.9-100.fc39.x86_64.img +options root=UUID=a7e970a5-14fb-4a8a-ab09-603d1ac3fee9 ro crashkernel=auto net.ifnames=0 rhgb console=tty0 console=ttyS0,115200n8 +grub_users $grub_users +grub_arg --unrestricted +grub_class fedora""", + }, + [ + { + "title": "Fedora Linux (6.11.9-100.fc39.x86_64) 39 (Thirty Nine)", + "version": "6.11.9-100.fc39.x86_64", + "linux": "/boot/vmlinuz-6.11.9-100.fc39.x86_64", + "initrd": "/boot/initramfs-6.11.9-100.fc39.x86_64.img", + "options": "root=UUID=a7e970a5-14fb-4a8a-ab09-603d1ac3fee9 ro crashkernel=auto net.ifnames=0 rhgb console=tty0 console=ttyS0,115200n8", + "grub_users": "$grub_users", + "grub_arg": "--unrestricted", + "grub_class": "fedora", + }, + ] + ), + # two entries + ( + { + "/boot/loader/entries/0649288e52434223afde4c36460a375e-6.11.9-100.fc39.x86_64.conf": """title Fedora Linux (6.11.9-100.fc39.x86_64) 39 (Thirty Nine) +version 6.11.9-100.fc39.x86_64 +linux /boot/vmlinuz-6.11.9-100.fc39.x86_64 +initrd /boot/initramfs-6.11.9-100.fc39.x86_64.img +options root=UUID=a7e970a5-14fb-4a8a-ab09-603d1ac3fee9 ro crashkernel=auto net.ifnames=0 rhgb console=tty0 console=ttyS0,115200n8 +grub_users $grub_users +grub_arg --unrestricted +grub_class fedora""", + "/boot/loader/entries/0649288e52434223afde4c36460a375e-6.11.9-101.fc39.x86_64.conf": """title Fedora Linux (6.11.9-101.fc39.x86_64) 39 (Thirty Nine) +version 6.11.9-101.fc39.x86_64 +linux /boot/vmlinuz-6.11.9-101.fc39.x86_64 +initrd /boot/initramfs-6.11.9-101.fc39.x86_64.img +options root=UUID=a7e970a5-14fb-4a8a-ab09-603d1ac3fee9 ro crashkernel=auto net.ifnames=0 rhgb console=tty0 console=ttyS0,115200n8 +grub_users $grub_users +grub_arg --unrestricted +grub_class fedora""", + }, + [ + { + "title": "Fedora Linux (6.11.9-100.fc39.x86_64) 39 (Thirty Nine)", + "version": "6.11.9-100.fc39.x86_64", + "linux": "/boot/vmlinuz-6.11.9-100.fc39.x86_64", + "initrd": "/boot/initramfs-6.11.9-100.fc39.x86_64.img", + "options": "root=UUID=a7e970a5-14fb-4a8a-ab09-603d1ac3fee9 ro crashkernel=auto net.ifnames=0 rhgb console=tty0 console=ttyS0,115200n8", + "grub_users": "$grub_users", + "grub_arg": "--unrestricted", + "grub_class": "fedora", + }, + { + "title": "Fedora Linux (6.11.9-101.fc39.x86_64) 39 (Thirty Nine)", + "version": "6.11.9-101.fc39.x86_64", + "linux": "/boot/vmlinuz-6.11.9-101.fc39.x86_64", + "initrd": "/boot/initramfs-6.11.9-101.fc39.x86_64.img", + "options": "root=UUID=a7e970a5-14fb-4a8a-ab09-603d1ac3fee9 ro crashkernel=auto net.ifnames=0 rhgb console=tty0 console=ttyS0,115200n8", + "grub_users": "$grub_users", + "grub_arg": "--unrestricted", + "grub_class": "fedora", + }, + ] + ), + # one entry with extra newlines + ( + { + "/boot/loader/entries/0649288e52434223afde4c36460a375e-6.11.9-100.fc39.x86_64.conf": """title Fedora Linux (6.11.9-100.fc39.x86_64) 39 (Thirty Nine) +version 6.11.9-100.fc39.x86_64 +linux /boot/vmlinuz-6.11.9-100.fc39.x86_64 +initrd /boot/initramfs-6.11.9-100.fc39.x86_64.img +options root=UUID=a7e970a5-14fb-4a8a-ab09-603d1ac3fee9 ro crashkernel=auto net.ifnames=0 rhgb console=tty0 console=ttyS0,115200n8 +grub_users $grub_users +grub_arg --unrestricted +grub_class fedora + +""", + }, + [ + { + "title": "Fedora Linux (6.11.9-100.fc39.x86_64) 39 (Thirty Nine)", + "version": "6.11.9-100.fc39.x86_64", + "linux": "/boot/vmlinuz-6.11.9-100.fc39.x86_64", + "initrd": "/boot/initramfs-6.11.9-100.fc39.x86_64.img", + "options": "root=UUID=a7e970a5-14fb-4a8a-ab09-603d1ac3fee9 ro crashkernel=auto net.ifnames=0 rhgb console=tty0 console=ttyS0,115200n8", + "grub_users": "$grub_users", + "grub_arg": "--unrestricted", + "grub_class": "fedora", + }, + ] + ), + # one entry with comments + ( + { + "/boot/loader/entries/0649288e52434223afde4c36460a375e-6.11.9-100.fc39.x86_64.conf": """title Fedora Linux (6.11.9-100.fc39.x86_64) 39 (Thirty Nine) +# this is a very useful comment +version 6.11.9-100.fc39.x86_64 +linux /boot/vmlinuz-6.11.9-100.fc39.x86_64 +initrd /boot/initramfs-6.11.9-100.fc39.x86_64.img +options root=UUID=a7e970a5-14fb-4a8a-ab09-603d1ac3fee9 ro crashkernel=auto net.ifnames=0 rhgb console=tty0 console=ttyS0,115200n8 +# this is another very useful comment +grub_users $grub_users +grub_arg --unrestricted +grub_class fedora""", + }, + [ + { + "title": "Fedora Linux (6.11.9-100.fc39.x86_64) 39 (Thirty Nine)", + "version": "6.11.9-100.fc39.x86_64", + "linux": "/boot/vmlinuz-6.11.9-100.fc39.x86_64", + "initrd": "/boot/initramfs-6.11.9-100.fc39.x86_64.img", + "options": "root=UUID=a7e970a5-14fb-4a8a-ab09-603d1ac3fee9 ro crashkernel=auto net.ifnames=0 rhgb console=tty0 console=ttyS0,115200n8", + "grub_users": "$grub_users", + "grub_arg": "--unrestricted", + "grub_class": "fedora", + }, + ] + ), +)) +def test_read_boot_entries(tmp_path, fake_tree, entries): + make_fake_tree(tmp_path, fake_tree) + assert osbuild_image_info.read_boot_entries(tmp_path / "boot") == entries