Skip to content

Commit 0e0a3d3

Browse files
jfrochesamrose
authored andcommitted
Refactor postgres extensions tests
Keep the postgres extension tests DRY by extracting shared logic into a shared library.
1 parent f0a8927 commit 0e0a3d3

File tree

10 files changed

+378
-676
lines changed

10 files changed

+378
-676
lines changed

nix/checks.nix

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -308,20 +308,14 @@
308308
postgresql_17_src
309309
;
310310
}
311-
// pkgs.lib.optionalAttrs (system == "x86_64-linux") {
312-
pg_cron = import ./ext/tests/pg_cron.nix {
311+
// pkgs.lib.optionalAttrs (system == "x86_64-linux") (
312+
{
313+
devShell = self'.devShells.default;
314+
}
315+
// (import ./ext/tests {
313316
inherit self;
314317
inherit pkgs;
315-
};
316-
pg_net = import ./ext/tests/pg_net.nix {
317-
inherit self;
318-
inherit pkgs;
319-
};
320-
wrappers = import ./ext/tests/wrappers.nix {
321-
inherit self;
322-
inherit pkgs;
323-
};
324-
devShell = self'.devShells.default;
325-
};
318+
})
319+
);
326320
};
327321
}

nix/ext/index_advisor.nix

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
stdenv,
55
fetchFromGitHub,
66
postgresql,
7+
callPackage,
78
}:
89

910
let
@@ -69,7 +70,8 @@ let
6970
in
7071
pkgs.buildEnv {
7172
name = pname;
72-
paths = packages;
73+
# Add dependency on hypopg for the extension to work
74+
paths = packages ++ [ (callPackage ./hypopg.nix { inherit postgresql; }) ];
7375
pathsToLink = [
7476
"/lib"
7577
"/share/postgresql/extension"

nix/ext/pg_cron/default.nix

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ buildEnv {
111111
passthru = {
112112
inherit versions numberOfVersions switch-ext-version;
113113
pname = "${pname}-all";
114+
hasBackgroundWorker = true;
115+
defaultSettings = {
116+
shared_preload_libraries = [ "pg_cron" ];
117+
"cron.database_name" = "postgres";
118+
};
114119
version =
115120
"multi-" + lib.concatStringsSep "-" (map (v: lib.replaceStrings [ "." ] [ "-" ] v) versions);
116121
};

nix/ext/pg_net.nix

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ pkgs.buildEnv {
118118
passthru = {
119119
inherit versions numberOfVersions;
120120
pname = "${pname}-all";
121+
hasBackgroundWorker = true;
122+
defaultSettings = {
123+
shared_preload_libraries = [ "pg_net" ];
124+
};
121125
version =
122126
"multi-" + lib.concatStringsSep "-" (map (v: lib.replaceStrings [ "." ] [ "-" ] v) versions);
123127
};

nix/ext/tests/default.nix

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
{ self, pkgs }:
2+
let
3+
testsDir = ./.;
4+
testFiles = builtins.attrNames (builtins.readDir testsDir);
5+
nixFiles = builtins.filter (
6+
name: builtins.match ".*\\.nix$" name != null && name != "default.nix"
7+
) testFiles;
8+
extTest =
9+
extension_name:
10+
let
11+
pname = extension_name;
12+
inherit (pkgs) lib;
13+
installedExtension =
14+
postgresMajorVersion: self.packages.${pkgs.system}."psql_${postgresMajorVersion}/exts/${pname}-all";
15+
versions = postgresqlMajorVersion: (installedExtension postgresqlMajorVersion).versions;
16+
postgresqlWithExtension =
17+
postgresql:
18+
let
19+
majorVersion = lib.versions.major postgresql.version;
20+
pkg = pkgs.buildEnv {
21+
name = "postgresql-${majorVersion}-${pname}";
22+
paths = [
23+
postgresql
24+
postgresql.lib
25+
(installedExtension majorVersion)
26+
];
27+
passthru = {
28+
inherit (postgresql) version psqlSchema;
29+
lib = pkg;
30+
withPackages = _: pkg;
31+
};
32+
nativeBuildInputs = [ pkgs.makeWrapper ];
33+
pathsToLink = [
34+
"/"
35+
"/bin"
36+
"/lib"
37+
];
38+
postBuild = ''
39+
wrapProgram $out/bin/postgres --set NIX_PGLIBDIR $out/lib
40+
wrapProgram $out/bin/pg_ctl --set NIX_PGLIBDIR $out/lib
41+
wrapProgram $out/bin/pg_upgrade --set NIX_PGLIBDIR $out/lib
42+
'';
43+
};
44+
in
45+
pkg;
46+
psql_15 = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_15;
47+
psql_17 = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_17;
48+
in
49+
self.inputs.nixpkgs.lib.nixos.runTest {
50+
name = pname;
51+
hostPkgs = pkgs;
52+
nodes.server =
53+
{ config, ... }:
54+
{
55+
virtualisation = {
56+
forwardPorts = [
57+
{
58+
from = "host";
59+
host.port = 13022;
60+
guest.port = 22;
61+
}
62+
];
63+
};
64+
services.openssh = {
65+
enable = true;
66+
};
67+
68+
services.postgresql = {
69+
enable = true;
70+
package = psql_15;
71+
enableTCPIP = true;
72+
initialScript = pkgs.writeText "init-postgres-with-password" ''
73+
CREATE USER test WITH PASSWORD 'secret';
74+
'';
75+
authentication = ''
76+
host test postgres samenet scram-sha-256
77+
'';
78+
settings = (installedExtension "15").defaultSettings or { };
79+
};
80+
81+
networking.firewall.allowedTCPPorts = [ config.services.postgresql.settings.port ];
82+
83+
specialisation.postgresql17.configuration = {
84+
services.postgresql = {
85+
package = lib.mkForce psql_17;
86+
};
87+
88+
systemd.services.postgresql-migrate = {
89+
serviceConfig = {
90+
Type = "oneshot";
91+
RemainAfterExit = true;
92+
User = "postgres";
93+
Group = "postgres";
94+
StateDirectory = "postgresql";
95+
WorkingDirectory = "${builtins.dirOf config.services.postgresql.dataDir}";
96+
};
97+
script =
98+
let
99+
oldPostgresql = psql_15;
100+
newPostgresql = psql_17;
101+
oldDataDir = "${builtins.dirOf config.services.postgresql.dataDir}/${oldPostgresql.psqlSchema}";
102+
newDataDir = "${builtins.dirOf config.services.postgresql.dataDir}/${newPostgresql.psqlSchema}";
103+
in
104+
''
105+
if [[ ! -d ${newDataDir} ]]; then
106+
install -d -m 0700 -o postgres -g postgres "${newDataDir}"
107+
${newPostgresql}/bin/initdb -D "${newDataDir}"
108+
${newPostgresql}/bin/pg_upgrade --old-datadir "${oldDataDir}" --new-datadir "${newDataDir}" \
109+
--old-bindir "${oldPostgresql}/bin" --new-bindir "${newPostgresql}/bin"
110+
else
111+
echo "${newDataDir} already exists"
112+
fi
113+
'';
114+
};
115+
116+
systemd.services.postgresql = {
117+
after = [ "postgresql-migrate.service" ];
118+
requires = [ "postgresql-migrate.service" ];
119+
};
120+
};
121+
};
122+
testScript =
123+
{ nodes, ... }:
124+
let
125+
pg17-configuration = "${nodes.server.system.build.toplevel}/specialisation/postgresql17";
126+
in
127+
''
128+
versions = {
129+
"15": [${lib.concatStringsSep ", " (map (s: ''"${s}"'') (versions "15"))}],
130+
"17": [${lib.concatStringsSep ", " (map (s: ''"${s}"'') (versions "17"))}],
131+
}
132+
extension_name = "${pname}"
133+
support_upgrade = True
134+
pg17_configuration = "${pg17-configuration}"
135+
ext_has_background_worker = ${
136+
if (installedExtension "15") ? hasBackgroundWorker then "True" else "False"
137+
}
138+
139+
${builtins.readFile ./lib.py}
140+
141+
start_all()
142+
143+
server.wait_for_unit("multi-user.target")
144+
server.wait_for_unit("postgresql.service")
145+
146+
test = PostgresExtensionTest(server, extension_name, versions, support_upgrade)
147+
148+
with subtest("Check upgrade path with postgresql 15"):
149+
test.check_upgrade_path("15")
150+
151+
last_version = None
152+
with subtest("Check the install of the last version of the extension"):
153+
last_version = test.check_install_last_version("15")
154+
155+
if ext_has_background_worker:
156+
with subtest("Test switch_${pname}_version"):
157+
test.check_switch_extension_with_background_worker(Path("${psql_15}/lib/${pname}.so"), "15")
158+
159+
with subtest("switch to postgresql 17"):
160+
server.succeed(
161+
f"{pg17_configuration}/bin/switch-to-configuration test >&2"
162+
)
163+
164+
with subtest("Check last version of the extension after upgrade"):
165+
test.assert_version_matches(last_version)
166+
167+
with subtest("Check upgrade path with postgresql 17"):
168+
test.check_upgrade_path("17")
169+
'';
170+
};
171+
in
172+
builtins.listToAttrs (
173+
map (file: {
174+
name = "ext-" + builtins.replaceStrings [ ".nix" ] [ "" ] file;
175+
value = import (testsDir + "/${file}") { inherit self pkgs; };
176+
}) nixFiles
177+
)
178+
// builtins.listToAttrs (
179+
map
180+
(extName: {
181+
name = "ext-${extName}";
182+
value = extTest extName;
183+
})
184+
[
185+
"index_advisor"
186+
"pg_cron"
187+
"pg_net"
188+
"wrappers"
189+
]
190+
)

0 commit comments

Comments
 (0)