]> git.scottworley.com Git - syncthing-autoregister/blame - tests/automatic-enrollment.nix
Follow nix version bump that changed the eval command line interface
[syncthing-autoregister] / tests / automatic-enrollment.nix
CommitLineData
b00b144b
SW
1import <nixpkgs/nixos/tests/make-test-python.nix> ({ pkgs, lib, ... }:
2 let
e16b7079 3 deviceIDFilename = "/tmp/syncthing-auto-register-test-device-ids.nix";
0043dc68 4 vmConfiguration = pkgs.writeText "vm-configuration.nix" ''
b00b144b
SW
5 { pkgs, ... }: {
6 imports = [
0043dc68 7 ${../.}/lib/test-nixos-rebuild-switch-config.nix
b00b144b 8 ${../.}/modules/syncthing-autoregister.nix
b00b144b
SW
9 ];
10 services.syncthing = {
11 enable = true;
12 openDefaultPorts = true;
7fb0951b
SW
13 autoRegister = {
14 enable = true;
e16b7079 15 path = "${deviceIDFilename}";
7fb0951b 16 };
b00b144b 17 };
0043dc68
SW
18
19 # For verifyDeviceIDSet()
b00b144b
SW
20 environment.systemPackages = with pkgs; [ nix ];
21 }
22 '';
0043dc68
SW
23
24 evaluatedVMConfiguration = import <nixos/nixos/lib/eval-config.nix> {
25 system = builtins.currentSystem;
26 modules = [ "${vmConfiguration}" ];
27 };
28
29 # Also include a syncthing configuration with a declarative device id
30 # because using this feature pulls in additional dependencies.
31 evaluatedSyncthingDeclarativeConfiguration = import <nixos/nixos/lib/eval-config.nix> {
32 system = builtins.currentSystem;
33 modules = [
34 ../lib/test-nixos-rebuild-switch-config.nix
35 {
36 services.syncthing = {
37 enable = true;
38 declarative.folders.forDeps.path = "/nope";
39 };
40 }
41 ];
42 };
43
44 initialConfiguration = {
45 imports = [ "${vmConfiguration}" ];
46 virtualisation.memorySize = 2048;
2475b534 47 system.activationScripts.installInitialConfiguration = {
0043dc68 48 text = ''
2475b534 49 mkdir -p /etc/nixos
0043dc68
SW
50 cp "${vmConfiguration}" /etc/nixos/configuration.nix
51 '';
2475b534
SW
52 deps = [ ];
53 };
0043dc68
SW
54 system.extraDependencies = [
55 evaluatedVMConfiguration.config.system.build.toplevel
56 evaluatedSyncthingDeclarativeConfiguration.config.system.build.toplevel
57 ];
58 };
59
97c3aa62
SW
60 configurationWithDeviceIDs =
61 pkgs.writeText "configuration-with-device-ids.nix" ''
62 {
63 imports = [
0043dc68 64 ${vmConfiguration}
e16b7079 65 ${deviceIDFilename}
97c3aa62
SW
66 ];
67 }
68 '';
0043dc68
SW
69
70 configurationWithFolder =
71 pkgs.writeText "configuration-with-folder.nix" ''
72 { config, ... }: {
73 imports = [ ${configurationWithDeviceIDs} ];
74 services.syncthing.declarative.folders.foo = {
75 devices = [ "a" "b" ];
76 path = "''${config.services.syncthing.dataDir}/foo";
77 };
78 }
79 '';
80
b00b144b
SW
81 in {
82
83 name = "syncthing";
84
cefa20b6 85 nodes = {
0043dc68
SW
86 a = initialConfiguration;
87 b = initialConfiguration;
cefa20b6 88 };
b00b144b
SW
89
90 testScript = ''
fc02e299
SW
91 import os
92
93
97c3aa62
SW
94 def setConfig(machine, config):
95 machine.copy_from_host(config, "/etc/nixos/configuration.nix")
96
97
0a262b79 98 def verifyDeviceIDSet(machine, expectedDevice):
4ac49460 99 machine.wait_until_succeeds(
274332f7 100 '(( "$(nix --experimental-features nix-command eval --impure --raw --expr "(import <nixos/nixos> {}).config.services.syncthing.declarative.devices.%s.id" | wc -c)" == 63 ))'
0a262b79
SW
101 % expectedDevice
102 )
103
104
36008f17
SW
105 def getDeviceFile(machine):
106 machine.copy_from_vm("${deviceIDFilename}")
107
108
109 def putDeviceFile(machine):
110 hostDeviceIDFilename = os.path.join(
111 os.environ["out"],
112 os.path.basename("${deviceIDFilename}"),
113 )
114 machine.copy_from_host(
115 hostDeviceIDFilename, "${deviceIDFilename}"
116 )
117 machine.succeed("chown syncthing ${deviceIDFilename}")
118
119
97c3aa62
SW
120 configurationWithDeviceIDs = (
121 "${configurationWithDeviceIDs}"
b00b144b 122 )
0043dc68
SW
123 configurationWithFolder = (
124 "${configurationWithFolder}"
125 )
97c3aa62 126
4ac49460 127 start_all()
cefa20b6
SW
128 a.wait_for_unit("syncthing-autoregister.service")
129 setConfig(a, configurationWithDeviceIDs)
0a262b79 130 verifyDeviceIDSet(a, "a")
36008f17 131 getDeviceFile(a)
fc02e299 132
4ac49460 133 b.wait_for_unit("syncthing-autoregister.service")
36008f17 134 putDeviceFile(b)
fc02e299
SW
135 setConfig(b, configurationWithDeviceIDs)
136 verifyDeviceIDSet(b, "a")
1d5d76e4 137 verifyDeviceIDSet(b, "b")
0043dc68
SW
138
139 getDeviceFile(b)
140 putDeviceFile(a)
141 setConfig(a, configurationWithFolder)
142 setConfig(b, configurationWithFolder)
143 a.succeed("nixos-rebuild switch")
144 b.succeed("nixos-rebuild switch")
145 a.wait_for_file("/var/lib/syncthing/foo")
146 b.wait_for_file("/var/lib/syncthing/foo")
147 a.succeed("echo a2b > /var/lib/syncthing/foo/a2b")
148 b.succeed("echo b2a > /var/lib/syncthing/foo/b2a")
149 a.wait_for_file("/var/lib/syncthing/foo/b2a")
150 b.wait_for_file("/var/lib/syncthing/foo/a2b")
b00b144b
SW
151 '';
152 })