Files
mtusi2026lab2/scripts/collect-evidence.ps1

79 lines
4.7 KiB
PowerShell

# Windows PowerShell 5.1+ equivalent of `make up && ./scripts/collect-evidence.sh`.
# Starts the Compose stand, runs every check and writes outputs to evidence\ (no tokens/passwords).
# Run from the repo root:
# powershell -NoProfile -ExecutionPolicy Bypass -File scripts\collect-evidence.ps1
# Optional: -SkipResearch (skip track 5.2), -SkipFailures (do not stop redis/postgres).
param([switch]$SkipResearch, [switch]$SkipFailures)
$ErrorActionPreference = 'Continue'
$root = Split-Path -Parent $PSScriptRoot
Set-Location $root
$env:PYTHONUTF8 = '1'
$env:PYTHONIOENCODING = 'utf-8'
$ev = Join-Path $root 'evidence'
New-Item -ItemType Directory -Force -Path $ev | Out-Null
if (Get-Command py -ErrorAction SilentlyContinue) { $script:PyExe = 'py'; $script:PyPre = @('-3') }
elseif (Get-Command python -ErrorAction SilentlyContinue) { $script:PyExe = 'python'; $script:PyPre = @() }
else { throw 'Python 3.9+ not found (install from python.org, enable "Add to PATH")' }
function Py { & $script:PyExe @script:PyPre @args }
function Dc { & docker compose --env-file .env @args }
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
$content = @((Get-Stamp), ('$ ' + $title)) + $lines + @('exit=' + $code)
Set-Content -Path (Join-Path $ev ($name + '.txt')) -Encoding UTF8 -Value $content
$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; docker compose version; images; ps' {
& docker version --format 'docker {{.Server.Version}}'; Dc version; Dc --profile ttl images; Dc --profile ttl ps
}
Invoke-Step 'make-check' 'python scripts/check_contract.py' { Py scripts/check_contract.py }
Invoke-Step 'smoke' 'python tests/smoke.py --base-url http://localhost:8080' { Py tests/smoke.py --base-url http://localhost:8080 }
Invoke-Step 'test-api' 'python student-tests/test_api.py' { Py student-tests/test_api.py }
Invoke-Step 'test-expiry' 'python student-tests/test_expiry.py # api-ttl, SESSION_TTL_SECONDS=2' { Py student-tests/test_expiry.py }
Invoke-Step 'test-cache-race' 'docker compose run --rm --no-deps 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 'restart-hook.txt') -Encoding UTF8 -Value @()
# Windows: .cmd wrapper around restart.ps1; pwsh on Linux/macOS: the POSIX hook.
$hook = if ($IsLinux -or $IsMacOS) { 'scripts/restart.sh' } else { 'scripts\restart.cmd' }
Invoke-Step 'persistence' ('python tests/resilience.py --mode persistence --action-script ' + $hook) {
Py tests/resilience.py --mode persistence --action-script $hook --base-url http://localhost:8080
}
if (-not $SkipFailures) {
Invoke-Step 'test-failures' 'python student-tests/test_failures.py # stops/starts redis and postgres' { Py student-tests/test_failures.py }
}
Invoke-Step 'migrations-rerun' 'docker compose exec -T api python -m messenger migrate' { Dc exec -T api python -m messenger migrate }
Invoke-Step 'load' 'python tests/load.py --duration 30 --concurrency 4 --output evidence/load.json' {
Py tests/load.py --base-url http://localhost:8080 --duration 30 --concurrency 4 --output evidence/load.json
}
$race = @(Dc logs --no-color api 2>$null | Select-String -SimpleMatch 'registration race resolved').Count
Set-Content -Path (Join-Path $ev 'race-unique-violations.txt') -Encoding ASCII -Value $race
if (-not $SkipResearch) {
Dc --profile research up -d --wait postgres-restore
Invoke-Step 'track2' 'python research/track2.py' { Py research/track2.py }
}
Write-Host 'Done: evidence collected in evidence\' -ForegroundColor Green