$ErrorActionPreference = 'Stop' Push-Location (Split-Path -Parent $PSScriptRoot) try { $project = 'mtusi-lab6-grade3' $api = @('api1', 'api2') $tested = @{} for ($attempt = 1; $attempt -le 8 -and $tested.Count -lt 2; $attempt++) { Write-Output "Public failover attempt $attempt" docker compose -p $project start --wait --wait-timeout 60 api1 api2 if ($LASTEXITCODE) { throw 'Could not restore both replicas.' } # An odd number of GETs changes the initial round-robin position # on even attempts. The unmodified public test still chooses the target. if ($attempt % 2 -eq 0) { docker compose -p $project exec -T lb wget -q -O /dev/null http://127.0.0.1:8080/health/live if ($LASTEXITCODE) { throw 'HAProxy is not ready.' } } try { docker compose -p $project run --rm --no-deps tools make test-failover BASE_URL=http://lb:8080 ACTION_SCRIPT=scripts/stop-one.sh if ($LASTEXITCODE) { throw 'Public failover test failed.' } $stopped = @(docker compose -p $project ps --status exited --services api1 api2) if ($LASTEXITCODE -or $stopped.Count -ne 1 -or $stopped[0] -notin $api) { throw 'Expected exactly one stopped API replica.' } $running = @(docker compose -p $project ps --status running --services api1 api2) if ($LASTEXITCODE -or $running.Count -ne 1 -or $running[0] -notin $api) { throw 'Expected exactly one surviving API replica.' } $tested[$stopped[0]] = $true Write-Output "Verified stopped=$($stopped[0]) survivor=$($running[0])" } finally { docker compose -p $project start --wait --wait-timeout 60 api1 api2 if ($LASTEXITCODE) { throw 'Could not restore both replicas after failover.' } } } if ($tested.Count -ne 2) { throw 'The public test did not select both API replicas.' } Write-Output 'PASS: public failover for api1 and api2, both replicas recovered.' } finally { Pop-Location }