]> git.scottworley.com Git - syncthing-autoregister/blobdiff - tests/automatic-enrollment.nix
Follow nix version bump that changed the eval command line interface
[syncthing-autoregister] / tests / automatic-enrollment.nix
index 46940488a952c7d87319dc737ef5fdf720ba8759..9b0b76f0d0a7fcdc3c951b8ecfc97d95920c8d8a 100644 (file)
 import <nixpkgs/nixos/tests/make-test-python.nix> ({ pkgs, lib, ... }:
   let
 import <nixpkgs/nixos/tests/make-test-python.nix> ({ pkgs, lib, ... }:
   let
-    configuration = pkgs.writeText "configuration.nix" ''
+    deviceIDFilename = "/tmp/syncthing-auto-register-test-device-ids.nix";
+    vmConfiguration = pkgs.writeText "vm-configuration.nix" ''
       { pkgs, ... }: {
         imports = [
       { pkgs, ... }: {
         imports = [
+          ${../.}/lib/test-nixos-rebuild-switch-config.nix
           ${../.}/modules/syncthing-autoregister.nix
           ${../.}/modules/syncthing-autoregister.nix
-          <nixos/nixos/modules/installer/cd-dvd/channel.nix>
         ];
         services.syncthing = {
           enable = true;
           openDefaultPorts = true;
           autoRegister = {
             enable = true;
         ];
         services.syncthing = {
           enable = true;
           openDefaultPorts = true;
           autoRegister = {
             enable = true;
-            path = "/tmp/syncthing-auto-register-test-device-ids.nix";
+            path = "${deviceIDFilename}";
           };
         };
           };
         };
+
+        # For verifyDeviceIDSet()
         environment.systemPackages = with pkgs; [ nix ];
       }
     '';
         environment.systemPackages = with pkgs; [ nix ];
       }
     '';
+
+    evaluatedVMConfiguration = import <nixos/nixos/lib/eval-config.nix> {
+      system = builtins.currentSystem;
+      modules = [ "${vmConfiguration}" ];
+    };
+
+    # Also include a syncthing configuration with a declarative device id
+    # because using this feature pulls in additional dependencies.
+    evaluatedSyncthingDeclarativeConfiguration = import <nixos/nixos/lib/eval-config.nix> {
+      system = builtins.currentSystem;
+      modules = [
+        ../lib/test-nixos-rebuild-switch-config.nix
+        {
+          services.syncthing = {
+            enable = true;
+            declarative.folders.forDeps.path = "/nope";
+          };
+        }
+      ];
+    };
+
+    initialConfiguration = {
+        imports = [ "${vmConfiguration}" ];
+        virtualisation.memorySize = 2048;
+        system.activationScripts.installInitialConfiguration = {
+          text = ''
+            mkdir -p /etc/nixos
+            cp "${vmConfiguration}" /etc/nixos/configuration.nix
+          '';
+          deps = [ ];
+        };
+        system.extraDependencies = [
+          evaluatedVMConfiguration.config.system.build.toplevel
+          evaluatedSyncthingDeclarativeConfiguration.config.system.build.toplevel
+        ];
+      };
+
     configurationWithDeviceIDs =
       pkgs.writeText "configuration-with-device-ids.nix" ''
         {
           imports = [
     configurationWithDeviceIDs =
       pkgs.writeText "configuration-with-device-ids.nix" ''
         {
           imports = [
-            ${configuration}
-            /tmp/syncthing-auto-register-test-device-ids.nix
+            ${vmConfiguration}
+            ${deviceIDFilename}
           ];
         }
       '';
           ];
         }
       '';
+
+    configurationWithFolder =
+      pkgs.writeText "configuration-with-folder.nix" ''
+        { config, ... }: {
+          imports = [ ${configurationWithDeviceIDs} ];
+          services.syncthing.declarative.folders.foo = {
+            devices = [ "a" "b" ];
+            path = "''${config.services.syncthing.dataDir}/foo";
+          };
+        }
+      '';
+
   in {
 
     name = "syncthing";
 
     nodes = {
   in {
 
     name = "syncthing";
 
     nodes = {
-      a = "${configuration}";
-      b = "${configuration}";
+      a = initialConfiguration;
+      b = initialConfiguration;
     };
 
     testScript = ''
     };
 
     testScript = ''
+      import os
+
+
       def setConfig(machine, config):
           machine.copy_from_host(config, "/etc/nixos/configuration.nix")
 
 
       def setConfig(machine, config):
           machine.copy_from_host(config, "/etc/nixos/configuration.nix")
 
 
-      initialConfiguration = "${configuration}"
+      def verifyDeviceIDSet(machine, expectedDevice):
+          machine.wait_until_succeeds(
+              '(( "$(nix --experimental-features nix-command eval --impure --raw --expr "(import <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}"
       )
       configurationWithDeviceIDs = (
           "${configurationWithDeviceIDs}"
       )
+      configurationWithFolder = (
+          "${configurationWithFolder}"
+      )
 
 
-      setConfig(a, initialConfiguration)
+      start_all()
       a.wait_for_unit("syncthing-autoregister.service")
       setConfig(a, configurationWithDeviceIDs)
       a.wait_for_unit("syncthing-autoregister.service")
       setConfig(a, configurationWithDeviceIDs)
-      a.succeed(
-          '(( "$(nix eval --raw -f "<nixos/nixos>" config.services.syncthing.declarative.devices.a.id | wc -c)" == 63 ))'
-      )
+      verifyDeviceIDSet(a, "a")
+      getDeviceFile(a)
+
+      b.wait_for_unit("syncthing-autoregister.service")
+      putDeviceFile(b)
+      setConfig(b, configurationWithDeviceIDs)
+      verifyDeviceIDSet(b, "a")
+      verifyDeviceIDSet(b, "b")
+
+      getDeviceFile(b)
+      putDeviceFile(a)
+      setConfig(a, configurationWithFolder)
+      setConfig(b, configurationWithFolder)
+      a.succeed("nixos-rebuild switch")
+      b.succeed("nixos-rebuild switch")
+      a.wait_for_file("/var/lib/syncthing/foo")
+      b.wait_for_file("/var/lib/syncthing/foo")
+      a.succeed("echo a2b > /var/lib/syncthing/foo/a2b")
+      b.succeed("echo b2a > /var/lib/syncthing/foo/b2a")
+      a.wait_for_file("/var/lib/syncthing/foo/b2a")
+      b.wait_for_file("/var/lib/syncthing/foo/a2b")
     '';
   })
     '';
   })