Files
mtusi2026lab5/scripts/worker-down.bat
T
2026-11-10 13:55:56 +03:00

101 lines
4.3 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%\lab5-workerdown
if not exist "%TMP%" mkdir "%TMP%"
call :guid
set RND=%GUIDVAL%
set U=user%RND%
call :register "%U%" "WorkerDown" u
if errorlevel 1 exit /b 1
set UID=!u_ID!
call :login "%U%" u
if errorlevel 1 exit /b 1
set TOKEN=!u_TOKEN!
curl.exe -s -X POST "%BASE%/api/v1/chats" ^
-H "Authorization: Bearer %TOKEN%" -H "Content-Type: application/json" ^
-d "{\"title\":\"Worker down\",\"member_ids\":[\"%UID%\"]}" ^
-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 === Stop worker ===
docker compose --compatibility stop worker
timeout /t 3 /nobreak >nul
echo === Upload while worker down: expect 202 queued ===
curl.exe -s -X POST "%BASE%/api/v1/chats/%CHAT_ID%/attachments?filename=sample.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 202 "upload with worker down"
if errorlevel 1 ( docker compose --compatibility start worker & 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 === Status must stay queued for ~5s (API does not process synchronously) ===
timeout /t 5 /nobreak >nul
curl.exe -s -H "Authorization: Bearer %TOKEN%" "%BASE%/api/v1/attachments/%ATT_ID%" -o "%TMP%\meta.json"
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\meta.json' -Raw | ConvertFrom-Json).status"') do set ST=%%a
if not "%ST%"=="queued" (
echo FAIL: expected queued while worker down, got %ST%
docker compose --compatibility start worker
exit /b 1
)
echo PASS: status stayed queued with worker down
echo === Download before ready must be 409 ===
curl.exe -s -o nul -H "Authorization: Bearer %TOKEN%" -w "%%{http_code}" "%BASE%/api/v1/attachments/%ATT_ID%/content" > "%TMP%\dl.code"
call :check_code dl.code 409 "download before ready"
if errorlevel 1 ( docker compose --compatibility start worker & exit /b 1 )
echo === Start worker and wait ready ===
docker compose --compatibility start worker
powershell -NoProfile -Command "for($i=0;$i -lt 60;$i++){ $m=Invoke-RestMethod -Uri '%BASE%/api/v1/attachments/%ATT_ID%' -Headers @{Authorization='Bearer %TOKEN%'}; if($m.status -eq 'ready'){ exit 0 }; if($m.status -eq 'failed'){ exit 2 }; Start-Sleep -Seconds 1 }; exit 1"
if errorlevel 1 (
echo FAIL: attachment did not become ready after worker restart
exit /b 1
)
echo PASS: job survived worker restart and reached ready
echo === Download now 200 ===
curl.exe -s -o "%TMP%\dl.png" -H "Authorization: Bearer %TOKEN%" -w "%%{http_code}" "%BASE%/api/v1/attachments/%ATT_ID%/content" > "%TMP%\dl2.code"
call :check_code dl2.code 200 "download after ready"
if errorlevel 1 exit /b 1
echo.
echo WORKER-DOWN CHECKS PASSED
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