From 7ded58beafa486e3cb09ddd85bb5feb1e133f149 Mon Sep 17 00:00:00 2001 From: Michael Hoang Date: Sun, 24 Dec 2023 00:23:24 +1100 Subject: [PATCH 1/2] tests: support overriding disko config --- example/swap.nix | 20 ++++++++++++++++++++ lib/tests.nix | 8 ++++++-- module.nix | 9 ++++++++- tests/swap.nix | 9 ++++++--- 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/example/swap.nix b/example/swap.nix index 5d590045..8c447623 100644 --- a/example/swap.nix +++ b/example/swap.nix @@ -41,6 +41,26 @@ }; }; }; + vdc = { + device = "/dev/vdc"; + type = "disk"; + content = { + type = "gpt"; + partitions = { + bigSwap = { + size = "16G"; + content = { + type = "swap"; + }; + }; + }; + }; + }; }; }; + + disko.tests.extraDiskoConfig = { + # We need to override the partition size as it is too big for the installTest. + devices.disk.vdc.content.partitions.bigSwap.size = "1G"; + }; } diff --git a/lib/tests.nix b/lib/tests.nix index 62cee833..a274e973 100644 --- a/lib/tests.nix +++ b/lib/tests.nix @@ -45,6 +45,7 @@ let { name , disko-config , extendModules ? null + , extraDiskoConfig ? { } , pkgs ? import { } , extraTestScript ? "" , bootCommands ? "" @@ -74,10 +75,13 @@ let importedDiskoConfig { inherit lib; } else importedDiskoConfig; - testConfigInstall = testLib.prepareDiskoConfig diskoConfigWithArgs (lib.tail testLib.devices); + + extendedDiskoConfigWithArgs = lib.recursiveUpdate diskoConfigWithArgs { disko = extraDiskoConfig; }; + + testConfigInstall = testLib.prepareDiskoConfig extendedDiskoConfigWithArgs (lib.tail testLib.devices); # we need to shift the disks by one because the first disk is the /dev/vda of the test runner # so /dev/vdb becomes /dev/vda etc. - testConfigBooted = testLib.prepareDiskoConfig diskoConfigWithArgs testLib.devices; + testConfigBooted = testLib.prepareDiskoConfig extendedDiskoConfigWithArgs testLib.devices; tsp-generator = pkgs.callPackage ../. { checked = true; }; tsp-format = (tsp-generator.formatScript testConfigInstall) pkgs; diff --git a/module.nix b/module.nix index 0c52892b..dd281cdf 100644 --- a/module.nix +++ b/module.nix @@ -77,6 +77,13 @@ in machine.succeed("test -e /var/secrets/my.secret") ''; }; + extraDiskoConfig = lib.mkOption { + description = '' + Extra disko configuration when running the tests. + ''; + type = lib.types.attrs; + default = { }; + }; extraConfig = lib.mkOption { description = '' Extra NixOS config for your test. Can be used to specify a different luks key for tests. @@ -102,10 +109,10 @@ in installTest = diskoLib.testLib.makeDiskoTest { inherit extendModules pkgs; + inherit (cfg.tests) efi extraDiskoConfig; name = "${config.networking.hostName}-disko"; disko-config = builtins.removeAttrs config [ "_module" ]; testMode = "direct"; - efi = cfg.tests.efi; extraSystemConfig = cfg.tests.extraConfig; extraTestScript = cfg.tests.extraChecks; }; diff --git a/tests/swap.nix b/tests/swap.nix index 57e7eb20..f20860d0 100644 --- a/tests/swap.nix +++ b/tests/swap.nix @@ -1,10 +1,13 @@ { pkgs ? import { } , diskoLib ? pkgs.callPackage ../lib { } }: -diskoLib.testLib.makeDiskoTest { +diskoLib.testLib.makeDiskoTest (let + disko-config = import ../example/swap.nix; +in { inherit pkgs; name = "swap"; - disko-config = ../example/swap.nix; + inherit disko-config; + inherit (disko-config.disko.tests) extraDiskoConfig; extraTestScript = '' import json machine.succeed("mountpoint /"); @@ -16,4 +19,4 @@ diskoLib.testLib.makeDiskoTest { extraSystemConfig = { environment.systemPackages = [ pkgs.jq ]; }; -} +}) From 6b98b81b9ce78e5ba3d6e54d6c7830b7cefcc585 Mon Sep 17 00:00:00 2001 From: Michael Hoang Date: Sun, 24 Dec 2023 00:42:23 +1100 Subject: [PATCH 2/2] luks-interactive-login: don't use passwordFile --- example/luks-btrfs-subvolumes.nix | 2 +- example/luks-interactive-login.nix | 8 +++++++- tests/luks-interactive-login.nix | 9 ++++++--- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/example/luks-btrfs-subvolumes.nix b/example/luks-btrfs-subvolumes.nix index 91915704..d4011d7f 100644 --- a/example/luks-btrfs-subvolumes.nix +++ b/example/luks-btrfs-subvolumes.nix @@ -25,7 +25,7 @@ type = "luks"; name = "crypted"; # disable settings.keyFile if you want to use interactive password entry - #passwordFile = "/tmp/secret.key"; # Interactive + # passwordFile = "/tmp/secret.key"; # Interactive settings = { allowDiscards = true; keyFile = "/tmp/secret.key"; diff --git a/example/luks-interactive-login.nix b/example/luks-interactive-login.nix index 2317b802..5ea60bf8 100644 --- a/example/luks-interactive-login.nix +++ b/example/luks-interactive-login.nix @@ -22,7 +22,6 @@ type = "luks"; name = "crypted"; settings.allowDiscards = true; - passwordFile = "/tmp/secret.key"; content = { type = "filesystem"; format = "ext4"; @@ -35,4 +34,11 @@ }; }; }; + + # If we don't set passwordFile above, we will be interactively prompted by the + # disko script to set the LUKS password. However, as passwordFile is necessary + # for installTest we set it here. + disko.tests.extraDiskoConfig = { + devices.disk.vdb.content.partitions.luks.content.passwordFile = "/tmp/secret.key"; + }; } diff --git a/tests/luks-interactive-login.nix b/tests/luks-interactive-login.nix index 6fae2e11..c780c268 100644 --- a/tests/luks-interactive-login.nix +++ b/tests/luks-interactive-login.nix @@ -1,10 +1,13 @@ { pkgs ? import { } , diskoLib ? pkgs.callPackage ../lib { } }: -diskoLib.testLib.makeDiskoTest { +diskoLib.testLib.makeDiskoTest (let + disko-config = import ../example/luks-interactive-login.nix; +in { inherit pkgs; name = "luks-interactive-login"; - disko-config = ../example/luks-interactive-login.nix; + inherit disko-config; + inherit (disko-config.disko.tests) extraDiskoConfig; extraTestScript = '' machine.succeed("cryptsetup isLuks /dev/vda2"); ''; @@ -12,4 +15,4 @@ diskoLib.testLib.makeDiskoTest { machine.wait_for_console_text("vda") machine.send_console("secretsecret\n") ''; -} +})