From 7ebd2400d65e5bfeea5c95e18c10aa7a4f362f07 Mon Sep 17 00:00:00 2001 From: Pascal Scholz Date: Tue, 21 Apr 2026 16:47:57 +0200 Subject: [PATCH 1/3] test-helper: Remove unused export We use `reset_system_image` nowhere, thus we remove it completely. Note: We'll add a similar function in a follow up commit, but decided to remove the function in this commit to reach a consistent state. Signed-off-by: Pascal Scholz On-behalf-of: SAP pascal.scholz@sap.com --- test_helper/test_helper/__init__.py | 2 -- test_helper/test_helper/test_helper.py | 14 -------------- 2 files changed, 16 deletions(-) diff --git a/test_helper/test_helper/__init__.py b/test_helper/test_helper/__init__.py index 5792ad3..f779f55 100644 --- a/test_helper/test_helper/__init__.py +++ b/test_helper/test_helper/__init__.py @@ -18,7 +18,6 @@ number_of_storage_devices, parse_devices_from_dom_def, pci_devices_by_bdf, - reset_system_image, setup_nested_cirros, setupTestComputeVM, setupTestControllerVM, @@ -64,7 +63,6 @@ "number_of_storage_devices", "parse_devices_from_dom_def", "pci_devices_by_bdf", - "reset_system_image", "setup_nested_cirros", "setupTestComputeVM", "setupTestControllerVM", diff --git a/test_helper/test_helper/test_helper.py b/test_helper/test_helper/test_helper.py index ca674e9..fafebb7 100644 --- a/test_helper/test_helper/test_helper.py +++ b/test_helper/test_helper/test_helper.py @@ -695,20 +695,6 @@ def hotplug_fail(machine: Machine, cmd: str) -> None: hotplug(machine, cmd, False) -def reset_system_image(machine: Machine) -> None: - """ - Replaces the (possibly modified) system image with its original - image. - - This helps avoid situations where a VM hangs during boot after the - underlying disk’s BDF was changed, since OVMF may store NVRAM - entries that reference specific BDF values. - """ - machine.succeed( - "rsync -aL --no-perms --inplace --checksum /etc/nixos.img /nfs-root/nixos.img" - ) - - def pci_devices_by_bdf(machine: Machine) -> dict[str, str]: """ Creates a dict of all PCI devices addressable by their BDF in the VM. From b7ff314ef5fcdbb0a71ea4edf2840b3d0d6a5800 Mon Sep 17 00:00:00 2001 From: Pascal Scholz Date: Tue, 21 Apr 2026 16:53:25 +0200 Subject: [PATCH 2/3] test-helper: Refactor system image initialization We perform the same operations for all system images. We therefore introduce a list of image names and create a helper that processes this list instead of repeating code for all images. Signed-off-by: Pascal Scholz On-behalf-of: SAP pascal.scholz@sap.com --- test_helper/test_helper/test_helper.py | 44 +++++++++++++++++++------- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/test_helper/test_helper/test_helper.py b/test_helper/test_helper/test_helper.py index fafebb7..6524403 100644 --- a/test_helper/test_helper/test_helper.py +++ b/test_helper/test_helper/test_helper.py @@ -22,6 +22,22 @@ VIRTCHD_RESTART_TIMEOUT_SEC = 15 CLOUD_HYPERVISOR_EXIT_RETRIES = 50 +# List of system images per VM guest type +SYSTEM_IMAGES = { + "linux": [ + "nixos.img", + "cirros.img", + "ubuntu.raw", + ], + "windows": [ + "windows-server.img", + ], +} + +# Store VM guest type globally as we don't have a chance to do so in our +# class. +TARGET_OS: Literal["linux", "windows"] + class LibvirtTestsBase(unittest.TestCase): """ @@ -118,18 +134,10 @@ def initialControllerVMSetup( ) controllerVM.wait_for_unit("multi-user.target") - if target_os == "windows": - controllerVM.succeed("cp /etc/windows-server.img /nfs-root/windows-server.img") - controllerVM.succeed("chmod 0666 /nfs-root/windows-server.img") - else: - controllerVM.succeed("cp /etc/nixos.img /nfs-root/") - controllerVM.succeed("chmod 0666 /nfs-root/nixos.img") - controllerVM.succeed("cp /etc/cirros.img /nfs-root/") - controllerVM.succeed("chmod 0666 /nfs-root/cirros.img") - controllerVM.succeed("cp /etc/ubuntu.raw /nfs-root/") - controllerVM.succeed("chmod 0666 /nfs-root/ubuntu.raw") - controllerVM.succeed("cp /etc/ubuntu-seed.iso /nfs-root/") - controllerVM.succeed("chmod 0666 /nfs-root/ubuntu-seed.iso") + + global TARGET_OS + TARGET_OS = target_os + copy_system_images(controllerVM) controllerVM.succeed("mkdir -p /var/lib/libvirt/storage-pools/nfs-share") @@ -695,6 +703,18 @@ def hotplug_fail(machine: Machine, cmd: str) -> None: hotplug(machine, cmd, False) +def copy_system_images(machine: Machine) -> None: + """ + Copies all system images to the `machines`'s NFS. + + All system images that are copied are assumed to reside at /etc. A copy for + each system image is created in /nfs-root. + """ + for image in SYSTEM_IMAGES[TARGET_OS]: + machine.succeed(f"cp /etc/{image} /nfs-root/") + machine.succeed(f"chmod 0666 /nfs-root/{image}") + + def pci_devices_by_bdf(machine: Machine) -> dict[str, str]: """ Creates a dict of all PCI devices addressable by their BDF in the VM. From 00dab25267580445e059141d0d7983c45d241ae4 Mon Sep 17 00:00:00 2001 From: Pascal Scholz Date: Tue, 21 Apr 2026 16:55:00 +0200 Subject: [PATCH 3/3] test_helper: Reset all system images Apply the same workaround for all system images that we already apply for `nixos` to make each test start with a fresh image in a known good state. Signed-off-by: Pascal Scholz On-behalf-of: SAP pascal.scholz@sap.com --- test_helper/test_helper/test_helper.py | 32 +++++++++++++++----------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/test_helper/test_helper/test_helper.py b/test_helper/test_helper/test_helper.py index 6524403..3781d25 100644 --- a/test_helper/test_helper/test_helper.py +++ b/test_helper/test_helper/test_helper.py @@ -342,21 +342,10 @@ def teardownTestControllerVM(controllerVM: Machine, test: unittest.TestCase) -> print(f"cmd: {cmd}") controllerVM.succeed(cmd) - # Reset the (possibly modified) system image. This helps avoid - # situations where the image has been modified by a test and thus + # Reset the (possibly modified) system images. This helps avoid + # situations where an image has been modified by a test and thus # doesn't boot in subsequent tests. - controllerVM.succeed( - "rsync -aL --no-perms --inplace --checksum /etc/nixos.img /nfs-root/nixos.img" - ) - - # Currently, we don't store any data about if we copied the windows image to - # the NFS. We therefore have to check if it's there before resetting it - # `test -e` returns 0 if a file exists and 1 otherwise. - test_return_code, _ = controllerVM.execute("test -e /nfs-root/windows-server.img") - if test_return_code == 0: - controllerVM.succeed( - "rsync -aL --no-perms --inplace --checksum /etc/windows-server.img /nfs-root/windows-server.img" - ) + reset_system_images(controllerVM) # Check the sanitizer last, as the assertion can fail. Otherwise, we might # skip some clean up. @@ -715,6 +704,21 @@ def copy_system_images(machine: Machine) -> None: machine.succeed(f"chmod 0666 /nfs-root/{image}") +def reset_system_images(machine: Machine) -> None: + """ + Replaces all (possibly modified) system images with their original + image. + + This helps avoid situations where a VM hangs during boot after the + underlying disk’s BDF was changed, since OVMF may store NVRAM + entries that reference specific BDF values. + """ + for image in SYSTEM_IMAGES[TARGET_OS]: + machine.succeed( + f"rsync -aL --no-perms --inplace --checksum /etc/{image} /nfs-root/{image}" + ) + + def pci_devices_by_bdf(machine: Machine) -> dict[str, str]: """ Creates a dict of all PCI devices addressable by their BDF in the VM.