| 1 | { pkgs }: |
| 2 | let |
| 3 | sandbox = builtins.fromJSON (builtins.readFile ../../config/mac-studio-sandbox.json); |
| 4 | in |
| 5 | pkgs.testers.runNixOSTest { |
| 6 | name = "mac-studio-sandbox"; |
| 7 | globalTimeout = 600; |
| 8 | |
| 9 | nodes.server = { |
| 10 | imports = [ ../mac-studio-sandbox.nix ]; |
| 11 | virtualisation = { |
| 12 | memorySize = 3072; |
| 13 | cores = 2; |
| 14 | emptyDiskImages = [ 2048 ]; |
| 15 | }; |
| 16 | networking.hostId = "a1b2c3d4"; |
| 17 | networking.interfaces.eth1.ipv4.addresses = [{ address = "10.0.0.1"; prefixLength = 24; }]; |
| 18 | networking.firewall.allowedTCPPorts = [ 2049 ]; |
| 19 | users.groups.chloe.gid = 3000; |
| 20 | users.users.clo = { isSystemUser = true; uid = 3000; group = "chloe"; }; |
| 21 | boot.supportedFilesystems = [ "zfs" ]; |
| 22 | boot.zfs.forceImportRoot = false; |
| 23 | environment.systemPackages = [ pkgs.python3 pkgs.zfs pkgs.util-linux ]; |
| 24 | systemd.services.sandbox-test-pool = { |
| 25 | before = [ "mac-studio-sandbox.service" ]; |
| 26 | serviceConfig = { Type = "oneshot"; RemainAfterExit = true; }; |
| 27 | script = '' |
| 28 | test "$(${pkgs.util-linux}/bin/blockdev --getsize64 /dev/vdb)" = 2147483648 |
| 29 | umask 077 |
| 30 | head -c 32 /dev/urandom > /run/sandbox-test-key |
| 31 | ${pkgs.zfs}/bin/zpool create -o cachefile=none -O mountpoint=none globe /dev/vdb |
| 32 | ${pkgs.zfs}/bin/zfs create -o encryption=aes-256-gcm -o keyformat=raw \ |
| 33 | -o keylocation=file:///run/sandbox-test-key -o mountpoint=/srv/clover \ |
| 34 | -o casesensitivity=insensitive -o acltype=posixacl globe/clover |
| 35 | ''; |
| 36 | }; |
| 37 | systemd.services.mac-studio-sandbox = { |
| 38 | requires = [ "sandbox-test-pool.service" ]; |
| 39 | after = [ "sandbox-test-pool.service" ]; |
| 40 | }; |
| 41 | }; |
| 42 | |
| 43 | nodes.client = { |
| 44 | virtualisation = { memorySize = 1024; cores = 1; }; |
| 45 | networking.interfaces.eth1.ipv4.addresses = [{ address = "10.0.0.222"; prefixLength = 24; }]; |
| 46 | boot.supportedFilesystems = [ "nfs" ]; |
| 47 | environment.systemPackages = [ pkgs.python3 pkgs.nfs-utils pkgs.util-linux ]; |
| 48 | }; |
| 49 | |
| 50 | testScript = '' |
| 51 | mountpoint = ${builtins.toJSON sandbox.mountPoint} |
| 52 | dataset = ${builtins.toJSON sandbox.dataset} |
| 53 | start_all() |
| 54 | server.wait_for_unit("mac-studio-sandbox.service") |
| 55 | server.wait_for_unit("nfs-server.service") |
| 56 | client.wait_until_succeeds("ip -4 address show dev eth1 | grep -q 10.0.0.222", timeout=20) |
| 57 | |
| 58 | with subtest("fresh encrypted case-sensitive dataset and idempotent setup"): |
| 59 | assert server.succeed(f"zfs get -H -o value encryptionroot {dataset}").strip() == "globe/clover" |
| 60 | assert server.succeed(f"zfs get -H -o value casesensitivity {dataset}").strip() == "sensitive" |
| 61 | assert server.succeed(f"findmnt -n -o SOURCE,FSTYPE --mountpoint {mountpoint}").split() == [dataset, "zfs"] |
| 62 | assert server.succeed(f"stat -c '%u:%g %a' {mountpoint}").strip() == "3000:3000 2770" |
| 63 | server.succeed(f"printf retained > {mountpoint}/retained; systemctl restart mac-studio-sandbox.service") |
| 64 | assert server.succeed(f"cat {mountpoint}/retained") == "retained" |
| 65 | server.wait_for_unit("nfs-server.service") |
| 66 | server.succeed("exportfs -v") |
| 67 | versions = server.succeed("cat /proc/fs/nfsd/versions").split() |
| 68 | print(f"Enabled NFS versions: {versions}") |
| 69 | assert "-3" in versions and "+4" in versions and "-4.0" not in versions |
| 70 | |
| 71 | with subtest("shared guard accepts the dataset bind and rejects a covering wrong mount"): |
| 72 | server.succeed(f"mkdir -p /mnt/guard-bind /mnt/guard-wrong; mount --bind {mountpoint} /mnt/guard-bind") |
| 73 | server.succeed(f"${pkgs.runtimeShell} ${../../service/samba/check-mount.sh} /mnt/guard-bind {dataset} true") |
| 74 | server.succeed("mount -t tmpfs -o size=1m tmpfs /mnt/guard-wrong; mount --bind /mnt/guard-wrong /mnt/guard-bind") |
| 75 | server.fail(f"${pkgs.runtimeShell} ${../../service/samba/check-mount.sh} /mnt/guard-bind {dataset} true") |
| 76 | server.succeed("umount /mnt/guard-bind; umount /mnt/guard-bind; umount /mnt/guard-wrong") |
| 77 | |
| 78 | with subtest("NFS ordinary writes, fsync, rename, execute bits and case distinctions"): |
| 79 | client.succeed("mkdir -p /mnt/sandbox; timeout 30 mount -t nfs -o vers=4.0,proto=tcp,hard,noatime 10.0.0.1:/ /mnt/sandbox") |
| 80 | client.succeed(r"""python3 - <<'PY' |
| 81 | from pathlib import Path |
| 82 | import os |
| 83 | p = Path('/mnt/sandbox') |
| 84 | with (p / 'pending').open('wb') as f: |
| 85 | f.write(b'synthetic native NFS bytes\n') |
| 86 | f.flush() |
| 87 | os.fsync(f.fileno()) |
| 88 | (p / 'pending').rename(p / 'finished') |
| 89 | assert (p / 'finished').read_bytes() == b'synthetic native NFS bytes\n' |
| 90 | (p / 'A').write_text('uppercase') |
| 91 | (p / 'a').write_text('lowercase') |
| 92 | assert (p / 'A').read_text() == 'uppercase' |
| 93 | assert (p / 'a').read_text() == 'lowercase' |
| 94 | (p / 'run').write_text('#!/bin/sh\nprintf sandbox-exec') |
| 95 | (p / 'run').chmod(0o755) |
| 96 | PY""") |
| 97 | assert client.succeed("/mnt/sandbox/run") == "sandbox-exec" |
| 98 | assert server.succeed(f"stat -c '%u:%g' {mountpoint}/finished").strip() == "3000:3000" |
| 99 | |
| 100 | with subtest("server observes client POSIX lock and release"): |
| 101 | client.succeed(r"""python3 - <<'PY' >/run/sandbox-lock.log 2>&1 & |
| 102 | from pathlib import Path |
| 103 | import fcntl, time |
| 104 | with open('/mnt/sandbox/locked', 'w') as f: |
| 105 | fcntl.lockf(f, fcntl.LOCK_EX) |
| 106 | Path('/run/sandbox-lock-ready').touch() |
| 107 | time.sleep(20) |
| 108 | PY |
| 109 | echo $! > /run/sandbox-lock-pid |
| 110 | """) |
| 111 | client.wait_until_succeeds("test -f /run/sandbox-lock-ready", timeout=15) |
| 112 | server.succeed(f"""python3 - <<'PY' |
| 113 | import fcntl |
| 114 | with open('{mountpoint}/locked', 'r+') as f: |
| 115 | try: |
| 116 | fcntl.lockf(f, fcntl.LOCK_EX | fcntl.LOCK_NB) |
| 117 | except BlockingIOError: |
| 118 | pass |
| 119 | else: |
| 120 | raise AssertionError('NFS client lock was not enforced on server') |
| 121 | PY""") |
| 122 | client.succeed("kill -TERM $(cat /run/sandbox-lock-pid)") |
| 123 | server.wait_until_succeeds(f"python3 -c \"import fcntl; f=open('{mountpoint}/locked','r+'); fcntl.lockf(f,fcntl.LOCK_EX|fcntl.LOCK_NB)\"", timeout=15) |
| 124 | |
| 125 | with subtest("NFS refuses a missing or wrong mounted dataset"): |
| 126 | client.succeed("timeout 30 umount /mnt/sandbox") |
| 127 | server.succeed(f"systemctl stop nfs-server.service nfs-mountd.service; zfs unmount {dataset}") |
| 128 | server.fail("systemctl start nfs-mountd.service") |
| 129 | server.fail("systemctl start nfs-server.service") |
| 130 | server.succeed("systemctl reset-failed nfs-server.service nfs-mountd.service") |
| 131 | server.succeed(f"printf untouched > {mountpoint}/placeholder") |
| 132 | server.fail("systemctl restart mac-studio-sandbox.service") |
| 133 | assert server.succeed(f"cat {mountpoint}/placeholder") == "untouched" |
| 134 | server.succeed(f"rm {mountpoint}/placeholder; systemctl reset-failed mac-studio-sandbox.service") |
| 135 | server.succeed(f"zfs create -o mountpoint={mountpoint} globe/wrong") |
| 136 | server.succeed(f"printf untouched > {mountpoint}/wrong-sentinel") |
| 137 | server.fail("systemctl start nfs-mountd.service") |
| 138 | server.fail("systemctl start nfs-server.service") |
| 139 | server.fail("systemctl restart mac-studio-sandbox.service") |
| 140 | assert server.succeed(f"findmnt -n -o SOURCE --mountpoint {mountpoint}").strip() == "globe/wrong" |
| 141 | assert server.succeed(f"cat {mountpoint}/wrong-sentinel") == "untouched" |
| 142 | server.succeed(f"zfs unmount globe/wrong; zfs set mountpoint=none globe/wrong; zfs mount {dataset}") |
| 143 | server.succeed("systemctl reset-failed; systemctl start mac-studio-sandbox.service nfs-server.service") |
| 144 | client.succeed("timeout 30 mount -t nfs -o vers=4.0,proto=tcp,hard,noatime 10.0.0.1:/ /mnt/sandbox") |
| 145 | assert client.succeed("cat /mnt/sandbox/finished") == "synthetic native NFS bytes\n" |
| 146 | client.succeed("timeout 30 umount /mnt/sandbox") |
| 147 | server.succeed("systemctl stop nfs-server.service nfs-mountd.service; zpool status -x") |
| 148 | ''; |
| 149 | } |