From 28d4a57b3aa668046773a41bd01860e9a0aec833 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Wed, 18 Sep 2024 17:21:05 +0200 Subject: [PATCH] add boot tests for iso image --- flake.nix | 10 +++- nix/image-installer/tests.nix | 105 ++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 nix/image-installer/tests.nix diff --git a/flake.nix b/flake.nix index ab15a11..26f729f 100644 --- a/flake.nix +++ b/flake.nix @@ -64,6 +64,13 @@ kexec-installer-unstable = pkgs.callPackage ./nix/kexec-installer/test.nix { kexecTarball = self.packages.x86_64-linux.kexec-installer-nixos-unstable-noninteractive; }; + inherit (pkgs.callPackages ./nix/image-installer/tests.nix { + nixpkgs = nixos-unstable; + nixosModules = self.nixosModules; + }) uefi-cdrom uefi-usb bios-cdrom bios-usb; + kexec-installer-stable = nixos-stable.legacyPackages.x86_64-linux.callPackage ./nix/kexec-installer/test.nix { + kexecTarball = self.packages.x86_64-linux.kexec-installer-nixos-stable-noninteractive; + }; shellcheck = pkgs.runCommand "shellcheck" { nativeBuildInputs = [ pkgs.shellcheck ]; @@ -71,9 +78,6 @@ shellcheck ${(pkgs.nixos [self.nixosModules.kexec-installer]).config.system.build.kexecRun} touch $out ''; - kexec-installer-stable = nixos-stable.legacyPackages.x86_64-linux.callPackage ./nix/kexec-installer/test.nix { - kexecTarball = self.packages.x86_64-linux.kexec-installer-nixos-stable-noninteractive; - }; }; in nixos-unstable.lib.recursiveUpdate packages { x86_64-linux = checks; }; diff --git a/nix/image-installer/tests.nix b/nix/image-installer/tests.nix new file mode 100644 index 0000000..031c3e1 --- /dev/null +++ b/nix/image-installer/tests.nix @@ -0,0 +1,105 @@ +{ + pkgs, + lib, + nixpkgs, + nixos, + nixosModules, +}: + +let + testConfig = ( + nixos [ + ( + { modulesPath, ... }: + { + imports = [ + nixosModules.image-installer + "${modulesPath}/testing/test-instrumentation.nix" + ]; + } + ) + ] + ); + iso = testConfig.config.system.build.isoImage; + mkStartCommand = + { + memory ? 2048, + cdrom ? null, + usb ? null, + uefi ? false, + extraFlags ? [ ], + }: + let + qemu-common = import (nixpkgs + "/nixos/lib/qemu-common.nix") { inherit lib pkgs; }; + qemu = qemu-common.qemuBinary pkgs.qemu_test; + + flags = + [ + "-m" + (toString memory) + "-netdev" + "user,id=net0" + "-device" + "virtio-net-pci,netdev=net0" + ] + ++ lib.optionals (cdrom != null) [ + "-cdrom" + cdrom + ] + ++ lib.optionals (usb != null) [ + "-device" + "usb-ehci" + "-drive" + "id=usbdisk,file=${usb},if=none,readonly" + "-device" + "usb-storage,drive=usbdisk" + ] + ++ lib.optionals uefi [ + "-drive" + "if=pflash,format=raw,unit=0,readonly=on,file=${pkgs.OVMF.firmware}" + "-drive" + "if=pflash,format=raw,unit=1,readonly=on,file=${pkgs.OVMF.variables}" + ] + ++ extraFlags; + + flagsStr = lib.concatStringsSep " " flags; + in + "${qemu} ${flagsStr}"; + + makeBootTest = + name: config: + let + startCommand = mkStartCommand config; + in + pkgs.testers.runNixOSTest { + name = "boot-${name}"; + nodes = { }; + testScript = '' + machine = create_machine("${startCommand}") + machine.start() + machine.wait_for_unit("multi-user.target") + machine.succeed("nix store verify --no-trust -r --option experimental-features nix-command /run/current-system") + + machine.shutdown() + ''; + }; +in +{ + uefi-cdrom = makeBootTest "uefi-cdrom" { + uefi = true; + cdrom = "${iso}/iso/nixos-installer-${pkgs.hostPlatform.system}.iso"; + }; + + uefi-usb = makeBootTest "uefi-usb" { + uefi = true; + usb = "${iso}/iso/nixos-installer-${pkgs.hostPlatform.system}.iso"; + }; + + bios-cdrom = makeBootTest "bios-cdrom" { + cdrom = "${iso}/iso/nixos-installer-${pkgs.hostPlatform.system}.iso"; + }; + + bios-usb = makeBootTest "bios-usb" { + usb = "${iso}/iso/nixos-installer-${pkgs.hostPlatform.system}.iso"; + }; +}