]> git.scottworley.com Git - syncthing-autoregister/blobdiff - tests/automatic-enrollment.nix
Fix race: Re-register on file change
[syncthing-autoregister] / tests / automatic-enrollment.nix
index 49325fa90e5f71d19270683c93f0964087b978fb..617f672e4da38f157986db9d2f7b2d1ef3be764b 100644 (file)
@@ -1,5 +1,6 @@
 import <nixpkgs/nixos/tests/make-test-python.nix> ({ pkgs, lib, ... }:
   let
+    deviceIDFilename = "/tmp/syncthing-auto-register-test-device-ids.nix";
     configuration = pkgs.writeText "configuration.nix" ''
       { pkgs, ... }: {
         imports = [
@@ -11,18 +12,30 @@ import <nixpkgs/nixos/tests/make-test-python.nix> ({ pkgs, lib, ... }:
           openDefaultPorts = true;
           autoRegister = {
             enable = true;
-            path = "/tmp/syncthing-auto-register-test-device-ids.nix";
+            path = "${deviceIDFilename}";
           };
         };
         environment.systemPackages = with pkgs; [ nix ];
       }
     '';
+    initialConfiguration = pkgs.writeText "initial-configuration.nix" ''
+      {
+        imports = [ "${configuration}" ];
+        system.activationScripts.installInitialConfiguration = {
+          text = '''
+            mkdir -p /etc/nixos
+            cp "${configuration}" /etc/nixos/configuration.nix
+          ''';
+          deps = [ ];
+        };
+      }
+    '';
     configurationWithDeviceIDs =
       pkgs.writeText "configuration-with-device-ids.nix" ''
         {
           imports = [
             ${configuration}
-            /tmp/syncthing-auto-register-test-device-ids.nix
+            ${deviceIDFilename}
           ];
         }
       '';
@@ -30,23 +43,55 @@ import <nixpkgs/nixos/tests/make-test-python.nix> ({ pkgs, lib, ... }:
 
     name = "syncthing";
 
-    machine = "${configuration}";
+    nodes = {
+      a = "${initialConfiguration}";
+      b = "${initialConfiguration}";
+    };
 
     testScript = ''
+      import os
+
+
       def setConfig(machine, config):
           machine.copy_from_host(config, "/etc/nixos/configuration.nix")
 
 
-      initialConfiguration = "${configuration}"
+      def verifyDeviceIDSet(machine, expectedDevice):
+          machine.wait_until_succeeds(
+              '(( "$(nix eval --raw -f "<nixos/nixos>" config.services.syncthing.declarative.devices.%s.id | wc -c)" == 63 ))'
+              % expectedDevice
+          )
+
+
+      def getDeviceFile(machine):
+          machine.copy_from_vm("${deviceIDFilename}")
+
+
+      def putDeviceFile(machine):
+          hostDeviceIDFilename = os.path.join(
+              os.environ["out"],
+              os.path.basename("${deviceIDFilename}"),
+          )
+          machine.copy_from_host(
+              hostDeviceIDFilename, "${deviceIDFilename}"
+          )
+          machine.succeed("chown syncthing ${deviceIDFilename}")
+
+
       configurationWithDeviceIDs = (
           "${configurationWithDeviceIDs}"
       )
 
-      setConfig(machine, initialConfiguration)
-      machine.wait_for_unit("syncthing-autoregister.service")
-      setConfig(machine, configurationWithDeviceIDs)
-      machine.succeed(
-          '(( "$(nix eval --raw -f "<nixos/nixos>" config.services.syncthing.declarative.devices.machine.id | wc -c)" == 63 ))'
-      )
+      start_all()
+      a.wait_for_unit("syncthing-autoregister.service")
+      setConfig(a, configurationWithDeviceIDs)
+      verifyDeviceIDSet(a, "a")
+      getDeviceFile(a)
+
+      b.wait_for_unit("syncthing-autoregister.service")
+      putDeviceFile(b)
+      setConfig(b, configurationWithDeviceIDs)
+      verifyDeviceIDSet(b, "a")
+      verifyDeviceIDSet(b, "b")
     '';
   })