1import ./make-test-python.nix ({ pkgs, ... }: let
2 inherit (import ./ssh-keys.nix pkgs)
3 snakeOilPrivateKey snakeOilPublicKey;
4
5 commonConfig = { pkgs, ... }: {
6 virtualisation.emptyDiskImages = [ 2048 ];
7 boot.supportedFilesystems = [ "zfs" ];
8 environment.systemPackages = [ pkgs.parted ];
9 };
10in {
11 name = "sanoid";
12 meta = with pkgs.lib.maintainers; {
13 maintainers = [ lopsided98 ];
14 };
15
16 nodes = {
17 source = { ... }: {
18 imports = [ commonConfig ];
19 networking.hostId = "daa82e91";
20
21 programs.ssh.extraConfig = ''
22 UserKnownHostsFile=/dev/null
23 StrictHostKeyChecking=no
24 '';
25
26 services.sanoid = {
27 enable = true;
28 templates.test = {
29 hourly = 12;
30 daily = 1;
31 monthly = 1;
32 yearly = 1;
33
34 autosnap = true;
35 };
36 datasets."pool/sanoid".use_template = [ "test" ];
37 datasets."pool/compat".useTemplate = [ "test" ];
38 extraArgs = [ "--verbose" ];
39 };
40
41 services.syncoid = {
42 enable = true;
43 sshKey = "/var/lib/syncoid/id_ecdsa";
44 commands = {
45 # Sync snapshot taken by sanoid
46 "pool/sanoid" = {
47 target = "root@target:pool/sanoid";
48 extraArgs = [ "--no-sync-snap" "--create-bookmark" ];
49 };
50 # Take snapshot and sync
51 "pool/syncoid".target = "root@target:pool/syncoid";
52
53 # Test pool without parent (regression test for https://github.com/NixOS/nixpkgs/pull/180111)
54 "pool".target = "root@target:pool/full-pool";
55
56 # Test backward compatible options (regression test for https://github.com/NixOS/nixpkgs/issues/181561)
57 "pool/compat" = {
58 target = "root@target:pool/compat";
59 extraArgs = [ "--no-sync-snap" ];
60 };
61 };
62 };
63 };
64 target = { ... }: {
65 imports = [ commonConfig ];
66 networking.hostId = "dcf39d36";
67
68 services.openssh.enable = true;
69 users.users.root.openssh.authorizedKeys.keys = [ snakeOilPublicKey ];
70 };
71 };
72
73 testScript = ''
74 source.succeed(
75 "mkdir /mnt",
76 "parted --script /dev/vdb -- mklabel msdos mkpart primary 1024M -1s",
77 "udevadm settle",
78 "zpool create pool -R /mnt /dev/vdb1",
79 "zfs create pool/sanoid",
80 "zfs create pool/compat",
81 "zfs create pool/syncoid",
82 "udevadm settle",
83 )
84 target.succeed(
85 "mkdir /mnt",
86 "parted --script /dev/vdb -- mklabel msdos mkpart primary 1024M -1s",
87 "udevadm settle",
88 "zpool create pool -R /mnt /dev/vdb1",
89 "udevadm settle",
90 )
91
92 source.succeed(
93 "mkdir -m 700 -p /var/lib/syncoid",
94 "cat '${snakeOilPrivateKey}' > /var/lib/syncoid/id_ecdsa",
95 "chmod 600 /var/lib/syncoid/id_ecdsa",
96 "chown -R syncoid:syncoid /var/lib/syncoid/",
97 )
98
99 assert len(source.succeed("zfs allow pool")) == 0, "Pool shouldn't have delegated permissions set before snapshotting"
100 assert len(source.succeed("zfs allow pool/sanoid")) == 0, "Sanoid dataset shouldn't have delegated permissions set before snapshotting"
101 assert len(source.succeed("zfs allow pool/syncoid")) == 0, "Syncoid dataset shouldn't have delegated permissions set before snapshotting"
102
103 # Take snapshot with sanoid
104 source.succeed("touch /mnt/pool/sanoid/test.txt")
105 source.succeed("touch /mnt/pool/compat/test.txt")
106 source.systemctl("start --wait sanoid.service")
107
108 assert len(source.succeed("zfs allow pool")) == 0, "Pool shouldn't have delegated permissions set after snapshotting"
109 assert len(source.succeed("zfs allow pool/sanoid")) == 0, "Sanoid dataset shouldn't have delegated permissions set after snapshotting"
110 assert len(source.succeed("zfs allow pool/syncoid")) == 0, "Syncoid dataset shouldn't have delegated permissions set after snapshotting"
111
112 # Sync snapshots
113 target.wait_for_open_port(22)
114 source.succeed("touch /mnt/pool/syncoid/test.txt")
115 source.systemctl("start --wait syncoid-pool-sanoid.service")
116 target.succeed("cat /mnt/pool/sanoid/test.txt")
117 source.systemctl("start --wait syncoid-pool-syncoid.service")
118 target.succeed("cat /mnt/pool/syncoid/test.txt")
119
120 source.systemctl("start --wait syncoid-pool.service")
121 target.succeed("[[ -d /mnt/pool/full-pool/syncoid ]]")
122
123 source.systemctl("start --wait syncoid-pool-compat.service")
124 target.succeed("cat /mnt/pool/compat/test.txt")
125
126 assert len(source.succeed("zfs allow pool")) == 0, "Pool shouldn't have delegated permissions set after syncing snapshots"
127 assert len(source.succeed("zfs allow pool/sanoid")) == 0, "Sanoid dataset shouldn't have delegated permissions set after syncing snapshots"
128 assert len(source.succeed("zfs allow pool/syncoid")) == 0, "Syncoid dataset shouldn't have delegated permissions set after syncing snapshots"
129 '';
130})