lab 2 solution
Skeleton contracts / contracts (pull_request) Canceled after 0s

This commit is contained in:
2026-10-23 17:24:27 +03:00
parent 65003b6119
commit 5c193f24f7
70 changed files with 7881 additions and 17 deletions
+37
View File
@@ -0,0 +1,37 @@
#!/bin/sh
# Create local Compose secrets under secrets/ (gitignored, never printed).
# Existing files are never overwritten, so rerunning the script is safe.
# Usage: sh scripts/bootstrap-secrets.sh
set -eu
root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
dir="$root/secrets"
# Files are created with mode 600: group and other have no access.
umask 077
mkdir -p "$dir"
random_hex() {
head -c 32 /dev/urandom | od -An -tx1 | tr -d ' \n'
}
# write <name> <value>: keeps the current file if it already has content.
write() {
name=$1
value=$2
if [ -s "$dir/$name" ]; then
printf 'keep secrets/%s\n' "$name"
return 0
fi
printf '%s\n' "$value" > "$dir/$name"
printf 'create secrets/%s\n' "$name"
}
write postgres_password "$(random_hex)"
write cursor_secret "$(random_hex)"
write cache_url 'redis://valkey:6379/0'
# The password is hex, so the URL needs no percent-encoding.
write database_url "postgresql+asyncpg://messenger:$(cat "$dir/postgres_password")@postgres:5432/messenger"
printf 'done: %s\n' "$dir"
printf 'next: docker compose up -d --build\n'
+75
View File
@@ -0,0 +1,75 @@
@echo off
rem Windows-native twin of scripts/restart.sh, same docker compose subcommands.
rem
rem Why it exists: tests/resilience.py launches the hook with Popen([script]) and
rem no shell. On native Windows that only works for .exe/.cmd/.bat - a .sh file
rem fails with WinError 193 (%1 is not a valid Win32 application). Linux and WSL
rem hosts use scripts/restart.sh instead; see REPORT.md.
rem
rem The hook restarts the API and every state store it depends on, keeping the
rem schema and the data: no volumes removed, no flush, no re-seed. Sessions live
rem in Valkey with AOF, so the same token keeps working after the restart.
rem Usage: scripts\restart.cmd
setlocal enableextensions enabledelayedexpansion
cd /d "%~dp0.."
rem Docker CLI finds its plugins in %ProgramW6432%\Docker\cli-plugins. The
rem environment `make` builds under MSYS drops that variable, and then every
rem `docker compose` call is answered with "unknown command: docker compose".
rem Restoring it keeps the hook working whichever shell started the test.
if not defined PROGRAMW6432 for %%d in ("%SystemDrive%\Program Files") do (
if exist "%%~d\Docker\cli-plugins" set "PROGRAMW6432=%%~d"
)
where docker >nul 2>&1
if errorlevel 1 (
echo docker is required 1>&2
exit /b 2
)
echo === services before restart ===
docker compose ps
if errorlevel 1 exit /b 1
echo === restart api postgres valkey (grace 10s) ===
docker compose restart --timeout 10 api postgres valkey
if errorlevel 1 (
rem One retry: a transient daemon error here leaves the stack in an unknown
rem state, and repeating the operator action is the natural next step.
echo restart attempt failed, retrying once 1>&2
ping -n 4 127.0.0.1 >nul
docker compose restart --timeout 10 api postgres valkey
if errorlevel 1 exit /b 1
)
echo === services after restart ===
docker compose ps
if errorlevel 1 exit /b 1
rem Every service must be reported running; order is not assumed, and the check
rem is retried because docker needs a moment to publish the new container state.
rem `timeout` is avoided on purpose: it fails without a console, and the caller
rem runs this hook with redirected output.
set "remaining=30"
:verify
set "missing="
for %%r in (api postgres valkey) do (
set "found="
for /f "delims=" %%s in ('docker compose ps --status running --services') do (
if /i "%%s"=="%%r" set "found=1"
)
if not defined found set "missing=!missing! %%r"
)
if not defined missing goto verified
if "!remaining!"=="0" goto still_down
set /a remaining-=1
ping -n 2 127.0.0.1 >nul
goto verify
:still_down
echo services not running after 30s:!missing! 1>&2
exit /b 1
:verified
echo restart complete
exit /b 0
+39
View File
@@ -0,0 +1,39 @@
#!/bin/sh
# Restart the API and every state store it depends on, keeping schema and data.
#
# Published as the action script for `make test-persistence`: it must not remove
# volumes, must not flush state and must not re-seed anything. Sessions live in
# Valkey with AOF, so the same token keeps working after the restart.
# Usage: sh scripts/restart.sh
set -eu
root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
cd "$root"
if ! docker compose version >/dev/null 2>&1; then
printf 'docker compose is required\n' >&2
exit 2
fi
printf '=== services before restart ===\n'
docker compose ps
printf '=== restart api postgres valkey (grace 10s) ===\n'
docker compose restart --timeout 10 api postgres valkey
printf '=== services after restart ===\n'
docker compose ps
running=$(docker compose ps --status running --services | sort | tr '\n' ' ')
printf 'running services: %s\n' "$running"
for service in api postgres valkey; do
case " $running " in
*" $service "*) ;;
*)
printf 'service %s is not running\n' "$service" >&2
exit 1
;;
esac
done
printf 'restart complete\n'