test(e2e): run Playwright against an isolated Compose stand
This commit is contained in:
1 parent
7a5105acc7
commit
4cd7d060bd
23 files changed
+431
-41
No files matched your search
+5
-3
@@ -11,6 +11,8 @@ CHECK_ENV_FILE := $(ROOT)/.env.example
|
||||
CORE_FILE := $(ROOT)/deploy/docker/docker-compose.yml
|
||||
ML_FILE := $(ROOT)/deploy/docker/docker-compose.ml.yml
|
||||
MONITORING_FILE := $(ROOT)/deploy/docker/docker-compose.monitoring.yml
|
||||
E2E_FILE := $(ROOT)/deploy/docker/docker-compose.e2e.yml
|
||||
PLAYWRIGHT_VERSION := $(shell sed -n 's/^ *"@playwright\/test": "\([^"]*\)",\{0,1\}$$/\1/p' "$(ROOT)/frontend/package.json")
|
||||
|
||||
.PHONY: help config build up down logs monitoring-config monitoring-up monitoring-down monitoring-logs ml-config ml-build ml-up ml-down ml-logs check smoke test-e2e
|
||||
|
||||
@@ -24,6 +26,7 @@ config: ## Render all Compose files with safe example values
|
||||
APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" $(DOCKER) compose --env-file "$(CHECK_ENV_FILE)" -p "$(APP_PROJECT)" -f "$(CORE_FILE)" config --quiet
|
||||
APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" $(DOCKER) compose --env-file "$(CHECK_ENV_FILE)" -p "$(ML_PROJECT)" -f "$(ML_FILE)" config --quiet
|
||||
APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" $(DOCKER) compose --env-file "$(CHECK_ENV_FILE)" -p "$(MONITORING_PROJECT)" -f "$(MONITORING_FILE)" config --quiet
|
||||
APP_PROJECT=lincodex-e2e-config APP_NETWORK=lincodex-e2e-config_default PLAYWRIGHT_VERSION="$(PLAYWRIGHT_VERSION)" E2E_UID=1000 E2E_GID=1000 E2E_SPEC_DIR="$(ROOT)/frontend/e2e" E2E_ARTIFACTS_DIR="$(ROOT)/frontend/test-results/e2e" $(DOCKER) compose --env-file "$(CHECK_ENV_FILE)" -p lincodex-e2e-config -f "$(CORE_FILE)" -f "$(E2E_FILE)" --profile e2e config --quiet
|
||||
|
||||
build: ## Build local frontend and backend images
|
||||
APP_PROJECT="$(APP_PROJECT)" $(DOCKER) compose --env-file "$(CHECK_ENV_FILE)" -p "$(APP_PROJECT)" -f "$(CORE_FILE)" build
|
||||
@@ -78,6 +81,5 @@ check: config ## Validate all Compose files without starting services
|
||||
smoke: ## Start an isolated stack and check infrastructure health routes
|
||||
@bash scripts/smoke.sh
|
||||
|
||||
test-e2e: ## Reserved for the future synthetic business-flow E2E suite
|
||||
@printf '%s\n' 'test-e2e is deferred until the synthetic business-flow suite is implemented.' >&2
|
||||
@exit 2
|
||||
test-e2e: ## Run the Playwright suite against an isolated throwaway stack
|
||||
@bash scripts/e2e.sh
|
||||
+3
-2
@@ -7,8 +7,9 @@ Configuration for running the application with Docker Compose and Nginx.
|
||||
- `docker/docker-compose.yml` starts the core stack with PostgreSQL, Redis, SeaweedFS, and Kafka.
|
||||
- `docker/docker-compose.ml.yml` and `docker/docker-compose.monitoring.yml` run ML and monitoring as separate projects.
|
||||
- `nginx/` configures frontend serving and API proxying.
|
||||
- `scripts/` contains the isolated stack smoke check.
|
||||
- `docker/docker-compose.e2e.yml` is an overlay used only by the browser suite: it removes the frontend host port and adds the Playwright runner.
|
||||
- `scripts/` contains the isolated stack smoke check and the browser E2E runner.
|
||||
|
||||
Dockerfiles live in service directories and `.env.example` at the repository root. From the root, `make -C deploy config` checks Compose and `make -C deploy up` starts the core stack after `.env` is prepared. Start monitoring separately with `make -C deploy monitoring-up`.
|
||||
Dockerfiles live in service directories and `.env.example` at the repository root. From the root, `make -C deploy config` checks Compose and `make -C deploy up` starts the core stack after `.env` is prepared. Start monitoring separately with `make -C deploy monitoring-up`. `make -C deploy test-e2e` runs the Playwright suite against a throwaway stack under a unique project name and removes it afterwards; see [TESTING.md](../docs/en/TESTING.md#browser-e2e-suite).
|
||||
|
||||
See the [deployment guide](../docs/en/DEPLOY.md).
|
||||
+3
-2
@@ -7,8 +7,9 @@
|
||||
- `docker/docker-compose.yml` поднимает основной стек с PostgreSQL, Redis, SeaweedFS и Kafka.
|
||||
- `docker/docker-compose.ml.yml` и `docker/docker-compose.monitoring.yml` запускают ML и мониторинг отдельными проектами.
|
||||
- `nginx/` содержит конфигурацию раздачи frontend и проксирования API.
|
||||
- `scripts/` содержит smoke-проверку изолированного стека.
|
||||
- `docker/docker-compose.e2e.yml` - overlay только для браузерного набора: убирает host-порт frontend и добавляет запуск Playwright.
|
||||
- `scripts/` содержит smoke-проверку изолированного стека и запуск браузерных E2E.
|
||||
|
||||
Dockerfile лежат в каталогах сервисов, `.env.example` - в корне репозитория. Из корня: `make -C deploy config` проверяет Compose, `make -C deploy up` запускает основной стек после подготовки `.env`. Мониторинг включается отдельно через `make -C deploy monitoring-up`.
|
||||
Dockerfile лежат в каталогах сервисов, `.env.example` - в корне репозитория. Из корня: `make -C deploy config` проверяет Compose, `make -C deploy up` запускает основной стек после подготовки `.env`. Мониторинг включается отдельно через `make -C deploy monitoring-up`. `make -C deploy test-e2e` запускает набор Playwright на одноразовом стеке с уникальным именем проекта и удаляет его после прогона; см. [TESTING.md](../docs/ru/TESTING.md#браузерный-e2e-набор).
|
||||
|
||||
Подробнее: [развертывание](../docs/ru/DEPLOY.md).
|
||||
@@ -0,0 +1,40 @@
|
||||
# Overlay for the isolated browser suite. Only deploy/scripts/e2e.sh combines
|
||||
# it with docker-compose.yml, under a unique project name and a generated
|
||||
# throwaway env file; never apply it to the working stand.
|
||||
services:
|
||||
frontend:
|
||||
# The browser shares this container's network namespace, so the stand
|
||||
# publishes no host port and cannot collide with a running stand.
|
||||
ports: !reset []
|
||||
|
||||
playwright:
|
||||
# The tag must match @playwright/test in frontend/package.json; the runner
|
||||
# reads the version from there.
|
||||
image: mcr.microsoft.com/playwright:v${PLAYWRIGHT_VERSION:?Set PLAYWRIGHT_VERSION}-noble
|
||||
profiles: ["e2e"]
|
||||
# localhost:8080 inside this namespace is the frontend Nginx, so the
|
||||
# browser origin equals the loopback APP_PUBLIC_URL the backend accepts.
|
||||
network_mode: service:frontend
|
||||
user: "${E2E_UID:?Set E2E_UID}:${E2E_GID:?Set E2E_GID}"
|
||||
init: true
|
||||
working_dir: /workspace/frontend
|
||||
environment:
|
||||
HOME: /tmp
|
||||
CI: ${E2E_CI:-}
|
||||
E2E_BASE_URL: http://localhost:8080
|
||||
E2E_ARTIFACTS_DIR: /artifacts
|
||||
# Only the dependencies installed from frontend/pnpm-lock.yaml and the
|
||||
# suite are read; results go to the separate writable mount.
|
||||
volumes:
|
||||
- ../../frontend:/workspace/frontend:ro
|
||||
- ${E2E_SPEC_DIR:?Set E2E_SPEC_DIR}:/workspace/frontend/e2e:ro
|
||||
- ${E2E_ARTIFACTS_DIR:?Set E2E_ARTIFACTS_DIR}:/artifacts
|
||||
command: ["node", "node_modules/@playwright/test/cli.js", "test"]
|
||||
cap_drop: [ALL]
|
||||
security_opt: ["no-new-privileges:true"]
|
||||
shm_size: 1gb
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "2.0"
|
||||
memory: 2G
|
||||
@@ -9,7 +9,7 @@ services:
|
||||
POSTGRES_EXPORTER_PASSWORD: ${POSTGRES_EXPORTER_PASSWORD:-}
|
||||
volumes:
|
||||
- postgres_data:/var/lib/postgresql
|
||||
- ./initdb/10-postgres-exporter.sh:/docker-entrypoint-initdb.d/10-postgres-exporter.sh:ro
|
||||
- ./initdb/10-postgres-exporter.sql:/docker-entrypoint-initdb.d/10-postgres-exporter.sql:ro
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U \"$${POSTGRES_USER}\" -d \"$${POSTGRES_DB}\""]
|
||||
interval: 10s
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
if [[ -z "${POSTGRES_EXPORTER_USER:-}" && -z "${POSTGRES_EXPORTER_PASSWORD:-}" ]]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
: "${POSTGRES_EXPORTER_USER:?POSTGRES_EXPORTER_USER and POSTGRES_EXPORTER_PASSWORD must both be set}"
|
||||
: "${POSTGRES_EXPORTER_PASSWORD:?POSTGRES_EXPORTER_USER and POSTGRES_EXPORTER_PASSWORD must both be set}"
|
||||
|
||||
psql \
|
||||
--username "$POSTGRES_USER" \
|
||||
--dbname "$POSTGRES_DB" \
|
||||
--set=exporter_user="$POSTGRES_EXPORTER_USER" \
|
||||
--set=exporter_password="$POSTGRES_EXPORTER_PASSWORD" <<'SQL'
|
||||
SELECT format('CREATE ROLE %I LOGIN PASSWORD %L', :'exporter_user', :'exporter_password')
|
||||
WHERE NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = :'exporter_user')
|
||||
\gexec
|
||||
|
||||
GRANT pg_monitor TO :"exporter_user";
|
||||
SQL
|
||||
@@ -0,0 +1,38 @@
|
||||
-- Creates the optional PostgreSQL metrics login on first volume
|
||||
-- initialization. The image entrypoint runs *.sql files with psql
|
||||
-- (ON_ERROR_STOP=1, as POSTGRES_USER in POSTGRES_DB), independent of file
|
||||
-- mode, unlike *.sh files, which it executes or sources depending on the
|
||||
-- executable bit reported by the bind mount. \getenv needs psql 15 or newer.
|
||||
--
|
||||
-- Both credentials empty or unset: nothing happens. Exactly one set: the
|
||||
-- initialization fails. Both set: the role is created if missing and gets
|
||||
-- pg_monitor.
|
||||
|
||||
\getenv exporter_user POSTGRES_EXPORTER_USER
|
||||
\getenv exporter_password POSTGRES_EXPORTER_PASSWORD
|
||||
\if :{?exporter_user}
|
||||
\else
|
||||
\set exporter_user ''
|
||||
\endif
|
||||
\if :{?exporter_password}
|
||||
\else
|
||||
\set exporter_password ''
|
||||
\endif
|
||||
|
||||
SELECT :'exporter_user' <> '' AS has_exporter_user,
|
||||
:'exporter_password' <> '' AS has_exporter_password
|
||||
\gset
|
||||
|
||||
\if :has_exporter_user
|
||||
\if :has_exporter_password
|
||||
SELECT format('CREATE ROLE %I LOGIN PASSWORD %L', :'exporter_user', :'exporter_password')
|
||||
WHERE NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = :'exporter_user')
|
||||
\gexec
|
||||
|
||||
GRANT pg_monitor TO :"exporter_user";
|
||||
\else
|
||||
DO $$ BEGIN RAISE EXCEPTION 'POSTGRES_EXPORTER_USER and POSTGRES_EXPORTER_PASSWORD must both be set'; END $$;
|
||||
\endif
|
||||
\elif :has_exporter_password
|
||||
DO $$ BEGIN RAISE EXCEPTION 'POSTGRES_EXPORTER_USER and POSTGRES_EXPORTER_PASSWORD must both be set'; END $$;
|
||||
\endif
|
||||
@@ -0,0 +1,163 @@
|
||||
#!/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.
|
||||
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"
|
||||
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"
|
||||
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)
|
||||
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" \
|
||||
PLAYWRIGHT_VERSION="$declared" E2E_CI="${CI:-}" \
|
||||
E2E_UID="$(id -u)" E2E_GID="$(id -g)" \
|
||||
E2E_SPEC_DIR="$spec_dir" E2E_ARTIFACTS_DIR="$artifacts_dir" \
|
||||
docker compose \
|
||||
--env-file "$env_file" \
|
||||
-p "$project" \
|
||||
-f "$compose_file" \
|
||||
-f "$e2e_file" \
|
||||
"$@"
|
||||
}
|
||||
|
||||
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
|
||||
if docker network inspect "$network" >/dev/null 2>&1; then
|
||||
fail "Refusing to use existing E2E network $network."
|
||||
fi
|
||||
if [[ -n "$(docker ps -aq --filter "label=com.docker.compose.project=$project")" ]]; then
|
||||
fail "Refusing to use existing Compose project $project."
|
||||
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 --build -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
|
||||
fi
|
||||
exit "$status"
|
||||
Reference in new issue
Block a user