forked from mtusi2026/mtusi2026lab3
96 lines
6.2 KiB
PowerShell
96 lines
6.2 KiB
PowerShell
# Windows PowerShell 5.1+ equivalent of scripts/collect-evidence.sh (lab3).
|
|
# Starts the Compose stand (LB + API replicas + migrate + PostgreSQL + Redis), runs every check and writes
|
|
# outputs to evidence\ (no tokens/passwords). Stops/restarts containers during failover/restart tests.
|
|
# powershell -NoProfile -ExecutionPolicy Bypass -File scripts\collect-evidence.ps1 [-WithTrack1] [-SkipFailures]
|
|
param([switch]$WithTrack1, [switch]$SkipFailures)
|
|
$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 | 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' }
|
|
|
|
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 '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-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 ((Join-Path $root 'student-tests') + ':/app/student-tests:ro') --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-failures' 'python student-tests/test_failures.py # stops/starts redis and postgres' { Invoke-Python student-tests/test_failures.py }
|
|
}
|
|
Invoke-Step 'migrations-rerun' 'docker compose run --rm --no-deps migrate' { Dc run --rm --no-deps migrate }
|
|
Invoke-Step 'lb-failover-series' 'python research/lb_experiments.py failover-series --mode both' { Invoke-Python research/lb_experiments.py failover-series --mode both }
|
|
Write-Host 'NOTE: rolling update uses scripts/rolling-update.sh (POSIX sh). On Windows run it from Git Bash/WSL.' -ForegroundColor Yellow
|
|
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
|
|
}
|
|
if ($WithTrack1) {
|
|
$env:COMPOSE_FILE = 'compose.yaml;research/compose.track1.yaml'
|
|
Invoke-Step 'track1' 'python research/track1.py run all' { Dc --profile bench build loadgen; Invoke-Python research/track1.py run all }
|
|
Remove-Item Env:COMPOSE_FILE
|
|
Invoke-Step 'track1-report' 'python research/track1.py report' { Invoke-Python research/track1.py report }
|
|
}
|
|
Write-Host 'Done: evidence collected in evidence\' -ForegroundColor Green
|