1 { system ? builtins.currentSystem,
3 pkgs ? import ../.. { inherit system config; }
6 with import ../lib/testing-python.nix { inherit system pkgs; };
10 postgresql-versions = import ../../pkgs/servers/sql/postgresql pkgs;
11 test-sql = pkgs.writeText "postgresql-test" ''
12 CREATE EXTENSION pgcrypto; -- just to check if lib loading works
16 INSERT INTO sth (id) VALUES (1);
17 INSERT INTO sth (id) VALUES (1);
18 INSERT INTO sth (id) VALUES (1);
19 INSERT INTO sth (id) VALUES (1);
20 INSERT INTO sth (id) VALUES (1);
21 CREATE TABLE xmltest ( doc xml );
22 INSERT INTO xmltest (doc) VALUES ('<test>ok</test>'); -- check if libxml2 enabled
24 make-postgresql-test = postgresql-name: postgresql-package: backup-all: makeTest {
25 name = postgresql-name;
26 meta = with pkgs.lib.maintainers; {
27 maintainers = [ zagy ];
30 nodes.machine = {...}:
32 services.postgresql = {
34 package = postgresql-package;
37 services.postgresqlBackup = {
39 databases = optional (!backup-all) "postgres";
44 backupName = if backup-all then "all" else "postgres";
45 backupService = if backup-all then "postgresqlBackup" else "postgresqlBackup-postgres";
46 backupFileBase = "/var/backup/postgresql/${backupName}";
48 def check_count(statement, lines):
49 return 'test $(sudo -u postgres psql postgres -tAc "{}"|wc -l) -eq {}'.format(
55 machine.wait_for_unit("postgresql")
57 with subtest("Postgresql is available just after unit start"):
59 "cat ${test-sql} | sudo -u postgres psql"
62 with subtest("Postgresql survives restart (bug #1735)"):
67 machine.wait_for_unit("postgresql")
69 machine.fail(check_count("SELECT * FROM sth;", 3))
70 machine.succeed(check_count("SELECT * FROM sth;", 5))
71 machine.fail(check_count("SELECT * FROM sth;", 4))
72 machine.succeed(check_count("SELECT xpath('/test/text()', doc) FROM xmltest;", 1))
74 with subtest("Backup service works"):
76 "systemctl start ${backupService}.service",
77 "zcat ${backupFileBase}.sql.gz | grep '<test>ok</test>'",
78 "ls -hal /var/backup/postgresql/ >/dev/console",
79 "stat -c '%a' ${backupFileBase}.sql.gz | grep 600",
81 with subtest("Backup service removes prev files"):
83 # Create dummy prev files.
84 "touch ${backupFileBase}.prev.sql{,.gz,.zstd}",
85 "chown postgres:postgres ${backupFileBase}.prev.sql{,.gz,.zstd}",
88 "systemctl start ${backupService}.service",
89 "ls -hal /var/backup/postgresql/ >/dev/console",
91 # Since nothing has changed in the database, the cur and prev files
93 "zcat ${backupFileBase}.sql.gz | grep '<test>ok</test>'",
94 "cmp ${backupFileBase}.sql.gz ${backupFileBase}.prev.sql.gz",
96 # The prev files with unused suffix should be removed.
97 "[ ! -f '${backupFileBase}.prev.sql' ]",
98 "[ ! -f '${backupFileBase}.prev.sql.zstd' ]",
100 # Both cur and prev file should only be accessible by the postgres user.
101 "stat -c '%a' ${backupFileBase}.sql.gz | grep 600",
102 "stat -c '%a' '${backupFileBase}.prev.sql.gz' | grep 600",
104 with subtest("Backup service fails gracefully"):
105 # Sabotage the backup process
106 machine.succeed("rm /run/postgresql/.s.PGSQL.5432")
108 "systemctl start ${backupService}.service",
111 "ls -hal /var/backup/postgresql/ >/dev/console",
112 "zcat ${backupFileBase}.prev.sql.gz | grep '<test>ok</test>'",
113 "stat ${backupFileBase}.in-progress.sql.gz",
115 # In a previous version, the second run would overwrite prev.sql.gz,
116 # so we test a second run as well.
118 "systemctl start ${backupService}.service",
121 "stat ${backupFileBase}.in-progress.sql.gz",
122 "zcat ${backupFileBase}.prev.sql.gz | grep '<test>ok</test>'",
126 with subtest("Initdb works"):
127 machine.succeed("sudo -u postgres initdb -D /tmp/testpostgres2")
134 (mapAttrs' (name: package: { inherit name; value=make-postgresql-test name package false;}) postgresql-versions) // {
135 postgresql_11-backup-all = make-postgresql-test "postgresql_11-backup-all" postgresql-versions.postgresql_11 true;