Files
mtusi2026lab6/scripts/collect-evidence.ps1

141 lines
9.9 KiB
PowerShell

# Windows PowerShell 5.1+ equivalent of scripts/collect-evidence.sh (lab6).
# Starts the Compose stand (LB + API replicas + workers + one-shot bootstrap + PostgreSQL + Redis + SeaweedFS S3 +
# NATS JetStream + OTel Collector + Prometheus + Loki + Tempo + Grafana), runs every check and writes outputs to
# evidence\ (no tokens/passwords/keys). Stops/restarts containers (worker, DB, Collector, Tempo, Loki, API
# replicas); volumes are never removed. Takes ~90 minutes (-SkipRegression: ~60, -SkipResearch: -35 more).
# powershell -NoProfile -ExecutionPolicy Bypass -File scripts\collect-evidence.ps1 [-SkipRegression] [-SkipResearch]
param([switch]$SkipRegression, [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 'track1'), (Join-Path $ev 'track2'), (Join-Path $ev 'incidents') | 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 's3-bootstrap' 'docker compose logs s3-init' { Dc logs --no-log-prefix s3-init }
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 'published-ports' 'docker compose ps: published ports' { Dc ps --format '{{.Service}} {{.Ports}}' | Sort-Object -Unique }
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 }
# ---------------------------------------------------------------- lab6: observability
Invoke-Step 'collector-config-validate' 'otelcol-contrib validate --config=collector.yaml' {
Dc run --rm --no-deps otel-collector validate --config=/etc/otelcol-contrib/collector.yaml
}
Invoke-Step 'test-observability' 'python student-tests/test_observability.py' { Invoke-Python student-tests/test_observability.py }
Invoke-Step 'observability' 'python research/obs_snapshot.py' { Invoke-Python research/obs_snapshot.py }
Set-Content -Path (Join-Path $ev 'restart-hook.txt') -Encoding UTF8 -Value @()
Invoke-Step 'test-telemetry-persistence' 'python student-tests/test_telemetry_persistence.py # restarts everything' {
Invoke-Python student-tests/test_telemetry_persistence.py
}
foreach ($sc in 'worker-stop', 'db-slow', 'handler-slow', 'db-down', 'telemetry-down', 'collector-down') {
Invoke-Step ('incident-' + $sc) ('python research/incidents.py ' + $sc) {
Invoke-Python research/incidents.py $sc --out evidence/incidents
}
}
# ---------------------------------------------------------------- public failover / persistence checks
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
}
# ---------------------------------------------------------------- lab2-lab5 regression with telemetry enabled
if (-not $SkipRegression) {
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
}
Invoke-Step 'test-jobs-failures' 'python student-tests/test_jobs_failures.py' { Invoke-Python student-tests/test_jobs_failures.py }
Invoke-Step 'test-storage-failures' 'python student-tests/test_storage_failures.py' { Invoke-Python student-tests/test_storage_failures.py }
Invoke-Step 'test-failures' 'python student-tests/test_failures.py' { Invoke-Python student-tests/test_failures.py }
Invoke-Step 'test-track2-lab5' 'python student-tests/test_pipeline_idempotency.py' { Invoke-Python student-tests/test_pipeline_idempotency.py }
Invoke-Step 'track2-chaos-seed1' 'python research/track2_chaos.py --seed 1' {
Invoke-Python research/track2_chaos.py --seed 1 --uploads 60 --output evidence/track2/chaos-seed1.json
}
}
# ---------------------------------------------------------------- track 1
if (-not $SkipResearch) {
Invoke-Step 'track1-telemetry-cost' 'python research/telemetry_cost.py --reps 3 --rate 60 --window 60' {
Invoke-Python research/telemetry_cost.py --reps 3 --rate 60 --window 60 --out evidence/track1
}
}
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 '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
}
Invoke-Step 'test-observability-final' 'python student-tests/test_observability.py' { Invoke-Python student-tests/test_observability.py }
Invoke-Step 'observability-final' 'python research/obs_snapshot.py' { Invoke-Python research/obs_snapshot.py }
Write-Host 'Done: evidence collected in evidence\' -ForegroundColor Green