4
0

agent_association_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // agent_association_test.go — #1874: un push event-driven (wireless-only, sin
  2. // sección system) no debe perder la asociación agente↔router. El registry
  3. // retiene la identidad (#852) y GET /api/agents tiene que usarla; sin eso el
  4. // router se quedaba sin routerId y la UI pintaba "agente no instalado" con el
  5. // agente vivo.
  6. package httpapi_test
  7. import (
  8. "encoding/json"
  9. "fmt"
  10. "net/http"
  11. "testing"
  12. "time"
  13. )
  14. type agentListEntry struct {
  15. Slug string `json:"slug"`
  16. RouterID string `json:"routerId"`
  17. Hostname string `json:"hostname"`
  18. Fresh bool `json:"fresh"`
  19. }
  20. func getAgents(t *testing.T, ts *agentTestServer) []agentListEntry {
  21. t.Helper()
  22. req, _ := http.NewRequest("GET", ts.URL+"/api/agents", nil)
  23. req.Header.Set("Cookie", "session="+ts.cookie)
  24. res, err := http.DefaultClient.Do(req)
  25. if err != nil {
  26. t.Fatalf("GET /api/agents: %v", err)
  27. }
  28. defer res.Body.Close()
  29. if res.StatusCode != http.StatusOK {
  30. t.Fatalf("GET /api/agents: %d", res.StatusCode)
  31. }
  32. var body struct {
  33. Agents []agentListEntry `json:"agents"`
  34. }
  35. if err := json.NewDecoder(res.Body).Decode(&body); err != nil {
  36. t.Fatalf("decode /api/agents: %v", err)
  37. }
  38. return body.Agents
  39. }
  40. func findAgentEntry(list []agentListEntry, slug string) *agentListEntry {
  41. for i := range list {
  42. if list[i].Slug == slug {
  43. return &list[i]
  44. }
  45. }
  46. return nil
  47. }
  48. func TestAgentAssociationSurvivesWirelessOnlyPush(t *testing.T) {
  49. ts := makeAgentTestServer(t)
  50. now := time.Now().Unix()
  51. // Router cuyo id NO coincide con el slug del agente, como en la flota
  52. // real (id gl-inet-gl-mt6000, agente flint2): la asociación solo puede
  53. // salir de la MAC del bridge o del hostname del board.
  54. if _, err := ts.db.Exec(
  55. `INSERT INTO routers (id, name, host, type, is_gateway, created_at) VALUES (?,?,?,?,?,?)`,
  56. "gl-inet-gl-mt6000", "GL.iNet GL-MT6000", "192.168.10.1", "glinet", 1, now*1000,
  57. ); err != nil {
  58. t.Fatalf("insert router: %v", err)
  59. }
  60. if _, err := ts.db.Exec(
  61. `UPDATE routers SET agent_only = 1, mac = ? WHERE id = ?`,
  62. "94:83:C4:A4:FB:71", "gl-inet-gl-mt6000",
  63. ); err != nil {
  64. t.Fatalf("update router: %v", err)
  65. }
  66. _, token, _ := createAgentToken(t, ts, "flint2")
  67. full := fmt.Sprintf(`{"router":"flint2","ts":%d,"version":"0.1.0","data":{"system":{"bridgeMac":"94:83:C4:A4:FB:71","board":{"hostname":"Flint2","model":"GL.iNet GL-MT6000"},"cpu":7},"wireless":{"clients":{}}}}`, now)
  68. res := ingest(t, ts, token, "10.0.9.1", full)
  69. res.Body.Close()
  70. if res.StatusCode != http.StatusAccepted {
  71. t.Fatalf("ingest full: %d", res.StatusCode)
  72. }
  73. entry := findAgentEntry(getAgents(t, ts), "flint2")
  74. if entry == nil || entry.RouterID != "gl-inet-gl-mt6000" || entry.Hostname != "flint2" {
  75. t.Fatalf("asociación tras push completo: %+v", entry)
  76. }
  77. // Push de evento nl80211: mismo contrato que en producción, sin system.
  78. wireless := fmt.Sprintf(`{"router":"flint2","ts":%d,"version":"0.1.0","data":{"arp":{},"dhcp":{"leases":[]},"wireless":{"clients":{}}}}`, now+1)
  79. res = ingest(t, ts, token, "10.0.9.1", wireless)
  80. res.Body.Close()
  81. if res.StatusCode != http.StatusAccepted {
  82. t.Fatalf("ingest wireless: %d", res.StatusCode)
  83. }
  84. entry = findAgentEntry(getAgents(t, ts), "flint2")
  85. if entry == nil {
  86. t.Fatal("el agente desapareció del listado")
  87. }
  88. if entry.RouterID != "gl-inet-gl-mt6000" {
  89. t.Fatalf("el push wireless-only perdió la asociación: routerId=%q", entry.RouterID)
  90. }
  91. if entry.Hostname != "flint2" {
  92. t.Fatalf("hostname retenido = %q, esperaba flint2", entry.Hostname)
  93. }
  94. if !entry.Fresh {
  95. t.Fatalf("el agente debería seguir fresco: %+v", entry)
  96. }
  97. }