Files
mtusi2026lab4/scripts/reconcile-demo.bat
2026-10-26 16:10:22 +03:00

88 lines
3.4 KiB
Batchfile

@echo off
setlocal EnableDelayedExpansion
cd /d "%~dp0.."
set BASE=http://localhost:8080
set PASS=Example-only-not-a-real-secret-123
set FIXTURE=tests\fixtures\sample.png
set TMP=%TEMP%\lab4-reconcile
if not exist "%TMP%" mkdir "%TMP%"
call :guid
set RND=%GUIDVAL%
set RUSER=rec%RND%
echo === Register + login ===
call :register "%RUSER%" "Reconcile" rec
if errorlevel 1 exit /b 1
set RID=!rec_ID!
call :login "%RUSER%" rec
if errorlevel 1 exit /b 1
set TOKEN=!rec_TOKEN!
echo === Create chat ===
curl.exe -s -X POST "%BASE%/api/v1/chats" ^
-H "Authorization: Bearer %TOKEN%" ^
-H "Content-Type: application/json" ^
-d "{\"title\":\"Reconcile demo\",\"member_ids\":[\"%RID%\"]}" ^
-o "%TMP%\chat.json" -w "%%{http_code}" > "%TMP%\chat.code"
call :check_code chat.code 201 "create chat"
if errorlevel 1 exit /b 1
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\chat.json' -Raw | ConvertFrom-Json).id"') do set CHAT_ID=%%a
echo === Upload attachment (creates S3 object + DB metadata) ===
curl.exe -s -X POST "%BASE%/api/v1/chats/%CHAT_ID%/attachments?filename=orphan.png" ^
-H "Authorization: Bearer %TOKEN%" ^
-H "Content-Type: image/png" ^
--data-binary "@%FIXTURE%" ^
-o "%TMP%\att.json" -w "%%{http_code}" > "%TMP%\att.code"
call :check_code att.code 201 "upload attachment"
if errorlevel 1 exit /b 1
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\att.json' -Raw | ConvertFrom-Json).id"') do set ATT_ID=%%a
echo attachment=%ATT_ID%
echo === Simulate crash: delete DB metadata, leave S3 object orphaned ===
docker compose exec -T postgres psql -U messenger -d messenger -c "DELETE FROM attachments WHERE id='%ATT_ID%';"
echo === Run reconcile with grace=0 (must delete the orphan) ===
docker compose --compatibility --profile tools run -e RECONCILE_GRACE_PERIOD_SECONDS=0 --rm reconcile
if errorlevel 1 (
echo FAIL: reconcile exited non-zero
exit /b 1
)
echo === Run reconcile again (idempotent: 0 orphans deleted) ===
docker compose --compatibility --profile tools run -e RECONCILE_GRACE_PERIOD_SECONDS=0 --rm reconcile
if errorlevel 1 exit /b 1
echo.
echo RECONCILE DEMO COMPLETE: inspect logs above for "reconcile deleted orphan" then "orphans_deleted 0".
exit /b 0
:guid
for /f "delims=" %%a in ('powershell -NoProfile -Command "[guid]::NewGuid().ToString('N').Substring(0,8)"') do set GUIDVAL=%%a
exit /b 0
:register
curl.exe -s -X POST "%BASE%/api/v1/users" ^
-H "Content-Type: application/json" ^
-d "{\"username\":\"%~1\",\"display_name\":\"%~2\",\"password\":\"%PASS%\"}" ^
-o "%TMP%\%~3.json" -w "%%{http_code}" > "%TMP%\%~3.code"
set /p RC=<"%TMP%\%~3.code"
if not "%RC%"=="201" ( echo FAIL: register %~1, HTTP %RC% & type "%TMP%\%~3.json" & exit /b 1 )
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~3.json' -Raw | ConvertFrom-Json).id"') do set "%~3_ID=%%a"
exit /b 0
:login
curl.exe -s -X POST "%BASE%/api/v1/auth/sessions" ^
-u "%~1:%PASS%" ^
-o "%TMP%\%~2.json" -w "%%{http_code}" > "%TMP%\%~2.code"
set /p RC=<"%TMP%\%~2.code"
if not "%RC%"=="201" ( echo FAIL: login %~1, HTTP %RC% & type "%TMP%\%~2.json" & exit /b 1 )
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~2.json' -Raw | ConvertFrom-Json).token"') do set "%~2_TOKEN=%%a"
exit /b 0
:check_code
set /p RC=<"%TMP%\%~1"
if not "%RC%"=="%~2" ( echo FAIL: %~3 expected %~2, got %RC% & exit /b 1 )
exit /b 0