]> git.scottworley.com Git - syncthing-autoregister/blobdiff - tests/automatic-enrollment.nix
Prefer modulesPath over <nixos/nixos/modules>
[syncthing-autoregister] / tests / automatic-enrollment.nix
index 6520c9f35e5067a9e76fb6b166ccb735c52e8218..29a0bdf730cbc85c2c910038341331fc6d86d422 100644 (file)
@@ -1,11 +1,11 @@
 import <nixpkgs/nixos/tests/make-test-python.nix> ({ pkgs, lib, ... }:
   let
     deviceIDFilename = "/tmp/syncthing-auto-register-test-device-ids.nix";
 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" ''
+    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;
         ];
         services.syncthing = {
           enable = true;
@@ -15,37 +15,76 @@ import <nixpkgs/nixos/tests/make-test-python.nix> ({ pkgs, lib, ... }:
             path = "${deviceIDFilename}";
           };
         };
             path = "${deviceIDFilename}";
           };
         };
+
+        # For verifyDeviceIDSet()
         environment.systemPackages = with pkgs; [ nix ];
       }
     '';
         environment.systemPackages = with pkgs; [ nix ];
       }
     '';
-    initialConfiguration = pkgs.writeText "initial-configuration.nix" ''
-      {
-        imports = [ "${configuration}" ];
+
+    evaluatedVMConfiguration = import <nixos/nixos/lib/eval-config.nix> {
+      system = builtins.currentSystem;
+      modules = [ "${vmConfiguration}" ];
+    };
+
+    # Also include a syncthing configuration with a 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;
+            folders.forDeps.path = "/nope";
+          };
+        }
+      ];
+    };
+
+    initialConfiguration = {
+        imports = [ "${vmConfiguration}" ];
+        virtualisation.memorySize = 2048;
         system.activationScripts.installInitialConfiguration = {
         system.activationScripts.installInitialConfiguration = {
-          text = '''
+          text = ''
             mkdir -p /etc/nixos
             mkdir -p /etc/nixos
-            cp "${configuration}" /etc/nixos/configuration.nix
-          ''';
+            cp "${vmConfiguration}" /etc/nixos/configuration.nix
+          '';
           deps = [ ];
         };
           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}
+            ${vmConfiguration}
             ${deviceIDFilename}
           ];
         }
       '';
             ${deviceIDFilename}
           ];
         }
       '';
+
+    configurationWithFolder =
+      pkgs.writeText "configuration-with-folder.nix" ''
+        { config, ... }: {
+          imports = [ ${configurationWithDeviceIDs} ];
+          services.syncthing.folders.foo = {
+            devices = [ "a" "b" ];
+            path = "''${config.services.syncthing.dataDir}/foo";
+          };
+        }
+      '';
+
   in {
 
     name = "syncthing";
 
     nodes = {
   in {
 
     name = "syncthing";
 
     nodes = {
-      a = "${initialConfiguration}";
-      b = "${initialConfiguration}";
+      a = initialConfiguration;
+      b = initialConfiguration;
     };
 
     testScript = ''
     };
 
     testScript = ''
@@ -57,8 +96,8 @@ import <nixpkgs/nixos/tests/make-test-python.nix> ({ pkgs, lib, ... }:
 
 
       def verifyDeviceIDSet(machine, expectedDevice):
 
 
       def verifyDeviceIDSet(machine, expectedDevice):
-          machine.succeed(
-              '(( "$(nix eval --raw -f "<nixos/nixos>" config.services.syncthing.declarative.devices.%s.id | wc -c)" == 63 ))'
+          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
           )
 
               % expectedDevice
           )
 
@@ -81,14 +120,33 @@ import <nixpkgs/nixos/tests/make-test-python.nix> ({ pkgs, lib, ... }:
       configurationWithDeviceIDs = (
           "${configurationWithDeviceIDs}"
       )
       configurationWithDeviceIDs = (
           "${configurationWithDeviceIDs}"
       )
+      configurationWithFolder = (
+          "${configurationWithFolder}"
+      )
 
 
+      start_all()
       a.wait_for_unit("syncthing-autoregister.service")
       setConfig(a, configurationWithDeviceIDs)
       verifyDeviceIDSet(a, "a")
       getDeviceFile(a)
 
       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")
       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")
     '';
   })
     '';
   })