125 lines
7.8 KiB
PowerShell
125 lines
7.8 KiB
PowerShell
[CmdletBinding()]
|
|
param([string]$PostgresBin = 'D:\pgsql17\bin')
|
|
|
|
Set-StrictMode -Version 3.0
|
|
$ErrorActionPreference = 'Stop'
|
|
$pgStarted = $false
|
|
$server = $null
|
|
$testRoot = Join-Path ([IO.Path]::GetTempPath()) ('yovision-bell-183-' + [guid]::NewGuid().ToString('N'))
|
|
$data = Join-Path $testRoot 'postgres'
|
|
$log = Join-Path $testRoot 'postgres.log'
|
|
$pgOut = Join-Path $testRoot 'pg.out'
|
|
$pgErr = Join-Path $testRoot 'pg.err'
|
|
$serverOut = Join-Path $testRoot 'bell.out.log'
|
|
$serverErr = Join-Path $testRoot 'bell.err.log'
|
|
$serverExe = Join-Path $testRoot 'bell-server.exe'
|
|
$serverRoot = (Resolve-Path (Join-Path $PSScriptRoot '..\..')).Path
|
|
|
|
function Get-FreePort {
|
|
$listener = [Net.Sockets.TcpListener]::new([Net.IPAddress]::Loopback, 0)
|
|
try { $listener.Start(); return ([Net.IPEndPoint]$listener.LocalEndpoint).Port } finally { $listener.Stop() }
|
|
}
|
|
function Wait-Port([int]$Port) {
|
|
for ($attempt = 0; $attempt -lt 120; $attempt++) {
|
|
try {
|
|
$client = [Net.Sockets.TcpClient]::new()
|
|
$open = $client.ConnectAsync('127.0.0.1', $Port).Wait(250) -and $client.Connected
|
|
$client.Dispose()
|
|
if ($open) { return }
|
|
} catch {}
|
|
Start-Sleep -Milliseconds 250
|
|
}
|
|
throw "PostgreSQL port $Port did not open"
|
|
}
|
|
function Wait-Health([string]$BaseUrl) {
|
|
for ($attempt = 0; $attempt -lt 100; $attempt++) {
|
|
try {
|
|
$health = Invoke-RestMethod -Uri "$BaseUrl/healthz" -TimeoutSec 2 -NoProxy
|
|
if ($health.status -eq 'ok') { return }
|
|
} catch {}
|
|
Start-Sleep -Milliseconds 300
|
|
}
|
|
throw 'Bell health endpoint did not become ready'
|
|
}
|
|
|
|
New-Item -ItemType Directory -Path $testRoot | Out-Null
|
|
$pgPort = Get-FreePort
|
|
$bellPort = Get-FreePort
|
|
$baseUrl = "http://127.0.0.1:$bellPort"
|
|
try {
|
|
foreach ($name in @('initdb.exe', 'pg_ctl.exe', 'createdb.exe', 'psql.exe')) {
|
|
if (-not (Test-Path -LiteralPath (Join-Path $PostgresBin $name) -PathType Leaf)) { throw "Missing PostgreSQL tool: $name" }
|
|
}
|
|
& (Join-Path $PostgresBin 'initdb.exe') -D $data -U postgres -A trust --encoding=UTF8 --no-locale | Out-Null
|
|
if ($LASTEXITCODE -ne 0) { throw 'initdb failed' }
|
|
$arguments = "-D `"$data`" -l `"$log`" -o `"-p $pgPort -h 127.0.0.1`" start"
|
|
Start-Process -FilePath (Join-Path $PostgresBin 'pg_ctl.exe') -ArgumentList $arguments -RedirectStandardOutput $pgOut -RedirectStandardError $pgErr -WindowStyle Hidden | Out-Null
|
|
Wait-Port $pgPort
|
|
$pgStarted = $true
|
|
& (Join-Path $PostgresBin 'createdb.exe') -h 127.0.0.1 -p $pgPort -U postgres bell_183
|
|
if ($LASTEXITCODE -ne 0) { throw 'createdb failed' }
|
|
|
|
$env:GOTOOLCHAIN = 'go1.26.5'
|
|
$env:BELL_DATABASE_URL = "host=127.0.0.1 port=$pgPort user=postgres dbname=bell_183 sslmode=disable"
|
|
$env:BELL_CONTACT_SCHEDULE_TEST_DATABASE_URL = $env:BELL_DATABASE_URL
|
|
$env:BELL_JWT_SECRET = [guid]::NewGuid().ToString('N') + [guid]::NewGuid().ToString('N')
|
|
$env:BELL_BOOTSTRAP_USERNAME = 'bell_183_admin'
|
|
$env:BELL_BOOTSTRAP_PASSWORD = [guid]::NewGuid().ToString('N')
|
|
$env:BELL_RULE_ALERT_OPERATOR_PASSWORD = [guid]::NewGuid().ToString('N')
|
|
$env:BELL_CONTACT_CHANNEL_KEY = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes('0123456789abcdef0123456789abcdef'))
|
|
$env:BELL_HOST = '127.0.0.1'
|
|
$env:BELL_PORT = $bellPort.ToString()
|
|
|
|
Push-Location $serverRoot
|
|
try {
|
|
go run . migrate -c config/settings.demo.yml *> (Join-Path $testRoot 'migrate.log')
|
|
if ($LASTEXITCODE -ne 0) { throw "migration failed: $(Join-Path $testRoot 'migrate.log')" }
|
|
go test ./tests/bell_contact_schedule -count=1 -v
|
|
if ($LASTEXITCODE -ne 0) { throw 'contact schedule tests failed' }
|
|
go build -o $serverExe .
|
|
if ($LASTEXITCODE -ne 0) { throw 'Bell build failed' }
|
|
} finally { Pop-Location }
|
|
|
|
$server = Start-Process -FilePath $serverExe -ArgumentList @('server', '-c', 'config/settings.demo.yml') -WorkingDirectory $serverRoot -RedirectStandardOutput $serverOut -RedirectStandardError $serverErr -WindowStyle Hidden -PassThru
|
|
Wait-Health $baseUrl
|
|
$adminBody = @{ username = $env:BELL_BOOTSTRAP_USERNAME; password = $env:BELL_BOOTSTRAP_PASSWORD; code = '0'; uuid = '0' } | ConvertTo-Json -Compress
|
|
$admin = Invoke-RestMethod -Method Post -Uri "$baseUrl/api/v1/login" -ContentType 'application/json' -Body $adminBody -NoProxy
|
|
$adminHeaders = @{ Authorization = "Bearer $($admin.token)" }
|
|
$contactBody = @{ name = 'HTTP联系人'; role = '测试值班' } | ConvertTo-Json -Compress
|
|
$created = Invoke-RestMethod -Method Post -Uri "$baseUrl/api/v1/bell/contacts" -Headers $adminHeaders -ContentType 'application/json; charset=utf-8' -Body $contactBody -NoProxy
|
|
if ([int]$created.code -ne 200) { throw 'administrator contact create failed' }
|
|
$address = '+8613900000003'
|
|
$channelBody = @{ kind = 'sms'; address = $address } | ConvertTo-Json -Compress
|
|
$channel = Invoke-RestMethod -Method Post -Uri "$baseUrl/api/v1/bell/contacts/$($created.data.id)/channels" -Headers $adminHeaders -ContentType 'application/json' -Body $channelBody -NoProxy
|
|
if ([int]$channel.code -ne 200 -or ($channel | ConvertTo-Json -Depth 10 -Compress).Contains($address)) { throw 'write-only channel HTTP boundary failed' }
|
|
$logRow = ''
|
|
for ($attempt = 0; $attempt -lt 40; $attempt++) {
|
|
$logRow = & (Join-Path $PostgresBin 'psql.exe') -h 127.0.0.1 -p $pgPort -U postgres -d bell_183 -Atc "SELECT id::text || '|' || coalesce(oper_param,'') FROM sys_opera_log WHERE oper_url LIKE '/api/v1/bell/contacts/%/channels' ORDER BY id DESC LIMIT 1"
|
|
if ($LASTEXITCODE -ne 0) { throw 'operation log query failed' }
|
|
if ($logRow) { break }
|
|
Start-Sleep -Milliseconds 100
|
|
}
|
|
if (-not $logRow -or $logRow.Contains($address)) { throw "operation log redaction failed: $logRow" }
|
|
$loggedBody = ($logRow -split '\|', 2)[1]
|
|
if ($loggedBody -and -not $loggedBody.Contains('"redacted":true')) { throw "unexpected operation log marker: $loggedBody" }
|
|
|
|
$operatorBody = @{ username = 'bell_132_operator'; password = $env:BELL_RULE_ALERT_OPERATOR_PASSWORD; code = '0'; uuid = '0' } | ConvertTo-Json -Compress
|
|
$operator = Invoke-RestMethod -Method Post -Uri "$baseUrl/api/v1/login" -ContentType 'application/json' -Body $operatorBody -NoProxy
|
|
$operatorHeaders = @{ Authorization = "Bearer $($operator.token)" }
|
|
foreach ($path in @('/api/v1/bell/contacts', '/api/v1/bell/duty-groups')) {
|
|
$read = Invoke-RestMethod -Uri "$baseUrl$path" -Headers $operatorHeaders -NoProxy
|
|
if ([int]$read.code -ne 200) { throw "operator read failed: $path" }
|
|
}
|
|
$denied = Invoke-RestMethod -Method Post -Uri "$baseUrl/api/v1/bell/contacts" -Headers $operatorHeaders -ContentType 'application/json' -Body $contactBody -NoProxy
|
|
if ([int]$denied.code -ne 403) { throw "operator write returned $($denied.code)" }
|
|
$menu = Invoke-RestMethod -Uri "$baseUrl/api/v1/menurole" -Headers $operatorHeaders -NoProxy
|
|
$menuJson = $menu.data | ConvertTo-Json -Depth 20 -Compress
|
|
foreach ($title in @('联系人与通道', '值班排班')) { if (-not $menuJson.Contains($title)) { throw "operator menu missing $title" } }
|
|
Write-Output 'BELL_183_HTTP admin_contact=200 channel_write_only=true operator_reads=200 operator_write=403 menus=true'
|
|
} finally {
|
|
if ($null -ne $server -and -not $server.HasExited) { Stop-Process -Id $server.Id -Force; $server.WaitForExit(5000) | Out-Null }
|
|
if ($pgStarted) { & (Join-Path $PostgresBin 'pg_ctl.exe') -D $data -m fast stop *> (Join-Path $testRoot 'stop.log') }
|
|
foreach ($name in @('BELL_DATABASE_URL', 'BELL_CONTACT_SCHEDULE_TEST_DATABASE_URL', 'BELL_JWT_SECRET', 'BELL_BOOTSTRAP_USERNAME', 'BELL_BOOTSTRAP_PASSWORD', 'BELL_RULE_ALERT_OPERATOR_PASSWORD', 'BELL_CONTACT_CHANNEL_KEY', 'BELL_HOST', 'BELL_PORT')) { Remove-Item "Env:$name" -ErrorAction SilentlyContinue }
|
|
Write-Verbose "Bell #183 artifacts: $testRoot"
|
|
}
|