about summary refs log tree commit diff stats
path: root/tests/by-name/at/atuin-sync/test.nix
blob: 3e018850184237b7523c94ca7adfc600df7aec65 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
{
  nixos-lib,
  pkgsUnstable,
  nixpkgs-unstable,
  vhackPackages,
  pkgs,
  extraModules,
  nixLib,
  ...
}:
nixos-lib.runTest {
  hostPkgs = pkgs;
  name = "atuin-sync";

  node = {
    specialArgs = {inherit pkgsUnstable vhackPackages nixpkgs-unstable nixLib;};

    # Use the nixpkgs as constructed by the `nixpkgs.*` options
    pkgs = null;
  };

  nodes = let
    atuinSession = "01969ec6b8d07e30a9d2df0911fbfe2a";
  in {
    acme = {
      imports = [
        ../../../common/acme/server.nix
        ../../../common/dns/client.nix
      ];
    };
    name_server = {nodes, ...}: {
      imports =
        extraModules
        ++ [
          ../../../common/acme/client.nix
          ../../../common/dns/server.nix
        ];

      vhack.dns.zones = {
        "atuin-sync.server" = {
          SOA = {
            nameServer = "ns";
            adminEmail = "admin@server.com";
            serial = 2025012301;
          };
          useOrigin = false;

          A = [
            nodes.server.networking.primaryIPAddress
          ];
          AAAA = [
            nodes.server.networking.primaryIPv6Address
          ];
        };
      };
    };

    server = {config, ...}: {
      imports =
        extraModules
        ++ [
          ../../../../modules
          ../../../common/acme/client.nix
          ../../../common/dns/client.nix
        ];

      vhack = {
        persist.enable = true;
        nginx.enable = true;
        atuin-sync = {
          enable = true;
          fqdn = "atuin-sync.server";
        };
      };
    };

    client1 = {config, ...}: {
      imports = [
        ../../../common/acme/client.nix
        ../../../common/dns/client.nix
      ];

      environment.sessionVariables.ATUIN_SESSION = atuinSession;

      environment.systemPackages = [
        pkgs.atuin
        pkgs.sqlite-interactive
      ];
    };
    client2 = {config, ...}: {
      imports = [
        ../../../common/acme/client.nix
        ../../../common/dns/client.nix
      ];

      environment.sessionVariables.ATUIN_SESSION = atuinSession;

      environment.systemPackages = [
        pkgs.atuin
        pkgs.sqlite-interactive
      ];
    };
  };

  testScript = {nodes, ...}: let
    syncLogin = pkgs.writeShellScript "login-atuin-sync-account" ''
      atuin login --username syncy --password password1234 --key "$1"
    '';

    syncRegister = pkgs.writeShellScript "register-atuin-sync-account" ''
      atuin register --username syncy --email syncy@email.com --password password1234
    '';

    mkSyncConfig = pkgs.writeShellScript "register-atuin-sync-account" ''
      mkdir --parents ~/.config/atuin/
      cat << EOF > ~/.config/atuin/config.toml
      sync_address = "https://atuin-sync.server"

      # Use the v2 sync
      [sync]
      records = true
      EOF
    '';

    runCommandAndRecordInAtuin = pkgs.writeShellScript "run-command-and-record-in-atuin" ''
      # SPDX-SnippetBegin
      # SPDX-SnippetCopyrightText: 2023 mentalisttraceur (https://github.com/mentalisttraceur)
      # Source: https://github.com/atuinsh/atuin/issues/1188#issuecomment-1698354107
      run_and_record_in_atuin()
      {
          local id
          local status
          local escaped_command="$(printf '%q ' "$@")"
          id="$(atuin history start -- "$escaped_command")"
          "$@"
          status=$?
          atuin history end --exit $status "$id"
          return $status
      }
      # SPDX-SnippetEnd

      run_and_record_in_atuin "$@"
    '';

    acme_scripts = import ../../../common/acme/scripts.nix {inherit pkgs;};
  in
    /*
    python
    */
    ''
      # Start dependencies for the other services
      acme.start()
      acme.wait_for_unit("pebble.service")
      name_server.start()
      name_server.wait_for_unit("nsd.service")

      # Start actual test
      start_all()

      with subtest("Add pebble ca key to all services"):
        for node in [name_server, server, client1, client2]:
          node.wait_for_unit("network-online.target")
          node.succeed("${acme_scripts.add_pebble_acme_ca}")

      server.wait_for_unit("atuin.service")
      server.wait_for_open_port(443)

      # Wait for the server to acquire the acme certificate
      client1.wait_until_succeeds("curl https://atuin-sync.server")

      with subtest("Setup client syncing"):
          for client in [client1, client2]:
            client.succeed("${mkSyncConfig}")

          client1.succeed("${syncRegister}")

          for client in [client1, client2]:
            # See https://docs.atuin.sh/guide/sync/
            client.succeed(f"${syncLogin} '{client1.succeed("atuin key")}'")

      with subtest("Can import shell history"):
          client1.succeed("${runCommandAndRecordInAtuin} echo hi - client 1")
          client2.succeed("${runCommandAndRecordInAtuin} echo hi - client 2")

      with subtest("Can sync tasks"):
          for client in [client1, client2]:
            client.succeed("atuin sync --force")
          client1.succeed("atuin sync --force")


      with subtest("Have correct tasks"):
          hist1 = client1.succeed("atuin history list --session --format '{command}'").strip().split('\n')
          hist2 = client2.succeed("atuin history list --session --format '{command}'").strip().split('\n')

          hist1.sort()
          hist2.sort()

          canonicalHistory = [
            "echo hi - client 1",
            "echo hi - client 2"
          ]

          assert hist1 == hist2, f"The clients don't have the same amount of history items, client1: '{hist1}', client2: '{hist2}'"
          assert hist1 == canonicalHistory, f"The history is not correct: '{hist1}' vs. '{canonicalHistory}'"
    '';
}