2022-08-26 06:41:58 +00:00
|
|
|
{ pkgs ? (import <nixpkgs> { })
|
2022-08-25 21:12:49 +00:00
|
|
|
, makeTest ? import <nixpkgs/nixos/tests/make-test-python.nix>
|
2022-09-30 10:55:28 +00:00
|
|
|
, eval-config ? import <nixpkgs/nixos/lib/eval-config.nix>
|
2022-08-25 21:12:49 +00:00
|
|
|
, ...
|
|
|
|
}:
|
|
|
|
{
|
2022-08-26 06:41:58 +00:00
|
|
|
makeDiskoTest =
|
|
|
|
{ disko-config
|
2022-09-30 10:55:28 +00:00
|
|
|
, extraTestScript ? ""
|
|
|
|
, bootCommands ? ""
|
2022-08-26 06:41:58 +00:00
|
|
|
, extraConfig ? { }
|
2022-09-30 10:55:28 +00:00
|
|
|
, grub-devices ? [ "nodev" ]
|
|
|
|
, efi ? true
|
|
|
|
, enableOCR ? false
|
2022-10-29 11:19:29 +00:00
|
|
|
, testMode ? "direct" # can be one of direct module cli
|
2022-11-10 08:46:06 +00:00
|
|
|
, testBoot ? true # if we actually want to test booting or just create/mount
|
2022-08-26 06:41:58 +00:00
|
|
|
}:
|
|
|
|
let
|
|
|
|
lib = pkgs.lib;
|
|
|
|
makeTest' = args:
|
|
|
|
makeTest args {
|
|
|
|
inherit pkgs;
|
|
|
|
inherit (pkgs) system;
|
|
|
|
};
|
2022-09-30 10:55:28 +00:00
|
|
|
disks = [ "/dev/vda" "/dev/vdb" "/dev/vdc" "/dev/vdd" "/dev/vde" "/dev/vdf" ];
|
2022-10-29 11:19:29 +00:00
|
|
|
tsp-create = pkgs.writeScript "create" ((pkgs.callPackage ../. { }).create (import disko-config { disks = builtins.tail disks; inherit lib; }));
|
|
|
|
tsp-mount = pkgs.writeScript "mount" ((pkgs.callPackage ../. { }).mount (import disko-config { disks = builtins.tail disks; inherit lib; }));
|
|
|
|
tsp-config = (pkgs.callPackage ../. { }).config (import disko-config { inherit disks; inherit lib; });
|
|
|
|
num-disks = builtins.length (lib.attrNames (import disko-config { inherit lib; }).disk);
|
2022-09-30 10:55:28 +00:00
|
|
|
installed-system = { modulesPath, ... }: {
|
|
|
|
imports = [
|
2022-10-29 11:19:29 +00:00
|
|
|
(lib.optionalAttrs (testMode == "direct" || testMode == "cli") tsp-config)
|
|
|
|
(lib.optionalAttrs (testMode == "module") {
|
|
|
|
imports = [ ../module.nix ];
|
|
|
|
disko = {
|
|
|
|
enableConfig = true;
|
|
|
|
devices = import disko-config { inherit disks lib; };
|
|
|
|
};
|
|
|
|
})
|
2022-09-30 10:55:28 +00:00
|
|
|
(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;
|
2022-08-26 06:41:58 +00:00
|
|
|
in
|
|
|
|
makeTest' {
|
|
|
|
name = "disko";
|
2022-08-25 21:12:49 +00:00
|
|
|
|
2022-09-30 10:55:28 +00:00
|
|
|
inherit enableOCR;
|
|
|
|
nodes.machine = { config, pkgs, modulesPath, ... }: {
|
|
|
|
imports = [
|
2022-10-29 11:19:29 +00:00
|
|
|
(lib.optionalAttrs (testMode == "module") {
|
|
|
|
imports = [ ../module.nix ];
|
|
|
|
disko = {
|
|
|
|
enableConfig = false;
|
|
|
|
devices = import disko-config { disks = builtins.tail disks; inherit lib; };
|
|
|
|
};
|
|
|
|
})
|
|
|
|
(lib.optionalAttrs (testMode == "cli") {
|
|
|
|
imports = [ (modulesPath + "/installer/cd-dvd/channel.nix") ];
|
|
|
|
system.extraDependencies = [
|
|
|
|
((pkgs.callPackage ../. { }).createScript (import disko-config { disks = builtins.tail disks; inherit lib; }) pkgs)
|
|
|
|
((pkgs.callPackage ../. { }).mountScript (import disko-config { disks = builtins.tail disks; inherit lib; }) pkgs)
|
|
|
|
];
|
|
|
|
})
|
2022-09-30 10:55:28 +00:00
|
|
|
(modulesPath + "/profiles/base.nix")
|
|
|
|
(modulesPath + "/profiles/minimal.nix")
|
|
|
|
extraConfig
|
|
|
|
];
|
2022-08-25 21:12:49 +00:00
|
|
|
|
2022-09-30 10:55:28 +00:00
|
|
|
# speed-up eval
|
|
|
|
documentation.enable = false;
|
2022-08-25 21:12:49 +00:00
|
|
|
|
2022-09-30 10:55:28 +00:00
|
|
|
nix.settings = {
|
|
|
|
substituters = lib.mkForce [];
|
|
|
|
hashed-mirrors = null;
|
|
|
|
connect-timeout = 1;
|
|
|
|
};
|
2022-08-25 21:12:49 +00:00
|
|
|
|
2022-09-30 10:55:28 +00:00
|
|
|
virtualisation.emptyDiskImages = builtins.genList (_: 4096) num-disks;
|
|
|
|
};
|
2022-08-25 21:12:49 +00:00
|
|
|
|
2022-08-26 06:41:58 +00:00
|
|
|
testScript = ''
|
2022-09-30 10:55:28 +00:00
|
|
|
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")
|
2022-10-29 11:19:29 +00:00
|
|
|
${lib.optionalString (testMode == "direct") ''
|
|
|
|
machine.succeed("${tsp-create}")
|
|
|
|
machine.succeed("${tsp-mount}")
|
|
|
|
machine.succeed("${tsp-mount}") # verify that the command is idempotent
|
|
|
|
''}
|
|
|
|
${lib.optionalString (testMode == "module") ''
|
|
|
|
machine.succeed("disko-create")
|
|
|
|
machine.succeed("disko-mount")
|
|
|
|
machine.succeed("disko-mount") # verify that the command is idempotent
|
|
|
|
''}
|
|
|
|
${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("${tsp-create}")
|
|
|
|
machine.succeed("${tsp-mount}")
|
|
|
|
machine.succeed("${tsp-mount}") # verify that the command is idempotent
|
|
|
|
''}
|
2022-09-30 10:55:28 +00:00
|
|
|
|
2022-11-10 08:46:06 +00:00
|
|
|
${lib.optionalString testBoot ''
|
|
|
|
# mount nix-store in /mnt
|
|
|
|
machine.succeed("mkdir -p /mnt/nix/store")
|
|
|
|
machine.succeed("mount --bind /nix/store /mnt/nix/store")
|
2022-09-30 10:55:28 +00:00
|
|
|
|
2022-11-10 08:46:06 +00:00
|
|
|
machine.succeed("nix-store --load-db < ${pkgs.closureInfo {rootPaths = [installedTopLevel];}}/registration")
|
2022-09-30 10:55:28 +00:00
|
|
|
|
2022-11-10 08:46:06 +00:00
|
|
|
# fix "this is not a NixOS installation"
|
|
|
|
machine.succeed("mkdir -p /mnt/etc")
|
|
|
|
machine.succeed("touch /mnt/etc/NIXOS")
|
2022-09-30 10:55:28 +00:00
|
|
|
|
2022-11-10 08:46:06 +00:00
|
|
|
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")
|
|
|
|
''}
|
2022-09-30 10:55:28 +00:00
|
|
|
|
2022-08-26 06:41:58 +00:00
|
|
|
${extraTestScript}
|
|
|
|
'';
|
|
|
|
};
|
2022-08-25 21:12:49 +00:00
|
|
|
}
|