Skip to content

Commit 7cd477c

Browse files
committed
feat: run pg_regress during extension tests
While testing the upgrade of our PostgreSQL extensions, we also want to run the pg_regress tests that come with the extension. This ensures that the extension is functioning correctly after installation and upgrades.
1 parent d4be4ed commit 7cd477c

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

nix/ext/pgvector.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,6 @@ pkgs.buildEnv {
8888
pname = "${pname}-all";
8989
version =
9090
"multi-" + lib.concatStringsSep "-" (map (v: lib.replaceStrings [ "." ] [ "-" ] v) versions);
91+
pgRegressTestName = "pgvector";
9192
};
9293
}

nix/ext/tests/default.nix

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ let
125125
pg17-configuration = "${nodes.server.system.build.toplevel}/specialisation/postgresql17";
126126
in
127127
''
128+
from pathlib import Path
128129
versions = {
129130
"15": [${lib.concatStringsSep ", " (map (s: ''"${s}"'') (versions "15"))}],
130131
"17": [${lib.concatStringsSep ", " (map (s: ''"${s}"'') (versions "17"))}],
@@ -135,6 +136,8 @@ let
135136
ext_has_background_worker = ${
136137
if (installedExtension "15") ? hasBackgroundWorker then "True" else "False"
137138
}
139+
sql_test_directory = Path("${../../tests}")
140+
pg_regress_test_name = "${(installedExtension "15").pgRegressTestName or pname}"
138141
139142
${builtins.readFile ./lib.py}
140143
@@ -143,11 +146,14 @@ let
143146
server.wait_for_unit("multi-user.target")
144147
server.wait_for_unit("postgresql.service")
145148
146-
test = PostgresExtensionTest(server, extension_name, versions, support_upgrade)
149+
test = PostgresExtensionTest(server, extension_name, versions, sql_test_directory, support_upgrade)
147150
148151
with subtest("Check upgrade path with postgresql 15"):
149152
test.check_upgrade_path("15")
150153
154+
with subtest("Check pg_regress with postgresql 15"):
155+
test.check_pg_regress(Path("${psql_15}/lib/pgxs/src/test/regress/pg_regress"), "15", pg_regress_test_name)
156+
151157
last_version = None
152158
with subtest("Check the install of the last version of the extension"):
153159
last_version = test.check_install_last_version("15")
@@ -166,6 +172,9 @@ let
166172
167173
with subtest("Check upgrade path with postgresql 17"):
168174
test.check_upgrade_path("17")
175+
176+
with subtest("Check pg_regress with postgresql 17"):
177+
test.check_pg_regress(Path("${psql_17}/lib/pgxs/src/test/regress/pg_regress"), "17", pg_regress_test_name)
169178
'';
170179
};
171180
in

nix/ext/tests/lib.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def __init__(
1818
vm: Machine,
1919
extension_name: str,
2020
versions: Versions,
21+
sql_test_dir: Path,
2122
support_upgrade: bool = True,
2223
):
2324
"""Initialize the PostgreSQL extension test framework.
@@ -26,12 +27,14 @@ def __init__(
2627
vm: Test machine instance for executing commands
2728
extension_name: Name of the PostgreSQL extension to test
2829
versions: Mapping of PostgreSQL versions to available extension versions
30+
sql_test_dir: Directory containing SQL test files for pg_regress
2931
support_upgrade: Whether the extension supports in-place upgrades
3032
"""
3133
self.vm = vm
3234
self.extension_name = extension_name
3335
self.versions = versions
3436
self.support_upgrade = support_upgrade
37+
self.sql_test_dir = sql_test_dir
3538

3639
def run_sql(self, query: str) -> str:
3740
return self.vm.succeed(
@@ -101,9 +104,9 @@ def check_upgrade_path(self, pg_version: str):
101104
)
102105

103106
# Install and verify first version
104-
firstVersion = available_versions[0]
107+
first_version = available_versions[0]
105108
self.drop_extension()
106-
self.install_extension(firstVersion)
109+
self.install_extension(first_version)
107110

108111
# Test remaining versions
109112
for version in available_versions[1:]:
@@ -160,10 +163,41 @@ def check_switch_extension_with_background_worker(
160163
f"{first_version}.so"
161164
), f"Expected {self.extension_name} version {first_version}, but found {ext_version}"
162165

163-
# Switch to the first version
166+
# Switch to the last version
164167
self.vm.succeed(f"switch_{self.extension_name}_version {last_version}")
165168
# Check that we are using the last version now
166169
ext_version = self.vm.succeed(f"readlink -f {extension_lib_path}").strip()
167170
assert ext_version.endswith(
168171
f"{last_version}.so"
169172
), f"Expected {self.extension_name} version {last_version}, but found {ext_version}"
173+
174+
def check_pg_regress(self, pg_regress: Path, pg_version: str, test_name: str):
175+
"""Run pg_regress tests for the extension on a given PostgreSQL version.
176+
177+
Args:
178+
pg_regress: Path to the pg_regress binary
179+
pg_version: PostgreSQL version to test (e.g., "14", "15")
180+
test_name: SQL test file to run with pg_regress
181+
"""
182+
sql_file = self.sql_test_dir / "sql" / f"{test_name}.sql"
183+
if not sql_file.exists():
184+
# check if we have a postgres version specific sql file
185+
test_name = f"z_{pg_version}_{test_name}"
186+
sql_file = self.sql_test_dir / "sql" / f"{test_name}.sql"
187+
if not sql_file.exists():
188+
print(f"Skipping pg_regress test for {pg_version}, no sql file found")
189+
return
190+
try:
191+
print(
192+
self.vm.succeed(
193+
f"""sudo -u postgres {pg_regress} --inputdir={self.sql_test_dir} --debug --use-existing --dbname=postgres --outputdir=/tmp/regression_output_{pg_version} "{test_name}" """
194+
)
195+
)
196+
except:
197+
print("Error running pg_regress, diff:")
198+
print(
199+
self.vm.succeed(
200+
f"cat /tmp/regression_output_{pg_version}/regression.diffs"
201+
)
202+
)
203+
raise

nix/ext/tests/timescaledb.nix

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ self.inputs.nixpkgs.lib.nixos.runTest {
6666
}
6767
extension_name = "${pname}"
6868
support_upgrade = True
69+
sql_test_directory = Path("${../../tests}")
6970
70-
test = PostgresExtensionTest(server, extension_name, versions, support_upgrade)
71+
test = PostgresExtensionTest(server, extension_name, versions, sql_test_directory, support_upgrade)
7172
7273
with subtest("Check upgrade path with postgresql 15"):
7374
test.check_upgrade_path("15")

0 commit comments

Comments
 (0)