4
0

backup_api_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // backup_api_test.go — #1462: snapshot standalone de la config por SSE.
  2. // Contrato de POST /api/agents/{slug}/backup (admin): igual de auth que el
  3. // upgrade, pero a diferencia de este NO se encola cuando el agente no está
  4. // conectado (409): un backup no es imperativo.
  5. package httpapi_test
  6. import (
  7. "context"
  8. "encoding/json"
  9. "io"
  10. "net/http"
  11. "net/http/httptest"
  12. "strings"
  13. "testing"
  14. "time"
  15. )
  16. func postBackup(t *testing.T, ts *upgradeTestServer, slug, cookie string) (int, map[string]any) {
  17. t.Helper()
  18. req, _ := http.NewRequest("POST", ts.URL+"/api/agents/"+slug+"/backup", nil)
  19. if cookie != "" {
  20. req.Header.Set("Cookie", "session="+cookie)
  21. }
  22. res, err := http.DefaultClient.Do(req)
  23. if err != nil {
  24. t.Fatalf("POST /backup: %v", err)
  25. }
  26. defer res.Body.Close()
  27. var body map[string]any
  28. _ = json.NewDecoder(res.Body).Decode(&body)
  29. return res.StatusCode, body
  30. }
  31. // openStreamRec igual que openStream pero devuelve el recorder para leer el
  32. // evento enviado al stream.
  33. func openStreamRec(t *testing.T, ts *upgradeTestServer, slug, token string) (*httptest.ResponseRecorder, context.CancelFunc, chan struct{}) {
  34. t.Helper()
  35. ctx, cancel := context.WithCancel(context.Background())
  36. req := httptest.NewRequest(http.MethodGet, ts.URL+"/api/agents/"+slug+"/stream", nil).WithContext(ctx)
  37. req.SetPathValue("slug", slug)
  38. req.Header.Set("Authorization", "Bearer "+token)
  39. rec := httptest.NewRecorder()
  40. done := make(chan struct{})
  41. go func() {
  42. ts.hub.HandleStream(rec, req)
  43. close(done)
  44. }()
  45. time.Sleep(200 * time.Millisecond)
  46. return rec, cancel, done
  47. }
  48. func TestBackupSinSesion401(t *testing.T) {
  49. ts := makeUpgradeTestServer(t)
  50. status, body := postBackup(t, ts, "patio", "")
  51. if status != 401 || body["error"] != "unauthorized" {
  52. t.Fatalf("sin sesión: got %d %v", status, body)
  53. }
  54. }
  55. func TestBackupSlugDesconocido404(t *testing.T) {
  56. ts := makeUpgradeTestServer(t)
  57. status, body := postBackup(t, ts, "noexiste", ts.cookie)
  58. if status != 404 || body["error"] != "not_found" {
  59. t.Fatalf("slug desconocido: got %d %v", status, body)
  60. }
  61. }
  62. // TestBackupNoConectado409: a diferencia del upgrade (#284) el backup NO se
  63. // encola: si el agente no está conectado por SSE se rechaza con 409.
  64. func TestBackupNoConectado409(t *testing.T) {
  65. ts := makeUpgradeTestServer(t)
  66. st, tok := createUpgradeToken(t, ts, "patio")
  67. if st != 201 || tok == "" {
  68. t.Fatalf("create: %d", st)
  69. }
  70. status, body := postBackup(t, ts, "patio", ts.cookie)
  71. if status != 409 || body["error"] != "not_connected" {
  72. t.Fatalf("no conectado: got %d %v", status, body)
  73. }
  74. }
  75. // TestBackupEnviado202: con el stream conectado el comando sale (202) y el
  76. // evento SSE "config_backup" llega al agente.
  77. func TestBackupEnviado202(t *testing.T) {
  78. ts := makeUpgradeTestServer(t)
  79. st, tok := createUpgradeToken(t, ts, "patio")
  80. if st != 201 || tok == "" {
  81. t.Fatalf("create: %d", st)
  82. }
  83. rec, cancel, done := openStreamRec(t, ts, "patio", tok)
  84. defer func() { cancel(); <-done }()
  85. status, body := postBackup(t, ts, "patio", ts.cookie)
  86. if status != 202 || body["status"] != "sent" {
  87. t.Fatalf("enviado: got %d %v", status, body)
  88. }
  89. deadline := time.Now().Add(3 * time.Second)
  90. for time.Now().Before(deadline) {
  91. if strings.Contains(rec.Body.String(), "config_backup") {
  92. return
  93. }
  94. time.Sleep(20 * time.Millisecond)
  95. }
  96. data, _ := io.ReadAll(rec.Result().Body)
  97. t.Fatalf("evento config_backup no llegó al stream; body: %q", string(data))
  98. }