From 74fd1d89ce9dbf7255c2313670d61deca5e6af89 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 09:17:32 -0500 Subject: [PATCH] CI: run site and membership tests offline in throwaway containers 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 --- .gitea/workflows/tests.yaml | 46 ++++++++++++++++ README.md | 4 ++ ci/membership-test.Dockerfile | 12 ++++ ci/run-tests.sh | 100 ++++++++++++++++++++++++++++++++++ ci/site-test.Dockerfile | 8 +++ 5 files changed, 170 insertions(+) create mode 100644 .gitea/workflows/tests.yaml create mode 100644 ci/membership-test.Dockerfile create mode 100644 ci/run-tests.sh create mode 100644 ci/site-test.Dockerfile diff --git a/.gitea/workflows/tests.yaml b/.gitea/workflows/tests.yaml new file mode 100644 index 0000000..e452ab7 --- /dev/null +++ b/.gitea/workflows/tests.yaml @@ -0,0 +1,46 @@ +# Site and membership tests. DevOps/software-infrastructure#52 (G4). +# +# Manual-only for now: the shared runner is still being stabilised +# (DevOps/software-infrastructure#36-#41). After S8 (#41) passes, replace the +# trigger with: +# +# push: +# branches: [main] +# pull_request: +# +# Every suite runs through ci/run-tests.sh in throwaway containers with no +# network access, so no test can reach Stripe or any production service. +# This workflow uses no secrets; don't add any. Run the same thing locally +# with `sh ci/run-tests.sh all`. + +name: tests + +on: + workflow_dispatch: + +concurrency: + group: tests-${{ github.ref }} + cancel-in-progress: true + +permissions: + contents: read + +jobs: + site: + name: site (node) + runs-on: ubuntu-latest + timeout-minutes: 15 + steps: + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + - run: sh ci/run-tests.sh site + + membership: + name: membership (Django, SQLite and PostgreSQL) + runs-on: ubuntu-latest + timeout-minutes: 30 + steps: + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + - name: SQLite + run: sh ci/run-tests.sh membership + - name: PostgreSQL + run: sh ci/run-tests.sh postgres diff --git a/README.md b/README.md index fde4293..3161c75 100644 --- a/README.md +++ b/README.md @@ -664,6 +664,10 @@ state/PKCE/nonce/issuer/audience/signature tests. PostgreSQL tests run the same suite against a disposable container using [membership/compose.test.yaml](membership/compose.test.yaml). +To run every suite the way CI does, in throwaway containers with no network +access and no secrets, use `sh ci/run-tests.sh all` (or `site`, `membership`, +`postgres`). The workflow is [.gitea/workflows/tests.yaml](.gitea/workflows/tests.yaml). + The suite covers verification expiry/replay, password validation, CSRF, directory identity linking, allowed checkout prices, customer scoping, duplicate/reordered webhooks, paid invoice periods, cancellation, failed renewals, unavailable Stripe, diff --git a/ci/membership-test.Dockerfile b/ci/membership-test.Dockerfile new file mode 100644 index 0000000..6cfe920 --- /dev/null +++ b/ci/membership-test.Dockerfile @@ -0,0 +1,12 @@ +# Offline test image for the membership app (membership/members/tests). +# Built by ci/run-tests.sh from a copy of the whole checkout, because some +# tests read htdocs/ and membership/tiers.eom.json from the repo root. +# Keep the base image in step with membership/Dockerfile. Not for deployment. +ARG PYTHON_IMAGE=python:3.12-slim +FROM ${PYTHON_IMAGE} +ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1 +WORKDIR /src/membership +COPY membership/requirements.txt ./ +RUN pip install --no-cache-dir -r requirements.txt && useradd --create-home --uid 10001 membership +COPY . /src +USER 10001 diff --git a/ci/run-tests.sh b/ci/run-tests.sh new file mode 100644 index 0000000..93ebb63 --- /dev/null +++ b/ci/run-tests.sh @@ -0,0 +1,100 @@ +#!/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 diff --git a/ci/site-test.Dockerfile b/ci/site-test.Dockerfile new file mode 100644 index 0000000..8654e9a --- /dev/null +++ b/ci/site-test.Dockerfile @@ -0,0 +1,8 @@ +# Offline test image for the static site checks in tests/*.test.mjs. +# Built by ci/run-tests.sh from a copy of the checkout. Not for deployment. +ARG NODE_IMAGE=node:24.22.2-slim +FROM ${NODE_IMAGE} +WORKDIR /src +COPY . ./ +USER 1000 +CMD ["sh", "-c", "node --test tests/*.test.mjs"]