diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index f919e5f5a141..9e80b7e7f8ad 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -463,7 +463,7 @@ in { greetd-no-shadow = handleTest ./greetd-no-shadow.nix {}; grocy = handleTest ./grocy.nix {}; grow-partition = runTest ./grow-partition.nix; - grub = handleTest ./grub.nix {}; + grub = import ./grub.nix { inherit pkgs runTest; }; guacamole-server = handleTest ./guacamole-server.nix {}; guix = handleTest ./guix {}; gvisor = handleTest ./gvisor.nix {}; diff --git a/nixos/tests/grub.nix b/nixos/tests/grub.nix index e0875e70f6a5..3820eff4892a 100644 --- a/nixos/tests/grub.nix +++ b/nixos/tests/grub.nix @@ -1,60 +1,123 @@ -import ./make-test-python.nix ({ lib, ... }: { - name = "grub"; +{ pkgs, runTest }: - meta = with lib.maintainers; { - maintainers = [ rnhmjoj ]; - }; +{ + # Basic GRUB setup with BIOS and a password + basic = runTest { + name = "grub-basic"; + meta.maintainers = with pkgs.lib.maintainers; [ rnhmjoj ]; - nodes.machine = { ... }: { - virtualisation.useBootLoader = true; - - boot.loader.timeout = null; - boot.loader.grub = { - enable = true; - users.alice.password = "supersecret"; - - # OCR is not accurate enough - extraConfig = "serial; terminal_output serial"; + nodes.machine = { ... }: { + virtualisation.useBootLoader = true; + boot.loader.timeout = null; + boot.loader.grub = { + enable = true; + users.alice.password = "supersecret"; + # OCR is not accurate enough + extraConfig = "serial; terminal_output serial"; + }; }; + + testScript = '' + def grub_login_as(user, password): + """ + Enters user and password to log into GRUB + """ + machine.wait_for_console_text("Enter username:") + machine.send_chars(user + "\n") + machine.wait_for_console_text("Enter password:") + machine.send_chars(password + "\n") + + + def grub_select_all_configurations(): + """ + Selects "All configurations" from the GRUB menu + to trigger a login request. + """ + machine.send_monitor_command("sendkey down") + machine.send_monitor_command("sendkey ret") + + + machine.start() + + # wait for grub screen + machine.wait_for_console_text("GNU GRUB") + + grub_select_all_configurations() + with subtest("Invalid credentials are rejected"): + grub_login_as("wronguser", "wrongsecret") + machine.wait_for_console_text("error: access denied.") + + grub_select_all_configurations() + with subtest("Valid credentials are accepted"): + grub_login_as("alice", "supersecret") + machine.send_chars("\n") # press enter to boot + machine.wait_for_console_text("Linux version") + + with subtest("Machine boots correctly"): + machine.wait_for_unit("multi-user.target") + ''; + }; + + # Test boot loader entries on EFI + bls-efi = runTest { + name = "grub-bls-efi"; + meta.maintainers = with pkgs.lib.maintainers; [ rnhmjoj ]; + + nodes.machine = { pkgs, ... }: { + virtualisation.useBootLoader = true; + virtualisation.useEFIBoot = true; + boot.loader.efi.canTouchEfiVariables = true; + boot.loader.grub.enable = true; + boot.loader.grub.efiSupport = true; + }; + + testScript = '' + with subtest("Machine boots correctly"): + machine.wait_for_unit("multi-user.target") + + with subtest("Boot entries are installed"): + entries = machine.succeed("bootctl list") + print(entries) + error = "NixOS boot entry not found in bootctl list." + assert "version: Generation 1" in entries, error + + with subtest("systemctl kexec can detect the kernel"): + machine.succeed("systemctl kexec --dry-run") + + with subtest("systemctl kexec really works"): + machine.execute("systemctl kexec", check_return=False) + machine.connected = False + machine.connect() + machine.wait_for_unit("multi-user.target") + ''; }; - testScript = '' - def grub_login_as(user, password): - """ - Enters user and password to log into GRUB - """ - machine.wait_for_console_text("Enter username:") - machine.send_chars(user + "\n") - machine.wait_for_console_text("Enter password:") - machine.send_chars(password + "\n") + # Test boot loader entries on BIOS + bls-bios = runTest { + name = "grub-bls-bios"; + meta.maintainers = with pkgs.lib.maintainers; [ rnhmjoj ]; + nodes.machine = { pkgs, ... }: { + virtualisation.useBootLoader = true; + boot.loader.grub.enable = true; + }; - def grub_select_all_configurations(): - """ - Selects "All configurations" from the GRUB menu - to trigger a login request. - """ - machine.send_monitor_command("sendkey down") - machine.send_monitor_command("sendkey ret") + testScript = '' + with subtest("Machine boots correctly"): + machine.wait_for_unit("multi-user.target") + with subtest("Boot entries are installed"): + machine.succeed("test -f /boot/loader/entries/nixos-generation-1.conf") - machine.start() + with subtest("systemctl kexec can detect the kernel"): + machine.succeed("systemctl kexec --dry-run") - # wait for grub screen - machine.wait_for_console_text("GNU GRUB") + with subtest("systemctl kexec really works"): + machine.execute("systemctl kexec", check_return=False) + machine.connected = False + machine.connect() + machine.wait_for_unit("multi-user.target") + ''; + }; - grub_select_all_configurations() - with subtest("Invalid credentials are rejected"): - grub_login_as("wronguser", "wrongsecret") - machine.wait_for_console_text("error: access denied.") - - grub_select_all_configurations() - with subtest("Valid credentials are accepted"): - grub_login_as("alice", "supersecret") - machine.send_chars("\n") # press enter to boot - machine.wait_for_console_text("Linux version") - - with subtest("Machine boots correctly"): - machine.wait_for_unit("multi-user.target") - ''; -}) +}