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

162 lines
4.8 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-failover-attachment
if not exist "%TMP%" mkdir "%TMP%"
call :guid
set RND=%GUIDVAL%
set FUSER=fo%RND%
echo === Register failover user ===
call :register "%FUSER%" "Failover" fo
if errorlevel 1 exit /b 1
set FUSER_ID=!fo_ID!
call :login "%FUSER%" fo
if errorlevel 1 exit /b 1
set TOKEN=!fo_TOKEN!
echo === Create chat ===
curl.exe -s -X POST "%BASE%/api/v1/chats" ^
-H "Authorization: Bearer %TOKEN%" ^
-H "Content-Type: application/json" ^
-d "{\"title\":\"Failover attachment\",\"member_ids\":[\"%FUSER_ID%\"]}" ^
-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 ===
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-FileHash -Algorithm SHA256 -Path '%FIXTURE%').Hash.ToLower()"') do set ORIG_SHA=%%a
curl.exe -s -X POST "%BASE%/api/v1/chats/%CHAT_ID%/attachments?filename=failover.png" ^
-H "Authorization: Bearer %TOKEN%" ^
-H "Content-Type: image/png" ^
--data-binary "@%FIXTURE%" ^
-D "%TMP%\upload.headers" ^
-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
for /f "tokens=1,* delims=:" %%a in ('findstr /i "X-Instance-Id" "%TMP%\upload.headers"') do set UPLOAD_INSTANCE=%%b
echo Uploaded by:%UPLOAD_INSTANCE%
echo attachment=%ATT_ID%
echo === Stop one API replica ===
set STOP_ID=
for /f "delims=" %%i in ('docker compose ps -q api') do if not defined STOP_ID set STOP_ID=%%i
if "%STOP_ID%"=="" (
echo FAIL: no api containers found
exit /b 1
)
echo Stopping container %STOP_ID%
docker stop --time 10 %STOP_ID% >nul
timeout /t 5 /nobreak >nul
echo === Download attachment after replica stop ===
set TRY=0
:download_retry
set /a TRY+=1
if %TRY% GTR 10 (
echo FAIL: download did not recover
docker compose --compatibility up -d api >nul
exit /b 1
)
curl.exe -s -X GET "%BASE%/api/v1/attachments/%ATT_ID%/content" ^
-H "Authorization: Bearer %TOKEN%" ^
-D "%TMP%\download.headers" ^
-o "%TMP%\download.png" ^
-w "%%{http_code}" > "%TMP%\download.code"
set /p RC=<"%TMP%\download.code"
if "%RC%"=="200" goto download_ok
echo Download attempt %TRY% got HTTP %RC%, retrying...
timeout /t 1 /nobreak >nul
goto download_retry
:download_ok
for /f "tokens=1,* delims=:" %%a in ('findstr /i "X-Instance-Id" "%TMP%\download.headers"') do set DOWNLOAD_INSTANCE=%%b
echo Downloaded by:%DOWNLOAD_INSTANCE%
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-FileHash -Algorithm SHA256 -Path '%TMP%\download.png').Hash.ToLower()"') do set DL_SHA=%%a
if not "%DL_SHA%"=="%ORIG_SHA%" (
echo FAIL: downloaded sha256 mismatch after failover
docker compose --compatibility up -d api >nul
exit /b 1
)
echo PASS: attachment bytes available after stopping one replica
echo === Restore API replicas ===
docker compose --compatibility up -d api
timeout /t 15 /nobreak >nul
docker compose ps
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
rem %1 username, %2 display_name, %3 prefix
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
rem %1 username, %2 prefix
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"
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~2.json' -Raw | ConvertFrom-Json).user.id"') do set "%~2_UID=%%a"
exit /b 0
:check_code
rem %1 code file, %2 expected status, %3 label
set /p RC=<"%TMP%\%~1"
if not "%RC%"=="%~2" (
echo FAIL: %~3 expected %~2, got %RC%
exit /b 1
)
exit /b 0