at 22.05-pre 5.1 kB view raw
1{ system ? builtins.currentSystem, 2 config ? {}, 3 pkgs ? import ../.. { inherit system config; } 4}: 5 6with import ../lib/testing-python.nix { inherit system pkgs; }; 7with pkgs.lib; 8 9let 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 13 CREATE TABLE sth ( 14 id int 15 ); 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 23 ''; 24 make-postgresql-test = postgresql-name: postgresql-package: backup-all: makeTest { 25 name = postgresql-name; 26 meta = with pkgs.lib.maintainers; { 27 maintainers = [ zagy ]; 28 }; 29 30 machine = {...}: 31 { 32 services.postgresql = { 33 enable = true; 34 package = postgresql-package; 35 }; 36 37 services.postgresqlBackup = { 38 enable = true; 39 databases = optional (!backup-all) "postgres"; 40 }; 41 }; 42 43 testScript = let 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}"; 47 in '' 48 def check_count(statement, lines): 49 return 'test $(sudo -u postgres psql postgres -tAc "{}"|wc -l) -eq {}'.format( 50 statement, lines 51 ) 52 53 54 machine.start() 55 machine.wait_for_unit("postgresql") 56 57 with subtest("Postgresql is available just after unit start"): 58 machine.succeed( 59 "cat ${test-sql} | sudo -u postgres psql" 60 ) 61 62 with subtest("Postgresql survives restart (bug #1735)"): 63 machine.shutdown() 64 import time 65 time.sleep(2) 66 machine.start() 67 machine.wait_for_unit("postgresql") 68 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)) 73 74 with subtest("Backup service works"): 75 machine.succeed( 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", 80 ) 81 with subtest("Backup service removes prev files"): 82 machine.succeed( 83 # Create dummy prev files. 84 "touch ${backupFileBase}.prev.sql{,.gz,.zstd}", 85 "chown postgres:postgres ${backupFileBase}.prev.sql{,.gz,.zstd}", 86 87 # Run backup. 88 "systemctl start ${backupService}.service", 89 "ls -hal /var/backup/postgresql/ >/dev/console", 90 91 # Since nothing has changed in the database, the cur and prev files 92 # should match. 93 "zcat ${backupFileBase}.sql.gz | grep '<test>ok</test>'", 94 "cmp ${backupFileBase}.sql.gz ${backupFileBase}.prev.sql.gz", 95 96 # The prev files with unused suffix should be removed. 97 "[ ! -f '${backupFileBase}.prev.sql' ]", 98 "[ ! -f '${backupFileBase}.prev.sql.zstd' ]", 99 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", 103 ) 104 with subtest("Backup service fails gracefully"): 105 # Sabotage the backup process 106 machine.succeed("rm /run/postgresql/.s.PGSQL.5432") 107 machine.fail( 108 "systemctl start ${backupService}.service", 109 ) 110 machine.succeed( 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", 114 ) 115 # In a previous version, the second run would overwrite prev.sql.gz, 116 # so we test a second run as well. 117 machine.fail( 118 "systemctl start ${backupService}.service", 119 ) 120 machine.succeed( 121 "stat ${backupFileBase}.in-progress.sql.gz", 122 "zcat ${backupFileBase}.prev.sql.gz | grep '<test>ok</test>'", 123 ) 124 125 126 with subtest("Initdb works"): 127 machine.succeed("sudo -u postgres initdb -D /tmp/testpostgres2") 128 129 machine.shutdown() 130 ''; 131 132 }; 133in 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; 136 } 137