ci/run-tests.sh builds test images from the checkout and runs the node site checks and the Django suite (SQLite, then a disposable PostgreSQL) with no network access, so no test can reach Stripe or production. No secrets are used. The workflow is manual-only until the shared runner is stable (S8); its header has the push/pull_request trigger to switch to. Refs DevOps/software-infrastructure#52 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
101 lines
3.5 KiB
Bash
101 lines
3.5 KiB
Bash
#!/bin/sh
|
|
# Run the www test suites in throwaway containers with no network access.
|
|
# CI runs exactly this (.gitea/workflows/tests.yaml); run it locally the same way:
|
|
#
|
|
# sh ci/run-tests.sh site # node --test tests/*.test.mjs
|
|
# sh ci/run-tests.sh membership # Django tests on in-memory SQLite
|
|
# sh ci/run-tests.sh postgres # Django tests on a disposable PostgreSQL
|
|
# sh ci/run-tests.sh all
|
|
#
|
|
# Safety: test containers run with --network none (PostgreSQL tests use an
|
|
# --internal network shared only with the database), so no test can reach
|
|
# Stripe, SMTP, LDAP, Dex or production, whatever it does. No secrets or real
|
|
# keys are passed in; the database password is random per run.
|
|
#
|
|
# The checkout is sent to Docker as a build context (minus .git), not
|
|
# bind-mounted, because on the shared runner the Docker daemon can't see the
|
|
# job's files. DevOps/software-infrastructure#52 (G4).
|
|
|
|
set -eu
|
|
|
|
cd "$(dirname "$0")/.."
|
|
|
|
suite="${1:-all}"
|
|
id="$(printf '%s' "${GITHUB_RUN_ID:-local}-${GITHUB_JOB:-run}-$$" | tr -c 'a-zA-Z0-9-' '-' | tr '[:upper:]' '[:lower:]')"
|
|
prefix="eom-www-test-$id"
|
|
postgres_image="${POSTGRES_IMAGE:-postgres:17}"
|
|
images=""
|
|
containers=""
|
|
network=""
|
|
|
|
cleanup() {
|
|
status=$?
|
|
for c in $containers; do docker rm -f "$c" >/dev/null 2>&1 || true; done
|
|
if [ -n "$network" ]; then docker network rm "$network" >/dev/null 2>&1 || true; fi
|
|
for i in $images; do docker image rm -f "$i" >/dev/null 2>&1 || true; done
|
|
exit "$status"
|
|
}
|
|
trap cleanup EXIT
|
|
trap 'exit 130' INT TERM
|
|
|
|
build() {
|
|
# $1 = Dockerfile under ci/, $2 = tag
|
|
images="$images $2"
|
|
tar -c --exclude=./.git -f - . | docker build --quiet -f "ci/$1" -t "$2" - >/dev/null
|
|
}
|
|
|
|
run_site() {
|
|
echo "== site: node --test tests/*.test.mjs (offline)"
|
|
build site-test.Dockerfile "$prefix-site"
|
|
docker run --rm --network none --name "$prefix-site" "$prefix-site"
|
|
}
|
|
|
|
membership_image() {
|
|
if [ -z "${membership_built:-}" ]; then
|
|
build membership-test.Dockerfile "$prefix-membership"
|
|
membership_built=1
|
|
fi
|
|
}
|
|
|
|
run_membership() {
|
|
echo "== membership: Django tests on SQLite (offline)"
|
|
membership_image
|
|
docker run --rm --network none --name "$prefix-sqlite" "$prefix-membership" \
|
|
python manage.py test members.tests --settings=config.test_settings --verbosity=1
|
|
}
|
|
|
|
run_postgres() {
|
|
echo "== membership: Django tests on disposable PostgreSQL (offline)"
|
|
membership_image
|
|
password="$(od -An -N24 -tx1 /dev/urandom | tr -d ' \n')"
|
|
network="$prefix-net"
|
|
docker network create --internal "$network" >/dev/null
|
|
containers="$containers $prefix-db"
|
|
docker run -d --name "$prefix-db" --network "$network" --network-alias db \
|
|
--tmpfs /var/lib/postgresql/data \
|
|
-e POSTGRES_DB=membership_test -e POSTGRES_USER=membership_test \
|
|
-e POSTGRES_PASSWORD="$password" "$postgres_image" >/dev/null
|
|
tries=0
|
|
# TCP check: the image's first-boot server listens only on a socket.
|
|
until docker exec "$prefix-db" pg_isready -q -h 127.0.0.1 -U membership_test -d membership_test 2>/dev/null; do
|
|
tries=$((tries + 1))
|
|
if [ "$tries" -ge 60 ]; then
|
|
echo "PostgreSQL did not become ready in 120 s" >&2
|
|
docker logs --tail 20 "$prefix-db" >&2 || true
|
|
return 1
|
|
fi
|
|
sleep 2
|
|
done
|
|
docker run --rm --network "$network" --name "$prefix-pg" \
|
|
-e MEMBERSHIP_TEST_DB_PASSWORD="$password" "$prefix-membership" \
|
|
python manage.py test members.tests --settings=config.postgres_test_settings --verbosity=1
|
|
}
|
|
|
|
case "$suite" in
|
|
site) run_site ;;
|
|
membership) run_membership ;;
|
|
postgres) run_postgres ;;
|
|
all) run_site; run_membership; run_postgres ;;
|
|
*) echo "usage: $0 [site|membership|postgres|all]" >&2; exit 2 ;;
|
|
esac
|