add testing infra

This commit is contained in:
2025-08-07 19:17:16 -07:00
parent 207722acb2
commit 2875d29293
5 changed files with 131 additions and 25 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/result

View File

@@ -220,5 +220,10 @@
path = deploy-rs.lib.x86_64-linux.activate.nixos self.nixosConfigurations.muffin; path = deploy-rs.lib.x86_64-linux.activate.nixos self.nixosConfigurations.muffin;
}; };
}; };
tests = import ./tests/tests.nix {
inherit pkgs lib;
config = self.nixosConfigurations.muffin.config;
};
}; };
} }

54
lib.nix
View File

@@ -9,34 +9,38 @@ inputs.nixpkgs.lib.extend (
lib = prev; lib = prev;
in in
{ {
ensureZfsMounts = # Should probably be moved to a package later instead of this
dirs: ensureZfsMounts = pkgs.writeShellApplication {
pkgs.writeShellApplication { name = "zfsEnsureMounted";
name = "zfslistmounted"; runtimeInputs = with pkgs; [
runtimeInputs = with pkgs; [ zfs
zfs gnugrep
gnugrep gawk
gawk coreutils
coreutils ];
];
text = '' text = ''
#!/bin/sh #!/bin/sh -x
TARGETS=$(echo "${lib.strings.concatStringsSep "\n" dirs}" | sort | uniq) if [[ "$#" -eq "0" ]]; then
MOUNTED=$(zfs list -o mountpoint,mounted -H | awk '$2 == "yes" {print $1}' | sort | uniq) echo "no arguments passed"
NUM_MATCHED=$(echo "$MOUNTED" | grep -Ec '${lib.strings.concatStringsSep "\|" dirs}') exit 1
if [[ "$NUM_MATCHED" -eq "${toString (lib.length dirs)}" ]]; then fi
exit 0
fi
FOUND=$(printf "%s\n%s" "$TARGETS" "$MOUNTED" | sort | uniq -c | awk '$1 == "2" {print $2}' | sort) TARGETS=$(echo "$@" | sort | uniq)
MISSING=$(printf "%s\n%s" "$FOUND" "$TARGETS" | sort | uniq -u | sort) MOUNTED=$(zfs list -o mountpoint,mounted -H | awk '$2 == "yes" {print $1}' | sort | uniq)
NUM_MATCHED=$(echo "$MOUNTED" | grep -Ec "$(echo "$@" | tr ' ' '\|')") # does not properly handle paths with strings
if [[ "$NUM_MATCHED" -eq "$#" ]]; then
exit 0
fi
echo "FAILURE, missing: $MISSING" 1>&2 FOUND=$(printf "%s\n%s" "$TARGETS" "$MOUNTED" | sort | uniq -c | awk '$1 == "2" {print $2}' | sort)
exit 1 MISSING=$(printf "%s\n%s" "$FOUND" "$TARGETS" | sort | uniq -u | sort)
'';
}; echo "FAILURE, missing: $MISSING" 1>&2
exit 1
'';
};
serviceMountDeps = serviceName: dirs: { serviceMountDeps = serviceName: dirs: {
systemd.services."${serviceName}_mounts" = { systemd.services."${serviceName}_mounts" = {
@@ -46,7 +50,7 @@ inputs.nixpkgs.lib.extend (
serviceConfig = { serviceConfig = {
Type = "oneshot"; Type = "oneshot";
RemainAfterExit = true; RemainAfterExit = true;
ExecStart = lib.getExe (final.ensureZfsMounts dirs); ExecStart = "${lib.getExe final.ensureZfsMounts} ${lib.strings.concatStringsSep " " dirs}";
}; };
}; };

9
tests/tests.nix Normal file
View File

@@ -0,0 +1,9 @@
{
config,
lib,
pkgs,
...
}:
{
zfsTest = import ./zfs.nix { inherit config lib pkgs; };
}

87
tests/zfs.nix Normal file
View File

@@ -0,0 +1,87 @@
{
config,
lib,
pkgs,
...
}:
pkgs.testers.runNixOSTest {
name = "test of tests";
nodes.machine =
{ pkgs, ... }:
{
imports = [
(lib.serviceMountDeps "foobar" [ "/mnt/foobar_data" ])
(lib.serviceMountDeps "foobarSadge" [
"/mnt/foobar_data"
"/mnt/does_not_exist_lol"
])
];
virtualisation = {
emptyDiskImages = [
4096
];
useBootLoader = true;
useEFIBoot = true;
};
boot.loader.systemd-boot.enable = true;
boot.loader.timeout = 0;
boot.loader.efi.canTouchEfiVariables = true;
networking.hostId = "deadbeef";
boot.kernelPackages = config.boot.kernelPackages;
boot.zfs.package = config.boot.zfs.package;
boot.supportedFilesystems = [ "zfs" ];
environment.systemPackages = [
pkgs.parted
lib.ensureZfsMounts
];
systemd.services.foobar = {
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = "${lib.getExe pkgs.bash} -c \"true\"";
};
};
systemd.services.foobarSadge = {
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = "${lib.getExe pkgs.bash} -c \"true\"";
};
};
};
testScript = ''
start_all()
machine.wait_for_unit("multi-user.target")
machine.succeed(
"parted --script /dev/vdb mklabel msdos",
"parted --script /dev/vdb -- mkpart primary 1024M -1s",
)
machine.fail("zfsEnsureMounted")
machine.fail("zfsEnsureMounted /mnt/test_mountpoint")
machine.succeed("zpool create rpool /dev/vdb1")
machine.succeed("zfs create -o mountpoint=/mnt/test_mountpoint rpool/test")
machine.succeed("zfsEnsureMounted /mnt/test_mountpoint")
machine.fail("zfsEnsureMounted /mnt/does_not_exist_lol")
machine.fail("zfsEnsureMounted /mnt/test_mountpoint /mnt/does_not_exist_lol")
machine.succeed("zfs create -o mountpoint=/mnt/test_mountpoint_dos rpool/test2")
machine.succeed("zfsEnsureMounted /mnt/test_mountpoint /mnt/test_mountpoint_dos")
machine.succeed("zfs create -o mountpoint=/mnt/foobar_data rpool/foobar")
machine.succeed("systemctl start foobar")
machine.fail("systemctl start foobarSadge")
'';
}