From 1ac0e76b15a3b13c83d25a78b32dee4070a584b7 Mon Sep 17 00:00:00 2001 From: lassulus Date: Thu, 25 Aug 2022 23:12:49 +0200 Subject: tests: add lib, autoimport tests --- tests/btrfs-subvolumes.nix | 37 ++++-------------------------------- tests/default.nix | 17 ++++++++++++----- tests/lib.nix | 47 ++++++++++++++++++++++++++++++++++++++++++++++ tests/luks-lvm.nix | 37 ++++-------------------------------- tests/lvm-raid.nix | 41 +++++++--------------------------------- tests/mdadm.nix | 37 ++++-------------------------------- tests/zfs.nix | 38 ++++--------------------------------- 7 files changed, 82 insertions(+), 172 deletions(-) create mode 100644 tests/lib.nix diff --git a/tests/btrfs-subvolumes.nix b/tests/btrfs-subvolumes.nix index 6527329..d901b0e 100644 --- a/tests/btrfs-subvolumes.nix +++ b/tests/btrfs-subvolumes.nix @@ -1,38 +1,9 @@ -{ makeTest ? import -, pkgs ? (import {}) +{ pkgs ? (import {}) +, makeDiskoTest ? (pkgs.callPackage ./lib.nix {}).makeDiskoTest }: -let - makeTest' = args: - makeTest args { - inherit pkgs; - inherit (pkgs) system; - }; +makeDiskoTest { disko-config = import ../example/btrfs-subvolumes.nix; - tsp-create = pkgs.writeScript "create" ((pkgs.callPackage ../. {}).create disko-config); - tsp-mount = pkgs.writeScript "mount" ((pkgs.callPackage ../. {}).mount disko-config); -in makeTest' { - name = "disko"; - - nodes.machine = - { config, pkgs, modulesPath, ... }: - - { - imports = [ - (modulesPath + "/profiles/installation-device.nix") - (modulesPath + "/profiles/base.nix") - ]; - - # speed-up eval - documentation.enable = false; - - virtualisation.emptyDiskImages = [ 512 ]; - }; - - 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 + extraTestScript = '' machine.succeed("test -e /mnt/test"); machine.succeed("btrfs subvolume list /mnt | grep -qs 'path test$'"); ''; diff --git a/tests/default.nix b/tests/default.nix index 3b24ba6..14b7a19 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -1,8 +1,15 @@ { makeTest ? import , pkgs ? (import {}) }@args: -{ - luks-lvm = import ./luks-lvm.nix args; - mdadm = import ./mdadm.nix args; - zfs = import ./zfs.nix args; -} +let + lib = pkgs.lib; + makeDiskoTest = (pkgs.callPackage ./lib.nix { inherit makeTest; }).makeDiskoTest; + allTestFilenames = + builtins.map (lib.removeSuffix ".nix") ( + builtins.filter + (x: lib.hasSuffix ".nix" x && x != "default.nix" && x != "lib.nix") + (lib.attrNames (builtins.readDir ./.)) + ); + + allTests = lib.genAttrs (allTestFilenames) (test: import (./. + "/${test}.nix") { inherit makeDiskoTest; }); +in allTests diff --git a/tests/lib.nix b/tests/lib.nix new file mode 100644 index 0000000..b98be69 --- /dev/null +++ b/tests/lib.nix @@ -0,0 +1,47 @@ +{ pkgs ? (import {}) +, makeTest ? import +, ... +}: +{ + makeDiskoTest = { + disko-config, + extraTestScript, + extraConfig ? {} + }: + let + lib = pkgs.lib; + makeTest' = args: + makeTest args { + 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 (builtins.filter (x: builtins.match "vd." x == []) (lib.attrNames disko-config.content)); + in makeTest' { + name = "disko"; + + nodes.machine = + { config, pkgs, modulesPath, ... }: + + { + imports = [ + (modulesPath + "/profiles/installation-device.nix") + (modulesPath + "/profiles/base.nix") + ]; + + # speed-up eval + documentation.enable = false; + + virtualisation.emptyDiskImages = builtins.genList (_: 512) num-disks; + } // extraConfig; + + 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 + ${extraTestScript} + ''; + }; +} diff --git a/tests/luks-lvm.nix b/tests/luks-lvm.nix index 3e2c5f2..305bff1 100644 --- a/tests/luks-lvm.nix +++ b/tests/luks-lvm.nix @@ -1,38 +1,9 @@ -{ makeTest ? import -, pkgs ? (import {}) +{ pkgs ? (import {}) +, makeDiskoTest ? (pkgs.callPackage ./lib.nix {}).makeDiskoTest }: -let - makeTest' = args: - makeTest args { - inherit pkgs; - inherit (pkgs) system; - }; +makeDiskoTest { disko-config = import ../example/luks-lvm.nix; - tsp-create = pkgs.writeScript "create" ((pkgs.callPackage ../. {}).create disko-config); - tsp-mount = pkgs.writeScript "mount" ((pkgs.callPackage ../. {}).mount disko-config); -in makeTest' { - name = "disko"; - - nodes.machine = - { config, pkgs, modulesPath, ... }: - - { - imports = [ - (modulesPath + "/profiles/installation-device.nix") - (modulesPath + "/profiles/base.nix") - ]; - - # speed-up eval - documentation.enable = false; - - virtualisation.emptyDiskImages = [ 512 ]; - }; - - 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 + extraTestScript = '' machine.succeed("cryptsetup isLuks /dev/vdb2"); machine.succeed("grep -qs '/mnt/home' /proc/mounts"); ''; diff --git a/tests/lvm-raid.nix b/tests/lvm-raid.nix index 5165645..d7c4096 100644 --- a/tests/lvm-raid.nix +++ b/tests/lvm-raid.nix @@ -1,39 +1,12 @@ -{ makeTest ? import -, pkgs ? (import {}) +{ pkgs ? (import {}) +, makeDiskoTest ? (pkgs.callPackage ./lib.nix {}).makeDiskoTest }: -let - makeTest' = args: - makeTest args { - inherit pkgs; - inherit (pkgs) system; - }; +makeDiskoTest { disko-config = import ../example/lvm-raid.nix; - tsp-create = pkgs.writeScript "create" ((pkgs.callPackage ../. {}).create disko-config); - tsp-mount = pkgs.writeScript "mount" ((pkgs.callPackage ../. {}).mount disko-config); -in makeTest' { - name = "disko"; - - nodes.machine = - { config, pkgs, modulesPath, ... }: - - { - imports = [ - (modulesPath + "/profiles/installation-device.nix") - (modulesPath + "/profiles/base.nix") - ]; - - # speed-up eval - documentation.enable = false; - - virtualisation.emptyDiskImages = [ 512 512 ]; - boot.kernelModules = [ "dm-raid" "dm-mirror" ]; - }; - - 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 + extraTestScript = '' machine.succeed("grep -qs '/mnt/home' /proc/mounts"); ''; + extraConfig = { + boot.kernelModules = [ "dm-raid" "dm-mirror" ]; + }; } diff --git a/tests/mdadm.nix b/tests/mdadm.nix index 37465e4..72f2faf 100644 --- a/tests/mdadm.nix +++ b/tests/mdadm.nix @@ -1,38 +1,9 @@ -{ makeTest ? import -, pkgs ? (import {}) +{ pkgs ? (import {}) +, makeDiskoTest ? (pkgs.callPackage ./lib.nix {}).makeDiskoTest }: -let - makeTest' = args: - makeTest args { - inherit pkgs; - inherit (pkgs) system; - }; +makeDiskoTest { disko-config = import ../example/mdadm.nix; - tsp-create = pkgs.writeScript "create" ((pkgs.callPackage ../. {}).create disko-config); - tsp-mount = pkgs.writeScript "mount" ((pkgs.callPackage ../. {}).mount disko-config); -in makeTest' { - name = "disko"; - - nodes.machine = - { config, pkgs, modulesPath, ... }: - - { - imports = [ - (modulesPath + "/profiles/installation-device.nix") - (modulesPath + "/profiles/base.nix") - ]; - - # speed-up eval - documentation.enable = false; - - virtualisation.emptyDiskImages = [ 512 512 ]; - }; - - 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 + extraTestScript = '' machine.succeed("test -b /dev/md/raid1"); machine.succeed("grep -qs '/mnt/raid' /proc/mounts"); ''; diff --git a/tests/zfs.nix b/tests/zfs.nix index bc807bc..3c8216c 100644 --- a/tests/zfs.nix +++ b/tests/zfs.nix @@ -1,40 +1,10 @@ -{ makeTest ? import -, pkgs ? (import {}) +{ pkgs ? (import {}) +, makeDiskoTest ? (pkgs.callPackage ./lib.nix {}).makeDiskoTest }: -let - makeTest' = args: - makeTest args { - inherit pkgs; - inherit (pkgs) system; - }; +makeDiskoTest { disko-config = import ../example/zfs.nix; - tsp-create = pkgs.writeScript "create" ((pkgs.callPackage ../. {}).create disko-config); - tsp-mount = pkgs.writeScript "mount" ((pkgs.callPackage ../. {}).mount disko-config); -in makeTest' { - name = "disko"; - - nodes.machine = - { config, pkgs, modulesPath, ... }: - - { - imports = [ - (modulesPath + "/profiles/installation-device.nix") - (modulesPath + "/profiles/base.nix") - ]; - - # speed-up eval - documentation.enable = false; - - virtualisation.emptyDiskImages = [ 512 512 ]; - }; - - 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 + extraTestScript = '' machine.succeed("test -b /dev/zvol/zroot/zfs_testvolume"); machine.succeed("grep -qs '/mnt/ext4onzfs' /proc/mounts"); ''; } - -- cgit v1.2.3