| 1 | { pkgs }: |
| 2 | pkgs.testers.runNixOSTest { |
| 3 | name = "knife-linux-storage"; |
| 4 | nodes.machine = { lib, ... }: { |
| 5 | imports = [ ../knife-linux.nix ]; |
| 6 | virtualisation = { memorySize = 2048; cores = 2; emptyDiskImages = [ 2048 2048 ]; }; |
| 7 | networking.hostId = "a1b2c3d4"; |
| 8 | boot.supportedFilesystems = [ "zfs" ]; |
| 9 | boot.zfs.forceImportRoot = false; |
| 10 | users.groups.chloe.gid = 3000; |
| 11 | users.users.clo = { isSystemUser = true; uid = 3000; group = "chloe"; }; |
| 12 | environment.systemPackages = [ pkgs.zfs pkgs.util-linux pkgs.rsync pkgs.acl pkgs.attr ]; |
| 13 | systemd.services.knife-linux.wantedBy = lib.mkForce [ ]; |
| 14 | systemd.services.knife-linux-storage = { |
| 15 | requires = lib.mkForce [ ]; |
| 16 | after = lib.mkForce [ ]; |
| 17 | serviceConfig = { |
| 18 | LoadCredentialEncrypted = lib.mkForce [ ]; |
| 19 | LoadCredential = "zfs-clover:/run/test-key"; |
| 20 | }; |
| 21 | }; |
| 22 | }; |
| 23 | testScript = '' |
| 24 | start_all() |
| 25 | machine.succeed("head -c 32 /dev/urandom | od -An -v -tx1 | tr -d ' \\n' > /run/test-key; chmod 600 /run/test-key") |
| 26 | machine.succeed("zpool create -O mountpoint=none globe /dev/vdb") |
| 27 | machine.succeed("zpool create -O mountpoint=none work /dev/vdc") |
| 28 | machine.succeed("zfs create -o encryption=aes-256-gcm -o keyformat=hex -o keylocation=file:///run/test-key -o mountpoint=/source globe/clover") |
| 29 | machine.succeed("zfs create -o mountpoint=/srv/knife-linux -o casesensitivity=sensitive -o acltype=posix -o xattr=sa globe/clover/knife-linux") |
| 30 | machine.succeed("mkdir -p /srv/knife-linux/home/.knife/userdata /srv/knife-linux/home/.local/bin; echo initial > /srv/knife-linux/home/.knife/userdata/state; echo launcher > /srv/knife-linux/home/.local/bin/knife; echo excluded > /srv/knife-linux/home/excluded") |
| 31 | machine.succeed("chown 3000:3000 /srv/knife-linux/home/.knife/userdata/state; setfacl -m u:3000:rw /srv/knife-linux/home/.knife/userdata/state; setfattr -n user.fixture -v retained /srv/knife-linux/home/.knife/userdata/state") |
| 32 | machine.succeed("zfs create -o encryption=aes-256-gcm -o keyformat=hex -o keylocation=file:///run/test-key -o mountpoint=/target -o casesensitivity=sensitive -o acltype=posix -o xattr=sa work/knife-linux") |
| 33 | machine.succeed("mkdir -p /target/home/.local/bin; rsync -aAX --numeric-ids --no-specials /srv/knife-linux/home/.knife /target/home/; rsync -aAX --numeric-ids --no-specials /srv/knife-linux/home/.local/bin/knife /target/home/.local/bin/") |
| 34 | machine.succeed("echo final > /srv/knife-linux/home/.knife/userdata/state; rsync -aAX --numeric-ids --no-specials /srv/knife-linux/home/.knife /target/home/") |
| 35 | machine.succeed("zfs unmount globe/clover/knife-linux; zfs set mountpoint=none globe/clover/knife-linux; zfs unmount work/knife-linux; zfs unload-key work/knife-linux; zfs set mountpoint=/srv/knife-linux work/knife-linux") |
| 36 | assert machine.succeed("zfs get -H -o value encryptionroot work/knife-linux").strip() == "work/knife-linux" |
| 37 | machine.succeed("systemctl start knife-linux-storage.service") |
| 38 | assert machine.succeed("cat /srv/knife-linux/home/.knife/userdata/state").strip() == "final" |
| 39 | assert machine.succeed("stat -c '%u:%g' /srv/knife-linux/home/.knife/userdata/state").strip() == "3000:3000" |
| 40 | assert machine.succeed("getfattr -n user.fixture --only-values /srv/knife-linux/home/.knife/userdata/state").strip() == "retained" |
| 41 | assert "user:clo:rw-" in machine.succeed("getfacl /srv/knife-linux/home/.knife/userdata/state") |
| 42 | machine.fail("test -e /srv/knife-linux/home/excluded") |
| 43 | assert machine.succeed("findmnt -n -o SOURCE --mountpoint /srv/knife-linux").strip() == "work/knife-linux" |
| 44 | machine.succeed("systemctl stop knife-linux-storage.service; zfs unmount work/knife-linux; zfs unload-key work/knife-linux") |
| 45 | machine.succeed("systemctl start knife-linux-storage.service") |
| 46 | assert machine.succeed("cat /srv/knife-linux/home/.knife/userdata/state").strip() == "final" |
| 47 | machine.succeed("systemctl stop knife-linux-storage.service; zfs unmount work/knife-linux; zfs set mountpoint=/wrong work/knife-linux") |
| 48 | machine.fail("systemctl start knife-linux-storage.service") |
| 49 | ''; |
| 50 | } |