|
16 | 16 | pgroonga = self'.packages."psql_15/exts/pgroonga";
|
17 | 17 | inherit (self.supabase) defaults;
|
18 | 18 | };
|
| 19 | + bashlog = builtins.fetchurl { |
| 20 | + url = "https://raw.githubusercontent.com/Zordrak/bashlog/master/log.sh"; |
| 21 | + sha256 = "1vrjcbzls0ba2qkg7ffddz2gxqn2rlj3wyvril2gz0mfi89y9vk9"; |
| 22 | + }; |
19 | 23 | in
|
20 | 24 | {
|
21 | 25 | checks =
|
|
168 | 172 | ];
|
169 | 173 | }
|
170 | 174 | ''
|
171 |
| - set -e |
| 175 | + source ${bashlog} |
| 176 | + set -uo pipefail |
172 | 177 |
|
173 |
| - # Start HTTP mock server for http extension tests |
| 178 | + export BASHLOG_FILE=1 |
| 179 | + export BASHLOG_FILE_PATH=debug.log |
174 | 180 | # Use a build-specific directory for coordination
|
175 | 181 | BUILD_TMP=$(mktemp -d)
|
| 182 | +
|
| 183 | + # Function to log command execution with stdout and stderr |
| 184 | + function log_cmd { |
| 185 | + local cmd_name="$1" |
| 186 | + shift |
| 187 | + log debug "Executing: $cmd_name $@" |
| 188 | + local exit_code=0 |
| 189 | + echo "\$ $cmd_name $@" >> debug.log |
| 190 | + "$cmd_name" "$@" 2>&1 >> debug.log || exit_code=$? |
| 191 | + log debug "Exit code: $exit_code" |
| 192 | + return $exit_code |
| 193 | + } |
| 194 | +
|
| 195 | + function on_exit { |
| 196 | + local exit_code=$? |
| 197 | + kill $HTTP_MOCK_PID 2>/dev/null || true |
| 198 | + rm -rf "$BUILD_TMP" |
| 199 | + if [ $exit_code -ne 0 ]; then |
| 200 | + log error "An error occurred. Exit code: $exit_code" |
| 201 | + log error "Debug logs:" |
| 202 | + cat debug.log || log error "No debug.log file found" |
| 203 | + fi |
| 204 | + } |
| 205 | + trap on_exit EXIT |
| 206 | + trap "" DEBUG |
| 207 | +
|
| 208 | + function check_postgres_ready { |
| 209 | + for i in {1..60}; do |
| 210 | + if log_cmd pg_isready -h ${self.supabase.defaults.host} -p ${pgPort} -U supabase_admin -q; then |
| 211 | + log info "PostgreSQL is ready" |
| 212 | + break |
| 213 | + fi |
| 214 | + sleep 1 |
| 215 | + if [ $i -eq 60 ]; then |
| 216 | + log error "PostgreSQL failed to start" |
| 217 | + exit 1 |
| 218 | + fi |
| 219 | + done |
| 220 | + } |
| 221 | +
|
| 222 | + # Start HTTP mock server for http extension tests |
176 | 223 | HTTP_MOCK_PORT_FILE="$BUILD_TMP/http-mock-port"
|
177 | 224 |
|
178 |
| - echo "Starting HTTP mock server (will find free port)..." |
179 |
| - HTTP_MOCK_PORT_FILE="$HTTP_MOCK_PORT_FILE" ${pkgs.python3}/bin/python3 ${./tests/http-mock-server.py} & |
| 225 | + log info "Starting HTTP mock server (will find free port)..." |
| 226 | + HTTP_MOCK_PORT_FILE="$HTTP_MOCK_PORT_FILE" log_cmd ${pkgs.python3}/bin/python3 ${./tests/http-mock-server.py} & |
180 | 227 | HTTP_MOCK_PID=$!
|
181 | 228 |
|
182 | 229 | # Clean up on exit
|
183 |
| - trap "kill $HTTP_MOCK_PID 2>/dev/null || true; rm -rf '$BUILD_TMP'" EXIT |
184 | 230 |
|
185 | 231 | # Wait for server to start and write port file
|
186 | 232 | for i in {1..10}; do
|
187 | 233 | if [ -f "$HTTP_MOCK_PORT_FILE" ]; then
|
188 | 234 | HTTP_MOCK_PORT=$(cat "$HTTP_MOCK_PORT_FILE")
|
189 |
| - echo "HTTP mock server started on port $HTTP_MOCK_PORT" |
| 235 | + log info "HTTP mock server started on port $HTTP_MOCK_PORT" |
190 | 236 | break
|
191 | 237 | fi
|
192 | 238 | sleep 1
|
193 | 239 | done
|
194 | 240 |
|
195 | 241 | if [ ! -f "$HTTP_MOCK_PORT_FILE" ]; then
|
196 |
| - echo "Failed to start HTTP mock server" |
| 242 | + log error "Failed to start HTTP mock server" |
197 | 243 | exit 1
|
198 | 244 | fi
|
199 | 245 |
|
|
203 | 249 | #First we need to create a generic pg cluster for pgtap tests and run those
|
204 | 250 | export GRN_PLUGINS_DIR=${pkgs.supabase-groonga}/lib/groonga/plugins
|
205 | 251 | PGTAP_CLUSTER=$(mktemp -d)
|
206 |
| - initdb --locale=C --username=supabase_admin -D "$PGTAP_CLUSTER" |
| 252 | + log info "Creating temporary PostgreSQL cluster at $PGTAP_CLUSTER" |
| 253 | + log_cmd initdb --locale=C --username=supabase_admin -D "$PGTAP_CLUSTER" |
207 | 254 | substitute ${./tests/postgresql.conf.in} "$PGTAP_CLUSTER"/postgresql.conf \
|
208 | 255 | --subst-var-by PGSODIUM_GETKEY_SCRIPT "${getkey-script}/bin/pgsodium-getkey"
|
209 | 256 | echo "listen_addresses = '*'" >> "$PGTAP_CLUSTER"/postgresql.conf
|
210 | 257 | echo "port = ${pgPort}" >> "$PGTAP_CLUSTER"/postgresql.conf
|
211 | 258 | echo "host all all 127.0.0.1/32 trust" >> $PGTAP_CLUSTER/pg_hba.conf
|
212 |
| - echo "Checking shared_preload_libraries setting:" |
213 |
| - grep -rn "shared_preload_libraries" "$PGTAP_CLUSTER"/postgresql.conf |
| 259 | + log info "Checking shared_preload_libraries setting:" |
| 260 | + log info $(grep -rn "shared_preload_libraries" "$PGTAP_CLUSTER"/postgresql.conf) |
214 | 261 | # Remove timescaledb if running orioledb-17 check
|
215 |
| - echo "I AM ${pgpkg.version}====================================================" |
| 262 | + log info "pgpkg.version is: ${pgpkg.version}" |
216 | 263 | if [[ "${pgpkg.version}" == *"17"* ]]; then
|
217 | 264 | perl -pi -e 's/ timescaledb,//g' "$PGTAP_CLUSTER/postgresql.conf"
|
218 | 265 | fi
|
219 | 266 | #NOTE in the future we may also need to add the orioledb extension to the cluster when cluster is oriole
|
220 |
| - echo "PGTAP_CLUSTER directory contents:" |
221 |
| - ls -la "$PGTAP_CLUSTER" |
222 | 267 |
|
223 | 268 | # Check if postgresql.conf exists
|
224 | 269 | if [ ! -f "$PGTAP_CLUSTER/postgresql.conf" ]; then
|
225 |
| - echo "postgresql.conf is missing!" |
| 270 | + log error "postgresql.conf is missing!" |
226 | 271 | exit 1
|
227 | 272 | fi
|
228 | 273 |
|
229 | 274 | # PostgreSQL startup
|
230 | 275 | if [[ "$(uname)" == "Darwin" ]]; then
|
231 |
| - pg_ctl -D "$PGTAP_CLUSTER" -l "$PGTAP_CLUSTER"/postgresql.log -o "-k "$PGTAP_CLUSTER" -p ${pgPort} -d 5" start 2>&1 |
| 276 | + log_cmd pg_ctl -D "$PGTAP_CLUSTER" -l "$PGTAP_CLUSTER"/postgresql.log -o "-k "$PGTAP_CLUSTER" -p ${pgPort} -d 5" start |
232 | 277 | else
|
233 | 278 | mkdir -p "$PGTAP_CLUSTER/sockets"
|
234 |
| - pg_ctl -D "$PGTAP_CLUSTER" -l "$PGTAP_CLUSTER"/postgresql.log -o "-k $PGTAP_CLUSTER/sockets -p ${pgPort} -d 5" start 2>&1 |
| 279 | + log_cmd pg_ctl -D "$PGTAP_CLUSTER" -l "$PGTAP_CLUSTER"/postgresql.log -o "-k $PGTAP_CLUSTER/sockets -p ${pgPort} -d 5" start |
235 | 280 | fi || {
|
236 |
| - echo "pg_ctl failed to start PostgreSQL" |
237 |
| - echo "Contents of postgresql.log:" |
| 281 | + log error "pg_ctl failed to start PostgreSQL" |
| 282 | + log error "Contents of postgresql.log:" |
238 | 283 | cat "$PGTAP_CLUSTER"/postgresql.log
|
239 | 284 | exit 1
|
240 | 285 | }
|
241 |
| - for i in {1..60}; do |
242 |
| - if pg_isready -h ${self.supabase.defaults.host} -p ${pgPort}; then |
243 |
| - echo "PostgreSQL is ready" |
244 |
| - break |
245 |
| - fi |
246 |
| - sleep 1 |
247 |
| - if [ $i -eq 60 ]; then |
248 |
| - echo "PostgreSQL is not ready after 60 seconds" |
249 |
| - echo "PostgreSQL status:" |
250 |
| - pg_ctl -D "$PGTAP_CLUSTER" status |
251 |
| - echo "PostgreSQL log content:" |
252 |
| - cat "$PGTAP_CLUSTER"/postgresql.log |
253 |
| - exit 1 |
254 |
| - fi |
255 |
| - done |
256 |
| - createdb -p ${pgPort} -h ${self.supabase.defaults.host} --username=supabase_admin testing |
257 |
| - if ! psql -p ${pgPort} -h ${self.supabase.defaults.host} --username=supabase_admin -d testing -v ON_ERROR_STOP=1 -Xf ${./tests/prime.sql}; then |
258 |
| - echo "Error executing SQL file. PostgreSQL log content:" |
| 286 | +
|
| 287 | + log info "Waiting for PostgreSQL to be ready..." |
| 288 | + check_postgres_ready |
| 289 | +
|
| 290 | + log info "Creating test database" |
| 291 | + log_cmd createdb -p ${pgPort} -h ${self.supabase.defaults.host} --username=supabase_admin testing |
| 292 | +
|
| 293 | + log info "Loading prime SQL file" |
| 294 | + if ! log_cmd psql -p ${pgPort} -h ${self.supabase.defaults.host} --username=supabase_admin -d testing -v ON_ERROR_STOP=1 -Xf ${./tests/prime.sql}; then |
| 295 | + log error "Error executing SQL file. PostgreSQL log content:" |
259 | 296 | cat "$PGTAP_CLUSTER"/postgresql.log
|
260 | 297 | pg_ctl -D "$PGTAP_CLUSTER" stop
|
261 | 298 | exit 1
|
262 | 299 | fi
|
263 | 300 |
|
264 | 301 | # Create a table to store test configuration
|
265 |
| - psql -p ${pgPort} -h ${self.supabase.defaults.host} --username=supabase_admin -d testing -c " |
| 302 | + log info "Creating test_config table" |
| 303 | + log_cmd psql -p ${pgPort} -h ${self.supabase.defaults.host} --username=supabase_admin -d testing -c " |
266 | 304 | CREATE TABLE IF NOT EXISTS test_config (key TEXT PRIMARY KEY, value TEXT);
|
267 | 305 | INSERT INTO test_config (key, value) VALUES ('http_mock_port', '$HTTP_MOCK_PORT')
|
268 | 306 | ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value;
|
269 | 307 | "
|
270 | 308 | SORTED_DIR=$(mktemp -d)
|
271 | 309 | for t in $(printf "%s\n" ${builtins.concatStringsSep " " sortedTestList}); do
|
272 |
| - psql -p ${pgPort} -h ${self.supabase.defaults.host} --username=supabase_admin -d testing -f "${./tests/sql}/$t.sql" || true |
| 310 | + log info "Running pgtap test: $t.sql" |
| 311 | + #XXX enable ON_ERROR_STOP ? |
| 312 | + log_cmd psql -p ${pgPort} -h ${self.supabase.defaults.host} --username=supabase_admin -d testing -f "${./tests/sql}/$t.sql" |
273 | 313 | done
|
274 | 314 | rm -rf "$SORTED_DIR"
|
275 |
| - pg_ctl -D "$PGTAP_CLUSTER" stop |
| 315 | + log_cmd pg_ctl -D "$PGTAP_CLUSTER" stop |
276 | 316 | rm -rf $PGTAP_CLUSTER
|
277 | 317 |
|
278 | 318 | # End of pgtap tests
|
279 | 319 | # from here on out we are running pg_regress tests, we use a different cluster for this
|
280 | 320 | # which is start by the start-postgres-server-bin script
|
281 | 321 | # start-postgres-server-bin script closely matches our AMI setup, configurations and migrations
|
282 | 322 |
|
| 323 | + log info "Starting PostgreSQL server for pg_regress tests" |
283 | 324 | unset GRN_PLUGINS_DIR
|
284 |
| - ${start-postgres-server-bin}/bin/start-postgres-server ${getVersionArg pgpkg} --daemonize |
| 325 | + log_cmd ${start-postgres-server-bin}/bin/start-postgres-server ${getVersionArg pgpkg} --daemonize |
285 | 326 |
|
286 |
| - for i in {1..60}; do |
287 |
| - if pg_isready -h ${self.supabase.defaults.host} -p ${pgPort} -U supabase_admin -q; then |
288 |
| - echo "PostgreSQL is ready" |
289 |
| - break |
290 |
| - fi |
291 |
| - sleep 1 |
292 |
| - if [ $i -eq 60 ]; then |
293 |
| - echo "PostgreSQL failed to start" |
294 |
| - exit 1 |
295 |
| - fi |
296 |
| - done |
| 327 | + check_postgres_ready |
297 | 328 |
|
298 |
| - if ! psql -p ${pgPort} -h ${self.supabase.defaults.host} --no-password --username=supabase_admin -d postgres -v ON_ERROR_STOP=1 -Xf ${./tests/prime.sql}; then |
299 |
| - echo "Error executing SQL file" |
| 329 | + log info "Loading prime SQL file" |
| 330 | + if ! log_cmd psql -p ${pgPort} -h ${self.supabase.defaults.host} --no-password --username=supabase_admin -d postgres -v ON_ERROR_STOP=1 -Xf ${./tests/prime.sql} 2>&1; then |
| 331 | + log error "Error executing SQL file" |
300 | 332 | exit 1
|
301 | 333 | fi
|
302 | 334 |
|
303 | 335 | # Create a table to store test configuration for pg_regress tests
|
304 |
| - psql -p ${pgPort} -h ${self.supabase.defaults.host} --no-password --username=supabase_admin -d postgres -c " |
| 336 | + log info "Creating test_config table for pg_regress tests" |
| 337 | + log_cmd psql -p ${pgPort} -h ${self.supabase.defaults.host} --no-password --username=supabase_admin -d postgres -c " |
305 | 338 | CREATE TABLE IF NOT EXISTS test_config (key TEXT PRIMARY KEY, value TEXT);
|
306 | 339 | INSERT INTO test_config (key, value) VALUES ('http_mock_port', '$HTTP_MOCK_PORT')
|
307 | 340 | ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value;
|
308 | 341 | "
|
309 | 342 |
|
310 | 343 | mkdir -p $out/regression_output
|
311 |
| - if ! pg_regress \ |
| 344 | + log info "Running pg_regress tests" |
| 345 | + if ! log_cmd pg_regress \ |
312 | 346 | --use-existing \
|
313 | 347 | --dbname=postgres \
|
314 | 348 | --inputdir=${./tests} \
|
315 | 349 | --outputdir=$out/regression_output \
|
316 | 350 | --host=${self.supabase.defaults.host} \
|
317 | 351 | --port=${pgPort} \
|
318 | 352 | --user=supabase_admin \
|
319 |
| - ${builtins.concatStringsSep " " sortedTestList}; then |
320 |
| - echo "pg_regress tests failed" |
| 353 | + ${builtins.concatStringsSep " " sortedTestList} 2>&1; then |
| 354 | + log error "pg_regress tests failed" |
321 | 355 | cat $out/regression_output/regression.diffs
|
322 | 356 | exit 1
|
323 | 357 | fi
|
| 358 | + log info "pg_regress tests completed successfully" |
324 | 359 |
|
325 |
| - echo "Running migrations tests" |
326 |
| - pg_prove -p ${pgPort} -U supabase_admin -h ${self.supabase.defaults.host} -d postgres -v ${../migrations/tests}/test.sql |
327 |
| -
|
328 |
| - # Copy logs to output |
329 |
| - for logfile in $(find /tmp -name postgresql.log -type f); do |
330 |
| - cp "$logfile" $out/postgresql.log |
331 |
| - done |
332 |
| - exit 0 |
| 360 | + log info "Running migrations tests" |
| 361 | + log_cmd pg_prove -p ${pgPort} -U supabase_admin -h ${self.supabase.defaults.host} -d postgres -v ${../migrations/tests}/test.sql |
| 362 | + log info "Migrations tests completed successfully" |
333 | 363 | '';
|
334 | 364 | in
|
335 | 365 | {
|
|
0 commit comments