Merge pull request #299 from nix-community/module-tests

allow using the disko test framework via the module
This commit is contained in:
Jörg Thalheim authored and GitHub committed 2023-07-21 13:37:39 +01:00
commit f2248036d2
24 files changed
+138 -79

No files matched your search

+2 -2
View File
@@ -9,7 +9,7 @@
partitions = { partitions = {
boot = { boot = {
size = "1M"; size = "1M";
type = "EF02"; type = "EF02"; # for grub MBR
}; };
ESP = { ESP = {
size = "100M"; size = "100M";
@@ -37,7 +37,7 @@
partitions = { partitions = {
boot = { boot = {
size = "1M"; size = "1M";
type = "EF02"; type = "EF02"; # for grub MBR
}; };
ESP = { ESP = {
size = "128M"; size = "128M";
+1
View File
@@ -12,6 +12,7 @@
name = "ESP"; name = "ESP";
start = "1M"; start = "1M";
end = "128MiB"; end = "128MiB";
type = "EF00";
content = { content = {
type = "filesystem"; type = "filesystem";
format = "vfat"; format = "vfat";
+1
View File
@@ -10,6 +10,7 @@
ESP = { ESP = {
start = "1MiB"; start = "1MiB";
end = "128MiB"; end = "128MiB";
type = "EF00";
content = { content = {
type = "filesystem"; type = "filesystem";
format = "vfat"; format = "vfat";
+1 -1
View File
@@ -10,7 +10,7 @@
partitions = { partitions = {
boot = { boot = {
size = "1M"; size = "1M";
type = "EF02"; type = "EF02"; # for grub MBR
}; };
root = { root = {
size = "100%"; size = "100%";
+2 -1
View File
@@ -8,11 +8,12 @@
partitions = { partitions = {
boot = { boot = {
size = "1M"; size = "1M";
type = "EF02"; type = "EF02"; # for grub MBR
}; };
ESP = { ESP = {
name = "ESP"; name = "ESP";
size = "512M"; size = "512M";
type = "EF00";
content = { content = {
type = "filesystem"; type = "filesystem";
format = "vfat"; format = "vfat";
+2 -1
View File
@@ -9,10 +9,11 @@
partitions = { partitions = {
boot = { boot = {
size = "1M"; size = "1M";
type = "EF02"; type = "EF02"; # for grub MBR
}; };
ESP = { ESP = {
size = "512M"; size = "512M";
type = "EF00";
content = { content = {
type = "filesystem"; type = "filesystem";
format = "vfat"; format = "vfat";
+1
View File
@@ -9,6 +9,7 @@
partitions = { partitions = {
ESP = { ESP = {
size = "100M"; size = "100M";
type = "EF00";
content = { content = {
type = "filesystem"; type = "filesystem";
format = "vfat"; format = "vfat";
+2
View File
@@ -9,6 +9,7 @@
partitions = { partitions = {
boot = { boot = {
size = "100M"; size = "100M";
type = "EF00";
content = { content = {
type = "mdraid"; type = "mdraid";
name = "boot"; name = "boot";
@@ -33,6 +34,7 @@
boot = { boot = {
name = "boot"; name = "boot";
size = "100M"; size = "100M";
type = "EF00";
content = { content = {
type = "mdraid"; type = "mdraid";
name = "boot"; name = "boot";
+1
View File
@@ -9,6 +9,7 @@
partitions = { partitions = {
boot = { boot = {
size = "100M"; size = "100M";
type = "EF00";
content = { content = {
type = "filesystem"; type = "filesystem";
format = "vfat"; format = "vfat";
+2 -2
View File
@@ -9,7 +9,7 @@
partitions = { partitions = {
boot = { boot = {
size = "1M"; size = "1M";
type = "EF02"; type = "EF02"; # for grub MBR
}; };
mdadm = { mdadm = {
size = "100%"; size = "100%";
@@ -29,7 +29,7 @@
partitions = { partitions = {
boot = { boot = {
size = "1M"; size = "1M";
type = "EF02"; type = "EF02"; # for grub MBR
}; };
mdadm = { mdadm = {
size = "100%"; size = "100%";
+1
View File
@@ -9,6 +9,7 @@
partitions = { partitions = {
ESP = { ESP = {
size = "100M"; size = "100M";
type = "EF00";
content = { content = {
type = "filesystem"; type = "filesystem";
format = "vfat"; format = "vfat";
+1
View File
@@ -9,6 +9,7 @@
partitions = { partitions = {
ESP = { ESP = {
size = "100M"; size = "100M";
type = "EF00";
content = { content = {
type = "filesystem"; type = "filesystem";
format = "vfat"; format = "vfat";
+1
View File
@@ -9,6 +9,7 @@
partitions = { partitions = {
ESP = { ESP = {
size = "100M"; size = "100M";
type = "EF00";
content = { content = {
type = "filesystem"; type = "filesystem";
format = "vfat"; format = "vfat";
+1
View File
@@ -9,6 +9,7 @@
partitions = { partitions = {
ESP = { ESP = {
size = "64M"; size = "64M";
type = "EF00";
content = { content = {
type = "filesystem"; type = "filesystem";
format = "vfat"; format = "vfat";
+1 -1
View File
@@ -407,7 +407,7 @@ let
${cfg.config._create} ${cfg.config._create}
''; '';
system.build.mountScriptNoDeps = (diskoLib.writeCheckedBash { inherit pkgs checked; noDeps = true; }) "disko-mount" '' mountScriptNoDeps = (diskoLib.writeCheckedBash { inherit pkgs checked; noDeps = true; }) "disko-mount" ''
${cfg.config._mount} ${cfg.config._mount}
''; '';
+75 -64
View File
@@ -10,21 +10,27 @@ let
# basically changes all the disk.*.devices to something like /dev/vda or /dev/vdb etc. # basically changes all the disk.*.devices to something like /dev/vda or /dev/vdb etc.
prepareDiskoConfig = toplevel: devices: prepareDiskoConfig = toplevel: devices:
let let
cleanedTopLevel = lib.filterAttrsRecursive (n: _: !lib.hasPrefix "_" n) toplevel;
preparedDisks = lib.foldlAttrs (acc: n: v: { preparedDisks = lib.foldlAttrs (acc: n: v: {
devices = lib.tail acc.devices; devices = lib.tail acc.devices;
value = acc.value // { grub-devices = acc.grub-devices ++ (lib.optional (lib.any (part: (part.type or "") == "EF02") (lib.attrValues (v.content.partitions or {}))) (lib.head acc.devices));
${n} = v // { disks = acc.disks // {
"${n}" = v // {
device = lib.head acc.devices; device = lib.head acc.devices;
content = v.content // { device = lib.head acc.devices; };
}; };
}; };
}) { }) {
inherit devices; inherit devices;
value = {}; grub-devices = [];
} toplevel.disko.devices.disk; disks = {};
} cleanedTopLevel.disko.devices.disk;
in in
toplevel // { cleanedTopLevel // {
disko.devices = toplevel.disko.devices // { boot.loader.grub.devices = if (preparedDisks.grub-devices != []) then preparedDisks.grub-devices else [ "nodev" ];
disk = preparedDisks.value; disko.devices = cleanedTopLevel.disko.devices // {
disk = preparedDisks.disks;
}; };
}; };
@@ -32,13 +38,12 @@ let
makeDiskoTest = makeDiskoTest =
{ name { name
, disko-config , disko-config
, nixos-config ? null , extendModules ? null
, pkgs ? import <nixpkgs> { } , pkgs ? import <nixpkgs> { }
, extraTestScript ? "" , extraTestScript ? ""
, bootCommands ? "" , bootCommands ? ""
, extraInstallerConfig ? { } , extraInstallerConfig ? { }
, extraSystemConfig ? { } , extraSystemConfig ? { }
, grub-devices ? [ "nodev" ]
, efi ? true , efi ? true
, postDisko ? "" , postDisko ? ""
, testMode ? "module" # can be one of direct module cli , testMode ? "module" # can be one of direct module cli
@@ -50,9 +55,14 @@ let
inherit pkgs; inherit pkgs;
inherit (pkgs) system; inherit (pkgs) system;
}; };
devices = [ "/dev/vda" "/dev/vdb" "/dev/vdc" "/dev/vdd" "/dev/vde" "/dev/vdf"];
# for installation we skip /dev/vda because it is the test runner disk # for installation we skip /dev/vda because it is the test runner disk
importedDiskoConfig = import disko-config; devices = [ "/dev/vda" "/dev/vdb" "/dev/vdc" "/dev/vdd" "/dev/vde" "/dev/vdf"];
importedDiskoConfig = if builtins.isPath disko-config then
import disko-config
else
disko-config;
diskoConfigWithArgs = if builtins.isFunction importedDiskoConfig then diskoConfigWithArgs = if builtins.isFunction importedDiskoConfig then
importedDiskoConfig { inherit lib; } importedDiskoConfig { inherit lib; }
else else
@@ -63,24 +73,15 @@ let
testConfigBooted = testlib.prepareDiskoConfig diskoConfigWithArgs devices; testConfigBooted = testlib.prepareDiskoConfig diskoConfigWithArgs devices;
tsp-generator = pkgs.callPackage ../. { checked = true; }; tsp-generator = pkgs.callPackage ../. { checked = true; };
tsp-create = (tsp-generator.createScript testConfigInstall) pkgs; tsp-format = (tsp-generator.formatScript testConfigInstall) pkgs;
tsp-mount = (tsp-generator.mountScript testConfigInstall) pkgs; tsp-mount = (tsp-generator.mountScript testConfigInstall) pkgs;
tsp-disko = (tsp-generator.diskoScript testConfigInstall) pkgs; tsp-disko = (tsp-generator.diskoScript testConfigInstall) pkgs;
tsp-config = tsp-generator.config testConfigBooted; tsp-config = tsp-generator.config testConfigBooted;
num-disks = builtins.length (lib.attrNames testConfigBooted.disko.devices.disk); num-disks = builtins.length (lib.attrNames testConfigBooted.disko.devices.disk);
installed-system = { modulesPath, ... }: {
# we always want the bind-mounted nix store. otherwise tests take forever
fileSystems."/nix/store" = lib.mkForce {
device = "nix-store";
fsType = "9p";
neededForBoot = true;
options = [ "trans=virtio" "version=9p2000.L" "cache=loose" ];
};
imports = (if nixos-config != null then [ installed-system = { modulesPath, ... }: {
nixos-config imports = [
] else [ (lib.optionalAttrs (testMode == "direct") tsp-config)
(lib.optionalAttrs (testMode == "direct" || testMode == "cli") tsp-config)
(lib.optionalAttrs (testMode == "module") { (lib.optionalAttrs (testMode == "module") {
disko.enableConfig = true; disko.enableConfig = true;
imports = [ imports = [
@@ -88,34 +89,56 @@ let
testConfigBooted testConfigBooted
]; ];
}) })
{ # config for tests to make them run faster or work at all
documentation.enable = false;
hardware.enableAllFirmware = lib.mkForce false;
networking.hostId = "8425e349"; # from profiles/base.nix, needed for zfs
boot.zfs.devNodes = "/dev/disk/by-uuid"; # needed because /dev/disk/by-id is empty in qemu-vms
boot.initrd.preDeviceCommands = ''
echo -n 'secretsecret' > /tmp/secret.key
'';
boot.consoleLogLevel = lib.mkForce 100;
boot.loader.grub = {
devices = grub-devices;
efiSupport = efi;
efiInstallAsRemovable = efi;
};
}
]) ++ [
(modulesPath + "/testing/test-instrumentation.nix") # we need these 2 modules always to be able to run the tests
(modulesPath + "/profiles/qemu-guest.nix")
extraSystemConfig
]; ];
# config for tests to make them run faster or work at all
documentation.enable = false;
hardware.enableAllFirmware = lib.mkForce false;
networking.hostId = "8425e349"; # from profiles/base.nix, needed for zfs
boot.initrd.preDeviceCommands = ''
echo -n 'secretsecret' > /tmp/secret.key
'';
boot.consoleLogLevel = lib.mkForce 100;
boot.loader.systemd-boot.enable = lib.mkDefault efi;
}; };
installed-system-eval = eval-config { installed-system-eval = eval-config {
modules = [ installed-system ]; modules = [ installed-system ];
inherit (pkgs) system; inherit (pkgs) system;
}; };
installedTopLevel = installed-system-eval.config.system.build.toplevel; installedTopLevel = ((if extendModules != null then extendModules else installed-system-eval.extendModules) {
modules = [{
imports = [
extraSystemConfig
({ modulesPath, ... }: {
imports = [
(modulesPath + "/testing/test-instrumentation.nix") # we need these 2 modules always to be able to run the tests
(modulesPath + "/profiles/qemu-guest.nix")
];
disko.devices = lib.mkForce testConfigBooted.disko.devices;
})
];
# since we boot on a different machine, the efi payload needs to be portable
boot.loader.grub.efiInstallAsRemovable = efi;
boot.loader.grub.efiSupport = efi;
boot.loader.systemd-boot.graceful = true;
# we always want the bind-mounted nix store. otherwise tests take forever
fileSystems."/nix/store" = lib.mkForce {
device = "nix-store";
fsType = "9p";
neededForBoot = true;
options = [ "trans=virtio" "version=9p2000.L" "cache=loose" ];
};
boot.zfs.devNodes = "/dev/disk/by-uuid"; # needed because /dev/disk/by-id is empty in qemu-vms
# grub will install to these devices, we need to force those or we are offset by 1
boot.loader.grub.devices = lib.mkForce testConfigInstall.boot.loader.grub.devices;
}];
}).config.system.build.toplevel;
in in
makeTest' { makeTest' {
name = "disko-${name}"; name = "disko-${name}";
@@ -132,13 +155,6 @@ let
devices = testConfigInstall.disko.devices; devices = testConfigInstall.disko.devices;
}; };
}) })
(lib.optionalAttrs (testMode == "cli") {
imports = [ (modulesPath + "/installer/cd-dvd/channel.nix") ];
system.extraDependencies = [
((pkgs.callPackage ../. { checked = true; }).createScript testConfigInstall pkgs)
((pkgs.callPackage ../. { checked = true; }).mountScript testConfigInstall pkgs)
];
})
(modulesPath + "/profiles/base.nix") (modulesPath + "/profiles/base.nix")
(modulesPath + "/profiles/minimal.nix") (modulesPath + "/profiles/minimal.nix")
extraInstallerConfig extraInstallerConfig
@@ -156,6 +172,10 @@ let
connect-timeout = 1; connect-timeout = 1;
}; };
networking.hostId = lib.mkIf (
(testConfigInstall ? networking.hostId) && (testConfigInstall.networking.hostId != null)
) testConfigInstall.networking.hostId;
virtualisation.emptyDiskImages = builtins.genList (_: 4096) num-disks; virtualisation.emptyDiskImages = builtins.genList (_: 4096) num-disks;
# useful for debugging via repl # useful for debugging via repl
@@ -181,28 +201,19 @@ let
machine.succeed("echo -n 'additionalSecret' > /tmp/additionalSecret.key") machine.succeed("echo -n 'additionalSecret' > /tmp/additionalSecret.key")
machine.succeed("echo -n 'secretsecret' > /tmp/secret.key") machine.succeed("echo -n 'secretsecret' > /tmp/secret.key")
${lib.optionalString (testMode == "direct") '' ${lib.optionalString (testMode == "direct") ''
machine.succeed("${tsp-create}") # running direct mode
machine.succeed("${tsp-format}")
machine.succeed("${tsp-mount}") machine.succeed("${tsp-mount}")
machine.succeed("${tsp-mount}") # verify that the command is idempotent machine.succeed("${tsp-mount}") # verify that the command is idempotent
machine.succeed("${tsp-disko}") # verify that we can destroy and recreate machine.succeed("${tsp-disko}") # verify that we can destroy and recreate
''} ''}
${lib.optionalString (testMode == "module") '' ${lib.optionalString (testMode == "module") ''
# running module mode
machine.succeed("${nodes.machine.system.build.formatScript}") machine.succeed("${nodes.machine.system.build.formatScript}")
machine.succeed("${nodes.machine.system.build.mountScript}") machine.succeed("${nodes.machine.system.build.mountScript}")
machine.succeed("${nodes.machine.system.build.mountScript}") # verify that the command is idempotent machine.succeed("${nodes.machine.system.build.mountScript}") # verify that the command is idempotent
machine.succeed("${nodes.machine.system.build.diskoScript}") # verify that we can destroy and recreate again machine.succeed("${nodes.machine.system.build.diskoScript}") # verify that we can destroy and recreate again
''} ''}
${lib.optionalString (testMode == "cli") ''
# TODO use the disko cli here
# machine.succeed("${../.}/disko --no-pkgs --mode create ${disko-config}")
# machine.succeed("${../.}/disko --no-pkgs --mode mount ${disko-config}")
# machine.succeed("${../.}/disko --no-pkgs --mode mount ${disko-config}") # verify that the command is idempotent
# machine.succeed("${../.}/disko --no-pkgs --mode zap_create_mount ${disko-config}") # verify that we can destroy and recreate again
machine.succeed("${tsp-create}")
machine.succeed("${tsp-mount}")
machine.succeed("${tsp-mount}") # verify that the command is idempotent
machine.succeed("${tsp-disko}") # verify that we can destroy and recreate
''}
${postDisko} ${postDisko}
+5 -2
View File
@@ -129,11 +129,14 @@ in
_config = lib.mkOption { _config = lib.mkOption {
internal = true; internal = true;
readOnly = true; readOnly = true;
default = map default = (map
(partition: (partition:
lib.optional (partition.content != null) partition.content._config lib.optional (partition.content != null) partition.content._config
) )
(lib.attrValues config.partitions); (lib.attrValues config.partitions))
++ (lib.optional (lib.any (part: part.type == "EF02") (lib.attrValues config.partitions)) {
boot.loader.grub.devices = [ config.device ];
});
description = "NixOS configuration"; description = "NixOS configuration";
}; };
_pkgs = lib.mkOption { _pkgs = lib.mkOption {
+21 -1
View File
@@ -1,8 +1,10 @@
{ config, lib, pkgs, ... }: { config, lib, pkgs, extendModules, ... }@args:
let let
diskoLib = import ./lib { diskoLib = import ./lib {
inherit lib; inherit lib;
rootMountPoint = config.disko.rootMountPoint; rootMountPoint = config.disko.rootMountPoint;
makeTest = import (pkgs.path + "/nixos/tests/make-test-python.nix");
eval-config = import (pkgs.path + "/nixos/lib/eval-config.nix");
}; };
cfg = config.disko; cfg = config.disko;
in in
@@ -34,6 +36,16 @@ in
type = lib.types.bool; type = lib.types.bool;
default = false; default = false;
}; };
tests = {
efi = lib.mkOption {
description = ''
Whether efi is enabled for the `system.build.installTest`.
We try to automatically detect efi based on the configured bootloader.
'';
type = lib.types.bool;
default = config.boot.loader.systemd-boot.enable || config.boot.loader.grub.efiSupport;
};
};
}; };
config = lib.mkIf (cfg.devices.disk != { }) { config = lib.mkIf (cfg.devices.disk != { }) {
system.build = (cfg.devices._scripts { inherit pkgs; checked = cfg.checkScripts; }) // { system.build = (cfg.devices._scripts { inherit pkgs; checked = cfg.checkScripts; }) // {
@@ -41,6 +53,14 @@ in
# we keep this old outputs for compatibility # we keep this old outputs for compatibility
disko = builtins.trace "the .disko output is deprecated, plase use .diskoScript instead" cfg.devices._scripts.diskoScript; disko = builtins.trace "the .disko output is deprecated, plase use .diskoScript instead" cfg.devices._scripts.diskoScript;
diskoNoDeps = builtins.trace "the .diskoNoDeps output is deprecated, plase use .diskoScriptNoDeps instead" cfg.devices._scripts.diskoScriptNoDeps; diskoNoDeps = builtins.trace "the .diskoNoDeps output is deprecated, plase use .diskoScriptNoDeps instead" cfg.devices._scripts.diskoScriptNoDeps;
installTest = diskoLib.testLib.makeDiskoTest {
inherit extendModules pkgs;
name = "${config.networking.hostName}-disko";
disko-config = builtins.removeAttrs config ["_module"];
testMode = "direct";
efi = cfg.tests.efi;
};
}; };
+4
View File
@@ -9,4 +9,8 @@ makeDiskoTest {
machine.succeed("test -b /dev/md/boot"); machine.succeed("test -b /dev/md/boot");
machine.succeed("mountpoint /boot"); machine.succeed("mountpoint /boot");
''; '';
extraSystemConfig = {
# sadly systemd-boot fails to install to a raid /boot device
boot.loader.systemd-boot.enable = false;
};
} }
+9 -1
View File
@@ -8,7 +8,7 @@ makeDiskoTest {
extraSystemConfig = { extraSystemConfig = {
fileSystems."/zfs_legacy_fs".options = [ "nofail" ]; # TODO find out why we need this! fileSystems."/zfs_legacy_fs".options = [ "nofail" ]; # TODO find out why we need this!
}; };
testMode = "cli"; testMode = "direct";
extraTestScript = '' extraTestScript = ''
machine.succeed("test -b /dev/zroot/zfs_testvolume"); machine.succeed("test -b /dev/zroot/zfs_testvolume");
machine.succeed("test -b /dev/md/raid1p1"); machine.succeed("test -b /dev/md/raid1p1");
@@ -19,7 +19,15 @@ makeDiskoTest {
machine.succeed("mountpoint /ext4onzfs"); machine.succeed("mountpoint /ext4onzfs");
machine.succeed("mountpoint /ext4_on_lvm"); machine.succeed("mountpoint /ext4_on_lvm");
''; '';
extraSystemConfig = {
imports = [
../module.nix
];
};
extraInstallerConfig = { extraInstallerConfig = {
boot.kernelModules = [ "dm-raid" "dm-mirror" ]; boot.kernelModules = [ "dm-raid" "dm-mirror" ];
imports = [
../module.nix
];
}; };
} }
-1
View File
@@ -9,5 +9,4 @@ makeDiskoTest {
machine.succeed("mountpoint /"); machine.succeed("mountpoint /");
''; '';
efi = false; efi = false;
grub-devices = [ "/dev/vdb" ];
} }
+4
View File
@@ -11,4 +11,8 @@ makeDiskoTest {
extraInstallerConfig = { extraInstallerConfig = {
boot.kernelModules = [ "dm-raid0" "dm-mirror" ]; boot.kernelModules = [ "dm-raid0" "dm-mirror" ];
}; };
extraSystemConfig = {
# sadly systemd-boot fails to install to a raid /boot device
boot.loader.systemd-boot.enable = false;
};
} }
-1
View File
@@ -10,5 +10,4 @@ makeDiskoTest {
machine.succeed("mountpoint /"); machine.succeed("mountpoint /");
''; '';
efi = false; efi = false;
grub-devices = [ "/dev/vdb" "/dev/vdc" ];
} }
-1
View File
@@ -9,5 +9,4 @@ makeDiskoTest {
machine.succeed("mountpoint /"); machine.succeed("mountpoint /");
''; '';
efi = false; efi = false;
grub-devices = [ "/dev/vdb" ];
} }