Files
lincodex/deploy/scripts/e2e.sh
T

191 lines
7.0 KiB
Bash

#!/usr/bin/env bash
# Runs the Playwright suite against an isolated, throwaway Compose stand.
#
# Each run gets a unique project name, a generated env file with random
# secrets and EMAIL_PROVIDER=disabled, no host port, and fresh volumes. The
# stand, its volumes, and its locally built images are removed on every exit.
# Artifacts of the last run stay in frontend/test-results/e2e.
#
# Optional environment:
# E2E_SPEC_DIR directory with specs to run instead of frontend/e2e, for
# example a scratch copy while debugging the suite.
# LINCODEX_RELEASE_TAG run against the release images of this tag (built
# by make -C deploy release-build) without building or pulling,
# keep those images, and record passing evidence for the
# release manifest. Specs must be the committed frontend/e2e.
# Only make -C deploy release-e2e sets it.
set -euo pipefail
root=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)
compose_file="$root/deploy/docker/docker-compose.yml"
e2e_file="$root/deploy/docker/docker-compose.e2e.yml"
release_file="$root/deploy/docker/docker-compose.release.yml"
release_script="$root/deploy/scripts/release_manifest.py"
release_tag="${LINCODEX_RELEASE_TAG:-}"
compose_files=(-f "$compose_file" -f "$e2e_file")
up_args=(--build)
if [[ -n "$release_tag" ]]; then
compose_files+=(-f "$release_file")
up_args=(--no-build --pull never)
fi
frontend_dir="$root/frontend"
artifacts_dir="$frontend_dir/test-results/e2e"
spec_dir="${E2E_SPEC_DIR:-$frontend_dir/e2e}"
suffix=$(od -An -N4 -tx1 /dev/urandom | tr -d ' \n')
project="lincodex-e2e-$suffix"
network="${project}_default"
storage_network="${project}_storage"
env_file=""
started=0
random_secret() {
od -An -N24 -tx1 /dev/urandom | tr -d ' \n'
}
fail() {
printf '%s\n' "$1" >&2
exit 2
}
# Read the exact @playwright/test version; the browser image tag must match it.
declared=$(sed -n 's/^ *"@playwright\/test": "\([^"]*\)",\{0,1\}$/\1/p' "$frontend_dir/package.json")
[[ "$declared" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] ||
fail "frontend/package.json must pin @playwright/test to an exact version, found '$declared'."
installed_manifest="$frontend_dir/node_modules/@playwright/test/package.json"
[[ -f "$installed_manifest" ]] ||
fail 'Frontend dependencies are missing. Run make -C frontend install first.'
installed=$(sed -n 's/^ *"version": "\([^"]*\)",\{0,1\}$/\1/p' "$installed_manifest")
[[ "$installed" == "$declared" ]] ||
fail "Installed @playwright/test $installed differs from $declared. Run make -C frontend install."
[[ -d "$spec_dir" ]] || fail "Spec directory $spec_dir does not exist."
spec_dir=$(cd "$spec_dir" && pwd)
if [[ -n "$release_tag" && "$spec_dir" != "$frontend_dir/e2e" ]]; then
fail 'Release evidence needs the committed suite; unset E2E_SPEC_DIR.'
fi
for dockerfile in "$root/backend/Dockerfile" "$root/frontend/Dockerfile"; do
[[ -f "$dockerfile" ]] || fail "Missing required build file $dockerfile."
done
# Compose runs with a cleared environment: shell variables such as
# EMAIL_PROVIDER, RESEND_API_KEY, or COMPOSE_FILE must not leak into the
# stand. Only Docker client settings pass through.
docker_env=(PATH="$PATH" HOME="$HOME")
while IFS='=' read -r name _; do
case "$name" in
DOCKER_* | BUILDKIT_* | TMPDIR) docker_env+=("$name=${!name}") ;;
esac
done < <(env)
compose() {
env -i "${docker_env[@]}" \
APP_PROJECT="$project" APP_NETWORK="$network" APP_STORAGE_NETWORK="$storage_network" \
APP_SUBNET=172.29.82.0/24 APP_FRONTEND_IP=172.29.82.10 \
PLAYWRIGHT_VERSION="$declared" E2E_CI="${CI:-}" \
E2E_UID="$(id -u)" E2E_GID="$(id -g)" \
E2E_SPEC_DIR="$spec_dir" E2E_ARTIFACTS_DIR="$artifacts_dir" LINCODEX_RELEASE_TAG="$release_tag" \
docker compose \
--env-file "$env_file" \
-p "$project" \
"${compose_files[@]}" \
"$@"
}
for volume in "${project}_postgres_data" "${project}_seaweedfs_data" "${project}_kafka_data"; do
if docker volume inspect "$volume" >/dev/null 2>&1; then
fail "Refusing to use existing E2E volume $volume."
fi
done
for existing in "$network" "$storage_network"; do
if docker network inspect "$existing" >/dev/null 2>&1; then
fail "Refusing to use existing E2E network $existing."
fi
done
if [[ -n "$(docker ps -aq --filter "label=com.docker.compose.project=$project")" ]]; then
fail "Refusing to use existing Compose project $project."
fi
if [[ -n "$release_tag" ]]; then
PYTHONDONTWRITEBYTECODE=1 python3 "$release_script" require-images --tag "$release_tag"
fi
cleanup() {
local result=$?
trap - EXIT INT TERM
if ((started)); then
if ((result != 0)); then
compose logs --no-color --timestamps >"$artifacts_dir/stand.log" 2>&1 || true
printf 'Stand logs: %s\n' "$artifacts_dir/stand.log" >&2
fi
if ! compose --profile e2e down --volumes --remove-orphans --rmi local; then
printf 'E2E cleanup failed; inspect only the %s Compose project.\n' "$project" >&2
result=1
fi
fi
[[ -z "$env_file" ]] || rm -f "$env_file"
printf 'E2E artifacts: %s\n' "$artifacts_dir"
exit "$result"
}
trap cleanup EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
mkdir -p "$artifacts_dir"
rm -f "$artifacts_dir/stand.log"
# Throwaway configuration with synthetic secrets. The backend sends no mail,
# and APP_PUBLIC_URL is the origin the browser uses inside the stand.
env_file=$(mktemp "${TMPDIR:-/tmp}/lincodex-e2e-env.XXXXXX")
chmod 600 "$env_file"
cat >"$env_file" <<EOF
POSTGRES_DB=lincodex
POSTGRES_USER=lincodex
POSTGRES_PASSWORD=$(random_secret)
REDIS_PASSWORD=$(random_secret)
SEAWEEDFS_ACCESS_KEY=e2e-$suffix
SEAWEEDFS_SECRET_KEY=$(random_secret)
SEAWEEDFS_BUCKET=lincodex
KAFKA_CLUSTER_ID=$(head -c 16 /dev/urandom | base64 | tr '+/' '-_' | tr -d '=\n')
EMAIL_PROVIDER=disabled
EMAIL_FROM=no-reply@example.com
EMAIL_TIMEOUT=10s
APP_PUBLIC_URL=http://localhost:8080
PASSWORD_HASH_CONCURRENCY=1
POSTGRES_EXPORTER_USER=
POSTGRES_EXPORTER_PASSWORD=
EOF
printf 'E2E project: %s (Playwright %s)\n' "$project" "$declared"
compose config --quiet
compose --profile e2e config --quiet
started=1
compose up "${up_args[@]}" -d --wait --wait-timeout 180
# The backend has no Compose health check; wait until it reports PostgreSQL,
# Redis, and SeaweedFS ready before the browser starts.
ready=0
for _ in {1..60}; do
if [[ "$(compose exec -T frontend wget -q -O - http://backend:8080/readyz 2>/dev/null || true)" == '{"status":"ready"}' ]]; then
ready=1
break
fi
sleep 1
done
((ready)) || {
printf '%s\n' 'Timed out waiting for backend readiness.' >&2
exit 1
}
set +e
compose run --rm --no-deps -T playwright
status=$?
set -e
if ((status != 0)); then
printf 'Playwright failed with exit code %s; traces and screenshots are in %s/results.\n' \
"$status" "$artifacts_dir" >&2
elif [[ -n "$release_tag" ]]; then
# The suite passed; bind the result to the image IDs that ran.
PYTHONDONTWRITEBYTECODE=1 python3 "$release_script" record-evidence --check e2e --project "$project" \
--tag "$release_tag" --tool-image "playwright=mcr.microsoft.com/playwright:v$declared-noble"
fi
exit "$status"