upgrade_all_1872_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package httpapi_test
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. // TestAgentsUpgradeAllExternalWithLiveAgent — #1872: el gate de upgrade-all
  7. // miraba routers.type y devolvía not_openwrt a una unidad con agente nativo y
  8. // fila external. Con el stream SSE abierto el comando sí puede salir; sin
  9. // stream, un scraper sigue dando not_openwrt.
  10. func TestAgentsUpgradeAllExternalWithLiveAgent(t *testing.T) {
  11. ts := makeUpgradeTestServer(t)
  12. // Tres filas external: una con agente conectado, otra sin conectar y una
  13. // tercera con tipo openwrt sin conectar (control: sigue encolando).
  14. for _, slug := range []string{"asus-live", "asus-dark", "owrt-dark"} {
  15. typ := "external"
  16. if slug == "owrt-dark" {
  17. typ = "openwrt"
  18. }
  19. if _, err := ts.db.Exec(
  20. `INSERT INTO routers (id, name, host, type, is_gateway, created_at) VALUES (?,?,?,?,?,?)`,
  21. slug, slug, "10.0.0.9", typ, 0, time.Now().UnixMilli(),
  22. ); err != nil {
  23. t.Fatalf("insert %s: %v", slug, err)
  24. }
  25. }
  26. // asus-live: agente nativo desactualizado, conectado por SSE → sent.
  27. st, tok := createUpgradeToken(t, ts, "asus-live")
  28. if st != 201 {
  29. t.Fatalf("create asus-live: %d", st)
  30. }
  31. upgradeIngest(t, ts, tok, upgradePayloadFor("asus-live", "0.0.9"))
  32. cancel, done := openStream(t, ts, "asus-live", tok)
  33. defer func() { cancel(); <-done }()
  34. // asus-dark: el mismo tipo external pero sin stream → not_openwrt.
  35. if st, tokD := createUpgradeToken(t, ts, "asus-dark"); st != 201 {
  36. t.Fatalf("create asus-dark: %d", st)
  37. } else {
  38. upgradeIngest(t, ts, tokD, upgradePayloadFor("asus-dark", "0.0.9"))
  39. }
  40. // owrt-dark: tipo aceptado sin conexión → queued (sin regresión).
  41. if st, tokO := createUpgradeToken(t, ts, "owrt-dark"); st != 201 {
  42. t.Fatalf("create owrt-dark: %d", st)
  43. } else {
  44. upgradeIngest(t, ts, tokO, upgradePayloadFor("owrt-dark", "0.0.9"))
  45. }
  46. res := doReq(t, "POST", ts.URL+"/api/agents/upgrade-all", ts.cookie, "")
  47. if res.StatusCode != 200 {
  48. t.Fatalf("upgrade-all: got %d", res.StatusCode)
  49. }
  50. body := readJSON(t, res)
  51. bySlug := map[string]string{}
  52. for _, a := range body["agents"].([]any) {
  53. m := a.(map[string]any)
  54. bySlug[m["slug"].(string)] = m["status"].(string)
  55. }
  56. if bySlug["asus-live"] != "sent" {
  57. t.Fatalf("external con agente conectado debería ser sent: %v", bySlug)
  58. }
  59. if bySlug["asus-dark"] != "not_openwrt" {
  60. t.Fatalf("external sin agente debería seguir en not_openwrt: %v", bySlug)
  61. }
  62. if bySlug["owrt-dark"] != "queued" {
  63. t.Fatalf("openwrt sin agente debería seguir en queued: %v", bySlug)
  64. }
  65. }
  66. // TestAgentHubConnected — el helper del hub que sostiene el gate de #1872.
  67. func TestAgentHubConnected(t *testing.T) {
  68. ts := makeUpgradeTestServer(t)
  69. st, tok := createUpgradeToken(t, ts, "hub-check")
  70. if st != 201 {
  71. t.Fatalf("create: %d", st)
  72. }
  73. if ts.hub.Connected("hub-check") {
  74. t.Fatal("sin stream no debería estar conectado")
  75. }
  76. cancel, done := openStream(t, ts, "hub-check", tok)
  77. if !ts.hub.Connected("hub-check") {
  78. t.Fatal("con stream debería estar conectado")
  79. }
  80. cancel()
  81. <-done
  82. }