8 meta.maintainers = [ lib.maintainers.tie ];
10 nodes.machine = { config, ... }: {
11 services.postgresql = {
13 # This test uses default peer authentication (socket and its directory is
14 # world-readably by default), so we essentially test that we can connect
15 # with DynamicUser= set.
18 ensureClauses.superuser = true;
23 listenAddress = "[::]:${toString pgheroPort}";
26 postgres.url = "<%= ENV['POSTGRES_DATABASE_URL'] %>";
27 nulldb.url = "nulldb:///";
31 PGHERO_USERNAME = pgheroUser;
32 PGHERO_PASSWORD = pgheroPass;
33 POSTGRES_DATABASE_URL = "postgresql:///postgres?host=/run/postgresql";
39 pgheroPort = ${toString pgheroPort}
40 pgheroUser = "${pgheroUser}"
41 pgheroPass = "${pgheroPass}"
43 pgheroUnauthorizedURL = f"http://localhost:{pgheroPort}"
44 pgheroBaseURL = f"http://{pgheroUser}:{pgheroPass}@localhost:{pgheroPort}"
46 def expect_http_code(node, code, url):
47 http_code = node.succeed(f"curl -s -o /dev/null -w '%{{http_code}}' '{url}'")
48 assert http_code.split("\n")[-1].strip() == code, \
49 f"expected HTTP status code {code} but got {http_code}"
51 machine.wait_for_unit("postgresql.service")
52 machine.wait_for_unit("pghero.service")
54 with subtest("requires HTTP Basic Auth credentials"):
55 expect_http_code(machine, "401", pgheroUnauthorizedURL)
57 with subtest("works with some databases being unavailable"):
58 expect_http_code(machine, "500", pgheroBaseURL + "/nulldb")
60 with subtest("connects to the PostgreSQL database"):
61 expect_http_code(machine, "200", pgheroBaseURL + "/postgres")