Files
lincodex/deploy/scripts/smoke.sh
T

126 lines
4.1 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
root=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)
compose_file="$root/deploy/docker/docker-compose.yml"
env_file="$root/.env.example"
project=lincodex-smoke
app_network="${project}_default"
port=18080
network="${project}_default"
started=0
compose() {
APP_PROJECT="$project" APP_NETWORK="$app_network" APP_HTTP_PORT="$port" docker compose \
--env-file "$env_file" \
-p "$project" \
-f "$compose_file" \
"$@"
}
for volume in "${project}_postgres_data" "${project}_seaweedfs_data" "${project}_kafka_data"; do
if docker volume inspect "$volume" >/dev/null 2>&1; then
printf 'Refusing to use existing smoke volume %s.\n' "$volume" >&2
exit 2
fi
done
if docker network inspect "$network" >/dev/null 2>&1; then
printf 'Refusing to use existing smoke network %s.\n' "$network" >&2
exit 2
fi
for dockerfile in "$root/backend/Dockerfile" "$root/frontend/Dockerfile"; do
if [[ ! -f "$dockerfile" ]]; then
printf 'Missing required build file %s.\n' "$dockerfile" >&2
exit 2
fi
done
if [[ -n "$(compose ps -q 2>/dev/null || true)" ]]; then
printf 'Refusing to use existing Compose project %s.\n' "$project" >&2
exit 2
fi
cleanup() {
local result=$?
trap - EXIT
if (( started )); then
if ! compose down --volumes --remove-orphans; then
printf '%s\n' 'Smoke cleanup failed; inspect only the lincodex-smoke Compose project.' >&2
result=1
fi
fi
exit "$result"
}
trap cleanup EXIT
compose config --quiet
started=1
compose up --build -d --wait --wait-timeout 120
wait_for() {
local url=$1
local attempt
for attempt in {1..60}; do
if curl --fail --silent --show-error "$url" >/dev/null 2>&1; then
return 0
fi
sleep 1
done
printf 'Timed out waiting for %s.\n' "$url" >&2
return 1
}
wait_for_readiness() {
local attempt response
for attempt in {1..30}; do
response=$(compose exec -T frontend wget -q -O - http://backend:8080/readyz 2>/dev/null || true)
if [[ "$response" == '{"status":"ready"}' ]]; then
return 0
fi
sleep 1
done
printf '%s\n' 'Timed out waiting for PostgreSQL, Redis, and SeaweedFS readiness.' >&2
return 1
}
wait_for "http://127.0.0.1:$port/healthz"
wait_for "http://127.0.0.1:$port/api/v1/healthz"
wait_for_readiness
status=$(curl --silent --show-error --output /dev/null --write-out '%{http_code}' \
"http://127.0.0.1:$port/api/v1/missing")
if [[ "$status" != 404 ]]; then
printf 'Unknown API route returned %s instead of 404.\n' "$status" >&2
exit 1
fi
compose exec -T redis sh -ec '
test "$(redis-cli ping)" = "NOAUTH Authentication required."
export REDISCLI_AUTH="$REDIS_PASSWORD"
test "$(redis-cli ping)" = PONG
test "$(redis-cli --raw CONFIG GET maxmemory-policy | tail -n 1)" = allkeys-lru
test "$(redis-cli --raw CONFIG GET appendonly | tail -n 1)" = no
test -z "$(redis-cli --raw CONFIG GET save | tail -n 1)"
'
compose stop redis
readiness=$(compose exec -T frontend wget -S -O /dev/null http://backend:8080/readyz 2>&1 || true)
if [[ "$readiness" != *"503 Service Unavailable"* ]]; then
printf 'Backend did not return 503 when Redis stopped: %s\n' "$readiness" >&2
exit 1
fi
curl --fail --silent --show-error "http://127.0.0.1:$port/api/v1/healthz" >/dev/null
compose up -d --wait --wait-timeout 60 redis
wait_for_readiness
anonymous=$(compose exec -T seaweedfs sh -c 'wget -S -O /dev/null "http://127.0.0.1:8333/$S3_BUCKET"' 2>&1 || true)
if [[ "$anonymous" != *"403 Forbidden"* ]]; then
printf 'SeaweedFS did not reject anonymous bucket access: %s\n' "$anonymous" >&2
exit 1
fi
compose stop seaweedfs
readiness=$(compose exec -T frontend wget -S -O /dev/null http://backend:8080/readyz 2>&1 || true)
if [[ "$readiness" != *"503 Service Unavailable"* ]]; then
printf 'Backend did not return 503 when SeaweedFS stopped: %s\n' "$readiness" >&2
exit 1
fi
curl --fail --silent --show-error "http://127.0.0.1:$port/api/v1/healthz" >/dev/null
compose up -d --wait --wait-timeout 90 seaweedfs
wait_for_readiness
printf '%s\n' 'Technical smoke passed: frontend, API routing, PostgreSQL, Redis, SeaweedFS, dependency recovery, and storage authentication.'