summaryrefslogtreecommitdiffstats
path: root/nixos/tests/systemd-sysupdate.nix
blob: 5ab5da7381716673b76947c4a1cfec34998073a4 (plain)
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
# Tests downloading a signed update artifact from a server to a target machine.
# This test does not rely on the `systemd.timer` units provided by the
# `systemd-sysupdate` module but triggers the `updatectl` tool directly to
# demonstrate how to initiate updates manually.

{ lib, pkgs, ... }:

let
  gpgKeyring = import ./common/gpg-keyring.nix { inherit pkgs; };
in
{
  name = "systemd-sysupdate";

  meta.maintainers = with lib.maintainers; [ nikstur ];

  nodes = {
    server =
      { pkgs, ... }:
      {
        networking.firewall.enable = false;
        services.nginx = {
          enable = true;
          virtualHosts."server" = {
            root = pkgs.runCommand "sysupdate-artifacts" { buildInputs = [ pkgs.gnupg ]; } ''
              mkdir -p $out
              cd $out

              echo "nixos" > nixos_1.txt
              sha256sum nixos_1.txt > SHA256SUMS

              export GNUPGHOME="$(mktemp -d)"
              cp -R ${gpgKeyring}/* $GNUPGHOME

              gpg --batch --sign --detach-sign --output SHA256SUMS.gpg SHA256SUMS
            '';
          };
        };
      };

    target = {
      systemd.sysupdate = {
        enable = true;
        transfers = {
          "text-file" = {
            Source = {
              Type = "url-file";
              Path = "http://server/";
              MatchPattern = "nixos_@v.txt";
            };
            Target = {
              Path = "/";
              MatchPattern = [ "nixos_@v.txt" ];
            };
          };
        };
      };

      environment.etc."systemd/import-pubring.gpg".source = "${gpgKeyring}/pubkey.gpg";
    };
  };

  testScript = ''
    server.wait_for_unit("nginx.service")

    print(target.succeed("updatectl list"))
    target.succeed("updatectl update")
    assert "nixos" in target.wait_until_succeeds("cat /nixos_1.txt", timeout=5)
  '';
}