Files
mtusi2026lab5/scripts/collect-evidence.ps1
T

125 lines
8.6 KiB
PowerShell

# Windows PowerShell 5.1+ equivalent of scripts/collect-evidence.sh (lab5).
# Starts the Compose stand (LB + API replicas + workers + migrate/s3-init/broker-init + PostgreSQL + Redis +
# SeaweedFS S3 + NATS JetStream), runs every check and writes outputs to evidence\ (no tokens/passwords/keys).
# Stops/restarts containers during failover/restart/worker/broker/S3 tests; volumes are never removed.
# Takes ~30-35 minutes (-SkipFailures / -SkipResearch make it shorter).
# powershell -NoProfile -ExecutionPolicy Bypass -File scripts\collect-evidence.ps1 [-SkipFailures] [-SkipResearch]
param([switch]$SkipFailures, [switch]$SkipResearch)
$ErrorActionPreference = 'Continue'
$root = Split-Path -Parent $PSScriptRoot
Set-Location $root
$env:PYTHONUTF8 = '1'
$env:PYTHONIOENCODING = 'utf-8'
# IPv4 explicitly: on Windows 'localhost' resolves to ::1 first; the LB listens on 127.0.0.1 only.
$env:NO_PROXY = '127.0.0.1,localhost'
$B = 'http://127.0.0.1:8080'
$env:BASE_URL = $B
$env:TTL_BASE_URL = 'http://127.0.0.1:8081'
$ev = Join-Path $root 'evidence'
New-Item -ItemType Directory -Force -Path $ev, (Join-Path $ev 'track2') | Out-Null
# Full path of the Python APPLICATION: a bare 'py' would resolve to a PowerShell function first.
$pyApp = Get-Command py -CommandType Application -ErrorAction SilentlyContinue | Select-Object -First 1
$pythonApp = Get-Command python -CommandType Application -ErrorAction SilentlyContinue | Select-Object -First 1
if ($pyApp) { $script:PyExe = $pyApp.Source; $script:PyPre = @('-3') }
elseif ($pythonApp) { $script:PyExe = $pythonApp.Source; $script:PyPre = @() }
else { throw 'Python 3.9+ not found (install from python.org, enable "Add to PATH")' }
function Invoke-Python { & $script:PyExe @script:PyPre @args }
function Dc { & docker compose --env-file .env @args }
$hookStop = if ($IsLinux -or $IsMacOS) { 'scripts/stop-one.sh' } else { 'scripts\stop-one.cmd' }
$hookRestart = if ($IsLinux -or $IsMacOS) { 'scripts/restart.sh' } else { 'scripts\restart.cmd' }
$startReplicas = Join-Path $PSScriptRoot 'start-replicas.ps1'
function Get-Stamp {
$commit = 'n/a'
if (Get-Command git -ErrorAction SilentlyContinue) {
$c = (& git rev-parse --short HEAD 2>$null)
if ($LASTEXITCODE -eq 0 -and $c) { $commit = $c }
}
'# ' + (Get-Date).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ssZ') + ' commit=' + $commit + ' host=' + [Environment]::OSVersion.VersionString + ' cpus=' + [Environment]::ProcessorCount
}
function Invoke-Step([string]$name, [string]$title, [scriptblock]$block) {
Write-Host ('>>> ' + $name) -ForegroundColor Cyan
$global:LASTEXITCODE = 0
$lines = @(& $block 2>&1 | ForEach-Object { "$_" })
$code = $global:LASTEXITCODE
Set-Content -Path (Join-Path $ev ($name + '.txt')) -Encoding UTF8 -Value (@((Get-Stamp), ('$ ' + $title)) + $lines + @('exit=' + $code))
$lines | Select-Object -Last 2 | ForEach-Object { Write-Host (' ' + $_) }
if ($code -ne 0) { Write-Host (' exit=' + $code + ' (see evidence\' + $name + '.txt)') -ForegroundColor Yellow }
}
& docker version --format '{{.Server.Version}}' *> $null
if ($LASTEXITCODE -ne 0) { throw 'Docker is not running: start Docker Desktop first' }
& (Join-Path $PSScriptRoot 'init-secrets.ps1')
Set-Location $root
Write-Host '>>> docker compose up (build, wait for healthchecks)' -ForegroundColor Cyan
Dc --profile ttl up -d --build --wait
if ($LASTEXITCODE -ne 0) { Dc logs --tail=80; throw 'docker compose up failed' }
$mount = (Join-Path $root 'student-tests') + ':/app/student-tests:ro'
Invoke-Step 'stack' 'docker version; compose images; ps' { & docker version --format 'docker {{.Server.Version}}'; Dc version; Dc --profile ttl images; Dc --profile ttl ps -a }
Invoke-Step 'migrations' 'schema_migrations; docker compose logs migrate' {
Dc exec -T postgres psql -U messenger -d messenger -c 'SELECT version, left(checksum,12) AS checksum, applied_at FROM schema_migrations ORDER BY version'
Dc logs --no-log-prefix migrate
}
Invoke-Step 'broker-bootstrap' 'docker compose logs broker-init' { Dc logs --no-log-prefix broker-init }
Invoke-Step 'broker-bootstrap-rerun' 'docker compose run --rm --no-deps broker-init # idempotent' { Dc run --rm --no-deps broker-init }
Invoke-Step 's3-bootstrap' 'docker compose logs s3-init' { Dc logs --no-log-prefix s3-init }
Invoke-Step 's3-check' 'docker compose exec api python -m messenger s3-check' { Dc exec -T api python -m messenger s3-check }
Invoke-Step 'no-local-files' 'docker inspect api/worker: ReadonlyRootfs, tmpfs, mounts' {
foreach ($cid in @(Dc ps -q api worker)) {
& docker inspect -f '{{.Name}} ReadonlyRootfs={{.HostConfig.ReadonlyRootfs}} Tmpfs={{json .HostConfig.Tmpfs}} Mounts={{range .Mounts}}{{.Type}}:{{.Destination}} {{end}}' $cid
}
}
Invoke-Step 'instances' 'X-Instance-Id of 6 requests via LB' {
1..6 | ForEach-Object { (Invoke-WebRequest -UseBasicParsing "$B/health/live").Headers['X-Instance-Id'] }
}
Invoke-Step 'make-check' 'python scripts/check_contract.py' { Invoke-Python scripts/check_contract.py }
Invoke-Step 'smoke' "python tests/smoke.py --base-url $B" { Invoke-Python tests/smoke.py --base-url $B }
Invoke-Step 'test-pipeline' 'python student-tests/test_pipeline.py' { Invoke-Python student-tests/test_pipeline.py }
Invoke-Step 'test-imgproc' 'docker compose run worker: python student-tests/test_imgproc.py' {
Dc run --rm --no-deps -T -v $mount --entrypoint python worker student-tests/test_imgproc.py
}
Invoke-Step 'defense-demo' 'python student-tests/defense_demo.py' { Invoke-Python student-tests/defense_demo.py }
Invoke-Step 'test-attachments' 'python student-tests/test_attachments.py' { Invoke-Python student-tests/test_attachments.py }
Invoke-Step 'test-api' 'python student-tests/test_api.py' { Invoke-Python student-tests/test_api.py }
Invoke-Step 'test-idempotency' 'python student-tests/test_idempotency.py' { Invoke-Python student-tests/test_idempotency.py }
Invoke-Step 'test-expiry' 'python student-tests/test_expiry.py # api-ttl via LB :8081' { Invoke-Python student-tests/test_expiry.py }
Invoke-Step 'test-cache-race' 'docker compose run api: python student-tests/test_cache_race.py' {
Dc run --rm --no-deps -T -v $mount --entrypoint python api student-tests/test_cache_race.py
}
Set-Content -Path (Join-Path $ev 'failover-hook.txt') -Encoding UTF8 -Value @()
foreach ($i in 1, 2) {
Invoke-Step ('failover-' + $i) ('python tests/resilience.py --mode failover --action-script ' + $hookStop) {
Invoke-Python tests/resilience.py --mode failover --action-script $hookStop --base-url $B
}
Invoke-Step ('restore-' + $i) 'scripts\start-replicas.ps1' { & $startReplicas }
}
Set-Content -Path (Join-Path $ev 'restart-hook.txt') -Encoding UTF8 -Value @()
Invoke-Step 'persistence' ('python tests/resilience.py --mode persistence --action-script ' + $hookRestart) {
Invoke-Python tests/resilience.py --mode persistence --action-script $hookRestart --base-url $B
}
if (-not $SkipFailures) {
Invoke-Step 'test-jobs-failures' 'python student-tests/test_jobs_failures.py # worker/broker/API stops, crash windows' { Invoke-Python student-tests/test_jobs_failures.py }
Invoke-Step 'test-storage-failures' 'python student-tests/test_storage_failures.py # stops/starts s3, fault profile' { Invoke-Python student-tests/test_storage_failures.py }
Invoke-Step 'test-failures' 'python student-tests/test_failures.py # stops/starts redis and postgres' { Invoke-Python student-tests/test_failures.py }
}
if (-not $SkipResearch) {
Invoke-Step 'test-track2' 'python student-tests/test_pipeline_idempotency.py # track 2' { Invoke-Python student-tests/test_pipeline_idempotency.py }
foreach ($s in 1, 2, 3) {
Invoke-Step ('track2-chaos-seed' + $s) ('python research/track2_chaos.py --seed ' + $s) {
Invoke-Python research/track2_chaos.py --seed $s --uploads 60 --output ('evidence/track2/chaos-seed' + $s + '.json')
}
}
}
Invoke-Step 'jobs-failed' 'docker compose exec api python -m messenger jobs failed --limit 20' { Dc exec -T api python -m messenger jobs failed --limit 20 }
Invoke-Step 'jobs-invariants' 'docker compose exec api python -m messenger jobs invariants --settled --verify-bytes' {
Dc exec -T api python -m messenger jobs invariants --settled --verify-bytes
}
Invoke-Step 'broker-state' 'docker compose exec api python -m messenger stats' { Dc exec -T api python -m messenger stats }
Invoke-Step 'migrations-rerun' 'docker compose run --rm --no-deps migrate' { Dc run --rm --no-deps migrate }
Invoke-Step 'load' 'python tests/load.py --duration 30 --concurrency 4' {
Invoke-Python tests/load.py --base-url $B --duration 30 --concurrency 4 --output evidence/load.json
}
Write-Host 'Done: evidence collected in evidence\' -ForegroundColor Green