tests/run-smoke.sh starts an isolated copy of the stack (compose project yolkbook-smoke, nginx on 127.0.0.1:18056, throwaway credentials), optionally loads a mysqldump backup, and runs tests/smoke.py against it. The stack and its volume are removed afterwards. The test covers static pages, CSP and login rate limiting through nginx; login and lockout; CRUD and validation for eggs, flock, feed and other purchases; stats; admin user management and impersonation; password and timezone changes; logout; and rejection of tampered and alg=none tokens. The first run with a given label of "baseline" snapshots every user's list and stats output; later runs must match it exactly, and must still accept a token issued during the baseline run. State is kept in ~/.cache/yolkbook-smoke (outside the repo, since snapshots contain real data). Co-Authored-By: Claude Opus 5.5 <[email protected]>
105 lines
3.9 KiB
Bash
Executable File
105 lines
3.9 KiB
Bash
Executable File
#!/bin/bash
|
|
# Yolkbook smoke test on an isolated copy of the stack.
|
|
#
|
|
# Usage: tests/run-smoke.sh <label> [backup.sql.gz] [--no-build]
|
|
#
|
|
# <label> name for this run. Use "baseline" before a change, anything else after
|
|
# (e.g. "after-fastapi"). Non-baseline runs are compared against the baseline.
|
|
# backup.sql.gz optional mysqldump (made with --databases) to load into the test database
|
|
# first. Without one the test runs on an empty database.
|
|
# --no-build reuse the existing yolkbook-smoke-api image instead of rebuilding ./backend
|
|
#
|
|
# Isolation: runs as compose project "yolkbook-smoke" (its own containers, network and volume),
|
|
# nginx on 127.0.0.1:18056, with throwaway credentials. It never touches the production stack
|
|
# or the real .env secrets. The stack is torn down (volume included) when the run ends.
|
|
#
|
|
# State lives in $SMOKE_DIR (default ~/.cache/yolkbook-smoke, chmod 700), outside the repo,
|
|
# because snapshots contain real data when a backup is loaded:
|
|
# test.env, saved-token.txt, snapshot-<label>.json
|
|
# Delete that directory to start fresh (new credentials, new baseline).
|
|
#
|
|
# Run the baseline and the comparison on the same day: dashboard stats depend on today's date.
|
|
set -euo pipefail
|
|
|
|
LABEL=${1:?usage: tests/run-smoke.sh <label> [backup.sql.gz] [--no-build]}
|
|
BACKUP=""; BUILD=1
|
|
for a in "${@:2}"; do
|
|
case $a in
|
|
--no-build) BUILD=0 ;;
|
|
*) BACKUP=$a ;;
|
|
esac
|
|
done
|
|
|
|
REPO=$(cd "$(dirname "$0")/.." && pwd)
|
|
SMOKE_DIR=${SMOKE_DIR:-$HOME/.cache/yolkbook-smoke}
|
|
mkdir -p "$SMOKE_DIR" && chmod 700 "$SMOKE_DIR"
|
|
umask 077
|
|
|
|
if [ ! -f "$SMOKE_DIR/test.env" ]; then
|
|
cat > "$SMOKE_DIR/test.env" <<EOF
|
|
MYSQL_ROOT_PASSWORD=$(openssl rand -hex 16)
|
|
MYSQL_DATABASE=eggtracker
|
|
MYSQL_USER=eggtracker
|
|
MYSQL_PASSWORD=$(openssl rand -hex 16)
|
|
ADMIN_USERNAME=smokeadmin
|
|
ADMIN_PASSWORD=$(openssl rand -hex 12)
|
|
JWT_SECRET=$(openssl rand -hex 32)
|
|
ALLOWED_ORIGINS=http://localhost:18056
|
|
SECURE_COOKIES=false
|
|
NTFY_URL=
|
|
NTFY_TOKEN=
|
|
EOF
|
|
fi
|
|
|
|
cat > "$SMOKE_DIR/override.yml" <<EOF
|
|
services:
|
|
db:
|
|
env_file: !override [ "$SMOKE_DIR/test.env" ]
|
|
api:
|
|
env_file: !override [ "$SMOKE_DIR/test.env" ]
|
|
image: yolkbook-smoke-api
|
|
nginx:
|
|
ports: !override [ "127.0.0.1:18056:80" ]
|
|
EOF
|
|
|
|
tc() { docker compose -p yolkbook-smoke --project-directory "$REPO" --env-file "$SMOKE_DIR/test.env" \
|
|
-f "$REPO/docker-compose.yml" -f "$SMOKE_DIR/override.yml" "$@"; }
|
|
trap 'tc down -v --remove-orphans >/dev/null 2>&1 || true' EXIT
|
|
|
|
[ $BUILD = 1 ] && docker build -q -t yolkbook-smoke-api "$REPO/backend" >/dev/null
|
|
|
|
tc down -v --remove-orphans >/dev/null 2>&1 || true
|
|
tc up -d db >/dev/null 2>&1
|
|
|
|
# The healthcheck can pass during MySQL's first-time init (temporary server), so wait until
|
|
# root can log in over TCP, which only works once the real server is up.
|
|
echo "Waiting for MySQL..."
|
|
for i in $(seq 1 100); do
|
|
tc exec -T db sh -c 'mysql -h127.0.0.1 -uroot -p"$MYSQL_ROOT_PASSWORD" -e "select 1"' >/dev/null 2>&1 && break
|
|
[ "$i" = 100 ] && { echo "MySQL did not come up"; exit 1; }
|
|
sleep 3
|
|
done
|
|
|
|
if [ -n "$BACKUP" ]; then
|
|
echo "Loading $BACKUP"
|
|
zcat -f "$BACKUP" | tc exec -T db sh -c 'mysql -uroot -p"$MYSQL_ROOT_PASSWORD"' 2>&1 | grep -v Warning || true
|
|
fi
|
|
|
|
tc up -d >/dev/null 2>&1
|
|
for i in $(seq 1 40); do
|
|
[ "$(tc ps api --format '{{.Health}}')" = healthy ] && break
|
|
[ "$i" = 40 ] && { echo "API did not become healthy"; tc logs api | tail -30; exit 1; }
|
|
sleep 3
|
|
done
|
|
tc ps --format '{{.Service}} {{.Image}} {{.Status}}'
|
|
|
|
set -a; . "$SMOKE_DIR/test.env"; set +a
|
|
RC=0
|
|
docker run --rm --user "$(id -u):$(id -g)" --network yolkbook-smoke_backend -v "$REPO/tests:/t:ro" -v "$SMOKE_DIR:/out" \
|
|
yolkbook-smoke-api python /t/smoke.py http://nginx http://api:8000 \
|
|
"$ADMIN_USERNAME" "$ADMIN_PASSWORD" /out "$LABEL" || RC=$?
|
|
|
|
echo "--- API errors/warnings during run (impersonation and admin-action warnings are expected) ---"
|
|
tc logs api 2>&1 | grep -E -i 'error|traceback' || echo "none"
|
|
exit $RC
|