]> git.scottworley.com Git - syncthing-autoregister/blame_incremental - tests/automatic-enrollment.nix
Follow nixfmt rename
[syncthing-autoregister] / tests / automatic-enrollment.nix
... / ...
CommitLineData
1import <nixpkgs/nixos/tests/make-test-python.nix> ({ pkgs, lib, ... }:
2 let
3 deviceIDFilename = "/tmp/syncthing-auto-register-test-device-ids.nix";
4 vmConfiguration = pkgs.writeText "vm-configuration.nix" ''
5 { pkgs, ... }: {
6 imports = [
7 ${../.}/lib/test-nixos-rebuild-switch-config.nix
8 ${../.}/modules/syncthing-autoregister.nix
9 ];
10 services.syncthing = {
11 enable = true;
12 openDefaultPorts = true;
13 autoRegister = {
14 enable = true;
15 path = "${deviceIDFilename}";
16 };
17 };
18
19 # For verifyDeviceIDSet()
20 environment.systemPackages = with pkgs; [ nix ];
21 }
22 '';
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 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 folders.forDeps.path = "/nope";
39 };
40 }
41 ];
42 };
43
44 initialConfiguration = {
45 imports = [ "${vmConfiguration}" ];
46 virtualisation.memorySize = 2048;
47 system.activationScripts.installInitialConfiguration = {
48 text = ''
49 mkdir -p /etc/nixos
50 cp "${vmConfiguration}" /etc/nixos/configuration.nix
51 '';
52 deps = [ ];
53 };
54 system.extraDependencies = [
55 evaluatedVMConfiguration.config.system.build.toplevel
56 evaluatedSyncthingDeclarativeConfiguration.config.system.build.toplevel
57 ];
58 };
59
60 configurationWithDeviceIDs =
61 pkgs.writeText "configuration-with-device-ids.nix" ''
62 {
63 imports = [
64 ${vmConfiguration}
65 ${deviceIDFilename}
66 ];
67 }
68 '';
69
70 configurationWithFolder =
71 pkgs.writeText "configuration-with-folder.nix" ''
72 { config, ... }: {
73 imports = [ ${configurationWithDeviceIDs} ];
74 services.syncthing.folders.foo = {
75 devices = [ "a" "b" ];
76 path = "''${config.services.syncthing.dataDir}/foo";
77 };
78 }
79 '';
80
81 in {
82
83 name = "syncthing";
84
85 nodes = {
86 a = initialConfiguration;
87 b = initialConfiguration;
88 };
89
90 testScript = ''
91 import os
92
93
94 def setConfig(machine, config):
95 machine.copy_from_host(config, "/etc/nixos/configuration.nix")
96
97
98 def verifyDeviceIDSet(machine, expectedDevice):
99 machine.wait_until_succeeds(
100 '(( "$(nix --experimental-features nix-command eval --impure --raw --expr "(import <nixos/nixos> {}).config.services.syncthing.declarative.devices.%s.id" | wc -c)" == 63 ))'
101 % expectedDevice
102 )
103
104
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
120 configurationWithDeviceIDs = (
121 "${configurationWithDeviceIDs}"
122 )
123 configurationWithFolder = (
124 "${configurationWithFolder}"
125 )
126
127 start_all()
128 a.wait_for_unit("syncthing-autoregister.service")
129 setConfig(a, configurationWithDeviceIDs)
130 verifyDeviceIDSet(a, "a")
131 getDeviceFile(a)
132
133 b.wait_for_unit("syncthing-autoregister.service")
134 putDeviceFile(b)
135 setConfig(b, configurationWithDeviceIDs)
136 verifyDeviceIDSet(b, "a")
137 verifyDeviceIDSet(b, "b")
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")
151 '';
152 })