forked from mtusi2026/mtusi2026lab5
93 lines
3.2 KiB
Bash
Executable File
93 lines
3.2 KiB
Bash
Executable File
#!/bin/sh
|
|
# A client that declares a large body and disconnects halfway must not leave a
|
|
# pending metadata row, an S3 object or a temp file behind. The handler creates
|
|
# the pending row only after the whole bounded body has been copied, so this is
|
|
# the expected clean failure.
|
|
set -eu
|
|
cd "$(dirname "$0")/.."
|
|
|
|
COMPOSE="docker compose --env-file .env"
|
|
network=$(docker inspect -f '{{range $k,$v := .NetworkSettings.Networks}}{{$k}}{{end}}' "$($COMPOSE ps -q haproxy)")
|
|
[ -n "$network" ] || {
|
|
echo "cannot determine the Compose network; is the stack running?" >&2
|
|
exit 2
|
|
}
|
|
|
|
POSTGRES_USER=$(sed -n 's/^POSTGRES_USER=//p' .env)
|
|
POSTGRES_DB=$(sed -n 's/^POSTGRES_DB=//p' .env)
|
|
POSTGRES_USER=${POSTGRES_USER:-messenger}
|
|
POSTGRES_DB=${POSTGRES_DB:-messenger}
|
|
|
|
count_rows() {
|
|
$COMPOSE exec -T postgres psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -tAc \
|
|
"SELECT count(*) FROM attachments" | tr -d '\r'
|
|
}
|
|
|
|
count_objects() {
|
|
docker run --rm --network "$network" --env-file .env -e HOME=/tmp \
|
|
--entrypoint /bin/sh rustfs/rc:v0.1.36 -c \
|
|
'rc alias set app "$S3_ENDPOINT" "$S3_ACCESS_KEY_ID" "$S3_SECRET_ACCESS_KEY" >/dev/null &&
|
|
rc bucket list -r --no-color "app/$S3_BUCKET/attachments/" | wc -l' | tr -d '\r '
|
|
}
|
|
|
|
rows_before=$(count_rows)
|
|
objects_before=$(count_objects)
|
|
|
|
# The helper prints the chat id and token for this one short-lived probe. The
|
|
# token is passed through the environment, not written to disk or evidence.
|
|
state=$(docker run --rm --network "$network" -v "$PWD:/work:ro" -w /work \
|
|
python:3.12-alpine python3 -c '
|
|
import sys
|
|
sys.path.insert(0, "tests")
|
|
from client import Client
|
|
alice, _, _, chat = Client("http://haproxy:8080").bootstrap()
|
|
print(chat["id"] + " " + alice["token"])
|
|
')
|
|
chat_id=$(printf '%s' "$state" | cut -d' ' -f1)
|
|
token=$(printf '%s' "$state" | cut -d' ' -f2)
|
|
|
|
docker run --rm --network "$network" -e CHAT_ID="$chat_id" -e TOKEN="$token" \
|
|
python:3.12-alpine python3 -c '
|
|
import os, socket, time
|
|
host, port = "haproxy", 8080
|
|
request = (
|
|
"POST /api/v1/chats/" + os.environ["CHAT_ID"] + "/attachments?filename=cut.png HTTP/1.1\r\n"
|
|
"Host: haproxy\r\n"
|
|
"Authorization: Bearer " + os.environ["TOKEN"] + "\r\n"
|
|
"Content-Type: image/png\r\n"
|
|
"Content-Length: 5000000\r\n"
|
|
"Connection: close\r\n\r\n"
|
|
).encode()
|
|
with socket.create_connection((host, port), timeout=5) as sock:
|
|
sock.sendall(request)
|
|
sock.sendall(b"\x89PNG\r\n\x1a\n" + b"\x00" * 1024)
|
|
time.sleep(0.2)
|
|
# close without sending the declared body
|
|
'
|
|
sleep 2
|
|
|
|
rows_after=$(count_rows)
|
|
objects_after=$(count_objects)
|
|
temp_files=0
|
|
for id in $($COMPOSE ps -q api); do
|
|
count=$(docker exec "$id" sh -c 'find /tmp/messenger-uploads -type f 2>/dev/null | wc -l' | tr -d '\r ')
|
|
temp_files=$((temp_files + count))
|
|
done
|
|
|
|
echo "attachments rows: $rows_before -> $rows_after"
|
|
echo "stored objects: $objects_before -> $objects_after"
|
|
echo "temp files left: $temp_files"
|
|
[ "$rows_before" = "$rows_after" ] || {
|
|
echo "interrupted upload left a metadata row" >&2
|
|
exit 1
|
|
}
|
|
[ "$objects_before" = "$objects_after" ] || {
|
|
echo "interrupted upload left an S3 object" >&2
|
|
exit 1
|
|
}
|
|
[ "$temp_files" = "0" ] || {
|
|
echo "interrupted upload left a temporary file" >&2
|
|
exit 1
|
|
}
|
|
echo "PASS: interrupted upload left no row, object or temp file"
|