92 lines
4.5 KiB
PowerShell
92 lines
4.5 KiB
PowerShell
param(
|
|
[ValidateRange(1, 65535)]
|
|
[int]$Port = 8080
|
|
)
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
$previousHostPort = [Environment]::GetEnvironmentVariable('LAB2_HOST_PORT', 'Process')
|
|
|
|
function Assert-NativeSuccess([string]$Step) {
|
|
if ($LASTEXITCODE -ne 0) { throw "$Step failed (exit code $LASTEXITCODE)" }
|
|
}
|
|
|
|
function Write-Evidence([string]$Line) {
|
|
Add-Content -LiteralPath $evidence -Value $Line -Encoding UTF8
|
|
Write-Host $Line
|
|
}
|
|
|
|
function Invoke-PythonCheck([string[]]$Arguments) {
|
|
Write-Evidence ("python " + ($Arguments -join ' '))
|
|
& python @Arguments | ForEach-Object { Write-Evidence $_ }
|
|
Assert-NativeSuccess 'Python check'
|
|
}
|
|
|
|
function Get-ContainerState {
|
|
$api = docker compose --env-file .env ps --quiet api
|
|
Assert-NativeSuccess 'API container lookup'
|
|
$db = docker compose --env-file .env ps --quiet db
|
|
Assert-NativeSuccess 'PostgreSQL container lookup'
|
|
if (!$api -or !$db) { throw 'Expected running api and db containers' }
|
|
$apiStarted = docker inspect --format '{{.State.StartedAt}}' $api
|
|
Assert-NativeSuccess 'API start time lookup'
|
|
$dbStarted = docker inspect --format '{{.State.StartedAt}}' $db
|
|
Assert-NativeSuccess 'PostgreSQL start time lookup'
|
|
$volume = docker inspect --format '{{range .Mounts}}{{.Name}}{{end}}' $db
|
|
Assert-NativeSuccess 'PostgreSQL volume lookup'
|
|
if (!$volume) { throw 'Expected a named PostgreSQL volume' }
|
|
[pscustomobject]@{ ApiStarted = $apiStarted; DbStarted = $dbStarted; Volume = $volume }
|
|
}
|
|
|
|
Push-Location -LiteralPath (Join-Path $PSScriptRoot '..')
|
|
try {
|
|
if ($PSBoundParameters.ContainsKey('Port')) {
|
|
$env:LAB2_HOST_PORT = [string]$Port
|
|
}
|
|
if (!(Test-Path -LiteralPath '.env')) {
|
|
$password = python -B -c 'import secrets; print(secrets.token_hex(24))'
|
|
Assert-NativeSuccess 'Test password generation'
|
|
$settings = Get-Content -LiteralPath '.env.example'
|
|
$settings -replace '^POSTGRES_PASSWORD=.*', "POSTGRES_PASSWORD=$password" -replace '^LAB2_HOST_PORT=.*', "LAB2_HOST_PORT=$Port" |
|
|
Set-Content -LiteralPath '.env' -Encoding Ascii
|
|
}
|
|
docker compose --env-file .env config --quiet
|
|
Assert-NativeSuccess 'Compose configuration'
|
|
docker compose --env-file .env up -d --build --wait --wait-timeout 60
|
|
Assert-NativeSuccess 'Compose startup'
|
|
$address = docker compose --env-file .env port api 8080
|
|
Assert-NativeSuccess 'API port lookup'
|
|
if ($address -notmatch '^127\.0\.0\.1:\d+$') { throw "Unexpected API address: $address" }
|
|
$base = "http://$address"
|
|
$evidence = 'evidence/results-docker.txt'
|
|
Set-Content -LiteralPath $evidence -Value ("Docker verification " + (Get-Date -Format o)) -Encoding UTF8
|
|
Write-Evidence "PASS: Compose build and startup ($base)"
|
|
Invoke-PythonCheck @('-X', 'utf8', 'scripts/check_contract.py')
|
|
Invoke-PythonCheck @('tests/smoke.py', '--base-url', $base)
|
|
Invoke-PythonCheck @('tests/student_tests.py', '--base-url', $base)
|
|
Invoke-PythonCheck @('tests/contract_edge_tests.py', '--base-url', $base)
|
|
$before = Get-ContainerState
|
|
Write-Evidence "Before restart: api=$($before.ApiStarted) db=$($before.DbStarted) volume=$($before.Volume)"
|
|
Invoke-PythonCheck @('tests/resilience.py', '--mode', 'persistence', '--action-script', 'scripts/restart.cmd', '--base-url', $base)
|
|
$after = Get-ContainerState
|
|
Write-Evidence "After restart: api=$($after.ApiStarted) db=$($after.DbStarted) volume=$($after.Volume)"
|
|
if ($before.ApiStarted -eq $after.ApiStarted -or $before.DbStarted -eq $after.DbStarted -or $before.Volume -ne $after.Volume) {
|
|
throw 'Restart check expected new start times for both containers and the same PostgreSQL volume'
|
|
}
|
|
Write-Evidence 'PASS: both Docker containers restarted and the database volume was retained'
|
|
|
|
$dbUser = docker compose --env-file .env exec -T db printenv POSTGRES_USER
|
|
Assert-NativeSuccess 'PostgreSQL role lookup'
|
|
$dbName = docker compose --env-file .env exec -T db printenv POSTGRES_DB
|
|
Assert-NativeSuccess 'PostgreSQL database lookup'
|
|
$migration = @(docker compose --env-file .env exec -T db psql -X -At -v ON_ERROR_STOP=1 -U $dbUser -d $dbName -c 'SELECT version FROM schema_migrations ORDER BY version')
|
|
Assert-NativeSuccess 'Migration check'
|
|
if ($migration.Count -ne 1 -or $migration[0] -ne '1') {
|
|
throw 'Expected one applied migration with version 1'
|
|
}
|
|
Write-Evidence 'PASS: PostgreSQL has exactly one migration record with version 1 after restart'
|
|
}
|
|
finally {
|
|
[Environment]::SetEnvironmentVariable('LAB2_HOST_PORT', $previousHostPort, 'Process')
|
|
Pop-Location
|
|
}
|