Skip to content

Commit 8e9ed1f

Browse files
committed
refactor: DRY NixOS test configurations
Extract common NixOS test node configuration into common.nix to eliminate duplication across extension tests. Tests with custom requirements use lib.mkMerge to override defaults. This helps us to see at a glance why a test deviates from the standard setup.
1 parent 7473753 commit 8e9ed1f

File tree

14 files changed

+350
-809
lines changed

14 files changed

+350
-809
lines changed

nix/ext/plpgsql-check.nix

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,6 @@ buildEnv {
132132
passthru = {
133133
inherit versions numberOfVersions switch-ext-version;
134134
hasBackgroundWorker = true;
135-
defaultSettings = {
136-
shared_preload_libraries = [
137-
"plpgsql"
138-
"plpgsql_check"
139-
];
140-
};
141135
version =
142136
"multi-" + lib.concatStringsSep "-" (map (v: lib.replaceStrings [ "." ] [ "-" ] v) versions);
143137
};

nix/ext/tests/default.nix

Lines changed: 13 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,14 @@ let
33
testsDir = ./.;
44
testFiles = builtins.attrNames (builtins.readDir testsDir);
55
nixFiles = builtins.filter (
6-
name: builtins.match ".*\\.nix$" name != null && name != "default.nix"
6+
name: builtins.match ".*\\.nix$" name != null && name != "default.nix" && name != "lib.nix"
77
) testFiles;
88
extTest =
99
extension_name:
1010
let
1111
pname = extension_name;
1212
inherit (pkgs) lib;
13-
installedExtension =
14-
postgresMajorVersion:
15-
self.legacyPackages.${pkgs.system}."psql_${postgresMajorVersion}".exts."${pname}";
16-
versions = postgresqlMajorVersion: (installedExtension postgresqlMajorVersion).versions;
13+
versions = postgresqlMajorVersion: (testLib.installedExtension postgresqlMajorVersion).versions;
1714
postgresqlWithExtension =
1815
postgresql:
1916
let
@@ -23,7 +20,7 @@ let
2320
paths = [
2421
postgresql
2522
postgresql.lib
26-
(installedExtension majorVersion)
23+
(testLib.installedExtension majorVersion)
2724
];
2825
passthru = {
2926
inherit (postgresql) version psqlSchema;
@@ -47,97 +44,17 @@ let
4744
};
4845
in
4946
pkg;
47+
testLib = import ./lib.nix {
48+
inherit self pkgs;
49+
testedExtensionName = extension_name;
50+
};
5051
psql_15 = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_15;
5152
psql_17 = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_17;
5253
in
5354
self.inputs.nixpkgs.lib.nixos.runTest {
5455
name = pname;
5556
hostPkgs = pkgs;
56-
nodes.server =
57-
{ config, ... }:
58-
{
59-
virtualisation = {
60-
forwardPorts = [
61-
{
62-
from = "host";
63-
host.port = 13022;
64-
guest.port = 22;
65-
}
66-
];
67-
};
68-
services.openssh = {
69-
enable = true;
70-
};
71-
72-
services.postgresql = {
73-
enable = true;
74-
package = psql_15;
75-
enableTCPIP = true;
76-
authentication = ''
77-
local all postgres peer map=postgres
78-
local all all peer map=root
79-
'';
80-
identMap = ''
81-
root root supabase_admin
82-
postgres postgres postgres
83-
'';
84-
ensureUsers = [
85-
{
86-
name = "supabase_admin";
87-
ensureClauses.superuser = true;
88-
}
89-
];
90-
settings = (installedExtension "15").defaultSettings or { };
91-
};
92-
93-
networking.firewall.allowedTCPPorts = [ config.services.postgresql.settings.port ];
94-
95-
specialisation.postgresql17.configuration = {
96-
services.postgresql = {
97-
package = lib.mkForce psql_17;
98-
settings = (installedExtension "17").defaultSettings or { };
99-
};
100-
101-
systemd.services.postgresql-migrate = {
102-
serviceConfig = {
103-
Type = "oneshot";
104-
RemainAfterExit = true;
105-
User = "postgres";
106-
Group = "postgres";
107-
StateDirectory = "postgresql";
108-
WorkingDirectory = "${builtins.dirOf config.services.postgresql.dataDir}";
109-
};
110-
script =
111-
let
112-
oldPostgresql = psql_15;
113-
newPostgresql = psql_17;
114-
oldDataDir = "${builtins.dirOf config.services.postgresql.dataDir}/${oldPostgresql.psqlSchema}";
115-
newDataDir = "${builtins.dirOf config.services.postgresql.dataDir}/${newPostgresql.psqlSchema}";
116-
in
117-
''
118-
if [[ ! -d ${newDataDir} ]]; then
119-
install -d -m 0700 -o postgres -g postgres "${newDataDir}"
120-
${newPostgresql}/bin/initdb -D "${newDataDir}"
121-
${newPostgresql}/bin/pg_upgrade --old-datadir "${oldDataDir}" --new-datadir "${newDataDir}" \
122-
--old-bindir "${oldPostgresql}/bin" --new-bindir "${newPostgresql}/bin" \
123-
${
124-
if config.services.postgresql.settings.shared_preload_libraries != null then
125-
" --old-options='-c shared_preload_libraries=${config.services.postgresql.settings.shared_preload_libraries}' --new-options='-c shared_preload_libraries=${config.services.postgresql.settings.shared_preload_libraries}'"
126-
else
127-
""
128-
}
129-
else
130-
echo "${newDataDir} already exists"
131-
fi
132-
'';
133-
};
134-
135-
systemd.services.postgresql = {
136-
after = [ "postgresql-migrate.service" ];
137-
requires = [ "postgresql-migrate.service" ];
138-
};
139-
};
140-
};
57+
nodes.server = { config, ... }: testLib.mkDefaultNixosTestNode { inherit config psql_15 psql_17; };
14158
testScript =
14259
{ nodes, ... }:
14360
let
@@ -153,12 +70,13 @@ let
15370
support_upgrade = True
15471
pg17_configuration = "${pg17-configuration}"
15572
ext_has_background_worker = ${
156-
if (installedExtension "15") ? hasBackgroundWorker then "True" else "False"
73+
if (testLib.installedExtension "15") ? hasBackgroundWorker then "True" else "False"
15774
}
15875
sql_test_directory = Path("${../../tests}")
159-
pg_regress_test_name = "${(installedExtension "15").pgRegressTestName or pname}"
160-
ext_schema = "${(installedExtension "15").defaultSchema or "public"}"
161-
lib_name = "${(installedExtension "15").libName or pname}"
76+
77+
pg_regress_test_name = "${(testLib.installedExtension "15").pgRegressTestName or pname}"
78+
ext_schema = "${(testLib.installedExtension "15").defaultSchema or "public"}"
79+
lib_name = "${(testLib.installedExtension "15").libName or pname}"
16280
print(f"Running tests for extension: {lib_name}")
16381
16482
${builtins.readFile ./lib.py}

nix/ext/tests/http.nix

Lines changed: 7 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -39,70 +39,17 @@ let
3939
};
4040
in
4141
pkg;
42+
testLib = import ./lib.nix {
43+
inherit self pkgs;
44+
testedExtensionName = pname;
45+
};
46+
psql_15 = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_15;
47+
psql_17 = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_17;
4248
in
4349
self.inputs.nixpkgs.lib.nixos.runTest {
4450
name = pname;
4551
hostPkgs = pkgs;
46-
nodes.server =
47-
{ config, ... }:
48-
{
49-
virtualisation = {
50-
forwardPorts = [
51-
{
52-
from = "host";
53-
host.port = 13022;
54-
guest.port = 22;
55-
}
56-
];
57-
};
58-
services.openssh = {
59-
enable = true;
60-
};
61-
62-
services.postgresql = {
63-
enable = true;
64-
package = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_15;
65-
};
66-
67-
specialisation.postgresql17.configuration = {
68-
services.postgresql = {
69-
package = lib.mkForce (postgresqlWithExtension self.packages.${pkgs.system}.postgresql_17);
70-
};
71-
72-
systemd.services.postgresql-migrate = {
73-
serviceConfig = {
74-
Type = "oneshot";
75-
RemainAfterExit = true;
76-
User = "postgres";
77-
Group = "postgres";
78-
StateDirectory = "postgresql";
79-
WorkingDirectory = "${builtins.dirOf config.services.postgresql.dataDir}";
80-
};
81-
script =
82-
let
83-
oldPostgresql = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_15;
84-
newPostgresql = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_17;
85-
oldDataDir = "${builtins.dirOf config.services.postgresql.dataDir}/${oldPostgresql.psqlSchema}";
86-
newDataDir = "${builtins.dirOf config.services.postgresql.dataDir}/${newPostgresql.psqlSchema}";
87-
in
88-
''
89-
if [[ ! -d ${newDataDir} ]]; then
90-
install -d -m 0700 -o postgres -g postgres "${newDataDir}"
91-
${newPostgresql}/bin/initdb -D "${newDataDir}"
92-
${newPostgresql}/bin/pg_upgrade --old-datadir "${oldDataDir}" --new-datadir "${newDataDir}" \
93-
--old-bindir "${oldPostgresql}/bin" --new-bindir "${newPostgresql}/bin"
94-
else
95-
echo "${newDataDir} already exists"
96-
fi
97-
'';
98-
};
99-
100-
systemd.services.postgresql = {
101-
after = [ "postgresql-migrate.service" ];
102-
requires = [ "postgresql-migrate.service" ];
103-
};
104-
};
105-
};
52+
nodes.server = { config, ... }: testLib.mkDefaultNixosTestNode { inherit config psql_15 psql_17; };
10653
testScript =
10754
{ nodes, ... }:
10855
let

nix/ext/tests/lib.nix

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# lib.nix
2+
#
3+
# Common utilities and configuration builders for PostgreSQL extension tests.
4+
#
5+
# This module provides reusable functions to create NixOS test nodes with
6+
# standard PostgreSQL configurations, reducing duplication across extension
7+
# test files.
8+
#
9+
# ## Exports
10+
#
11+
# - `installedExtension`: Function to get the installed extension package for a PostgreSQL version
12+
# - `mkDefaultNixosTestNode`: Creates a NixOS test node with standard PostgreSQL setup
13+
#
14+
# ## Examples
15+
#
16+
# See existing test files for real-world examples:
17+
# - Simple tests: postgis.nix, http.nix, plpgsql_check.nix
18+
# - With custom settings: vault.nix, pgsodium.nix
19+
# - With environment variables: pgroonga.nix
20+
# - Complex specialisations: pgrouting.nix
21+
{
22+
self,
23+
pkgs,
24+
testedExtensionName,
25+
}:
26+
rec {
27+
# Get the installed extension package for a specific PostgreSQL major version.
28+
#
29+
# Type: installedExtension :: String -> Derivation
30+
#
31+
# Example:
32+
# installedExtension "15" => derivation
33+
installedExtension =
34+
postgresMajorVersion:
35+
self.legacyPackages.${pkgs.system}."psql_${postgresMajorVersion}".exts."${testedExtensionName}";
36+
37+
# Create a default NixOS test node with PostgreSQL configured for extension testing.
38+
#
39+
# When psql_17 is null, the postgresql17 specialisation is disabled.
40+
# This is useful for extensions that only support PostgreSQL 15.
41+
#
42+
# Override or extend configuration using lib.mkMerge:
43+
# Example:
44+
# lib.mkMerge [
45+
# (mkDefaultNixosTestNode { inherit config psql_15 psql_17; })
46+
# { services.postgresql.settings.shared_preload_libraries = lib.mkForce "my_ext"; }
47+
# ]
48+
mkDefaultNixosTestNode =
49+
{
50+
config, # The node's config attribute
51+
psql_15, # PostgreSQL 15 package with extension
52+
psql_17 ? null, # PostgreSQL 17 package with extension (optional)
53+
...
54+
}:
55+
{
56+
virtualisation = {
57+
forwardPorts = [
58+
{
59+
from = "host";
60+
host.port = 13022;
61+
guest.port = 22;
62+
}
63+
];
64+
};
65+
services.openssh = {
66+
enable = true;
67+
};
68+
69+
services.postgresql = {
70+
enable = true;
71+
package = psql_15;
72+
enableTCPIP = true;
73+
authentication = ''
74+
local all postgres peer map=postgres
75+
local all all peer map=root
76+
'';
77+
identMap = ''
78+
root root supabase_admin
79+
postgres postgres postgres
80+
'';
81+
ensureUsers = [
82+
{
83+
name = "supabase_admin";
84+
ensureClauses.superuser = true;
85+
}
86+
];
87+
settings = (installedExtension "15").defaultSettings or { };
88+
};
89+
90+
networking.firewall.allowedTCPPorts = [ config.services.postgresql.settings.port ];
91+
92+
specialisation.postgresql17.configuration = pkgs.lib.mkIf (psql_17 != null) {
93+
services.postgresql = {
94+
package = pkgs.lib.mkForce psql_17;
95+
settings = (installedExtension "17").defaultSettings or { };
96+
};
97+
98+
systemd.services.postgresql-migrate = {
99+
serviceConfig = {
100+
Type = "oneshot";
101+
RemainAfterExit = true;
102+
User = "postgres";
103+
Group = "postgres";
104+
StateDirectory = "postgresql";
105+
WorkingDirectory = "${builtins.dirOf config.services.postgresql.dataDir}";
106+
};
107+
script =
108+
let
109+
oldPostgresql = psql_15;
110+
newPostgresql = psql_17;
111+
oldDataDir = "${builtins.dirOf config.services.postgresql.dataDir}/${oldPostgresql.psqlSchema}";
112+
newDataDir = "${builtins.dirOf config.services.postgresql.dataDir}/${newPostgresql.psqlSchema}";
113+
in
114+
''
115+
if [[ ! -d ${newDataDir} ]]; then
116+
install -d -m 0700 -o postgres -g postgres "${newDataDir}"
117+
${newPostgresql}/bin/initdb -D "${newDataDir}"
118+
${newPostgresql}/bin/pg_upgrade --old-datadir "${oldDataDir}" --new-datadir "${newDataDir}" \
119+
--old-bindir "${oldPostgresql}/bin" --new-bindir "${newPostgresql}/bin" \
120+
${
121+
if config.services.postgresql.settings.shared_preload_libraries != null then
122+
" --old-options='-c shared_preload_libraries=${config.services.postgresql.settings.shared_preload_libraries}' --new-options='-c shared_preload_libraries=${config.services.postgresql.settings.shared_preload_libraries}'"
123+
else
124+
""
125+
}
126+
else
127+
echo "${newDataDir} already exists"
128+
fi
129+
'';
130+
};
131+
132+
systemd.services.postgresql = {
133+
after = [ "postgresql-migrate.service" ];
134+
requires = [ "postgresql-migrate.service" ];
135+
};
136+
};
137+
};
138+
}

0 commit comments

Comments
 (0)