nix: Make postgrest-with-postgresql-xxx postgrest-test-io work better

The upside is that postgrest-with-postgresql-xxx postgrest-test-io works as expected
now. The downside is, that postgrest-with-postgresql-xxx psql now starts without
any schema. This now needs an explicit postgrest-with-postgresql-xxx -f path/to.sql
to do anything useful.

Resolves #2864
This commit is contained in:
Wolfgang Walther
2024-02-10 20:51:55 +01:00
committed by Wolfgang Walther
parent dcfdd8dfc9
commit 410fa9508b
5 changed files with 59 additions and 57 deletions
+1 -1
View File
@@ -92,7 +92,7 @@ jobs:
- name: Run IO tests - name: Run IO tests
if: always() if: always()
run: postgrest-with-postgresql-${{ matrix.pgVersion }} -f test/io/fixtures.sql postgrest-test-io -vv run: postgrest-with-postgresql-${{ matrix.pgVersion }} postgrest-test-io -vv
Test-Memory-Nix: Test-Memory-Nix:
+1 -1
View File
@@ -60,7 +60,7 @@ let
abs_output="$(realpath "$_arg_output")" abs_output="$(realpath "$_arg_output")"
# shellcheck disable=SC2145 # shellcheck disable=SC2145
${withTools.withPg} --fixtures "$_arg_testdir"/fixtures.sql \ ${withTools.withPg} -f "$_arg_testdir"/fixtures.sql \
${withTools.withSlowPg} \ ${withTools.withSlowPg} \
${withTools.withPgrst} \ ${withTools.withPgrst} \
${withTools.withSlowPgrst} \ ${withTools.withSlowPgrst} \
+1 -1
View File
@@ -17,7 +17,7 @@ let
withPath = [ postgrestProfiled curl ]; withPath = [ postgrestProfiled curl ];
} }
'' ''
${withTools.withPg} test/memory/memory-tests.sh ${withTools.withPg} -f test/spec/fixtures/load.sql test/memory/memory-tests.sh
''; '';
in in
+11 -9
View File
@@ -28,8 +28,8 @@ let
withEnv = postgrest.env; withEnv = postgrest.env;
} }
'' ''
${withTools.withPg} ${cabal-install}/bin/cabal v2-run ${devCabalOptions} \ ${withTools.withPg} -f test/spec/fixtures/load.sql \
test:spec -- "''${_arg_leftovers[@]}" ${cabal-install}/bin/cabal v2-run ${devCabalOptions} test:spec -- "''${_arg_leftovers[@]}"
''; '';
testDoctests = testDoctests =
@@ -59,9 +59,10 @@ let
withEnv = postgrest.env; withEnv = postgrest.env;
} }
'' ''
${withTools.withPg} ${runtimeShell} -c " \ ${withTools.withPg} -f test/spec/fixtures/load.sql \
${cabal-install}/bin/cabal v2-run ${devCabalOptions} test:spec && \ ${runtimeShell} -c " \
${cabal-install}/bin/cabal v2-run ${devCabalOptions} test:spec" ${cabal-install}/bin/cabal v2-run ${devCabalOptions} test:spec && \
${cabal-install}/bin/cabal v2-run ${devCabalOptions} test:spec"
''; '';
ioTestPython = ioTestPython =
@@ -99,7 +100,7 @@ let
withPath = [ jq ]; withPath = [ jq ];
} }
'' ''
${withTools.withPg} \ ${withTools.withPg} -f test/spec/fixtures.sql \
${cabal-install}/bin/cabal v2-run ${devCabalOptions} --verbose=0 -- \ ${cabal-install}/bin/cabal v2-run ${devCabalOptions} --verbose=0 -- \
postgrest --dump-schema \ postgrest --dump-schema \
| ${yq}/bin/yq -y . | ${yq}/bin/yq -y .
@@ -137,11 +138,12 @@ let
# collect all tests # collect all tests
HPCTIXFILE="$tmpdir"/io.tix \ HPCTIXFILE="$tmpdir"/io.tix \
${withTools.withPg} -f test/io/fixtures.sql ${cabal-install}/bin/cabal v2-exec ${devCabalOptions} -- \ ${withTools.withPg} -f test/io/fixtures.sql \
${ioTestPython}/bin/pytest -v test/io ${cabal-install}/bin/cabal v2-exec ${devCabalOptions} -- ${ioTestPython}/bin/pytest -v test/io
HPCTIXFILE="$tmpdir"/spec.tix \ HPCTIXFILE="$tmpdir"/spec.tix \
${withTools.withPg} ${cabal-install}/bin/cabal v2-run ${devCabalOptions} test:spec ${withTools.withPg} -f test/spec/fixtures/load.sql \
${cabal-install}/bin/cabal v2-run ${devCabalOptions} test:spec
# Note: No coverage for doctests, as doctests leverage GHCi and GHCi does not support hpc # Note: No coverage for doctests, as doctests leverage GHCi and GHCi does not support hpc
+45 -45
View File
@@ -17,15 +17,14 @@ let
{ name, postgresql }: { name, postgresql }:
let let
commandName = "postgrest-with-${name}"; commandName = "postgrest-with-${name}";
superuserRole = "postgres";
in in
checkedShellScript checkedShellScript
{ {
name = commandName; name = commandName;
docs = "Run the given command in a temporary database with ${name}. If you wish to mutate the database, login with the '${superuserRole}' role."; docs = "Run the given command in a temporary database with ${name}. If you wish to mutate the database, login with the postgres role.";
args = args =
[ [
"ARG_OPTIONAL_SINGLE([fixtures], [f], [SQL file to load fixtures from], [test/spec/fixtures/load.sql])" "ARG_OPTIONAL_SINGLE([fixtures], [f], [SQL file to load fixtures from])"
"ARG_POSITIONAL_SINGLE([command], [Command to run])" "ARG_POSITIONAL_SINGLE([command], [Command to run])"
"ARG_LEFTOVERS([command arguments])" "ARG_LEFTOVERS([command arguments])"
"ARG_USE_ENV([PGUSER], [postgrest_test_authenticator], [Authenticator PG role])" "ARG_USE_ENV([PGUSER], [postgrest_test_authenticator], [Authenticator PG role])"
@@ -41,63 +40,64 @@ let
withTmpDir = true; withTmpDir = true;
} }
'' ''
# avoid starting multiple layers of withTmpDb
if test -v PGHOST; then
exec "$_arg_command" "''${_arg_leftovers[@]}"
fi
setuplog="$tmpdir/setup.log" setuplog="$tmpdir/setup.log"
log () { log () {
echo "$1" >> "$setuplog" echo "$1" >> "$setuplog"
} }
mkdir -p "$tmpdir"/{db,socket} # Avoid starting multiple layers of withTmpDb, but make sure to have the last invocation
# remove data dir, even if we keep tmpdir - no need to upload it to artifacts # load fixtures. Otherwise postgrest-with-postgresql-xx postgrest-test-io would not be possible.
trap 'rm -rf $tmpdir/db' EXIT if ! test -v PGHOST; then
export PGDATA="$tmpdir/db" mkdir -p "$tmpdir"/{db,socket}
export PGHOST="$tmpdir/socket" # remove data dir, even if we keep tmpdir - no need to upload it to artifacts
export PGUSER trap 'rm -rf $tmpdir/db' EXIT
export PGDATABASE
export PGRST_DB_SCHEMAS
export PGTZ
export PGOPTIONS
HBA_FILE="$tmpdir/pg_hba.conf" export PGDATA="$tmpdir/db"
echo "local $PGDATABASE some_protected_user password" > "$HBA_FILE" export PGHOST="$tmpdir/socket"
echo "local $PGDATABASE all trust" >> "$HBA_FILE" export PGUSER
export PGDATABASE
export PGRST_DB_SCHEMAS
export PGTZ
export PGOPTIONS
log "Initializing database cluster..." HBA_FILE="$tmpdir/pg_hba.conf"
# We try to make the database cluster as independent as possible from the host echo "local $PGDATABASE some_protected_user password" > "$HBA_FILE"
# by specifying the timezone, locale and encoding. echo "local $PGDATABASE all trust" >> "$HBA_FILE"
# initdb -U creates a superuser(man initdb)
TZ=$PGTZ initdb --no-locale --encoding=UTF8 --nosync -U "${superuserRole}" --auth=trust \
>> "$setuplog"
log "Starting the database cluster..." log "Initializing database cluster..."
# Instead of listening on a local port, we will listen on a unix domain socket. # We try to make the database cluster as independent as possible from the host
pg_ctl -l "$tmpdir/db.log" -w start -o "-F -c listen_addresses=\"\" -c hba_file=$HBA_FILE -k $PGHOST -c log_statement=\"all\" " \ # by specifying the timezone, locale and encoding.
>> "$setuplog" # initdb -U creates a superuser(man initdb)
TZ=$PGTZ initdb --no-locale --encoding=UTF8 --nosync -U postgres --auth=trust \
>> "$setuplog"
# shellcheck disable=SC2317 log "Starting the database cluster..."
stop () { # Instead of listening on a local port, we will listen on a unix domain socket.
log "Stopping the database cluster..." pg_ctl -l "$tmpdir/db.log" -w start -o "-F -c listen_addresses=\"\" -c hba_file=$HBA_FILE -k $PGHOST -c log_statement=\"all\" " \
pg_ctl stop -m i >> "$setuplog" >> "$setuplog"
rm -rf "$tmpdir/db"
}
trap stop EXIT
log "Creating a minimally privileged $PGUSER connection role..." # shellcheck disable=SC2317
createuser "$PGUSER" -U "${superuserRole}" --host="$tmpdir/socket" --no-createdb --no-inherit --no-superuser --no-createrole --no-replication --login stop () {
log "Stopping the database cluster..."
pg_ctl stop -m i >> "$setuplog"
rm -rf "$tmpdir/db"
}
trap stop EXIT
log "Loading fixtures under the ${superuserRole} role..." log "Creating a minimally privileged $PGUSER connection role..."
psql -U "${superuserRole}" -v PGUSER="$PGUSER" -v ON_ERROR_STOP=1 -f "$_arg_fixtures" >> "$setuplog" createuser "$PGUSER" -U postgres --host="$tmpdir/socket" --no-createdb --no-inherit --no-superuser --no-createrole --no-replication --login
log "Done. Running command..." echo "${commandName}: You can connect with: psql 'postgres:///$PGDATABASE?host=$tmpdir/socket' -U postgres"
echo "${commandName}: You can tail the logs with: tail -f $tmpdir/db.log"
fi
echo "${commandName}: You can connect with: psql 'postgres:///$PGDATABASE?host=$tmpdir/socket' -U ${superuserRole}" if test "$_arg_fixtures"; then
echo "${commandName}: You can tail the logs with: tail -f $tmpdir/db.log" log "Loading fixtures under the postgres role..."
psql -U postgres -v PGUSER="$PGUSER" -v ON_ERROR_STOP=1 -f "$_arg_fixtures" >> "$setuplog"
log "Done. Running command..."
fi
("$_arg_command" "''${_arg_leftovers[@]}") ("$_arg_command" "''${_arg_leftovers[@]}")
''; '';