about summary refs log tree commit diff
path: root/nixos/tests/pghero.nix
blob: bce32da008862209c7aa8e93f86d4bd54ba4519f (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
let
  pgheroPort = 1337;
  pgheroUser = "pghero";
  pgheroPass = "pghero";
in
{ lib, ... }: {
  name = "pghero";
  meta.maintainers = [ lib.maintainers.tie ];

  nodes.machine = { config, ... }: {
    services.postgresql = {
      enable = true;
      # This test uses default peer authentication (socket and its directory is
      # world-readably by default), so we essentially test that we can connect
      # with DynamicUser= set.
      ensureUsers = [{
        name = "pghero";
        ensureClauses.superuser = true;
      }];
    };
    services.pghero = {
      enable = true;
      listenAddress = "[::]:${toString pgheroPort}";
      settings = {
        databases = {
          postgres.url = "<%= ENV['POSTGRES_DATABASE_URL'] %>";
          nulldb.url = "nulldb:///";
        };
      };
      environment = {
        PGHERO_USERNAME = pgheroUser;
        PGHERO_PASSWORD = pgheroPass;
        POSTGRES_DATABASE_URL = "postgresql:///postgres?host=/run/postgresql";
      };
    };
  };

  testScript = ''
    pgheroPort = ${toString pgheroPort}
    pgheroUser = "${pgheroUser}"
    pgheroPass = "${pgheroPass}"

    pgheroUnauthorizedURL = f"http://localhost:{pgheroPort}"
    pgheroBaseURL = f"http://{pgheroUser}:{pgheroPass}@localhost:{pgheroPort}"

    def expect_http_code(node, code, url):
        http_code = node.succeed(f"curl -s -o /dev/null -w '%{{http_code}}' '{url}'")
        assert http_code.split("\n")[-1].strip() == code, \
          f"expected HTTP status code {code} but got {http_code}"

    machine.wait_for_unit("postgresql.service")
    machine.wait_for_unit("pghero.service")

    with subtest("requires HTTP Basic Auth credentials"):
      expect_http_code(machine, "401", pgheroUnauthorizedURL)

    with subtest("works with some databases being unavailable"):
      expect_http_code(machine, "500", pgheroBaseURL + "/nulldb")

    with subtest("connects to the PostgreSQL database"):
      expect_http_code(machine, "200", pgheroBaseURL + "/postgres")
  '';
}