Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: support overriding disko config #480

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/luks-btrfs-subvolumes.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
8 changes: 7 additions & 1 deletion example/luks-interactive-login.nix
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
type = "luks";
name = "crypted";
settings.allowDiscards = true;
passwordFile = "/tmp/secret.key";
content = {
type = "filesystem";
format = "ext4";
Expand All @@ -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";
};
}
20 changes: 20 additions & 0 deletions example/swap.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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";
};
}
8 changes: 6 additions & 2 deletions lib/tests.nix
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ let
{ name
, disko-config
, extendModules ? null
, extraDiskoConfig ? { }
, pkgs ? import <nixpkgs> { }
, extraTestScript ? ""
, bootCommands ? ""
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 8 additions & 1 deletion module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
};
Expand Down
9 changes: 6 additions & 3 deletions tests/luks-interactive-login.nix
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
{ pkgs ? import <nixpkgs> { }
, 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");
'';
bootCommands = ''
machine.wait_for_console_text("vda")
machine.send_console("secretsecret\n")
'';
}
})
9 changes: 6 additions & 3 deletions tests/swap.nix
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{ pkgs ? import <nixpkgs> { }
, 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 /");
Expand All @@ -16,4 +19,4 @@ diskoLib.testLib.makeDiskoTest {
extraSystemConfig = {
environment.systemPackages = [ pkgs.jq ];
};
}
})