zat.dev/zds
atproto pds · mirror of https://tangled.org/zat.dev/zds
zds 0.25.1 Latest 2 packages, all images
0
0
#!/usr/bin/env sh
# Run zds under tools/container-start.sh with Litestream streaming to a local
# S3 stand-in (moto), write through the API, then check that
# tools/restore-drill.sh gets every write back, both after a clean stop and
# while zds is still running, and that an empty volume is refilled from the
# replica when ZDS_LITESTREAM_RESTORE_IF_MISSING is on. Needs litestream,
# sqlite3 and uv on PATH.
set -eu
root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
work=$(mktemp -d "${TMPDIR:-/tmp}/zds-smoke-backups.XXXXXX")
port="${ZDS_SMOKE_PORT:-2587}"
s3_port="${ZDS_SMOKE_S3_PORT:-2588}"
base="http://127.0.0.1:${port}"
admin="smoke-admin-token"
cleanup() {
status=$?
for pid in ${zds_pid:-} ${s3_pid:-}; do
kill "$pid" 2>/dev/null || true
wait "$pid" 2>/dev/null || true
done
if [ "$status" -eq 0 ]; then
rm -rf "$work"
else
echo "backup smoke failed; diagnostic files: $work" >&2
fi
}
trap cleanup EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
for tool in litestream sqlite3 uv curl; do
command -v "$tool" >/dev/null || { echo "backup smoke needs $tool on PATH" >&2; exit 1; }
done
cd "$root"
if [ "${ZDS_SMOKE_PREBUILT:-0}" != 1 ]; then
zig build
fi
mkdir -p "$work/bin"
ln -s "$root/zig-out/bin/zds" "$work/bin/zds"
uv run --quiet --no-project --with 'moto[server]' moto_server -H 127.0.0.1 -p "$s3_port" \
>"$work/s3.log" 2>&1 &
s3_pid=$!
export AWS_ACCESS_KEY_ID=smoke AWS_SECRET_ACCESS_KEY=smoke AWS_REGION=us-east-1
export AWS_ENDPOINT_URL_S3="http://127.0.0.1:${s3_port}"
export ZDS_LITESTREAM_BUCKET=zds-smoke-backups
export ZDS_LITESTREAM_CONFIG="$root/config/litestream.yml"
# the shortest interval, so the running-server check does not wait long
export ZDS_LITESTREAM_SYNC_INTERVAL=1s
i=0
until curl -fsS -o /dev/null -X PUT "$AWS_ENDPOINT_URL_S3/$ZDS_LITESTREAM_BUCKET" 2>/dev/null; do
i=$((i + 1))
[ "$i" -lt 60 ] || { echo "S3 stand-in did not start" >&2; exit 1; }
sleep 0.5
done
export ZDS_DB="$work/data/zds.sqlite3"
mkdir -p "$work/data/blobs"
start_zds() {
PATH="$work/bin:$PATH" \
ZDS_HOST=127.0.0.1 ZDS_PORT="$port" ZDS_PUBLIC_URL="$base" ZDS_SERVER_DID=did:web:localhost \
ZDS_BLOBSTORE_PATH="$work/data/blobs" ZDS_JWT_SECRET=smoke-jwt-secret ZDS_ADMIN_TOKEN="$admin" \
ZDS_CRAWLERS="" ZDS_MAIL_PROVIDER=outbox \
"$root/tools/container-start.sh" >>"$work/zds.log" 2>&1 &
zds_pid=$!
i=0
until curl -fsS -o /dev/null "$base/xrpc/_health" 2>/dev/null; do
i=$((i + 1))
if [ "$i" -ge 80 ] || ! kill -0 "$zds_pid" 2>/dev/null; then
echo "zds did not start under Litestream; log: $work/zds.log" >&2
exit 1
fi
sleep 0.25
done
}
stop_zds() {
kill -TERM "$zds_pid"
wait "$zds_pid" || true
zds_pid=""
}
mint() {
n=$1
while [ "$n" -gt 0 ]; do
curl -fsS -o /dev/null "$base/xrpc/com.atproto.server.createInviteCode" \
-H "authorization: Bearer $admin" -H 'content-type: application/json' \
--data '{"useCount":1}'
n=$((n - 1))
done
}
codes() { sqlite3 -batch -noheader "$1" "SELECT count(*) FROM invite_codes"; }
drill() {
ZDS_DRILL_KEEP=1 "$root/tools/restore-drill.sh" >"$work/drill.log" 2>&1 || {
cat "$work/drill.log" >&2
exit 1
}
sed -n 's/^scratch directory kept: //p' "$work/drill.log"
}
# 1. a new install (no database, empty replica) starts, and a stop syncs
# once more after zds exits. Litestream attaches to the database on its
# first one-second tick, so the writes go in after that.
start_zds
sleep 2
mint 25
stop_zds
want=$(codes "$ZDS_DB")
scratch=$(drill)
got=$(codes "$scratch/zds.sqlite3")
[ "$got" = "$want" ] || { echo "after a clean stop the replica had $got invite codes, want $want" >&2; exit 1; }
echo "ok clean stop: replica has all $want invite codes"
# 2. a running server: writes reach the bucket within a few sync intervals.
start_zds
mint 25
sleep 3
want=$(codes "$ZDS_DB")
export ZDS_DRILL_LIVE_URL="$base"
scratch=$(drill)
unset ZDS_DRILL_LIVE_URL
grep -q '^repos at the live rev: 0 of 0$' "$work/drill.log"
got=$(codes "$scratch/zds.sqlite3")
[ "$got" = "$want" ] || { echo "while running the replica had $got invite codes, want $want" >&2; exit 1; }
echo "ok running: replica caught up to $want invite codes"
stop_zds
# 3. an empty volume never replaces the backup: zds refuses to start on it,
# and comes back with every write when asked to restore.
rm -f "$ZDS_DB" "$ZDS_DB-wal" "$ZDS_DB-shm"
if PATH="$work/bin:$PATH" "$root/tools/container-start.sh" >>"$work/zds.log" 2>&1; then
echo "zds started on an empty volume over a non-empty replica" >&2
exit 1
fi
[ ! -e "$ZDS_DB" ] || { echo "the refused start left a database behind" >&2; exit 1; }
grep -q 'refusing to replicate an empty database' "$work/zds.log"
echo "ok empty volume: zds refuses to start over the backup"
ZDS_LITESTREAM_RESTORE_IF_MISSING=true start_zds
got=$(codes "$ZDS_DB")
[ "$got" = "$want" ] || { echo "restore-if-missing came back with $got invite codes, want $want" >&2; exit 1; }
echo "ok restore-if-missing: zds came back with $want invite codes"
stop_zds
# 4. the drill boots zds against the restored copy.
ZDS_DRILL_BIN="$root/zig-out/bin/zds" "$root/tools/restore-drill.sh" >"$work/drill-boot.log" 2>&1 || {
cat "$work/drill-boot.log" >&2
exit 1
}
grep -q '^zds booted on the restored copy' "$work/drill-boot.log"
echo "ok restore drill boots zds on the restored copy"
echo "backup smoke passed"