Files
ianitor/scripts/smoke-windows.ps1
T

130 lines
7.3 KiB
PowerShell

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
#
# Windows Caddy mode, end to end through the installer (T4). Everything happens in -Work, on
# non-standard ports, with a throw-away Domi root; no service is registered, no certificate is
# trusted, nothing is left running:
#
# 1. deploy\windows\install.ps1 -NoService (the release ianitor-control.exe, a given caddy.exe)
# 2. Caddy and control started in the console, as the services would start them
# 3. the intermediate signed offline with the throw-away root (docs/pki.md), control restarted
# so it adds the admin host
# 4. a previews-only token opens a branch preview with scripts\preview.ps1 (Fabrica's client)
# 5. the preview answers through Caddy with a Domi certificate; the admin host reaches control
# 6. the preview is closed and stops answering
#
# scripts\smoke-windows.ps1 -Control <ianitor-control.exe> -Caddy <caddy.exe> -Work <empty dir>
param(
[Parameter(Mandatory = $true)][string]$Control,
[Parameter(Mandatory = $true)][string]$Caddy,
[Parameter(Mandatory = $true)][string]$Work
)
$ErrorActionPreference = 'Stop'
$repo = Split-Path -Parent $PSScriptRoot
$Http = 18080; $Https = 18443; $Api = 17878; $Admin = 12019; $AppPort = 23456
$procs = @()
function Step($m) { Write-Host "== $m" }
function Check($ok, $m) { if (-not $ok) { throw "FAILED: $m" } ; Write-Host " ok: $m" }
try {
if (Test-Path $Work) { throw "$Work exists; give an empty, new folder" }
New-Item -ItemType Directory $Work | Out-Null
$pkg = Join-Path $Work 'pkg'; New-Item -ItemType Directory $pkg | Out-Null
Copy-Item $Control (Join-Path $pkg 'ianitor-control.exe')
Copy-Item (Join-Path $repo 'deploy\windows\install.ps1') $pkg
$ctl = Join-Path $Work 'bin\ianitor-control.exe'
$root = Join-Path $Work 'root'
$offline = Join-Path $Work 'offline-root'
$pass = Join-Path $Work 'offline-pass.txt'
[IO.File]::WriteAllText($pass, 'smoke-test-offline-passphrase')
Step 'a throw-away Domi root (offline step)'
& (Join-Path $pkg 'ianitor-control.exe') ca init --out $offline --passphrase-file $pass
Check ($LASTEXITCODE -eq 0) 'ca init'
Step 'install.ps1 -NoService'
& (Join-Path $pkg 'install.ps1') -NoService -AdminHost 'ianitor.smoke.test' -Tls domi `
-DomiRoot (Join-Path $offline 'domi-root.crt') -PreviewDomain 'pv.smoke.test' `
-PreviewUpstreams "127.0.0.1:20000-29999" -Root $root -Bin (Join-Path $Work 'bin') `
-HttpPort $Http -HttpsPort $Https -ApiPort $Api -CaddyAdminPort $Admin -Caddy $Caddy
$config = Join-Path $root 'control.toml'
Check (Test-Path $config) 'control.toml written'
function Start-Control {
$env:IANITOR_LOG_FILE = Join-Path $root 'logs\control.log'
$p = Start-Process -PassThru -WindowStyle Hidden -FilePath $ctl -ArgumentList '--config', "`"$config`""
Remove-Item Env:\IANITOR_LOG_FILE
for ($i = 0; $i -lt 60; $i++) {
try { $c = New-Object Net.Sockets.TcpClient('127.0.0.1', $Api); $c.Close(); return $p } catch { Start-Sleep -Milliseconds 500 }
}
throw 'control did not listen'
}
Step 'Caddy and control, as the services start them'
$env:APPDATA = Join-Path $Work 'caddy-appdata'
$procs += Start-Process -PassThru -WindowStyle Hidden -FilePath (Join-Path $Work 'bin\caddy.exe') `
-ArgumentList 'run', '--config', "`"$(Join-Path $root 'caddy\caddy.json')`"" `
-RedirectStandardError (Join-Path $root 'logs\caddy.log') -RedirectStandardOutput (Join-Path $root 'logs\caddy.out')
$p = Start-Control; $procs += $p
$adminTok = (& $ctl token create --name smoke-admin --config $config) | Select-Object -Last 1
$previewTok = (& $ctl token create --name fabrica-previews --scope previews --config $config) | Select-Object -Last 1
$auth = @{ Authorization = "Bearer $adminTok" }
$base = "http://127.0.0.1:$Api"
Step 'the intermediate, signed offline'
$r = Invoke-RestMethod -Method POST -Uri "$base/api/v1/certs/intermediate/renewal" -Headers $auth
$csr = Join-Path $Work 'intermediate.csr'
Invoke-WebRequest -UseBasicParsing -Uri "$base/api/v1/pki-requests/$($r.request_id)/csr" -Headers $auth -OutFile $csr
$crt = Join-Path $Work 'intermediate.crt'
& $ctl ca sign --root $offline --csr $csr --out $crt --yes --passphrase-file $pass
Check ($LASTEXITCODE -eq 0) 'ca sign'
& $ctl pki install $crt --config $config
Check ($LASTEXITCODE -eq 0) 'pki install'
Step 'control again (it adds the admin host now that it can issue its certificate)'
Stop-Process -Id $p.Id -Force; Start-Sleep 1
$procs = @($procs | Where-Object { $_.Id -ne $p.Id })
$p = Start-Control; $procs += $p
$deadline = (Get-Date).AddSeconds(120)
do {
Start-Sleep 2
$hosts = (Invoke-RestMethod -Uri "$base/api/v1/hosts" -Headers $auth).hosts.hostname
} until (($hosts -contains 'ianitor.smoke.test') -or (Get-Date) -gt $deadline)
Check ($hosts -contains 'ianitor.smoke.test') 'admin host in the routes'
Step 'a branch build on 127.0.0.1'
$site = Join-Path $Work 'app'; New-Item -ItemType Directory $site | Out-Null
[IO.File]::WriteAllText((Join-Path $site 'index.html'), 'hello from fix-42')
$procs += Start-Process -PassThru -WindowStyle Hidden -FilePath 'python' -ArgumentList '-m', 'http.server', $AppPort, '--bind', '127.0.0.1', '--directory', "`"$site`""
Start-Sleep 2
Step 'Fabrica opens the preview (scripts\preview.ps1, previews-only token)'
$env:IANITOR_URL = $base; $env:IANITOR_PREVIEW_TOKEN = $previewTok
$url = & (Join-Path $repo 'scripts\preview.ps1') open -Project 'shop' -Branch 'fix-42' -Upstream "http://127.0.0.1:$AppPort" -Ttl 3600
Check ($url -eq 'https://fix-42--shop.pv.smoke.test/') "preview URL $url"
$ca = Join-Path $offline 'domi-root.crt'
$body = & curl.exe -sS --cacert $ca --resolve "fix-42--shop.pv.smoke.test:${Https}:127.0.0.1" "https://fix-42--shop.pv.smoke.test:$Https/"
Check ($body -eq 'hello from fix-42') "the preview answers through Caddy with a Domi certificate ($body)"
$code = & curl.exe -s -o NUL -w '%{http_code}' --cacert $ca --resolve "ianitor.smoke.test:${Https}:127.0.0.1" "https://ianitor.smoke.test:$Https/api/v1/team"
Check ($code -eq '401') "the admin host reaches control and needs a sign-in ($code)"
$deny = & curl.exe -s -o NUL -w '%{http_code}' -H "Authorization: Bearer $previewTok" "$base/api/v1/hosts"
Check ($deny -eq '403') "the preview token cannot read the hosts ($deny)"
$list = & (Join-Path $repo 'scripts\preview.ps1') list
Check ($list -match 'fix-42--shop') 'listed'
Step 'closed on merge'
& (Join-Path $repo 'scripts\preview.ps1') close -Project 'shop' -Branch 'fix-42' | Out-Null
$deadline = (Get-Date).AddSeconds(90)
do {
Start-Sleep 2
$code = & curl.exe -s -o NUL -w '%{http_code}' --cacert $ca --resolve "fix-42--shop.pv.smoke.test:${Https}:127.0.0.1" "https://fix-42--shop.pv.smoke.test:$Https/"
} until (($code -ne '200') -or (Get-Date) -gt $deadline)
Check ($code -ne '200') "gone after close ($code)"
Write-Host 'SMOKE PASSED'
} finally {
foreach ($p in $procs) { try { Stop-Process -Id $p.Id -Force -ErrorAction SilentlyContinue } catch {} }
Remove-Item Env:\IANITOR_PREVIEW_TOKEN -ErrorAction SilentlyContinue
}