Skip to content

Commit 3553505

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 1081090 commit 3553505

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;
@@ -44,97 +41,17 @@ let
4441
};
4542
in
4643
pkg;
44+
testLib = import ./lib.nix {
45+
inherit self pkgs;
46+
testedExtensionName = extension_name;
47+
};
4748
psql_15 = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_15;
4849
psql_17 = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_17;
4950
in
5051
self.inputs.nixpkgs.lib.nixos.runTest {
5152
name = pname;
5253
hostPkgs = pkgs;
53-
nodes.server =
54-
{ config, ... }:
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 = {
93-
services.postgresql = {
94-
package = 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-
};
54+
nodes.server = { config, ... }: testLib.mkDefaultNixosTestNode { inherit config psql_15 psql_17; };
13855
testScript =
13956
{ nodes, ... }:
14057
let
@@ -150,12 +67,13 @@ let
15067
support_upgrade = True
15168
pg17_configuration = "${pg17-configuration}"
15269
ext_has_background_worker = ${
153-
if (installedExtension "15") ? hasBackgroundWorker then "True" else "False"
70+
if (testLib.installedExtension "15") ? hasBackgroundWorker then "True" else "False"
15471
}
15572
sql_test_directory = Path("${../../tests}")
156-
pg_regress_test_name = "${(installedExtension "15").pgRegressTestName or pname}"
157-
ext_schema = "${(installedExtension "15").defaultSchema or "public"}"
158-
lib_name = "${(installedExtension "15").libName or pname}"
73+
74+
pg_regress_test_name = "${(testLib.installedExtension "15").pgRegressTestName or pname}"
75+
ext_schema = "${(testLib.installedExtension "15").defaultSchema or "public"}"
76+
lib_name = "${(testLib.installedExtension "15").libName or pname}"
15977
print(f"Running tests for extension: {lib_name}")
16078
16179
${builtins.readFile ./lib.py}

nix/ext/tests/http.nix

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