Files
mtusi2026lab6/scripts/test-durable.sh
T

40 lines
2.7 KiB
Bash
Executable File

#!/bin/sh
# Demonstrate accepted upload survival with worker stopped and all dependencies restarted.
set -eu
: "${LOCAL_TEST_WORK:?Run through test-local.sh}" "${BASE_URL:?}"
umask 077
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
LOCAL_ACTION=pause-worker scripts/local-action.sh
username="durable_$(openssl rand -hex 6)"
password=$(openssl rand -hex 16)
jq -n --arg username "$username" --arg password "$password" '{username:$username,display_name:"Durability test",password:$password}' > "$work/user.json"
curl -fsS "$BASE_URL/api/v1/users" -H 'Content-Type: application/json' --data-binary @"$work/user.json" > "$work/user-response.json"
# Temporary curl config avoids placing credentials or bearer tokens in process arguments.
printf 'user = "%s:%s"\n' "$username" "$password" > "$work/basic.conf"
curl -fsS --config "$work/basic.conf" -X POST "$BASE_URL/api/v1/auth/sessions" > "$work/session.json"
printf 'header = "Authorization: Bearer %s"\n' "$(jq -r .token "$work/session.json")" > "$work/auth.conf"
jq -n --arg id "$(jq -r .id "$work/user-response.json")" '{title:"Durability",member_ids:[$id]}' > "$work/chat.json"
curl -fsS --config "$work/auth.conf" "$BASE_URL/api/v1/chats" -H 'Content-Type: application/json' --data-binary @"$work/chat.json" > "$work/chat-response.json"
chat=$(jq -r .id "$work/chat-response.json")
status=$(curl -sS --config "$work/auth.conf" -o "$work/attachment.json" -w '%{http_code}' "$BASE_URL/api/v1/chats/$chat/attachments?filename=sample.png" -H 'Content-Type: image/png' --data-binary @tests/fixtures/sample.png)
test "$status" = 202
jq -e '.status=="queued"' "$work/attachment.json" >/dev/null
id=$(jq -r .id "$work/attachment.json")
jq -n --arg id "$id" '{text:"",attachment_ids:[$id]}' > "$work/message.json"
curl -fsS --config "$work/auth.conf" "$BASE_URL/api/v1/chats/$chat/messages" -H 'Content-Type: application/json' -H 'Idempotency-Key: durable-upload' --data-binary @"$work/message.json" > /dev/null
status=$(curl -sS --config "$work/auth.conf" -o /dev/null -w '%{http_code}' "$BASE_URL/api/v1/attachments/$id/content")
test "$status" = 409
printf 'worker_stopped=true upload_status=202 snapshot=queued message_created=true content_status=409\n'
LOCAL_ACTION=restart scripts/local-action.sh
ready=false
for attempt in $(seq 1 60); do
curl -fsS --config "$work/auth.conf" "$BASE_URL/api/v1/attachments/$id" > "$work/status.json"
if jq -e '.status=="ready" and (.variants|length)==3' "$work/status.json" >/dev/null; then ready=true; break; fi
sleep 1
done
$ready
curl -fsS --config "$work/auth.conf" "$BASE_URL/api/v1/attachments/$id/content" > "$work/original.png"
cmp tests/fixtures/sample.png "$work/original.png"
printf 'PASS: accepted intent survived API, worker, PostgreSQL, S3 and AOF broker restart; ready=1 variants=3 original_unchanged=true\n'