at 21.11-pre 3.0 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 in '' 47 def check_count(statement, lines): 48 return 'test $(sudo -u postgres psql postgres -tAc "{}"|wc -l) -eq {}'.format( 49 statement, lines 50 ) 51 52 53 machine.start() 54 machine.wait_for_unit("postgresql") 55 56 with subtest("Postgresql is available just after unit start"): 57 machine.succeed( 58 "cat ${test-sql} | sudo -u postgres psql" 59 ) 60 61 with subtest("Postgresql survives restart (bug #1735)"): 62 machine.shutdown() 63 time.sleep(2) 64 machine.start() 65 machine.wait_for_unit("postgresql") 66 67 machine.fail(check_count("SELECT * FROM sth;", 3)) 68 machine.succeed(check_count("SELECT * FROM sth;", 5)) 69 machine.fail(check_count("SELECT * FROM sth;", 4)) 70 machine.succeed(check_count("SELECT xpath('/test/text()', doc) FROM xmltest;", 1)) 71 72 with subtest("Backup service works"): 73 machine.succeed( 74 "systemctl start ${backupService}.service", 75 "zcat /var/backup/postgresql/${backupName}.sql.gz | grep '<test>ok</test>'", 76 "stat -c '%a' /var/backup/postgresql/${backupName}.sql.gz | grep 600", 77 ) 78 79 with subtest("Initdb works"): 80 machine.succeed("sudo -u postgres initdb -D /tmp/testpostgres2") 81 82 machine.shutdown() 83 ''; 84 85 }; 86in 87 (mapAttrs' (name: package: { inherit name; value=make-postgresql-test name package false;}) postgresql-versions) // { 88 postgresql_11-backup-all = make-postgresql-test "postgresql_11-backup-all" postgresql-versions.postgresql_11 true; 89 } 90