Files
pages/tools/ci/test-postgres.sh
T
2026-11-15 05:47:14 +09:00

39 lines
1.4 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
# Synthetic data only. Share the PostgreSQL network namespace so neither
# published ports nor a newly allocated Docker subnet are needed.
suffix="${GITHUB_RUN_ID:-local}-$$-${RANDOM}"
postgres="pageswiki-postgres-test-${suffix}"
tests="pageswiki-postgres-go-${suffix}"
test_image="pageswiki-postgres-test:${suffix}"
cleanup() {
docker rm -fv "$tests" "$postgres" >/dev/null 2>&1 || true
docker image rm "$test_image" >/dev/null 2>&1 || true
}
trap cleanup EXIT
docker build --target source --tag "$test_image" .
docker run --detach --name "$postgres" --network none \
--env POSTGRES_DB=pageswiki_test \
--env POSTGRES_PASSWORD=pageswiki-synthetic-ci-only \
"${PAGESWIKI_CI_POSTGRES_IMAGE:-postgres:17}"
ready=false
for ((attempt=0; attempt<30; attempt++)); do
if docker exec "$postgres" pg_isready -U postgres -d pageswiki_test >/dev/null 2>&1; then
ready=true
break
fi
sleep 1
done
if [[ "$ready" != true ]]; then
echo 'Synthetic PostgreSQL did not become ready' >&2
exit 1
fi
docker run --rm --name "$tests" --network "container:$postgres" \
--tmpfs /test-tmp:rw,size=2147483648 \
--env TMPDIR=/test-tmp --env GOTMPDIR=/tmp \
--env GOPROXY=off --env GOSUMDB=off \
--env PAGESWIKI_TEST_POSTGRES_DSN='postgres://postgres:pageswiki-synthetic-ci-only@127.0.0.1:5432/pageswiki_test?sslmode=disable' \
"$test_image" go test -p 1 ./...