X-Git-Url: http://git.scottworley.com/syncthing-autoregister/blobdiff_plain/2475b5344ca33d7aad5b57f42310994d60b80ee3..b87b912176c310395c14104abac74bb729dce54d:/tests/automatic-enrollment.nix?ds=inline diff --git a/tests/automatic-enrollment.nix b/tests/automatic-enrollment.nix index 1a6b360..29a0bdf 100644 --- a/tests/automatic-enrollment.nix +++ b/tests/automatic-enrollment.nix @@ -1,70 +1,152 @@ import ({ 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 = [ + ${../.}/lib/test-nixos-rebuild-switch-config.nix ${../.}/modules/syncthing-autoregister.nix - ]; 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 ]; } ''; - initialConfiguration = pkgs.writeText "initial-configuration.nix" '' - { - imports = [ "${configuration}" ]; + + evaluatedVMConfiguration = import { + system = builtins.currentSystem; + modules = [ "${vmConfiguration}" ]; + }; + + # Also include a syncthing configuration with a device id + # because using this feature pulls in additional dependencies. + evaluatedSyncthingDeclarativeConfiguration = import { + 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 = { - text = ''' + text = '' mkdir -p /etc/nixos - cp "${configuration}" /etc/nixos/configuration.nix - '''; + 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 = [ - ${configuration} - /tmp/syncthing-auto-register-test-device-ids.nix + ${vmConfiguration} + ${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 = { - a = "${initialConfiguration}"; - b = "${initialConfiguration}"; + a = initialConfiguration; + b = initialConfiguration; }; testScript = '' + import os + + def setConfig(machine, config): machine.copy_from_host(config, "/etc/nixos/configuration.nix") def verifyDeviceIDSet(machine, expectedDevice): - machine.succeed( - '(( "$(nix eval --raw -f "" config.services.syncthing.declarative.devices.%s.id | wc -c)" == 63 ))' + machine.wait_until_succeeds( + '(( "$(nix --experimental-features nix-command eval --impure --raw --expr "(import {}).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}" ) + configurationWithFolder = ( + "${configurationWithFolder}" + ) + 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") + + 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") ''; })