phase 2: promote services/, tests/, patches/, lib/, scripts/
This commit is contained in:
153
tests/zfs.nix
Normal file
153
tests/zfs.nix
Normal file
@@ -0,0 +1,153 @@
|
||||
{
|
||||
config,
|
||||
lib,
|
||||
pkgs,
|
||||
inputs,
|
||||
...
|
||||
}:
|
||||
let
|
||||
# Create pkgs with ensureZfsMounts overlay
|
||||
testPkgs = pkgs.appendOverlays [ (import ../modules/overlays.nix) ];
|
||||
in
|
||||
testPkgs.testers.runNixOSTest {
|
||||
name = "zfs test";
|
||||
|
||||
nodes.machine =
|
||||
{ pkgs, ... }:
|
||||
{
|
||||
imports = [
|
||||
# Test valid paths within zpool
|
||||
(lib.serviceMountWithZpool "test-service" "rpool" [ "/mnt/rpool_data" ])
|
||||
|
||||
# Test service with paths outside zpool (should fail assertion)
|
||||
(lib.serviceMountWithZpool "invalid-service" "rpool2" [ "/mnt/rpool_data" ])
|
||||
|
||||
# Test multi-command logic: service with multiple serviceMountWithZpool calls
|
||||
(lib.serviceMountWithZpool "multi-service" "rpool" [ "/mnt/rpool_data" ])
|
||||
(lib.serviceMountWithZpool "multi-service" "rpool2" [ "/mnt/rpool2_data" ])
|
||||
|
||||
# Test multi-command logic: service with multiple serviceMountWithZpool calls
|
||||
# BUT this one should fail as `/mnt/rpool_moar_data` is not on rpool2
|
||||
(lib.serviceMountWithZpool "multi-service-fail" "rpool" [ "/mnt/rpool_data" ])
|
||||
(lib.serviceMountWithZpool "multi-service-fail" "rpool2" [ "/mnt/rpool_moar_data" ])
|
||||
];
|
||||
|
||||
virtualisation = {
|
||||
emptyDiskImages = [
|
||||
4096
|
||||
4096
|
||||
];
|
||||
# Add this to avoid ZFS hanging issues
|
||||
additionalPaths = [ pkgs.zfs ];
|
||||
};
|
||||
networking.hostId = "deadbeef";
|
||||
boot.kernelPackages = config.boot.kernelPackages;
|
||||
boot.zfs.package = config.boot.zfs.package;
|
||||
boot.supportedFilesystems = [ "zfs" ];
|
||||
|
||||
environment.systemPackages = with pkgs; [
|
||||
parted
|
||||
ensureZfsMounts
|
||||
];
|
||||
|
||||
systemd.services."test-service" = {
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
RemainAfterExit = true;
|
||||
ExecStart = lib.getExe pkgs.bash;
|
||||
};
|
||||
};
|
||||
|
||||
systemd.services."invalid-service" = {
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
RemainAfterExit = true;
|
||||
ExecStart = lib.getExe pkgs.bash;
|
||||
};
|
||||
};
|
||||
|
||||
systemd.services."multi-service" = {
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
RemainAfterExit = true;
|
||||
ExecStart = lib.getExe pkgs.bash;
|
||||
};
|
||||
};
|
||||
|
||||
systemd.services."multi-service-fail" = {
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
RemainAfterExit = true;
|
||||
ExecStart = lib.getExe pkgs.bash;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
testScript = ''
|
||||
start_all()
|
||||
machine.wait_for_unit("multi-user.target")
|
||||
|
||||
# Setup ZFS pool
|
||||
machine.succeed(
|
||||
"parted --script /dev/vdb mklabel msdos",
|
||||
"parted --script /dev/vdb -- mkpart primary 1024M -1s",
|
||||
"zpool create rpool /dev/vdb1"
|
||||
)
|
||||
|
||||
# Setup ZFS pool 2
|
||||
machine.succeed(
|
||||
"parted --script /dev/vdc mklabel msdos",
|
||||
"parted --script /dev/vdc -- mkpart primary 1024M -1s",
|
||||
"zpool create rpool2 /dev/vdc1"
|
||||
)
|
||||
|
||||
machine.succeed("zfs create -o mountpoint=/mnt/rpool_data rpool/data")
|
||||
|
||||
machine.succeed("zfs create -o mountpoint=/mnt/rpool2_data rpool2/data")
|
||||
|
||||
machine.succeed("zfs create -o mountpoint=/mnt/rpool_moar_data rpool/moar_data")
|
||||
|
||||
# Test that valid service starts successfully
|
||||
machine.succeed("systemctl start test-service")
|
||||
|
||||
# Manually test our validation logic by checking the debug output
|
||||
zfs_output = machine.succeed("zfs list -H -o name,mountpoint")
|
||||
print("ZFS LIST OUTPUT:")
|
||||
print(zfs_output)
|
||||
|
||||
dataset = machine.succeed("zfs list -H -o name,mountpoint | awk '/\\/mnt\\/rpool_data/ { print $1 }'")
|
||||
print("DATASET FOR /mnt/rpool_data:")
|
||||
print(dataset)
|
||||
|
||||
# Test that invalid-service mount service fails validation
|
||||
machine.fail("systemctl start invalid-service.service")
|
||||
|
||||
# Check the journal for our detailed validation error message
|
||||
journal_output = machine.succeed("journalctl -u invalid-service-mounts.service --no-pager")
|
||||
print("JOURNAL OUTPUT:")
|
||||
print(journal_output)
|
||||
|
||||
# Verify our validation error is in the journal using Python string matching
|
||||
assert "ERROR: ZFS pool mismatch for /mnt/rpool_data" in journal_output
|
||||
assert "Expected pool: rpool2" in journal_output
|
||||
assert "Actual pool: rpool" in journal_output
|
||||
|
||||
|
||||
# Test that invalid-service mount service fails validation
|
||||
machine.fail("systemctl start multi-service-fail.service")
|
||||
|
||||
# Check the journal for our detailed validation error message
|
||||
journal_output = machine.succeed("journalctl -u multi-service-fail-mounts.service --no-pager")
|
||||
print("JOURNAL OUTPUT:")
|
||||
print(journal_output)
|
||||
|
||||
# Verify our validation error is in the journal using Python string matching
|
||||
assert "ERROR: ZFS pool mismatch for /mnt/rpool_moar_data" in journal_output, "no zfs pool mismatch found (1)"
|
||||
assert "Expected pool: rpool2" in journal_output, "no zfs pool mismatch found (2)"
|
||||
assert "Actual pool: rpool" in journal_output, "no zfs pool mismatch found (3)"
|
||||
|
||||
|
||||
machine.succeed("systemctl start multi-service")
|
||||
machine.succeed("systemctl is-active multi-service-mounts.service")
|
||||
'';
|
||||
}
|
||||
Reference in New Issue
Block a user