Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

86 lignes
3.4KB

  1. # Post-deploy smoke test for a running Agent Memory server (by default the production server in .amk.env).
  2. # Checks readiness, the API key (GET /me) and consolidation (dry run, writes nothing).
  3. #
  4. # pwsh scripts/smoke-prod.ps1 uses AMK_URL, AMK_KEY and AMK_NAMESPACE from .amk.env
  5. # pwsh scripts/smoke-prod.ps1 -BaseUrl http://localhost:8801/api/v1 -Key amk_... -NamespaceId <id>
  6. #
  7. # The key is never printed. Exit code 0 when every check passes, 1 otherwise.
  8. [CmdletBinding()]
  9. param(
  10. [string]$BaseUrl,
  11. [string]$Key,
  12. [string]$NamespaceId,
  13. [string]$EnvFile = (Join-Path $PSScriptRoot '..' '.amk.env')
  14. )
  15. $ErrorActionPreference = 'Stop'
  16. # Values not given on the command line come from .amk.env (KEY=value lines; git-ignored, local only).
  17. if (Test-Path $EnvFile) {
  18. $settings = @{}
  19. foreach ($line in Get-Content $EnvFile) {
  20. if ($line -match '^\s*([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*?)\s*$') {
  21. $settings[$Matches[1]] = $Matches[2].Trim('"').Trim("'")
  22. }
  23. }
  24. if (-not $BaseUrl) { $BaseUrl = $settings['AMK_URL'] }
  25. if (-not $Key) { $Key = $settings['AMK_KEY'] }
  26. if (-not $NamespaceId) { $NamespaceId = $settings['AMK_NAMESPACE'] }
  27. }
  28. if (-not $BaseUrl -or -not $Key -or -not $NamespaceId) {
  29. Write-Error "Need -BaseUrl, -Key and -NamespaceId (or AMK_URL, AMK_KEY and AMK_NAMESPACE in $EnvFile)."
  30. }
  31. $BaseUrl = $BaseUrl.TrimEnd('/')
  32. $headers = @{ Authorization = "Bearer $Key" }
  33. $failed = 0
  34. function Invoke-Check {
  35. param([string]$Name, [string]$Method, [string]$Path, [hashtable]$Headers, [string]$Body, [scriptblock]$Test)
  36. try {
  37. $params = @{ Uri = "$BaseUrl$Path"; Method = $Method; SkipHttpErrorCheck = $true; TimeoutSec = 30 }
  38. if ($Headers) { $params.Headers = $Headers }
  39. if ($Body) { $params.Body = $Body; $params.ContentType = 'application/json' }
  40. $resp = Invoke-WebRequest @params
  41. $data = $null
  42. if ($resp.Content) { $data = $resp.Content | ConvertFrom-Json }
  43. $detail = & $Test $resp.StatusCode $data
  44. if ($detail -is [string] -and $detail.StartsWith('FAIL')) {
  45. Write-Host "FAIL $Name - $($detail.Substring(4).Trim())" -ForegroundColor Red
  46. $script:failed++
  47. } else {
  48. Write-Host "ok $Name - $detail" -ForegroundColor Green
  49. }
  50. } catch {
  51. Write-Host "FAIL $Name - $($_.Exception.Message)" -ForegroundColor Red
  52. $script:failed++
  53. }
  54. }
  55. Write-Host "Smoke test against $BaseUrl"
  56. Invoke-Check 'health/ready' GET '/health/ready' $null $null {
  57. param($code, $d)
  58. if ($code -ne 200) { return "FAIL HTTP $code" }
  59. "HTTP 200, status $($d.status)"
  60. }
  61. Invoke-Check 'me (API key)' GET '/me' $headers $null {
  62. param($code, $d)
  63. if ($code -ne 200) { return "FAIL HTTP $code (key invalid or server not updated)" }
  64. $paths = ($d.grants | ForEach-Object { "$($_.path) [$($_.permissions -join ',')]" }) -join '; '
  65. "principal $($d.name) ($($d.type)), grants: $paths"
  66. }
  67. $consBody = @{ namespace_id = $NamespaceId } | ConvertTo-Json -Compress
  68. Invoke-Check 'consolidations (dry run)' POST '/consolidations' $headers $consBody {
  69. param($code, $d)
  70. if ($code -ne 200) { return "FAIL HTTP $code" }
  71. "experiences considered $($d.experiences_considered), groups $(@($d.groups).Count), candidates created $($d.candidates_created)"
  72. }
  73. if ($failed -gt 0) {
  74. Write-Host "$failed check(s) failed" -ForegroundColor Red
  75. exit 1
  76. }
  77. Write-Host 'All checks passed' -ForegroundColor Green
  78. exit 0

Powered by TurnKey Linux.