]> git.scottworley.com Git - syncthing-autoregister/blob - tests/automatic-enrollment.nix
951a4153e000be6bb453522fab7f4bcde415d1f4
[syncthing-autoregister] / tests / automatic-enrollment.nix
1 import <nixpkgs/nixos/tests/make-test-python.nix> ({ pkgs, lib, ... }:
2 let
3 deviceIDFilename = "/tmp/syncthing-auto-register-test-device-ids.nix";
4 configuration = pkgs.writeText "configuration.nix" ''
5 { pkgs, ... }: {
6 imports = [
7 ${../.}/modules/syncthing-autoregister.nix
8 <nixos/nixos/modules/installer/cd-dvd/channel.nix>
9 ];
10 services.syncthing = {
11 enable = true;
12 openDefaultPorts = true;
13 autoRegister = {
14 enable = true;
15 path = "${deviceIDFilename}";
16 };
17 };
18 environment.systemPackages = with pkgs; [ nix ];
19 }
20 '';
21 initialConfiguration = pkgs.writeText "initial-configuration.nix" ''
22 {
23 imports = [ "${configuration}" ];
24 system.activationScripts.installInitialConfiguration = {
25 text = '''
26 mkdir -p /etc/nixos
27 cp "${configuration}" /etc/nixos/configuration.nix
28 ''';
29 deps = [ ];
30 };
31 }
32 '';
33 configurationWithDeviceIDs =
34 pkgs.writeText "configuration-with-device-ids.nix" ''
35 {
36 imports = [
37 ${configuration}
38 ${deviceIDFilename}
39 ];
40 }
41 '';
42 in {
43
44 name = "syncthing";
45
46 nodes = {
47 a = "${initialConfiguration}";
48 b = "${initialConfiguration}";
49 };
50
51 testScript = ''
52 import os
53
54
55 def setConfig(machine, config):
56 machine.copy_from_host(config, "/etc/nixos/configuration.nix")
57
58
59 def verifyDeviceIDSet(machine, expectedDevice):
60 machine.succeed(
61 '(( "$(nix eval --raw -f "<nixos/nixos>" config.services.syncthing.declarative.devices.%s.id | wc -c)" == 63 ))'
62 % expectedDevice
63 )
64
65
66 def getDeviceFile(machine):
67 machine.copy_from_vm("${deviceIDFilename}")
68
69
70 def putDeviceFile(machine):
71 hostDeviceIDFilename = os.path.join(
72 os.environ["out"],
73 os.path.basename("${deviceIDFilename}"),
74 )
75 machine.copy_from_host(
76 hostDeviceIDFilename, "${deviceIDFilename}"
77 )
78 machine.succeed("chown syncthing ${deviceIDFilename}")
79
80
81 configurationWithDeviceIDs = (
82 "${configurationWithDeviceIDs}"
83 )
84
85 a.wait_for_unit("syncthing-autoregister.service")
86 setConfig(a, configurationWithDeviceIDs)
87 verifyDeviceIDSet(a, "a")
88 getDeviceFile(a)
89
90 putDeviceFile(b)
91 setConfig(b, configurationWithDeviceIDs)
92 verifyDeviceIDSet(b, "a")
93 b.wait_for_unit("syncthing-autoregister.service")
94 verifyDeviceIDSet(b, "a")
95 verifyDeviceIDSet(b, "b")
96 '';
97 })