mirror of
https://github.com/go-gitea/gitea.git
synced 2026-10-02 10:54:25 +02:00
Rework e2e test setup for full isolation
Always start an isolated ephemeral Gitea instance with its own temp directory, SQLite database, and config file. This addresses review feedback that using the developer's existing instance is unreliable. - Rewrite test-e2e.sh to create a temp workdir, find a free port, write a minimal app.ini, start the server, and clean up on exit - Build a separate gitea-e2e binary using TEST_TAGS (includes sqlite) - Simplify CI workflow: remove manual app.ini, server start, and redundant build steps - Rename all env vars to use GITEA_TEST_E2E_* prefix - Rename test user from "e2e" to "e2e-user" Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
6f0497a894
commit
f6037c90d3
+62
-61
@@ -1,89 +1,90 @@
|
||||
#!/bin/bash
|
||||
set -euo pipefail
|
||||
|
||||
# Determine the Gitea server URL, either from E2E_URL env var or from custom/conf/app.ini
|
||||
if [ -z "${E2E_URL:-}" ]; then
|
||||
INI_FILE="custom/conf/app.ini"
|
||||
if [ ! -f "$INI_FILE" ]; then
|
||||
echo "error: $INI_FILE not found and E2E_URL not set" >&2
|
||||
echo "Either start Gitea with a config or set E2E_URL explicitly:" >&2
|
||||
echo " E2E_URL=http://localhost:3000 make test-e2e" >&2
|
||||
exit 1
|
||||
fi
|
||||
# Note: this does not respect INI sections, assumes ROOT_URL only appears under [server]
|
||||
ROOT_URL=$(sed -n 's/^ROOT_URL\s*=\s*//p' "$INI_FILE" | tr -d '[:space:]')
|
||||
if [ -z "$ROOT_URL" ]; then
|
||||
echo "error: ROOT_URL not found in $INI_FILE" >&2
|
||||
exit 1
|
||||
fi
|
||||
E2E_URL="$ROOT_URL"
|
||||
fi
|
||||
# Create isolated work directory
|
||||
WORK_DIR=$(mktemp -d)
|
||||
|
||||
# Normalize URL: trim trailing slash to avoid double slashes when appending paths
|
||||
E2E_URL="${E2E_URL%/}"
|
||||
# Find a random free port
|
||||
FREE_PORT=$(node -e "const s=require('net').createServer();s.listen(0,'127.0.0.1',()=>{console.log(s.address().port);s.close()})")
|
||||
|
||||
echo "Using Gitea server: $E2E_URL"
|
||||
|
||||
# Disable CAPTCHA for e2e tests
|
||||
export GITEA__service__ENABLE_CAPTCHA=false
|
||||
|
||||
SERVER_PID=""
|
||||
cleanup() {
|
||||
if [ -n "$SERVER_PID" ]; then
|
||||
echo "Stopping temporary Gitea server (PID $SERVER_PID)..."
|
||||
if [ -n "${SERVER_PID:-}" ]; then
|
||||
kill "$SERVER_PID" 2>/dev/null || true
|
||||
wait "$SERVER_PID" 2>/dev/null || true
|
||||
fi
|
||||
rm -rf "$WORK_DIR"
|
||||
}
|
||||
trap cleanup EXIT
|
||||
|
||||
# For local development, if no gitea server is running, start a temporary one.
|
||||
if [ -z "${CI:-}" ] && ! curl -sf --max-time 5 "$E2E_URL" > /dev/null 2>&1; then
|
||||
if [ ! -x "./$EXECUTABLE" ]; then
|
||||
echo "error: ./$EXECUTABLE not found or not executable, run 'make backend' first" >&2
|
||||
exit 1
|
||||
fi
|
||||
echo "Starting temporary Gitea server..."
|
||||
if [ -n "${E2E_DEBUG:-}" ]; then
|
||||
"./$EXECUTABLE" web &
|
||||
else
|
||||
"./$EXECUTABLE" web > /dev/null 2>&1 &
|
||||
fi
|
||||
SERVER_PID=$!
|
||||
fi
|
||||
# Write config file for isolated instance
|
||||
mkdir -p "$WORK_DIR/custom/conf"
|
||||
cat > "$WORK_DIR/custom/conf/app.ini" <<EOF
|
||||
[database]
|
||||
DB_TYPE = sqlite3
|
||||
PATH = $WORK_DIR/data/gitea.db
|
||||
|
||||
# Verify server is reachable, retry for up to 2 minutes for slow startup
|
||||
[server]
|
||||
HTTP_PORT = $FREE_PORT
|
||||
ROOT_URL = http://localhost:$FREE_PORT
|
||||
STATIC_ROOT_PATH = $(pwd)
|
||||
|
||||
[security]
|
||||
INSTALL_LOCK = true
|
||||
|
||||
[service]
|
||||
ENABLE_CAPTCHA = false
|
||||
|
||||
[log]
|
||||
MODE = console
|
||||
LEVEL = Warn
|
||||
EOF
|
||||
|
||||
export GITEA_WORK_DIR="$WORK_DIR"
|
||||
|
||||
# Start Gitea server
|
||||
echo "Starting Gitea server on port $FREE_PORT (workdir: $WORK_DIR)..."
|
||||
if [ -n "${GITEA_TEST_E2E_DEBUG:-}" ]; then
|
||||
"./$EXECUTABLE" web &
|
||||
else
|
||||
"./$EXECUTABLE" web > "$WORK_DIR/server.log" 2>&1 &
|
||||
fi
|
||||
SERVER_PID=$!
|
||||
|
||||
# Wait for server to be reachable
|
||||
E2E_URL="http://localhost:$FREE_PORT"
|
||||
MAX_WAIT=120
|
||||
ELAPSED=0
|
||||
while ! curl -sf --max-time 5 "$E2E_URL" > /dev/null 2>&1; do
|
||||
if [ -n "$SERVER_PID" ] && ! kill -0 "$SERVER_PID" 2>/dev/null; then
|
||||
echo "error: Gitea server process exited unexpectedly" >&2
|
||||
if ! kill -0 "$SERVER_PID" 2>/dev/null; then
|
||||
echo "error: Gitea server process exited unexpectedly. Server log:" >&2
|
||||
cat "$WORK_DIR/server.log" 2>/dev/null >&2 || true
|
||||
exit 1
|
||||
fi
|
||||
if [ "$ELAPSED" -ge "$MAX_WAIT" ]; then
|
||||
echo "error: Gitea server at $E2E_URL is not reachable after ${MAX_WAIT}s" >&2
|
||||
echo "error: Gitea server not reachable after ${MAX_WAIT}s. Server log:" >&2
|
||||
cat "$WORK_DIR/server.log" 2>/dev/null >&2 || true
|
||||
exit 1
|
||||
fi
|
||||
sleep 2
|
||||
ELAPSED=$((ELAPSED + 2))
|
||||
done
|
||||
|
||||
# Create e2e test user if it does not already exist
|
||||
E2E_USER="e2e"
|
||||
E2E_EMAIL="e2e@e2e.gitea.com"
|
||||
E2E_PASSWORD="password"
|
||||
if ! curl -sf --max-time 5 "$E2E_URL/api/v1/users/$E2E_USER" > /dev/null 2>&1; then
|
||||
echo "Creating e2e test user..."
|
||||
if "./$EXECUTABLE" admin user create --username "$E2E_USER" --email "$E2E_EMAIL" --password "$E2E_PASSWORD" --must-change-password=false --admin; then
|
||||
echo "User '$E2E_USER' created"
|
||||
else
|
||||
echo "error: failed to create user '$E2E_USER'" >&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
echo "Gitea server is ready at $E2E_URL"
|
||||
|
||||
export E2E_URL
|
||||
export E2E_USER
|
||||
export E2E_PASSWORD
|
||||
# Create admin test user
|
||||
GITEA_TEST_E2E_USER="e2e-user"
|
||||
GITEA_TEST_E2E_EMAIL="e2e-user@e2e.gitea.com"
|
||||
GITEA_TEST_E2E_PASSWORD="password"
|
||||
"./$EXECUTABLE" admin user create \
|
||||
--username "$GITEA_TEST_E2E_USER" \
|
||||
--email "$GITEA_TEST_E2E_EMAIL" \
|
||||
--password "$GITEA_TEST_E2E_PASSWORD" \
|
||||
--must-change-password=false \
|
||||
--admin
|
||||
|
||||
export GITEA_TEST_E2E_URL="$E2E_URL"
|
||||
export GITEA_TEST_E2E_USER
|
||||
export GITEA_TEST_E2E_EMAIL
|
||||
export GITEA_TEST_E2E_PASSWORD
|
||||
|
||||
pnpm exec playwright test "$@"
|
||||
|
||||
Reference in New Issue
Block a user