|
- # Post-deploy smoke test for a running Agent Memory server (by default the production server in .amk.env).
- # Checks readiness, the API key (GET /me) and consolidation (dry run, writes nothing).
- #
- # pwsh scripts/smoke-prod.ps1 uses AMK_URL, AMK_KEY and AMK_NAMESPACE from .amk.env
- # pwsh scripts/smoke-prod.ps1 -BaseUrl http://localhost:8801/api/v1 -Key amk_... -NamespaceId <id>
- #
- # The key is never printed. Exit code 0 when every check passes, 1 otherwise.
- [CmdletBinding()]
- param(
- [string]$BaseUrl,
- [string]$Key,
- [string]$NamespaceId,
- [string]$EnvFile = (Join-Path $PSScriptRoot '..' '.amk.env')
- )
- $ErrorActionPreference = 'Stop'
-
- # Values not given on the command line come from .amk.env (KEY=value lines; git-ignored, local only).
- if (Test-Path $EnvFile) {
- $settings = @{}
- foreach ($line in Get-Content $EnvFile) {
- if ($line -match '^\s*([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*?)\s*$') {
- $settings[$Matches[1]] = $Matches[2].Trim('"').Trim("'")
- }
- }
- if (-not $BaseUrl) { $BaseUrl = $settings['AMK_URL'] }
- if (-not $Key) { $Key = $settings['AMK_KEY'] }
- if (-not $NamespaceId) { $NamespaceId = $settings['AMK_NAMESPACE'] }
- }
- if (-not $BaseUrl -or -not $Key -or -not $NamespaceId) {
- Write-Error "Need -BaseUrl, -Key and -NamespaceId (or AMK_URL, AMK_KEY and AMK_NAMESPACE in $EnvFile)."
- }
- $BaseUrl = $BaseUrl.TrimEnd('/')
- $headers = @{ Authorization = "Bearer $Key" }
- $failed = 0
-
- function Invoke-Check {
- param([string]$Name, [string]$Method, [string]$Path, [hashtable]$Headers, [string]$Body, [scriptblock]$Test)
- try {
- $params = @{ Uri = "$BaseUrl$Path"; Method = $Method; SkipHttpErrorCheck = $true; TimeoutSec = 30 }
- if ($Headers) { $params.Headers = $Headers }
- if ($Body) { $params.Body = $Body; $params.ContentType = 'application/json' }
- $resp = Invoke-WebRequest @params
- $data = $null
- if ($resp.Content) { $data = $resp.Content | ConvertFrom-Json }
- $detail = & $Test $resp.StatusCode $data
- if ($detail -is [string] -and $detail.StartsWith('FAIL')) {
- Write-Host "FAIL $Name - $($detail.Substring(4).Trim())" -ForegroundColor Red
- $script:failed++
- } else {
- Write-Host "ok $Name - $detail" -ForegroundColor Green
- }
- } catch {
- Write-Host "FAIL $Name - $($_.Exception.Message)" -ForegroundColor Red
- $script:failed++
- }
- }
-
- Write-Host "Smoke test against $BaseUrl"
-
- Invoke-Check 'health/ready' GET '/health/ready' $null $null {
- param($code, $d)
- if ($code -ne 200) { return "FAIL HTTP $code" }
- "HTTP 200, status $($d.status)"
- }
-
- Invoke-Check 'me (API key)' GET '/me' $headers $null {
- param($code, $d)
- if ($code -ne 200) { return "FAIL HTTP $code (key invalid or server not updated)" }
- $paths = ($d.grants | ForEach-Object { "$($_.path) [$($_.permissions -join ',')]" }) -join '; '
- "principal $($d.name) ($($d.type)), grants: $paths"
- }
-
- $consBody = @{ namespace_id = $NamespaceId } | ConvertTo-Json -Compress
- Invoke-Check 'consolidations (dry run)' POST '/consolidations' $headers $consBody {
- param($code, $d)
- if ($code -ne 200) { return "FAIL HTTP $code" }
- "experiences considered $($d.experiences_considered), groups $(@($d.groups).Count), candidates created $($d.candidates_created)"
- }
-
- if ($failed -gt 0) {
- Write-Host "$failed check(s) failed" -ForegroundColor Red
- exit 1
- }
- Write-Host 'All checks passed' -ForegroundColor Green
- exit 0
|