169 lines
6.2 KiB
Bash
169 lines
6.2 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"
|
|
storage_network="${project}_storage"
|
|
started=0
|
|
|
|
# The smoke stack sends no mail: it uses EMAIL_PROVIDER=disabled from
|
|
# .env.example and points mail links at its own loopback port.
|
|
compose() {
|
|
APP_PROJECT="$project" APP_NETWORK="$app_network" APP_STORAGE_NETWORK="$storage_network" APP_HTTP_PORT="$port" \
|
|
APP_PUBLIC_URL="http://localhost:$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
|
|
for existing in "$network" "$storage_network"; do
|
|
if docker network inspect "$existing" >/dev/null 2>&1; then
|
|
printf 'Refusing to use existing smoke network %s.\n' "$existing" >&2
|
|
exit 2
|
|
fi
|
|
done
|
|
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"
|
|
backend_logs=$(compose logs --no-color backend)
|
|
if [[ "$backend_logs" != *'mail transport selected'*'disabled'* ]]; then
|
|
printf '%s\n' 'Backend did not report EMAIL_PROVIDER=disabled at startup.' >&2
|
|
exit 1
|
|
fi
|
|
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
|
|
# The storage network is the primary boundary. The frontend stands in for
|
|
# every other core container: it must neither resolve nor reach SeaweedFS, by
|
|
# name or by its storage address. A DNS or connection failure is a pass.
|
|
storage_address=$(docker inspect -f "{{(index .NetworkSettings.Networks \"$storage_network\").IPAddress}}" "$(compose ps -q seaweedfs)")
|
|
if [[ -z "$storage_address" ]]; then
|
|
printf 'SeaweedFS has no address on %s.\n' "$storage_network" >&2
|
|
exit 1
|
|
fi
|
|
for url in "http://seaweedfs:8333/" "http://$storage_address:8333/"; do
|
|
isolation=$(compose exec -T frontend wget -T 5 -S -O /dev/null "$url" 2>&1 || true)
|
|
if [[ "$isolation" == *"HTTP/"* ]]; then
|
|
printf 'The frontend reached SeaweedFS outside the storage network at %s: %s\n' "$url" "$isolation" >&2
|
|
exit 1
|
|
fi
|
|
done
|
|
# Defense in depth inside the storage network. Requests to the container's own
|
|
# storage address do not come from loopback, so they stand in for another
|
|
# member of that network; the filer, master, and volume HTTP APIs answer 401.
|
|
compose exec -T seaweedfs sh -ec '
|
|
expect_unauthorized() {
|
|
response=$(wget -S -O /dev/null "$1" 2>&1 || true)
|
|
case "$response" in
|
|
*"401 Unauthorized"*) ;;
|
|
*) printf "SeaweedFS %s did not return 401: %s\n" "$1" "$response" >&2; exit 1 ;;
|
|
esac
|
|
}
|
|
expect_unauthorized http://seaweedfs:8888/buckets/
|
|
expect_unauthorized http://seaweedfs:9333/dir/assign
|
|
fid=$(wget -q -O - http://127.0.0.1:9333/dir/assign | sed -n "s/.*\"fid\":\"\([^\"]*\)\".*/\1/p")
|
|
test -n "$fid"
|
|
expect_unauthorized "http://seaweedfs:9340/$fid"
|
|
'
|
|
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, storage authentication and network isolation, and the disabled mail transport.'
|