Skip to content

Commit 638c0b4

Browse files
committed
test: revert to use test in nix/tests for timescale
1 parent 578af45 commit 638c0b4

File tree

2 files changed

+144
-60
lines changed

2 files changed

+144
-60
lines changed

nix/ext/tests/default.nix

Lines changed: 44 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ let
1010
let
1111
pname = extension_name;
1212
inherit (pkgs) lib;
13-
# Special case for timescaledb-apache: only test with PostgreSQL 15
14-
isTimescaledb = extension_name == "timescaledb-apache";
1513
installedExtension =
1614
postgresMajorVersion: self.packages.${pkgs.system}."psql_${postgresMajorVersion}/exts/${pname}-all";
1715
versions = postgresqlMajorVersion: (installedExtension postgresqlMajorVersion).versions;
@@ -46,8 +44,7 @@ let
4644
in
4745
pkg;
4846
psql_15 = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_15;
49-
psql_17 =
50-
if isTimescaledb then null else postgresqlWithExtension self.packages.${pkgs.system}.postgresql_17;
47+
psql_17 = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_17;
5148
in
5249
self.inputs.nixpkgs.lib.nixos.runTest {
5350
name = pname;
@@ -83,44 +80,42 @@ let
8380

8481
networking.firewall.allowedTCPPorts = [ config.services.postgresql.settings.port ];
8582

86-
specialisation = lib.optionalAttrs (!isTimescaledb) {
87-
postgresql17.configuration = {
88-
services.postgresql = {
89-
package = lib.mkForce psql_17;
90-
};
83+
specialisation.postgresql17.configuration = {
84+
services.postgresql = {
85+
package = lib.mkForce psql_17;
86+
};
9187

92-
systemd.services.postgresql-migrate = {
93-
serviceConfig = {
94-
Type = "oneshot";
95-
RemainAfterExit = true;
96-
User = "postgres";
97-
Group = "postgres";
98-
StateDirectory = "postgresql";
99-
WorkingDirectory = "${builtins.dirOf config.services.postgresql.dataDir}";
100-
};
101-
script =
102-
let
103-
oldPostgresql = psql_15;
104-
newPostgresql = psql_17;
105-
oldDataDir = "${builtins.dirOf config.services.postgresql.dataDir}/${oldPostgresql.psqlSchema}";
106-
newDataDir = "${builtins.dirOf config.services.postgresql.dataDir}/${newPostgresql.psqlSchema}";
107-
in
108-
''
109-
if [[ ! -d ${newDataDir} ]]; then
110-
install -d -m 0700 -o postgres -g postgres "${newDataDir}"
111-
${newPostgresql}/bin/initdb -D "${newDataDir}"
112-
${newPostgresql}/bin/pg_upgrade --old-datadir "${oldDataDir}" --new-datadir "${newDataDir}" \
113-
--old-bindir "${oldPostgresql}/bin" --new-bindir "${newPostgresql}/bin"
114-
else
115-
echo "${newDataDir} already exists"
116-
fi
117-
'';
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}";
11896
};
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+
};
119115

120-
systemd.services.postgresql = {
121-
after = [ "postgresql-migrate.service" ];
122-
requires = [ "postgresql-migrate.service" ];
123-
};
116+
systemd.services.postgresql = {
117+
after = [ "postgresql-migrate.service" ];
118+
requires = [ "postgresql-migrate.service" ];
124119
};
125120
};
126121
};
@@ -132,12 +127,7 @@ let
132127
''
133128
versions = {
134129
"15": [${lib.concatStringsSep ", " (map (s: ''"${s}"'') (versions "15"))}],
135-
${
136-
if isTimescaledb then
137-
""
138-
else
139-
''"17": [${lib.concatStringsSep ", " (map (s: ''"${s}"'') (versions "17"))}],''
140-
}
130+
"17": [${lib.concatStringsSep ", " (map (s: ''"${s}"'') (versions "17"))}],
141131
}
142132
extension_name = "${pname}"
143133
support_upgrade = True
@@ -166,22 +156,16 @@ let
166156
with subtest("Test switch_${pname}_version"):
167157
test.check_switch_extension_with_background_worker(Path("${psql_15}/lib/${pname}.so"), "15")
168158
169-
${
170-
if isTimescaledb then
171-
""
172-
else
173-
''
174-
with subtest("switch to postgresql 17"):
175-
server.succeed(
176-
f"{pg17_configuration}/bin/switch-to-configuration test >&2"
177-
)
159+
with subtest("switch to postgresql 17"):
160+
server.succeed(
161+
f"{pg17_configuration}/bin/switch-to-configuration test >&2"
162+
)
178163
179-
with subtest("Check last version of the extension after upgrade"):
180-
test.assert_version_matches(last_version)
164+
with subtest("Check last version of the extension after upgrade"):
165+
test.assert_version_matches(last_version)
181166
182-
with subtest("Check upgrade path with postgresql 17"):
183-
test.check_upgrade_path("17")''
184-
}
167+
with subtest("Check upgrade path with postgresql 17"):
168+
test.check_upgrade_path("17")
185169
'';
186170
};
187171
in
@@ -202,7 +186,7 @@ builtins.listToAttrs (
202186
"index_advisor"
203187
"pg_cron"
204188
"pg_net"
205-
"timescaledb-apache"
189+
"timescaledb"
206190
"vector"
207191
"wrappers"
208192
]

nix/tests/timescaledb.nix

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
{ self, pkgs }:
2+
let
3+
inherit (pkgs) lib;
4+
installedExtension =
5+
postgresMajorVersion:
6+
self.packages.${pkgs.system}."psql_${postgresMajorVersion}/exts/timescaledb-all";
7+
versions = (installedExtension "15").versions;
8+
firstVersion = lib.head versions;
9+
postgresqlWithExtension =
10+
postgresql:
11+
let
12+
majorVersion = lib.versions.major postgresql.version;
13+
pkg = pkgs.buildEnv {
14+
name = "postgresql-${majorVersion}-timescaledb";
15+
paths = [
16+
postgresql
17+
postgresql.lib
18+
(installedExtension majorVersion)
19+
];
20+
passthru = {
21+
inherit (postgresql) version psqlSchema;
22+
lib = pkg;
23+
withPackages = _: pkg;
24+
};
25+
nativeBuildInputs = [ pkgs.makeWrapper ];
26+
pathsToLink = [
27+
"/"
28+
"/bin"
29+
"/lib"
30+
];
31+
postBuild = ''
32+
wrapProgram $out/bin/postgres --set NIX_PGLIBDIR $out/lib
33+
wrapProgram $out/bin/pg_ctl --set NIX_PGLIBDIR $out/lib
34+
wrapProgram $out/bin/pg_upgrade --set NIX_PGLIBDIR $out/lib
35+
'';
36+
};
37+
in
38+
pkg;
39+
in
40+
self.inputs.nixpkgs.lib.nixos.runTest {
41+
name = "timescaledb";
42+
hostPkgs = pkgs;
43+
nodes.server =
44+
{ ... }:
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+
users.users.root.openssh.authorizedKeys.keys = [
59+
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIArkmq6Th79Z4klW6Urgi4phN8yq769/l/10jlE00tU9"
60+
];
61+
62+
services.postgresql = {
63+
enable = true;
64+
package = postgresqlWithExtension self.packages.${pkgs.system}.postgresql_15;
65+
settings = {
66+
shared_preload_libraries = "timescaledb";
67+
};
68+
};
69+
70+
specialisation.postgresql15.configuration = {
71+
services.postgresql = {
72+
package = lib.mkForce (postgresqlWithExtension self.packages.${pkgs.system}.postgresql_15);
73+
};
74+
};
75+
};
76+
testScript =
77+
{ ... }:
78+
''
79+
def run_sql(query):
80+
return server.succeed(f"""sudo -u postgres psql -t -A -F\",\" -c \"{query}\" """).strip()
81+
82+
def check_upgrade_path():
83+
with subtest("Check timescaledb upgrade path"):
84+
server.succeed("sudo -u postgres psql -c 'DROP EXTENSION IF EXISTS timescaledb;'")
85+
run_sql(r"""CREATE EXTENSION timescaledb WITH VERSION \"${firstVersion}\";""")
86+
installed_version = run_sql(r"""SELECT extversion FROM pg_extension WHERE extname = 'timescaledb';""")
87+
assert installed_version == "${firstVersion}", f"Expected timescaledb version ${firstVersion}, but found {installed_version}"
88+
for version in [${lib.concatStringsSep ", " (map (s: ''"${s}"'') versions)}][1:]:
89+
run_sql(f"""ALTER EXTENSION timescaledb UPDATE TO '{version}';""")
90+
installed_version = run_sql(r"""SELECT extversion FROM pg_extension WHERE extname = 'timescaledb';""")
91+
assert installed_version == version, f"Expected timescaledb version {version}, but found {installed_version}"
92+
93+
start_all()
94+
95+
server.wait_for_unit("multi-user.target")
96+
server.wait_for_unit("postgresql.service")
97+
98+
check_upgrade_path()
99+
'';
100+
}

0 commit comments

Comments
 (0)