postgresql: remove thisAttr argument by calling tests directly

Previously, it was not possible to run tests on an overridden derivation, because
the derivation under test was always pulled from pkgs.

With this change, the following will return the same test:

postgresql_jit.tests

and

(postgresql.override { jitSupport = true; }).tests

+123 -102
+11 -4
nixos/tests/postgresql-jit.nix
··· 1 { system ? builtins.currentSystem 2 , config ? {} 3 , pkgs ? import ../.. { inherit system config; } 4 }: 5 6 with import ../lib/testing-python.nix { inherit system pkgs; }; ··· 9 inherit (pkgs) lib; 10 packages = builtins.attrNames (import ../../pkgs/servers/sql/postgresql pkgs); 11 12 - mkJitTest = packageName: makeTest { 13 - name = "${packageName}"; 14 meta.maintainers = with lib.maintainers; [ ma27 ]; 15 nodes.machine = { pkgs, lib, ... }: { 16 services.postgresql = { 17 enable = true; 18 enableJIT = true; 19 - package = pkgs.${packageName}; 20 initialScript = pkgs.writeText "init.sql" '' 21 create table demo (id int); 22 insert into demo (id) select generate_series(1, 5); ··· 45 ''; 46 }; 47 in 48 - lib.genAttrs packages mkJitTest
··· 1 { system ? builtins.currentSystem 2 , config ? {} 3 , pkgs ? import ../.. { inherit system config; } 4 + , package ? null 5 }: 6 7 with import ../lib/testing-python.nix { inherit system pkgs; }; ··· 10 inherit (pkgs) lib; 11 packages = builtins.attrNames (import ../../pkgs/servers/sql/postgresql pkgs); 12 13 + mkJitTestFromName = name: 14 + mkJitTest pkgs.${name}; 15 + 16 + mkJitTest = package: makeTest { 17 + name = package.name; 18 meta.maintainers = with lib.maintainers; [ ma27 ]; 19 nodes.machine = { pkgs, lib, ... }: { 20 services.postgresql = { 21 + inherit package; 22 enable = true; 23 enableJIT = true; 24 initialScript = pkgs.writeText "init.sql" '' 25 create table demo (id int); 26 insert into demo (id) select generate_series(1, 5); ··· 49 ''; 50 }; 51 in 52 + if package == null then 53 + lib.genAttrs packages mkJitTestFromName 54 + else 55 + mkJitTest package
+101 -94
nixos/tests/postgresql-wal-receiver.nix
··· 1 { system ? builtins.currentSystem, 2 config ? {}, 3 - pkgs ? import ../.. { inherit system config; } 4 }: 5 6 with import ../lib/testing-python.nix { inherit system pkgs; }; ··· 9 lib = pkgs.lib; 10 11 # Makes a test for a PostgreSQL package, given by name and looked up from `pkgs`. 12 - makePostgresqlWalReceiverTest = postgresqlPackage: 13 { 14 - name = postgresqlPackage; 15 - value = 16 - let 17 - pkg = pkgs."${postgresqlPackage}"; 18 - postgresqlDataDir = "/var/lib/postgresql/${pkg.psqlSchema}"; 19 - replicationUser = "wal_receiver_user"; 20 - replicationSlot = "wal_receiver_slot"; 21 - replicationConn = "postgresql://${replicationUser}@localhost"; 22 - baseBackupDir = "/tmp/pg_basebackup"; 23 - walBackupDir = "/tmp/pg_wal"; 24 - atLeast12 = lib.versionAtLeast pkg.version "12.0"; 25 26 - recoveryFile = if atLeast12 27 - then pkgs.writeTextDir "recovery.signal" "" 28 - else pkgs.writeTextDir "recovery.conf" "restore_command = 'cp ${walBackupDir}/%f %p'"; 29 30 - in makeTest { 31 - name = "postgresql-wal-receiver-${postgresqlPackage}"; 32 - meta.maintainers = with lib.maintainers; [ pacien ]; 33 34 - nodes.machine = { ... }: { 35 - services.postgresql = { 36 - package = pkg; 37 - enable = true; 38 - settings = lib.mkMerge [ 39 - { 40 - wal_level = "archive"; # alias for replica on pg >= 9.6 41 - max_wal_senders = 10; 42 - max_replication_slots = 10; 43 - } 44 - (lib.mkIf atLeast12 { 45 - restore_command = "cp ${walBackupDir}/%f %p"; 46 - recovery_end_command = "touch recovery.done"; 47 - }) 48 - ]; 49 - authentication = '' 50 - host replication ${replicationUser} all trust 51 - ''; 52 - initialScript = pkgs.writeText "init.sql" '' 53 - create user ${replicationUser} replication; 54 - select * from pg_create_physical_replication_slot('${replicationSlot}'); 55 - ''; 56 - }; 57 58 - services.postgresqlWalReceiver.receivers.main = { 59 - postgresqlPackage = pkg; 60 - connection = replicationConn; 61 - slot = replicationSlot; 62 - directory = walBackupDir; 63 - }; 64 - # This is only to speedup test, it isn't time racing. Service is set to autorestart always, 65 - # default 60sec is fine for real system, but is too much for a test 66 - systemd.services.postgresql-wal-receiver-main.serviceConfig.RestartSec = lib.mkForce 5; 67 }; 68 69 - testScript = '' 70 - # make an initial base backup 71 - machine.wait_for_unit("postgresql") 72 - machine.wait_for_unit("postgresql-wal-receiver-main") 73 - # WAL receiver healthchecks PG every 5 seconds, so let's be sure they have connected each other 74 - # required only for 9.4 75 - machine.sleep(5) 76 - machine.succeed( 77 - "${pkg}/bin/pg_basebackup --dbname=${replicationConn} --pgdata=${baseBackupDir}" 78 - ) 79 80 - # create a dummy table with 100 records 81 - machine.succeed( 82 - "sudo -u postgres psql --command='create table dummy as select * from generate_series(1, 100) as val;'" 83 - ) 84 85 - # stop postgres and destroy data 86 - machine.systemctl("stop postgresql") 87 - machine.systemctl("stop postgresql-wal-receiver-main") 88 - machine.succeed("rm -r ${postgresqlDataDir}/{base,global,pg_*}") 89 90 - # restore the base backup 91 - machine.succeed( 92 - "cp -r ${baseBackupDir}/* ${postgresqlDataDir} && chown postgres:postgres -R ${postgresqlDataDir}" 93 - ) 94 95 - # prepare WAL and recovery 96 - machine.succeed("chmod a+rX -R ${walBackupDir}") 97 - machine.execute( 98 - "for part in ${walBackupDir}/*.partial; do mv $part ''${part%%.*}; done" 99 - ) # make use of partial segments too 100 - machine.succeed( 101 - "cp ${recoveryFile}/* ${postgresqlDataDir}/ && chmod 666 ${postgresqlDataDir}/recovery*" 102 - ) 103 104 - # replay WAL 105 - machine.systemctl("start postgresql") 106 - machine.wait_for_file("${postgresqlDataDir}/recovery.done") 107 - machine.systemctl("restart postgresql") 108 - machine.wait_for_unit("postgresql") 109 110 - # check that our records have been restored 111 - machine.succeed( 112 - "test $(sudo -u postgres psql --pset='pager=off' --tuples-only --command='select count(distinct val) from dummy;') -eq 100" 113 - ) 114 - ''; 115 - }; 116 }; 117 118 - # Maps the generic function over all attributes of PostgreSQL packages 119 - in builtins.listToAttrs (map makePostgresqlWalReceiverTest (builtins.attrNames (import ../../pkgs/servers/sql/postgresql pkgs)))
··· 1 { system ? builtins.currentSystem, 2 config ? {}, 3 + pkgs ? import ../.. { inherit system config; }, 4 + package ? null 5 }: 6 7 with import ../lib/testing-python.nix { inherit system pkgs; }; ··· 10 lib = pkgs.lib; 11 12 # Makes a test for a PostgreSQL package, given by name and looked up from `pkgs`. 13 + makeTestAttribute = name: 14 { 15 + inherit name; 16 + value = makePostgresqlWalReceiverTest pkgs."${name}"; 17 + }; 18 19 + makePostgresqlWalReceiverTest = pkg: 20 + let 21 + postgresqlDataDir = "/var/lib/postgresql/${pkg.psqlSchema}"; 22 + replicationUser = "wal_receiver_user"; 23 + replicationSlot = "wal_receiver_slot"; 24 + replicationConn = "postgresql://${replicationUser}@localhost"; 25 + baseBackupDir = "/tmp/pg_basebackup"; 26 + walBackupDir = "/tmp/pg_wal"; 27 + atLeast12 = lib.versionAtLeast pkg.version "12.0"; 28 29 + recoveryFile = if atLeast12 30 + then pkgs.writeTextDir "recovery.signal" "" 31 + else pkgs.writeTextDir "recovery.conf" "restore_command = 'cp ${walBackupDir}/%f %p'"; 32 33 + in makeTest { 34 + name = "postgresql-wal-receiver-${pkg.name}"; 35 + meta.maintainers = with lib.maintainers; [ pacien ]; 36 + 37 + nodes.machine = { ... }: { 38 + services.postgresql = { 39 + package = pkg; 40 + enable = true; 41 + settings = lib.mkMerge [ 42 + { 43 + wal_level = "archive"; # alias for replica on pg >= 9.6 44 + max_wal_senders = 10; 45 + max_replication_slots = 10; 46 + } 47 + (lib.mkIf atLeast12 { 48 + restore_command = "cp ${walBackupDir}/%f %p"; 49 + recovery_end_command = "touch recovery.done"; 50 + }) 51 + ]; 52 + authentication = '' 53 + host replication ${replicationUser} all trust 54 + ''; 55 + initialScript = pkgs.writeText "init.sql" '' 56 + create user ${replicationUser} replication; 57 + select * from pg_create_physical_replication_slot('${replicationSlot}'); 58 + ''; 59 + }; 60 61 + services.postgresqlWalReceiver.receivers.main = { 62 + postgresqlPackage = pkg; 63 + connection = replicationConn; 64 + slot = replicationSlot; 65 + directory = walBackupDir; 66 }; 67 + # This is only to speedup test, it isn't time racing. Service is set to autorestart always, 68 + # default 60sec is fine for real system, but is too much for a test 69 + systemd.services.postgresql-wal-receiver-main.serviceConfig.RestartSec = lib.mkForce 5; 70 + }; 71 72 + testScript = '' 73 + # make an initial base backup 74 + machine.wait_for_unit("postgresql") 75 + machine.wait_for_unit("postgresql-wal-receiver-main") 76 + # WAL receiver healthchecks PG every 5 seconds, so let's be sure they have connected each other 77 + # required only for 9.4 78 + machine.sleep(5) 79 + machine.succeed( 80 + "${pkg}/bin/pg_basebackup --dbname=${replicationConn} --pgdata=${baseBackupDir}" 81 + ) 82 83 + # create a dummy table with 100 records 84 + machine.succeed( 85 + "sudo -u postgres psql --command='create table dummy as select * from generate_series(1, 100) as val;'" 86 + ) 87 88 + # stop postgres and destroy data 89 + machine.systemctl("stop postgresql") 90 + machine.systemctl("stop postgresql-wal-receiver-main") 91 + machine.succeed("rm -r ${postgresqlDataDir}/{base,global,pg_*}") 92 93 + # restore the base backup 94 + machine.succeed( 95 + "cp -r ${baseBackupDir}/* ${postgresqlDataDir} && chown postgres:postgres -R ${postgresqlDataDir}" 96 + ) 97 98 + # prepare WAL and recovery 99 + machine.succeed("chmod a+rX -R ${walBackupDir}") 100 + machine.execute( 101 + "for part in ${walBackupDir}/*.partial; do mv $part ''${part%%.*}; done" 102 + ) # make use of partial segments too 103 + machine.succeed( 104 + "cp ${recoveryFile}/* ${postgresqlDataDir}/ && chmod 666 ${postgresqlDataDir}/recovery*" 105 + ) 106 107 + # replay WAL 108 + machine.systemctl("start postgresql") 109 + machine.wait_for_file("${postgresqlDataDir}/recovery.done") 110 + machine.systemctl("restart postgresql") 111 + machine.wait_for_unit("postgresql") 112 113 + # check that our records have been restored 114 + machine.succeed( 115 + "test $(sudo -u postgres psql --pset='pager=off' --tuples-only --command='select count(distinct val) from dummy;') -eq 100" 116 + ) 117 + ''; 118 }; 119 120 + in 121 + if package == null then 122 + # all-tests.nix: Maps the generic function over all attributes of PostgreSQL packages 123 + builtins.listToAttrs (map makeTestAttribute (builtins.attrNames (import ../../pkgs/servers/sql/postgresql pkgs))) 124 + else 125 + # Called directly from <package>.tests 126 + makePostgresqlWalReceiverTest package
-1
pkgs/servers/sql/postgresql/default.nix
··· 15 in 16 self.lib.nameValuePair attrName (import path { 17 inherit jitSupport self; 18 - thisAttr = attrName; 19 }) 20 ) versions; 21
··· 15 in 16 self.lib.nameValuePair attrName (import path { 17 inherit jitSupport self; 18 }) 19 ) versions; 20
+11 -3
pkgs/servers/sql/postgresql/generic.nix
··· 19 , version, hash, muslPatches 20 21 # for tests 22 - , testers, nixosTests, thisAttr 23 24 # JIT 25 , jitSupport ··· 254 this.pkgs; 255 256 tests = { 257 - postgresql = nixosTests.postgresql-wal-receiver.${thisAttr}; 258 pkg-config = testers.testMetaPkgConfig finalAttrs.finalPackage; 259 } // lib.optionalAttrs jitSupport { 260 - postgresql-jit = nixosTests.postgresql-jit.${thisAttr}; 261 }; 262 }; 263
··· 19 , version, hash, muslPatches 20 21 # for tests 22 + , testers, nixosTests 23 24 # JIT 25 , jitSupport ··· 254 this.pkgs; 255 256 tests = { 257 + postgresql-wal-receiver = import ../../../../nixos/tests/postgresql-wal-receiver.nix { 258 + inherit (stdenv) system; 259 + pkgs = self; 260 + package = this; 261 + }; 262 pkg-config = testers.testMetaPkgConfig finalAttrs.finalPackage; 263 } // lib.optionalAttrs jitSupport { 264 + postgresql-jit = import ../../../../nixos/tests/postgresql-jit.nix { 265 + inherit (stdenv) system; 266 + pkgs = self; 267 + package = this; 268 + }; 269 }; 270 }; 271