π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Rewrite the Postgres test runner as a linear script
Manav Rathi955a84324449
diff --git a/server/scripts/test-with-postgres.sh b/server/scripts/test-with-postgres.shindex 322736ea84..9b14d2e556 100755--- a/server/scripts/test-with-postgres.sh+++ b/server/scripts/test-with-postgres.sh@@ -1,112 +1,64 @@#!/bin/sh-set -eu--script_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)-server_dir=$(CDPATH= cd -- "$script_dir/.." && pwd)-test_db="ente_test_$(date +%Y%m%d%H%M%S)_$$"-pg_port="${ENTE_TEST_PGPORT:-45432}"-postgres_mode="${ENTE_TEST_POSTGRES:-auto}"-container_name="ente-server-test-postgres-$$"+# Runs the server test suite against a disposable Postgres database.+# Uses a throwaway Docker Postgres by default; ENTE_TEST_POSTGRES=local+# uses the existing local Postgres instead. See RUNNING.md.-is_local_target() {- case "${1:-}" in- ""|localhost|127.0.0.1|::1|/var/run/postgresql|/tmp)- return 0- ;;- *)- return 1- ;;- esac-}+set -eu-cleanup() {- if [ "$postgres_mode" = "docker" ]; then- docker rm -f "$container_name" >/dev/null 2>&1 || true- return- fi+cd "$(dirname "$0")/.."- psql -v ON_ERROR_STOP=1 -d postgres >/dev/null 2>&1 <<SQL || true-SELECT pg_terminate_backend(pid)-FROM pg_stat_activity-WHERE datname = '$test_db';+test_db="ente_test_$(date +%Y%m%d%H%M%S)_$$"+mode="${ENTE_TEST_POSTGRES:-auto}"-DROP DATABASE IF EXISTS "$test_db";-SQL-}+case "$mode" in+ auto) if docker info >/dev/null 2>&1; then mode=docker; else mode=local; fi ;;+ docker | local) ;;+ *) echo "Unsupported ENTE_TEST_POSTGRES=$mode. Use auto, docker, or local." >&2; exit 1 ;;+esac-start_docker_postgres() {+if [ "$mode" = docker ]; then+ container="ente-server-test-postgres-$$"+ trap 'docker rm -f "$container" >/dev/null 2>&1 || true' EXIT INT TERMdocker run --detach --rm \- --name "$container_name" \+ --name "$container" \--env POSTGRES_DB="$test_db" \--env POSTGRES_PASSWORD=test_pass \- --publish "127.0.0.1:$pg_port:5432" \+ --publish 127.0.0.1::5432 \postgres:15 >/dev/null-- attempts=30- while [ "$attempts" -gt 0 ]; do- if docker exec "$container_name" pg_isready -q -h localhost -p 5432 -d "$test_db" -U postgres; then- return- fi- attempts=$((attempts - 1))+ port=$(docker port "$container" 5432/tcp)+ port=${port##*:}+ # Probe over TCP: the socket-only temporary server that runs during+ # the image's first-boot initialization must not count as ready.+ i=0+ until docker exec "$container" pg_isready -q -h localhost -U postgres; do+ i=$((i + 1))+ [ "$i" -lt 30 ] || { echo "Postgres not ready after 30s." >&2; exit 1; }sleep 1done- echo "Postgres did not become ready after 30 seconds." >&2- exit 1-}--create_local_db() {- if ! command -v psql >/dev/null 2>&1; then- echo "psql is required when using local Postgres." >&2- exit 1- fi- if [ -n "${DATABASE_URL:-}" ] || [ -n "${PGSERVICE:-}" ] || [ -n "${PGSERVICEFILE:-}" ]; then+ export PGHOST=localhost PGPORT="$port" PGUSER=postgres PGPASSWORD=test_pass+else+ # Guard the psql below from being redirected to a non-local cluster.+ [ -z "${DATABASE_URL:-}${PGSERVICE:-}${PGSERVICEFILE:-}" ] || {echo "Refusing DATABASE_URL/PGSERVICE/PGSERVICEFILE; they can redirect psql to another database." >&2exit 1- fi- if [ "${ALLOW_NONLOCAL_TEST_DB:-0}" != "1" ]; then- if ! is_local_target "${PGHOST:-}"; then- echo "Refusing non-local PGHOST=${PGHOST}. Set ALLOW_NONLOCAL_TEST_DB=1 to override." >&2- exit 1- fi- if ! is_local_target "${PGHOSTADDR:-}"; then- echo "Refusing non-local PGHOSTADDR=${PGHOSTADDR}. Set ALLOW_NONLOCAL_TEST_DB=1 to override." >&2- exit 1- fi- fi-- if [ -z "${PGHOST:-}" ] && [ -z "${PGHOSTADDR:-}" ]; then- export PGHOST=localhost- fi- psql -v ON_ERROR_STOP=1 -d postgres <<SQL-CREATE DATABASE "$test_db";-SQL-}--cd "$server_dir"--case "$postgres_mode" in- auto)- if command -v docker >/dev/null 2>&1 && docker info >/dev/null 2>&1; then- postgres_mode=docker- else- postgres_mode=local- fi- ;;- docker|local)- ;;- *)- echo "Unsupported ENTE_TEST_POSTGRES=$postgres_mode. Use auto, docker, or local." >&2- exit 1- ;;-esac--if [ "$postgres_mode" = "docker" ]; then- trap cleanup EXIT INT TERM- start_docker_postgres- ENV=test PGHOST=localhost PGPORT="$pg_port" PGUSER=postgres PGPASSWORD=test_pass PGDATABASE="$test_db" go test -p 1 -count=1 "$@" ./...-else- create_local_db- trap cleanup EXIT INT TERM- ENV=test PGDATABASE="$test_db" go test -p 1 -count=1 "$@" ./...+ }+ for host in "${PGHOST:-}" "${PGHOSTADDR:-}"; do+ case "$host" in+ "" | localhost | 127.0.0.1 | ::1 | /var/run/postgresql | /tmp) ;;+ *)+ [ "${ALLOW_NONLOCAL_TEST_DB:-}" = 1 ] || {+ echo "Refusing non-local Postgres target $host. Set ALLOW_NONLOCAL_TEST_DB=1 to override." >&2+ exit 1+ }+ ;;+ esac+ done+ # psql defaults to the unix socket while lib/pq defaults to TCP+ # localhost; pin both to the same server.+ [ -n "${PGHOST:-}${PGHOSTADDR:-}" ] || export PGHOST=localhost+ psql -qd postgres -c "CREATE DATABASE \"$test_db\""+ trap 'psql -qd postgres -c "DROP DATABASE IF EXISTS \"$test_db\" WITH (FORCE)" >/dev/null 2>&1 || true' EXIT INT TERMfi++ENV=test PGDATABASE="$test_db" go test -p 1 -count=1 "$@" ./...