Skip to content

Commit 0eacbc8

Browse files
committed
tests: supautils pg_regress
chore: cleanup
1 parent 99dabf4 commit 0eacbc8

File tree

5 files changed

+268
-0
lines changed

5 files changed

+268
-0
lines changed

nix/ext/tests/supautils.nix

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
{ self, pkgs }:
2+
let
3+
pname = "supautils";
4+
inherit (pkgs) lib;
5+
installedExtension =
6+
postgresMajorVersion: self.packages.${pkgs.system}."psql_${postgresMajorVersion}/exts/${pname}-all";
7+
versions = postgresqlMajorVersion: (installedExtension postgresqlMajorVersion).versions;
8+
postgresqlWithExtension =
9+
postgresql:
10+
let
11+
majorVersion = lib.versions.major postgresql.version;
12+
pkg = pkgs.buildEnv {
13+
name = "postgresql-${majorVersion}-${pname}";
14+
paths = [
15+
postgresql
16+
postgresql.lib
17+
(installedExtension majorVersion)
18+
];
19+
passthru = {
20+
inherit (postgresql) version psqlSchema;
21+
lib = pkg;
22+
withPackages = _: pkg;
23+
};
24+
nativeBuildInputs = [ pkgs.makeWrapper ];
25+
pathsToLink = [
26+
"/"
27+
"/bin"
28+
"/lib"
29+
];
30+
postBuild = ''
31+
wrapProgram $out/bin/postgres --set NIX_PGLIBDIR $out/lib
32+
wrapProgram $out/bin/pg_ctl --set NIX_PGLIBDIR $out/lib
33+
wrapProgram $out/bin/pg_upgrade --set NIX_PGLIBDIR $out/lib
34+
'';
35+
};
36+
in
37+
pkg;
38+
psql_15 = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_15;
39+
psql_17 = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_17;
40+
in
41+
self.inputs.nixpkgs.lib.nixos.runTest {
42+
name = pname;
43+
hostPkgs = pkgs;
44+
nodes.server =
45+
{ config, ... }:
46+
{
47+
services.postgresql = {
48+
enable = true;
49+
package = (postgresqlWithExtension psql_15);
50+
settings = {
51+
shared_preload_libraries = "supautils";
52+
"supautils.privileged_extensions" = "";
53+
} // ((installedExtension "15").defaultSettings or { });
54+
};
55+
56+
specialisation.postgresql17.configuration = {
57+
services.postgresql = {
58+
package = lib.mkForce psql_17;
59+
settings = {
60+
shared_preload_libraries = "supautils";
61+
"supautils.privileged_extensions" = "";
62+
} // ((installedExtension "17").defaultSettings or { });
63+
};
64+
65+
systemd.services.postgresql-migrate = {
66+
serviceConfig = {
67+
Type = "oneshot";
68+
RemainAfterExit = true;
69+
User = "postgres";
70+
Group = "postgres";
71+
StateDirectory = "postgresql";
72+
WorkingDirectory = "${builtins.dirOf config.services.postgresql.dataDir}";
73+
};
74+
script =
75+
let
76+
oldPostgresql = psql_15;
77+
newPostgresql = psql_17;
78+
oldDataDir = "${builtins.dirOf config.services.postgresql.dataDir}/${oldPostgresql.psqlSchema}";
79+
newDataDir = "${builtins.dirOf config.services.postgresql.dataDir}/${newPostgresql.psqlSchema}";
80+
in
81+
''
82+
if [[ ! -d ${newDataDir} ]]; then
83+
install -d -m 0700 -o postgres -g postgres "${newDataDir}"
84+
${newPostgresql}/bin/initdb -D "${newDataDir}"
85+
${newPostgresql}/bin/pg_upgrade --old-datadir "${oldDataDir}" --new-datadir "${newDataDir}" \
86+
--old-bindir "${oldPostgresql}/bin" --new-bindir "${newPostgresql}/bin" \
87+
--old-options='-c shared_preload_libraries=supautils' --new-options='-c shared_preload_libraries=supautils'
88+
else
89+
echo "${newDataDir} already exists"
90+
fi
91+
'';
92+
};
93+
94+
systemd.services.postgresql = {
95+
after = [ "postgresql-migrate.service" ];
96+
requires = [ "postgresql-migrate.service" ];
97+
};
98+
};
99+
};
100+
testScript =
101+
{ nodes, ... }:
102+
let
103+
pg17-configuration = "${nodes.server.system.build.toplevel}/specialisation/postgresql17";
104+
in
105+
''
106+
from pathlib import Path
107+
versions = {
108+
"15": [${lib.concatStringsSep ", " (map (s: ''"${s}"'') (versions "15"))}],
109+
"17": [${lib.concatStringsSep ", " (map (s: ''"${s}"'') (versions "17"))}],
110+
}
111+
extension_name = "${pname}"
112+
support_upgrade = False
113+
pg17_configuration = "${pg17-configuration}"
114+
ext_has_background_worker = ${
115+
if (installedExtension "15") ? hasBackgroundWorker then "True" else "False"
116+
}
117+
sql_test_directory = Path("${../../tests}")
118+
pg_regress_test_name = "${(installedExtension "15").pgRegressTestName or pname}"
119+
120+
${builtins.readFile ./lib.py}
121+
122+
start_all()
123+
124+
server.wait_for_unit("multi-user.target")
125+
server.wait_for_unit("postgresql.service")
126+
127+
test = PostgresExtensionTest(server, extension_name, versions, sql_test_directory, support_upgrade)
128+
129+
with subtest("Check pg_regress with postgresql 15 after extension upgrade"):
130+
test.check_pg_regress(Path("${psql_15}/lib/pgxs/src/test/regress/pg_regress"), "15", pg_regress_test_name)
131+
132+
with subtest("switch to postgresql 17"):
133+
server.succeed(
134+
f"{pg17_configuration}/bin/switch-to-configuration test >&2"
135+
)
136+
137+
with subtest("Check pg_regress with postgresql 17 after extension upgrade"):
138+
test.check_pg_regress(Path("${psql_17}/lib/pgxs/src/test/regress/pg_regress"), "17", pg_regress_test_name)
139+
'';
140+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
begin;
2+
load 'supautils';
3+
-- verify that supautils configuration parameters exist
4+
select current_setting('supautils.privileged_extensions', true) is not null as has_privileged_extensions;
5+
has_privileged_extensions
6+
---------------------------
7+
t
8+
(1 row)
9+
10+
select current_setting('supautils.privileged_role', true) is not null as has_privileged_role;
11+
has_privileged_role
12+
---------------------
13+
t
14+
(1 row)
15+
16+
-- switch to postgres role and verify access to settings
17+
set role postgres;
18+
select current_setting('supautils.privileged_extensions', true) as privileged_extensions;
19+
privileged_extensions
20+
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
21+
address_standardizer, address_standardizer_data_us, autoinc, bloom, btree_gin, btree_gist, citext, cube, dblink, dict_int, dict_xsyn, earthdistance, fuzzystrmatch, hstore, http, hypopg, index_advisor, insert_username, intarray, isn, ltree, moddatetime, orioledb, pg_buffercache, pg_cron, pg_graphql, pg_hashids, pg_jsonschema, pg_net, pg_prewarm, pg_repack, pg_stat_monitor, pg_stat_statements, pg_tle, pg_trgm, pg_walinspect, pgaudit, pgcrypto, pgjwt, pgroonga, pgroonga_database, pgrouting, pgrowlocks, pgsodium, pgstattuple, pgtap, plcoffee, pljava, plls, plpgsql_check, plv8, postgis, postgis_raster, postgis_sfcgal, postgis_tiger_geocoder, postgis_topology, postgres_fdw, refint, rum, seg, sslinfo, supabase_vault, supautils, tablefunc, tcn, timescaledb, tsm_system_rows, tsm_system_time, unaccent, uuid-ossp, vector, wrappers
22+
(1 row)
23+
24+
-- create a simple schema to verify normal operations work
25+
create schema v;
26+
create table v.test_table (
27+
id serial primary key,
28+
data text
29+
);
30+
insert into v.test_table (data)
31+
values ('test1'), ('test2');
32+
select * from v.test_table order by id;
33+
id | data
34+
----+-------
35+
1 | test1
36+
2 | test2
37+
(2 rows)
38+
39+
rollback;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
begin;
2+
load 'supautils';
3+
-- verify that supautils configuration parameters exist
4+
select current_setting('supautils.privileged_extensions', true) is not null as has_privileged_extensions;
5+
has_privileged_extensions
6+
---------------------------
7+
t
8+
(1 row)
9+
10+
select current_setting('supautils.privileged_role', true) is not null as has_privileged_role;
11+
has_privileged_role
12+
---------------------
13+
t
14+
(1 row)
15+
16+
-- switch to postgres role and verify access to settings
17+
set role postgres;
18+
select current_setting('supautils.privileged_extensions', true) as privileged_extensions;
19+
privileged_extensions
20+
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
21+
address_standardizer, address_standardizer_data_us, autoinc, bloom, btree_gin, btree_gist, citext, cube, dblink, dict_int, dict_xsyn, earthdistance, fuzzystrmatch, hstore, http, hypopg, index_advisor, insert_username, intarray, isn, ltree, moddatetime, orioledb, pg_buffercache, pg_cron, pg_graphql, pg_hashids, pg_jsonschema, pg_net, pg_prewarm, pg_repack, pg_stat_monitor, pg_stat_statements, pg_tle, pg_trgm, pg_walinspect, pgaudit, pgcrypto, pgjwt, pgroonga, pgroonga_database, pgrouting, pgrowlocks, pgsodium, pgstattuple, pgtap, plcoffee, pljava, plls, plpgsql_check, postgis, postgis_raster, postgis_sfcgal, postgis_tiger_geocoder, postgis_topology, postgres_fdw, refint, rum, seg, sslinfo, supabase_vault, supautils, tablefunc, tcn, tsm_system_rows, tsm_system_time, unaccent, uuid-ossp, vector, wrappers
22+
(1 row)
23+
24+
-- create a simple schema to verify normal operations work
25+
create schema v;
26+
create table v.test_table (
27+
id serial primary key,
28+
data text
29+
);
30+
insert into v.test_table (data)
31+
values ('test1'), ('test2');
32+
select * from v.test_table order by id;
33+
id | data
34+
----+-------
35+
1 | test1
36+
2 | test2
37+
(2 rows)
38+
39+
rollback;

nix/tests/sql/z_15_supautils.sql

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
begin;
2+
load 'supautils';
3+
4+
-- verify that supautils configuration parameters exist
5+
select current_setting('supautils.privileged_extensions', true) is not null as has_privileged_extensions;
6+
select current_setting('supautils.privileged_role', true) is not null as has_privileged_role;
7+
8+
-- switch to postgres role and verify access to settings
9+
set role postgres;
10+
select current_setting('supautils.privileged_extensions', true) as privileged_extensions;
11+
12+
-- create a simple schema to verify normal operations work
13+
create schema v;
14+
15+
create table v.test_table (
16+
id serial primary key,
17+
data text
18+
);
19+
20+
insert into v.test_table (data)
21+
values ('test1'), ('test2');
22+
23+
select * from v.test_table order by id;
24+
25+
rollback;

nix/tests/sql/z_17_supautils.sql

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
begin;
2+
load 'supautils';
3+
4+
-- verify that supautils configuration parameters exist
5+
select current_setting('supautils.privileged_extensions', true) is not null as has_privileged_extensions;
6+
select current_setting('supautils.privileged_role', true) is not null as has_privileged_role;
7+
8+
-- switch to postgres role and verify access to settings
9+
set role postgres;
10+
select current_setting('supautils.privileged_extensions', true) as privileged_extensions;
11+
12+
-- create a simple schema to verify normal operations work
13+
create schema v;
14+
15+
create table v.test_table (
16+
id serial primary key,
17+
data text
18+
);
19+
20+
insert into v.test_table (data)
21+
values ('test1'), ('test2');
22+
23+
select * from v.test_table order by id;
24+
25+
rollback;

0 commit comments

Comments
 (0)