upgrade_progress_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // upgrade_progress_test.go — #284: progreso en vivo del self-update.
  2. // Contrato de POST /api/agents/{slug}/upgrade-progress (Bearer del agente),
  3. // la semilla "requested" al enviar el comando upgrade/upgrade-all, la
  4. // transición a restarting/failed desde upgrade-result y la exposición del
  5. // campo `upgrade` en GET /api/agents.
  6. package httpapi_test
  7. import (
  8. "io"
  9. "net/http"
  10. "strings"
  11. "testing"
  12. )
  13. // postProgress hace POST /api/agents/{slug}/upgrade-progress con el Bearer dado.
  14. func postProgress(t *testing.T, ts *upgradeTestServer, slug, token, body string) int {
  15. t.Helper()
  16. req, _ := http.NewRequest("POST", ts.URL+"/api/agents/"+slug+"/upgrade-progress", strings.NewReader(body))
  17. req.Header.Set("Content-Type", "application/json")
  18. if token != "" {
  19. req.Header.Set("Authorization", "Bearer "+token)
  20. }
  21. res, err := http.DefaultClient.Do(req)
  22. if err != nil {
  23. t.Fatalf("upgrade-progress: %v", err)
  24. }
  25. defer res.Body.Close()
  26. _, _ = io.Copy(io.Discard, res.Body)
  27. return res.StatusCode
  28. }
  29. // agentUpgradeField devuelve el campo `upgrade` del slug en GET /api/agents
  30. // (nil si no viene).
  31. func agentUpgradeField(t *testing.T, ts *upgradeTestServer, slug string) map[string]any {
  32. t.Helper()
  33. res := get(t, ts.URL, "/api/agents", ts.cookie)
  34. body := readJSON(t, res)
  35. for _, a := range body["agents"].([]any) {
  36. m, _ := a.(map[string]any)
  37. if m["slug"] == slug {
  38. u, _ := m["upgrade"].(map[string]any)
  39. return u
  40. }
  41. }
  42. t.Fatalf("%s ausente en /api/agents: %v", slug, body)
  43. return nil
  44. }
  45. // TestUpgradeProgressAuth: 401 sin token o con token inválido; 200 con el
  46. // token del agente; 400 con step desconocido.
  47. func TestUpgradeProgressAuth(t *testing.T) {
  48. ts := makeUpgradeTestServer(t)
  49. st, tok := createUpgradeToken(t, ts, "patio")
  50. if st != 201 || tok == "" {
  51. t.Fatalf("create: %d", st)
  52. }
  53. if got := postProgress(t, ts, "patio", "", `{"step":"downloading"}`); got != 401 {
  54. t.Fatalf("sin token: got %d want 401", got)
  55. }
  56. if got := postProgress(t, ts, "patio", "deadbeef", `{"step":"downloading"}`); got != 401 {
  57. t.Fatalf("token malo: got %d want 401", got)
  58. }
  59. if got := postProgress(t, ts, "patio", tok, `{"step":"nonsense"}`); got != 400 {
  60. t.Fatalf("step inválido: got %d want 400", got)
  61. }
  62. if got := postProgress(t, ts, "patio", tok, `not-json`); got != 400 {
  63. t.Fatalf("body roto: got %d want 400", got)
  64. }
  65. if got := postProgress(t, ts, "patio", tok, `{"step":"downloading","pct":42}`); got != 200 {
  66. t.Fatalf("válido: got %d want 200", got)
  67. }
  68. }
  69. // TestUpgradeProgressExposedInAgentsList: el comando upgrade siembra
  70. // "requested", los reportes del agente actualizan el paso y upgrade-result
  71. // transita a restarting (ok) o failed (error). El campo caduca por TTL.
  72. func TestUpgradeProgressExposedInAgentsList(t *testing.T) {
  73. ts := makeUpgradeTestServer(t)
  74. st, tok := createUpgradeToken(t, ts, "patio")
  75. if st != 201 {
  76. t.Fatalf("create: %d", st)
  77. }
  78. cancel, done := openStream(t, ts, "patio", tok)
  79. defer func() { cancel(); <-done }()
  80. // Comando enviado → paso "requested".
  81. status, _ := postUpgrade(t, ts, "patio", ts.cookie)
  82. if status != 202 {
  83. t.Fatalf("upgrade: got %d want 202", status)
  84. }
  85. u := agentUpgradeField(t, ts, "patio")
  86. if u["step"] != "requested" {
  87. t.Fatalf("tras upgrade: step %v, want requested", u["step"])
  88. }
  89. // Paso intermedio del agente → downloading con pct.
  90. if got := postProgress(t, ts, "patio", tok, `{"step":"downloading","pct":40}`); got != 200 {
  91. t.Fatalf("progress: got %d want 200", got)
  92. }
  93. u = agentUpgradeField(t, ts, "patio")
  94. if u["step"] != "downloading" {
  95. t.Fatalf("step: %v, want downloading", u["step"])
  96. }
  97. if u["pct"].(float64) != 40 {
  98. t.Fatalf("pct: %v, want 40", u["pct"])
  99. }
  100. // pct fuera de rango → se clampea a 100.
  101. if got := postProgress(t, ts, "patio", tok, `{"step":"downloading","pct":250}`); got != 200 {
  102. t.Fatalf("progress clamp: got %d want 200", got)
  103. }
  104. u = agentUpgradeField(t, ts, "patio")
  105. if u["pct"].(float64) != 100 {
  106. t.Fatalf("pct clamp: %v, want 100", u["pct"])
  107. }
  108. // Resultado ok → restarting (el agente se dispone a reiniciarse).
  109. req, _ := http.NewRequest("POST", ts.URL+"/api/agents/patio/upgrade-result", strings.NewReader(`{"ok":true}`))
  110. req.Header.Set("Content-Type", "application/json")
  111. req.Header.Set("Authorization", "Bearer "+tok)
  112. res, _ := http.DefaultClient.Do(req)
  113. res.Body.Close()
  114. if res.StatusCode != 200 {
  115. t.Fatalf("upgrade-result: got %d want 200", res.StatusCode)
  116. }
  117. u = agentUpgradeField(t, ts, "patio")
  118. if u["step"] != "restarting" {
  119. t.Fatalf("tras ok: step %v, want restarting", u["step"])
  120. }
  121. // Resultado con error → failed con el mensaje.
  122. req, _ = http.NewRequest("POST", ts.URL+"/api/agents/patio/upgrade-result", strings.NewReader(`{"ok":false,"error":"swap failed"}`))
  123. req.Header.Set("Content-Type", "application/json")
  124. req.Header.Set("Authorization", "Bearer "+tok)
  125. res, _ = http.DefaultClient.Do(req)
  126. res.Body.Close()
  127. u = agentUpgradeField(t, ts, "patio")
  128. if u["step"] != "failed" {
  129. t.Fatalf("tras error: step %v, want failed", u["step"])
  130. }
  131. if u["error"] != "swap failed" {
  132. t.Fatalf("error: %v, want swap failed", u["error"])
  133. }
  134. }