add nixos tests for disko.config, extend/fix existing tests

This commit is contained in:
lassulus 2022-09-30 12:55:28 +02:00
parent a215a19759
commit 9f7f23abdb
22 changed files with 448 additions and 110 deletions

117
example/boot-raid1.nix Normal file
View File

@ -0,0 +1,117 @@
{ disks ? [ "/dev/vdb" "/dev/vdc" ] }: {
disk = {
one = {
type = "disk";
device = builtins.elemAt disks 0;
content = {
type = "table";
format = "gpt";
partitions = [
{
name = "boot";
type = "partition";
start = "0";
end = "1M";
part-type = "primary";
flags = ["bios_grub"];
}
{
type = "partition";
name = "ESP";
start = "1MiB";
end = "128MiB";
fs-type = "fat32";
bootable = true;
content = {
type = "mdraid";
name = "boot";
};
}
{
type = "partition";
name = "mdadm";
start = "128MiB";
end = "100%";
content = {
type = "mdraid";
name = "raid1";
};
}
];
};
};
two = {
type = "disk";
device = builtins.elemAt disks 1;
content = {
type = "table";
format = "gpt";
partitions = [
{
name = "boot";
type = "partition";
start = "0";
end = "1M";
part-type = "primary";
flags = ["bios_grub"];
}
{
type = "partition";
name = "ESP";
start = "1MiB";
end = "128MiB";
fs-type = "fat32";
bootable = true;
content = {
type = "mdraid";
name = "boot";
};
}
{
type = "partition";
name = "mdadm";
start = "128MiB";
end = "100%";
content = {
type = "mdraid";
name = "raid1";
};
}
];
};
};
};
mdadm = {
boot = {
type = "mdadm";
level = 1;
metadata = "1.0";
content = {
type = "filesystem";
format = "vfat";
mountpoint = "/boot";
};
};
raid1 = {
type = "mdadm";
level = 1;
content = {
type = "table";
format = "gpt";
partitions = [
{
type = "partition";
name = "primary";
start = "1MiB";
end = "100%";
content = {
type = "filesystem";
format = "ext4";
mountpoint = "/";
};
}
];
};
};
};
}

View File

@ -1,16 +1,29 @@
{
{ disks ? [ "/dev/vdb" ] }: {
disk = {
vdb = {
type = "disk";
device = "/dev/vdb";
device = builtins.elemAt disks 0;
content = {
type = "table";
format = "gpt";
partitions = [
{
type = "partition";
name = "ESP";
start = "1MiB";
end = "128MiB";
fs-type = "fat32";
bootable = true;
content = {
type = "filesystem";
format = "vfat";
mountpoint = "/boot";
};
}
{
name = "root";
type = "partition";
start = "0%";
start = "128MiB";
end = "100%";
content = {
type = "btrfs";

View File

@ -1,25 +1,40 @@
{
{ disks ? [ "/dev/vdb" "/dev/vdc" ] }: {
disk = {
disk1 = {
disk0 = {
type = "disk";
device = "/dev/vdb";
device = builtins.elemAt disks 0;
content = {
type = "table";
format = "gpt";
partitions = [
{
type = "partition";
start = "0";
end = "1M";
name = "grub";
flags = ["bios_grub"];
name = "ESP";
start = "1MiB";
end = "128MiB";
fs-type = "fat32";
bootable = true;
content = {
type = "filesystem";
format = "vfat";
mountpoint = "/boot";
};
}
];
};
};
disk1 = {
type = "disk";
device = builtins.elemAt disks 1;
content = {
type = "table";
format = "gpt";
partitions = [
{
type = "partition";
start = "1M";
end = "100%";
name = "luks";
bootable = true;
content = {
type = "luks";
name = "crypted1";
@ -39,24 +54,16 @@
};
disk2 = {
type = "disk";
device = "/dev/vdc";
device = builtins.elemAt disks 2;
content = {
type = "table";
format = "gpt";
partitions = [
{
type = "partition";
start = "0";
end = "1M";
name = "grub";
flags = ["bios_grub"];
}
{
type = "partition";
start = "1M";
end = "100%";
name = "luks";
bootable = true;
content = {
type = "luks";
name = "crypted2";
@ -81,17 +88,17 @@
level = 1;
content = {
type = "table";
format = "msdos";
format = "gpt";
partitions = [
{
type = "partition";
name = "xfs";
name = "bla";
start = "1MiB";
end = "100%";
content = {
type = "filesystem";
format = "xfs";
mountpoint = "/xfs_mdadm_lvm";
format = "ext4";
mountpoint = "/ext4_mdadm_lvm";
};
}
];

View File

@ -1,14 +0,0 @@
# usage: nix-instantiate --eval --json --strict example | jq -r .
let
cfg = import ./config.nix;
#cfg = import ./config-gpt-bios.nix;
in
# TODO: get rid of NIX_PATH dependency here
with import ../. {};
{
config = config cfg;
create = create cfg;
mount = mount cfg;
}

View File

@ -1,8 +1,8 @@
# Example to create a bios compatible gpt partition
{
{ disks ? [ "/dev/vdb" ] }: {
disk = {
vdb = {
device = "/dev/vdb";
device = builtins.elemAt disks 0;
type = "disk";
content = {
type = "table";

View File

@ -1,8 +1,8 @@
{
{ disks ? [ "/dev/vdb" ] }: {
disk = {
vdb = {
type = "disk";
device = "/dev/vdb";
device = builtins.elemAt disks 0;
content = {
type = "table";
format = "gpt";
@ -10,7 +10,6 @@
{
type = "partition";
name = "ESP";
# fs-type = "FAT32";
start = "1MiB";
end = "100MiB";
bootable = true;
@ -34,7 +33,6 @@
keyFile = "/tmp/secret.key";
extraArgs = [
"--hash sha512"
"--iter-time 5000"
];
content = {
type = "lvm_pv";

View File

@ -1,16 +1,28 @@
{
{ disks ? [ "/dev/vdb" "/dev/vdc" ] }: {
disk = {
vdb = {
one = {
type = "disk";
device = "/dev/vdb";
device = builtins.elemAt disks 0;
content = {
type = "table";
format = "gpt";
partitions = [
{
name = "boot";
type = "partition";
start = "0";
end = "100M";
fs-type = "fat32";
bootable = true;
content = {
type = "mdraid";
name = "boot";
};
}
{
type = "partition";
name = "primary";
start = "0%";
start = "100M";
end = "100%";
content = {
type = "lvm_pv";
@ -20,17 +32,29 @@
];
};
};
vdc = {
two = {
type = "disk";
device = "/dev/vdc";
device = builtins.elemAt disks 1;
content = {
type = "table";
format = "gpt";
partitions = [
{
name = "boot";
type = "partition";
start = "0";
end = "100M";
fs-type = "fat32";
bootable = true;
content = {
type = "mdraid";
name = "boot";
};
}
{
type = "partition";
name = "primary";
start = "0%";
start = "100M";
end = "100%";
content = {
type = "lvm_pv";
@ -41,6 +65,18 @@
};
};
};
mdadm = {
boot = {
type = "mdadm";
level = 1;
metadata = "1.0";
content = {
type = "filesystem";
format = "vfat";
mountpoint = "/boot";
};
};
};
lvm_vg = {
pool = {
type = "lvm_vg";

View File

@ -1,12 +1,20 @@
{
{ disks ? [ "/dev/vdb" "/dev/vdc" ] }: {
disk = {
vdb = {
type = "disk";
device = "/dev/vdb";
device = builtins.elemAt disks 0;
content = {
type = "table";
format = "gpt";
partitions = [
{
name = "boot";
type = "partition";
start = "0";
end = "1M";
part-type = "primary";
flags = ["bios_grub"];
}
{
type = "partition";
name = "mdadm";
@ -22,11 +30,19 @@
};
vdc = {
type = "disk";
device = "/dev/vdc";
device = builtins.elemAt disks 1;
content = {
type = "table";
format = "gpt";
partitions = [
{
name = "boot";
type = "partition";
start = "0";
end = "1M";
part-type = "primary";
flags = ["bios_grub"];
}
{
type = "partition";
name = "mdadm";
@ -57,7 +73,7 @@
content = {
type = "filesystem";
format = "ext4";
mountpoint = "/raid";
mountpoint = "/";
};
}
];

View File

@ -1,15 +1,30 @@
{
{ disks ? [ "/dev/vdb" "/dev/vdc" ] }: {
disk = {
vdb = {
type = "disk";
device = "/dev/vdb";
device = builtins.elemAt disks 0;
content = {
type = "table";
format = "gpt";
partitions = [
{
type = "partition";
start = "0%";
name = "ESP";
start = "1MiB";
end = "100MiB";
bootable = true;
content = {
type = "filesystem";
format = "vfat";
mountpoint = "/boot";
options = [
"defaults"
];
};
}
{
type = "partition";
start = "100MiB";
end = "100%";
name = "primary";
bootable = true;
@ -24,7 +39,7 @@
};
vdc = {
type = "disk";
device = "/dev/vdc";
device = builtins.elemAt disks 1;
content = {
type = "zfs";
pool = "zroot";
@ -34,10 +49,16 @@
zpool = {
zroot = {
type = "zpool";
rootFsOptions.mountpoint = "none";
datasets = {
zfs_fs = {
"root" = {
zfs_type = "filesystem";
options.mountpoint = "none";
};
"root/zfs_fs" = {
zfs_type = "filesystem";
mountpoint = "/zfs_fs";
options.mountpoint = "/zfs_fs";
options."com.sun:auto-snapshot" = "true";
};
};

View File

@ -1,19 +1,56 @@
{
{ disks ? [ "/dev/vdb" "/dev/vdc" ] }: {
disk = {
vdb = {
x = {
type = "disk";
device = "/dev/vdb";
device = builtins.elemAt disks 0;
content = {
type = "zfs";
pool = "zroot";
type = "table";
format = "gpt";
partitions = [
{
type = "partition";
name = "ESP";
start = "0";
end = "64MiB";
fs-type = "fat32";
bootable = true;
content = {
type = "filesystem";
format = "vfat";
mountpoint = "/boot";
};
}
{
type = "partition";
name = "zfs";
start = "128MiB";
end = "100%";
content = {
type = "zfs";
pool = "zroot";
};
}
];
};
};
vdc = {
y = {
type = "disk";
device = "/dev/vdc";
device = builtins.elemAt disks 1;
content = {
type = "zfs";
pool = "zroot";
type = "table";
format = "gpt";
partitions = [
{
type = "partition";
name = "zfs";
start = "128MiB";
end = "100%";
content = {
type = "zfs";
pool = "zroot";
};
}
];
};
};
};

View File

@ -14,6 +14,7 @@
import ./tests {
inherit pkgs;
makeTest = import (pkgs.path + "/nixos/tests/make-test-python.nix");
eval-config = import (pkgs.path + "/nixos/lib/eval-config.nix");
};
};
}

10
tests/boot-raid1.nix Normal file
View File

@ -0,0 +1,10 @@
{ pkgs ? (import <nixpkgs> { })
, makeDiskoTest ? (pkgs.callPackage ./lib.nix { }).makeDiskoTest
}:
makeDiskoTest {
disko-config = import ../example/boot-raid1.nix;
extraTestScript = ''
machine.succeed("test -b /dev/md/boot");
machine.succeed("mountpoint /boot");
'';
}

View File

@ -4,8 +4,8 @@
makeDiskoTest {
disko-config = import ../example/btrfs-subvolumes.nix;
extraTestScript = ''
machine.succeed("test -e /mnt/test");
machine.succeed("btrfs subvolume list /mnt | grep -qs 'path test$'");
machine.succeed("test -e /test");
machine.succeed("btrfs subvolume list / | grep -qs 'path test$'");
'';
}

View File

@ -3,16 +3,23 @@
}:
makeDiskoTest {
disko-config = import ../example/complex.nix;
extraConfig = {
fileSystems."/zfs_legacy_fs".options = [ "nofail" ]; # TODO find out why we need this!
};
extraTestScript = ''
machine.succeed("test -b /dev/zroot/zfs_testvolume");
machine.succeed("test -b /dev/md/raid1p1");
machine.succeed("mountpoint /mnt");
machine.succeed("mountpoint /mnt/zfs_fs");
machine.succeed("mountpoint /mnt/zfs_legacy_fs");
machine.succeed("mountpoint /mnt/ext4onzfs");
machine.succeed("mountpoint /mnt/ext4_on_lvm");
machine.succeed("mountpoint /zfs_fs");
machine.succeed("mountpoint /zfs_legacy_fs");
machine.succeed("mountpoint /ext4onzfs");
machine.succeed("mountpoint /ext4_on_lvm");
'';
enableOCR = true;
bootCommands = ''
machine.wait_for_text("Passphrase for")
machine.send_chars("secret\n")
'';
extraConfig = {
boot.kernelModules = [ "dm-raid" "dm-mirror" ];

View File

@ -1,9 +1,10 @@
{ makeTest ? import <nixpkgs/nixos/tests/make-test-python.nix>
, eval-config ? import <nixpkgs/nixos/lib/eval-config.nix>
, pkgs ? (import <nixpkgs> { })
}@args:
let
lib = pkgs.lib;
makeDiskoTest = (pkgs.callPackage ./lib.nix { inherit makeTest; }).makeDiskoTest;
makeDiskoTest = (pkgs.callPackage ./lib.nix { inherit makeTest eval-config; }).makeDiskoTest;
evalTest = name: configFile: let
disko-config = import configFile;

View File

@ -4,7 +4,8 @@
makeDiskoTest {
disko-config = import ../example/gpt-bios-compat.nix;
extraTestScript = ''
machine.succeed("mountpoint /mnt");
machine.succeed("grub-install --target=i386-pc /dev/vdb");
machine.succeed("mountpoint /");
'';
efi = false;
grub-devices = [ "/dev/vdb" ];
}

View File

@ -1,12 +1,17 @@
{ pkgs ? (import <nixpkgs> { })
, makeTest ? import <nixpkgs/nixos/tests/make-test-python.nix>
, eval-config ? import <nixpkgs/nixos/lib/eval-config.nix>
, ...
}:
{
makeDiskoTest =
{ disko-config
, extraTestScript
, extraTestScript ? ""
, bootCommands ? ""
, extraConfig ? { }
, grub-devices ? [ "nodev" ]
, efi ? true
, enableOCR ? false
}:
let
lib = pkgs.lib;
@ -15,33 +20,107 @@
inherit pkgs;
inherit (pkgs) system;
};
tsp-create = pkgs.writeScript "create" ((pkgs.callPackage ../. { }).create disko-config);
tsp-mount = pkgs.writeScript "mount" ((pkgs.callPackage ../. { }).mount disko-config);
num-disks = builtins.length (lib.attrNames disko-config.disk);
disks = [ "/dev/vda" "/dev/vdb" "/dev/vdc" "/dev/vdd" "/dev/vde" "/dev/vdf" ];
tsp-create = pkgs.writeScript "create" ((pkgs.callPackage ../. { }).create (disko-config { disks = builtins.tail disks; }));
tsp-mount = pkgs.writeScript "mount" ((pkgs.callPackage ../. { }).mount (disko-config { disks = builtins.tail disks; }));
tsp-config = (pkgs.callPackage ../. { }).config (disko-config { inherit disks; });
num-disks = builtins.length (lib.attrNames (disko-config {}).disk);
installed-system = { modulesPath, ... }: {
imports = [
tsp-config
(modulesPath + "/testing/test-instrumentation.nix")
(modulesPath + "/profiles/qemu-guest.nix")
(modulesPath + "/profiles/minimal.nix")
extraConfig
];
fileSystems."/nix/store" = {
device = "nix-store";
fsType = "9p";
neededForBoot = true;
options = [ "trans=virtio" "version=9p2000.L" "cache=loose" ];
};
documentation.enable = false;
hardware.enableAllFirmware = lib.mkForce false;
networking.hostId = "8425e349"; # from profiles/base.nix, needed for zfs
boot.kernelParams = lib.mkAfter [ "console=tty0" ]; # needed to have serial interaction during boot
boot.zfs.devNodes = "/dev/disk/by-uuid"; # needed because /dev/disk/by-id is empty in qemu-vms
boot.consoleLogLevel = lib.mkForce 100;
boot.loader.grub = {
devices = grub-devices;
efiSupport = efi;
efiInstallAsRemovable = efi;
};
};
installedTopLevel = (eval-config {
modules = [ installed-system ];
system = "x86_64-linux";
}).config.system.build.toplevel;
in
makeTest' {
name = "disko";
nodes.machine =
{ config, pkgs, modulesPath, ... }:
inherit enableOCR;
nodes.machine = { config, pkgs, modulesPath, ... }: {
imports = [
(modulesPath + "/profiles/base.nix")
(modulesPath + "/profiles/minimal.nix")
extraConfig
];
{
imports = [
(modulesPath + "/profiles/installation-device.nix")
(modulesPath + "/profiles/base.nix")
];
# speed-up eval
documentation.enable = false;
# speed-up eval
documentation.enable = false;
nix.settings = {
substituters = lib.mkForce [];
hashed-mirrors = null;
connect-timeout = 1;
};
virtualisation.emptyDiskImages = builtins.genList (_: 512) num-disks;
} // extraConfig;
virtualisation.emptyDiskImages = builtins.genList (_: 4096) num-disks;
};
testScript = ''
machine.succeed("echo 'secret' > /tmp/secret.key");
machine.succeed("${tsp-create}");
machine.succeed("${tsp-mount}");
machine.succeed("${tsp-mount}"); # verify that the command is idempotent
def disks(oldmachine, num_disks):
disk_flags = ""
for i in range(num_disks):
disk_flags += f' -drive file={oldmachine.state_dir}/empty{i}.qcow2,id=drive{i + 1},if=none,index={i + 1},werror=report'
disk_flags += f' -device virtio-blk-pci,drive=drive{i + 1}'
return disk_flags
def create_test_machine(oldmachine=None, args={}): # taken from <nixpkgs/nixos/tests/installer.nix>
machine = create_machine({
"qemuFlags": "-cpu max -m 1024 -virtfs local,path=/nix/store,security_model=none,mount_tag=nix-store" + disks(oldmachine, ${toString num-disks}),
${lib.optionalString efi ''"bios": "${pkgs.OVMF.fd}/FV/OVMF.fd",''}
} | args)
driver.machines.append(machine)
return machine
machine.start()
machine.succeed("echo -n 'secret' > /tmp/secret.key")
machine.succeed("${tsp-create}")
machine.succeed("${tsp-mount}")
machine.succeed("${tsp-mount}") # verify that the command is idempotent
# mount nix-store in /mnt
machine.succeed("mkdir -p /mnt/nix/store")
machine.succeed("mount --bind /nix/store /mnt/nix/store")
machine.succeed("nix-store --load-db < ${pkgs.closureInfo {rootPaths = [installedTopLevel];}}/registration")
# fix "this is not a NixOS installation"
machine.succeed("mkdir -p /mnt/etc")
machine.succeed("touch /mnt/etc/NIXOS")
machine.succeed("mkdir -p /mnt/nix/var/nix/profiles")
machine.succeed("nix-env -p /mnt/nix/var/nix/profiles/system --set ${installedTopLevel}")
machine.succeed("NIXOS_INSTALL_BOOTLOADER=1 nixos-enter --root /mnt -- ${installedTopLevel}/bin/switch-to-configuration boot")
machine.succeed("sync")
machine.shutdown()
machine = create_test_machine(oldmachine=machine, args={ "name": "booted_machine" })
machine.start()
${bootCommands}
machine.wait_for_unit("local-fs.target")
${extraTestScript}
'';
};

View File

@ -4,7 +4,12 @@
makeDiskoTest {
disko-config = import ../example/luks-lvm.nix;
extraTestScript = ''
machine.succeed("cryptsetup isLuks /dev/vdb2");
machine.succeed("mountpoint /mnt/home");
machine.succeed("cryptsetup isLuks /dev/vda2");
machine.succeed("mountpoint /home");
'';
enableOCR = true;
bootCommands = ''
machine.wait_for_text("Passphrase for")
machine.send_chars("secret\n")
'';
}

View File

@ -4,9 +4,9 @@
makeDiskoTest {
disko-config = import ../example/lvm-raid.nix;
extraTestScript = ''
machine.succeed("mountpoint /mnt/home");
machine.succeed("mountpoint /home");
'';
extraConfig = {
boot.kernelModules = [ "dm-raid" "dm-mirror" ];
boot.kernelModules = [ "dm-raid0" "dm-mirror" ];
};
}

View File

@ -5,6 +5,8 @@ makeDiskoTest {
disko-config = import ../example/mdadm.nix;
extraTestScript = ''
machine.succeed("test -b /dev/md/raid1");
machine.succeed("mountpoint /mnt/raid");
machine.succeed("mountpoint /");
'';
efi = false;
grub-devices = [ "/dev/vdb" "/dev/vdc" ];
}

View File

@ -4,9 +4,8 @@
makeDiskoTest {
disko-config = import ../example/zfs-over-legacy.nix;
extraTestScript = ''
machine.succeed("test -e /mnt/zfs_fs");
machine.succeed("mountpoint /mnt");
machine.succeed("mountpoint /mnt/zfs_fs");
machine.succeed("test -e /zfs_fs");
machine.succeed("mountpoint /zfs_fs");
'';
}

View File

@ -3,6 +3,9 @@
}:
makeDiskoTest {
disko-config = import ../example/zfs.nix;
extraConfig = {
fileSystems."/zfs_legacy_fs".options = [ "nofail" ]; # TODO find out why we need this!
};
extraTestScript = ''
machine.succeed("test -b /dev/zvol/zroot/zfs_testvolume");
@ -19,9 +22,8 @@ makeDiskoTest {
assert_property("zroot/zfs_testvolume", "volsize", "10M")
assert_property("zroot/zfs_unmounted_fs", "mountpoint", "none")
machine.succeed("mountpoint /mnt");
machine.succeed("mountpoint /mnt/zfs_fs");
machine.succeed("mountpoint /mnt/zfs_legacy_fs");
machine.succeed("mountpoint /mnt/ext4onzfs");
machine.succeed("mountpoint /zfs_fs");
machine.succeed("mountpoint /zfs_legacy_fs");
machine.succeed("mountpoint /ext4onzfs");
'';
}