Skip to content

Commit

Permalink
nixosTests.postgresql: use a common pattern throughout all tests
Browse files Browse the repository at this point in the history
Avoiding "with", using the same names and basic structure in each test.

Consistency is key!
  • Loading branch information
wolfgangwalther committed Nov 8, 2024
1 parent 274977f commit 066ab04
Show file tree
Hide file tree
Showing 10 changed files with 393 additions and 399 deletions.
12 changes: 6 additions & 6 deletions nixos/tests/postgresql/anonymizer.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
let
inherit (pkgs) lib;

makeAnonymizerTest = postgresqlPackage:
makeTestFor = package:
makeTest {
name = "postgresql_anonymizer-${postgresqlPackage.name}";
name = "postgresql_anonymizer-${package.name}";
meta.maintainers = lib.teams.flyingcircus.members;

nodes.machine = { pkgs, ... }: {
environment.systemPackages = [ pkgs.pg-dump-anon ];
services.postgresql = {
inherit package;
enable = true;
package = postgresqlPackage;
extraPlugins = ps: [ ps.anonymizer ];
settings.shared_preload_libraries = [ "anon" ];
};
Expand Down Expand Up @@ -102,9 +102,9 @@ let
'';
};
in
pkgs.lib.recurseIntoAttrs (
pkgs.lib.concatMapAttrs (n: p: { ${n} = makeAnonymizerTest p; }) pkgs.postgresqlVersions
lib.recurseIntoAttrs (
lib.concatMapAttrs (n: p: { ${n} = makeTestFor p; }) (lib.filterAttrs (_: p: !p.pkgs.anonymizer.meta.broken) pkgs.postgresqlVersions)
// {
passthru.override = p: makeAnonymizerTest p;
passthru.override = p: makeTestFor p;
}
)
14 changes: 6 additions & 8 deletions nixos/tests/postgresql/pgjwt.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,21 @@
let
inherit (pkgs) lib;

makePgjwtTest = postgresqlPackage:
makeTestFor = package:
makeTest {
name = "pgjwt-${postgresqlPackage.name}";
name = "pgjwt-${package.name}";
meta = with lib.maintainers; {
maintainers = [ spinus willibutz ];
};

nodes = {
master = { ... }:
nodes.master = { ... }:
{
services.postgresql = {
inherit package;
enable = true;
package = postgresqlPackage;
extraPlugins = ps: with ps; [ pgjwt pgtap ];
};
};
};

testScript = { nodes, ... }:
let
Expand All @@ -39,8 +37,8 @@ let
};
in
lib.recurseIntoAttrs (
lib.concatMapAttrs (n: p: { ${n} = makePgjwtTest p; }) pkgs.postgresqlVersions
lib.concatMapAttrs (n: p: { ${n} = makeTestFor p; }) (lib.filterAttrs (_: p: !p.pkgs.pgjwt.meta.broken) pkgs.postgresqlVersions)
// {
passthru.override = p: makePgjwtTest p;
passthru.override = p: makeTestFor p;
}
)
78 changes: 39 additions & 39 deletions nixos/tests/postgresql/pgvecto-rs.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
, makeTest
}:

with pkgs.lib;

let
inherit (pkgs) lib;

# Test cases from https://docs.pgvecto.rs/use-cases/hybrid-search.html
test-sql = pkgs.writeText "postgresql-test" ''
CREATE EXTENSION vectors;
Expand All @@ -22,53 +22,53 @@ let
('a thin dog sat on a mat and ate a thin rat', '[10, 11, 12]');
'';

makePgVectorsTest = postgresqlPackage: makeTest {
name = "pgvecto-rs-${postgresqlPackage.name}";
meta = with pkgs.lib.maintainers; {
maintainers = [ diogotcorreia ];
};

nodes.machine = { ... }:
{
services.postgresql = {
enable = true;
package = postgresqlPackage;
extraPlugins = ps: with ps; [
pgvecto-rs
];
settings.shared_preload_libraries = "vectors";
};
makeTestFor = postgresqlPackage:
makeTest {
name = "pgvecto-rs-${postgresqlPackage.name}";
meta = with lib.maintainers; {
maintainers = [ diogotcorreia ];
};

testScript = { nodes, ... }:
let
inherit (nodes.machine.services.postgresql.package.pkgs) pgvecto-rs;
in
''
def check_count(statement, lines):
return 'test $(sudo -u postgres psql postgres -tAc "{}"|wc -l) -eq {}'.format(
statement, lines
)
nodes.machine = { ... }:
{
services.postgresql = {
enable = true;
package = postgresqlPackage;
extraPlugins = ps: with ps; [
pgvecto-rs
];
settings.shared_preload_libraries = "vectors";
};
};

testScript = { nodes, ... }:
let
inherit (nodes.machine.services.postgresql.package.pkgs) pgvecto-rs;
in
''
def check_count(statement, lines):
return 'test $(sudo -u postgres psql postgres -tAc "{}"|wc -l) -eq {}'.format(
statement, lines
)
machine.start()
machine.wait_for_unit("postgresql")
with subtest("Postgresql with extension vectors is available just after unit start"):
machine.succeed(check_count("SELECT * FROM pg_available_extensions WHERE name = 'vectors' AND default_version = '${pgvecto-rs.version}';", 1))
machine.start()
machine.wait_for_unit("postgresql")
machine.succeed("sudo -u postgres psql -f ${test-sql}")
with subtest("Postgresql with extension vectors is available just after unit start"):
machine.succeed(check_count("SELECT * FROM pg_available_extensions WHERE name = 'vectors' AND default_version = '${pgvecto-rs.version}';", 1))
machine.succeed(check_count("SELECT content, embedding FROM items WHERE to_tsvector('english', content) @@ 'cat & rat'::tsquery;", 2))
machine.succeed("sudo -u postgres psql -f ${test-sql}")
machine.shutdown()
'';
machine.succeed(check_count("SELECT content, embedding FROM items WHERE to_tsvector('english', content) @@ 'cat & rat'::tsquery;", 2))
};
machine.shutdown()
'';
};
in
recurseIntoAttrs (
concatMapAttrs (n: p: { ${n} = makePgVectorsTest p; }) (filterAttrs (n: p: !p.pkgs.pgvecto-rs.meta.broken) pkgs.postgresqlVersions)
lib.recurseIntoAttrs (
lib.concatMapAttrs (n: p: { ${n} = makeTestFor p; }) (lib.filterAttrs (_: p: !p.pkgs.pgvecto-rs.meta.broken) pkgs.postgresqlVersions)
// {
passthru.override = p: makePgVectorsTest p;
passthru.override = p: makeTestFor p;
}
)
69 changes: 36 additions & 33 deletions nixos/tests/postgresql/postgresql-jit.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,48 @@
let
inherit (pkgs) lib;

mkJitTest = package: makeTest {
name = "postgresql-jit-${package.name}";
meta.maintainers = with lib.maintainers; [ ma27 ];
nodes.machine = { pkgs, lib, ... }: {
services.postgresql = {
inherit package;
enable = true;
enableJIT = true;
initialScript = pkgs.writeText "init.sql" ''
create table demo (id int);
insert into demo (id) select generate_series(1, 5);
'';
makeTestFor = package:
makeTest {
name = "postgresql-jit-${package.name}";
meta.maintainers = with lib.maintainers; [ ma27 ];

nodes.machine = { pkgs, ... }: {
services.postgresql = {
inherit package;
enable = true;
enableJIT = true;
initialScript = pkgs.writeText "init.sql" ''
create table demo (id int);
insert into demo (id) select generate_series(1, 5);
'';
};
};
};
testScript = ''
machine.start()
machine.wait_for_unit("postgresql.service")

with subtest("JIT is enabled"):
machine.succeed("sudo -u postgres psql <<<'show jit;' | grep 'on'")
testScript = ''
machine.start()
machine.wait_for_unit("postgresql.service")
with subtest("Test JIT works fine"):
output = machine.succeed(
"cat ${pkgs.writeText "test.sql" ''
set jit_above_cost = 1;
EXPLAIN ANALYZE SELECT CONCAT('jit result = ', SUM(id)) FROM demo;
SELECT CONCAT('jit result = ', SUM(id)) from demo;
''} | sudo -u postgres psql"
)
assert "JIT:" in output
assert "jit result = 15" in output
with subtest("JIT is enabled"):
machine.succeed("sudo -u postgres psql <<<'show jit;' | grep 'on'")
machine.shutdown()
'';
};
with subtest("Test JIT works fine"):
output = machine.succeed(
"cat ${pkgs.writeText "test.sql" ''
set jit_above_cost = 1;
EXPLAIN ANALYZE SELECT CONCAT('jit result = ', SUM(id)) FROM demo;
SELECT CONCAT('jit result = ', SUM(id)) from demo;
''} | sudo -u postgres psql"
)
assert "JIT:" in output
assert "jit result = 15" in output
machine.shutdown()
'';
};
in
lib.recurseIntoAttrs (
lib.concatMapAttrs (n: p: { ${n} = mkJitTest p; }) (lib.filterAttrs (n: _: lib.hasSuffix "_jit" n) pkgs.postgresqlVersions)
lib.concatMapAttrs (n: p: { ${n} = makeTestFor p; }) (lib.filterAttrs (n: _: lib.hasSuffix "_jit" n) pkgs.postgresqlVersions)
// {
passthru.override = p: mkJitTest p;
passthru.override = p: makeTestFor p;
}
)
75 changes: 36 additions & 39 deletions nixos/tests/postgresql/postgresql-tls-client-cert.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,41 @@
}:

let
lib = pkgs.lib;
inherit (pkgs) lib;

makePostgresqlTlsClientCertTest = pkg:
let
runWithOpenSSL = file: cmd: pkgs.runCommand file
{
buildInputs = [ pkgs.openssl ];
}
cmd;
caKey = runWithOpenSSL "ca.key" "openssl ecparam -name prime256v1 -genkey -noout -out $out";
caCert = runWithOpenSSL
"ca.crt"
''
openssl req -new -x509 -sha256 -key ${caKey} -out $out -subj "/CN=test.example" -days 36500
'';
serverKey =
runWithOpenSSL "server.key" "openssl ecparam -name prime256v1 -genkey -noout -out $out";
serverKeyPath = "/var/lib/postgresql";
serverCert =
runWithOpenSSL "server.crt" ''
openssl req -new -sha256 -key ${serverKey} -out server.csr -subj "/CN=db.test.example"
openssl x509 -req -in server.csr -CA ${caCert} -CAkey ${caKey} \
-CAcreateserial -out $out -days 36500 -sha256
'';
clientKey =
runWithOpenSSL "client.key" "openssl ecparam -name prime256v1 -genkey -noout -out $out";
clientCert =
runWithOpenSSL "client.crt" ''
openssl req -new -sha256 -key ${clientKey} -out client.csr -subj "/CN=test"
openssl x509 -req -in client.csr -CA ${caCert} -CAkey ${caKey} \
-CAcreateserial -out $out -days 36500 -sha256
'';
clientKeyPath = "/root";
runWithOpenSSL = file: cmd: pkgs.runCommand file
{
buildInputs = [ pkgs.openssl ];
}
cmd;
caKey = runWithOpenSSL "ca.key" "openssl ecparam -name prime256v1 -genkey -noout -out $out";
caCert = runWithOpenSSL
"ca.crt"
''
openssl req -new -x509 -sha256 -key ${caKey} -out $out -subj "/CN=test.example" -days 36500
'';
serverKey =
runWithOpenSSL "server.key" "openssl ecparam -name prime256v1 -genkey -noout -out $out";
serverKeyPath = "/var/lib/postgresql";
serverCert =
runWithOpenSSL "server.crt" ''
openssl req -new -sha256 -key ${serverKey} -out server.csr -subj "/CN=db.test.example"
openssl x509 -req -in server.csr -CA ${caCert} -CAkey ${caKey} \
-CAcreateserial -out $out -days 36500 -sha256
'';
clientKey =
runWithOpenSSL "client.key" "openssl ecparam -name prime256v1 -genkey -noout -out $out";
clientCert =
runWithOpenSSL "client.crt" ''
openssl req -new -sha256 -key ${clientKey} -out client.csr -subj "/CN=test"
openssl x509 -req -in client.csr -CA ${caCert} -CAkey ${caKey} \
-CAcreateserial -out $out -days 36500 -sha256
'';
clientKeyPath = "/root";

in
makeTestFor = package:
makeTest {
name = "postgresql-tls-client-cert-${pkg.name}";
name = "postgresql-tls-client-cert-${package.name}";
meta.maintainers = with lib.maintainers; [ erictapen ];

nodes.server = { ... }: {
Expand All @@ -52,7 +50,7 @@ let
'';
};
services.postgresql = {
package = pkg;
inherit package;
enable = true;
enableTCPIP = true;
ensureUsers = [
Expand Down Expand Up @@ -102,7 +100,7 @@ let
PGSSLKEY = "${clientKeyPath}/client.key";
PGSSLROOTCERT = caCert;
};
systemPackages = [ pkg ];
systemPackages = [ package ];
};
networking = {
interfaces.eth1 = {
Expand All @@ -120,11 +118,10 @@ let
client.succeed("psql -c \"SELECT 1;\"")
'';
};

in
lib.recurseIntoAttrs (
lib.concatMapAttrs (n: p: { ${n} = makePostgresqlTlsClientCertTest p; }) pkgs.postgresqlVersions
lib.concatMapAttrs (n: p: { ${n} = makeTestFor p; }) pkgs.postgresqlVersions
// {
passthru.override = p: makePostgresqlTlsClientCertTest p;
passthru.override = p: makeTestFor p;
}
)
Loading

0 comments on commit 066ab04

Please sign in to comment.