Commit a3b08b7c authored by Pasha Fistanto's avatar Pasha Fistanto Committed by Doron Behar
Browse files

nixosTests.syncthing-many-devices: refactor IDsToDelete handling sets

Make it a list and not an attribute set, and create these objects in the
server's config using a single Nix string concatenation, with as much as
possible consistency between devices & folders.
parent 0233be9d
Loading
Loading
Loading
Loading
+37 −28
Original line number Diff line number Diff line
@@ -102,10 +102,10 @@ let
      not,
    }:
    lib.pipe IDsToDelete [
      (lib.mapAttrsToList (
        t: id:
      (map (
        obj:
        checkSettingWithId {
          inherit t id;
          inherit (obj) t id;
          inherit not;
        }
      ))
@@ -114,13 +114,19 @@ let
  # These IDs are added to syncthing using the API, similarly to how the
  # generated systemd unit's bash script does it. Only we add it and expect the
  # systemd unit bash script to remove them when executed.
  IDsToDelete = {
    # Also created using the syncthing generate command above
    device = "LZ2CTHT-3W2M7BC-CMKDFZL-DLUQJFS-WJR73PA-NZGODWG-DZBHCHI-OXTQXAK";
    # Intentionally this is a substring of the IDs of the 'test_folder's, as
    # explained in: https://github.com/NixOS/nixpkgs/issues/259256
    folder = "DeleteMe";
  };
  IDsToDelete = [
    {
      t = "device";
      id = "LZ2CTHT-3W2M7BC-CMKDFZL-DLUQJFS-WJR73PA-NZGODWG-DZBHCHI-OXTQXAK";
      # Arbitrary, doesn't really matter
      name = "DeleteThisDevice";
    }
    # Folders' id strings also use for their path when created.
    {
      t = "folder";
      id = "DeleteMe";
    }
  ];
  addDeviceToDeleteScript = pkgs.writers.writeBash "syncthing-add-device-to-delete.sh" ''
    set -euo pipefail

@@ -141,24 +147,27 @@ let
            --retry 5 --retry-delay 1 --retry-all-errors \
            "$@"
    }
    ${lib.concatMapStringsSep "\n" (obj: ''
      curl -d ${
        lib.escapeShellArg (
        builtins.toJSON {
          deviceID = IDsToDelete.device;
          name = "DeleteMe";
          builtins.toJSON (
            if obj.t == "device" then
              {
                deviceID = obj.id;
                inherit (obj) name;
              }
      )
    } \
        -X POST 127.0.0.1:8384/rest/config/devices
    curl -d ${
      lib.escapeShellArg (
        builtins.toJSON {
          id = IDsToDelete.folder;
          path = "/var/lib/syncthing/DeleteMe";
            else if obj.t == "folder" then
              {
                inherit (obj) id;
                path = "/var/lib/syncthing/${obj.id}";
              }
            else
              throw "unsupported object type ${obj.t}"
          )
        )
      } \
        -X POST 127.0.0.1:8384/rest/config/folders
        -X POST 127.0.0.1:8384/rest/config/${obj.t}s
    '') IDsToDelete}
  '';
in
{