agent_kind_test.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package httpapi_test
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strings"
  6. "testing"
  7. "time"
  8. )
  9. // TestAgentsNetgripKind (#363): un push con kind "netgrip" se lista como tal
  10. // y el servidor rechaza rearm de un NetGrip FRESH (el agente es el panel
  11. // NetGrip; un NetGrip stale SÍ se rearma reiniciando el servicio, #569).
  12. func TestAgentsNetgripKind(t *testing.T) {
  13. ts := makeAgentTestServer(t)
  14. _, token, _ := createAgentToken(t, ts, "patio")
  15. payload := fmt.Sprintf(`{"router":"patio","ts":%d,"version":"0.23.0","kind":"netgrip","data":{"system":{"sysinfo":{"uptime":100,"load":[0,0,0],"memory":{"total":100,"free":50,"buffered":0,"available":50}},"cpu":10,"temp":40},"wireless":{"clients":{}},"dhcp":{"leases":[]},"fdb":{"macs":{}}}}`, time.Now().Unix())
  16. res := ingest(t, ts, token, "10.0.3.7", payload)
  17. res.Body.Close()
  18. if res.StatusCode != 202 {
  19. t.Fatalf("ingest netgrip: %d", res.StatusCode)
  20. }
  21. res = get(t, ts.URL, "/api/agents", ts.cookie)
  22. body := readJSON(t, res)
  23. agents, _ := body["agents"].([]any)
  24. var patio map[string]any
  25. for _, a := range agents {
  26. m, _ := a.(map[string]any)
  27. if m["slug"] == "patio" {
  28. patio = m
  29. }
  30. }
  31. if patio == nil {
  32. t.Fatalf("patio no está en la lista: %v", body)
  33. }
  34. if patio["kind"] != "netgrip" {
  35. t.Fatalf("patio kind: %v (want netgrip)", patio["kind"])
  36. }
  37. if patio["version"] != "0.23.0" {
  38. t.Fatalf("patio version: %v", patio["version"])
  39. }
  40. // updateAvailable para netgrip depende de la latest release REAL de
  41. // NetGrip en GitHub (cache 5 min): NO se asserting aquí (la comparación
  42. // semver está unit-testeada en TestNetgripLatestCacheAndCmp, interno).
  43. post := func(path string) int {
  44. req, _ := http.NewRequest("POST", ts.URL+path, nil)
  45. req.Header.Set("Cookie", "session="+ts.cookie)
  46. r, err := http.DefaultClient.Do(req)
  47. if err != nil {
  48. t.Fatalf("%s: %v", path, err)
  49. }
  50. defer r.Body.Close()
  51. return r.StatusCode
  52. }
  53. // #363 v2: el upgrade SÍ se acepta para NetGrip (dispara su self-update).
  54. // Sin conexión SSE en el test el comando queda encolado → 202.
  55. if c := post("/api/agents/patio/upgrade"); c != http.StatusAccepted {
  56. t.Fatalf("upgrade netgrip: %d (want 202)", c)
  57. }
  58. if c := post("/api/agents/patio/rearm"); c != http.StatusConflict {
  59. t.Fatalf("rearm netgrip: %d (want 409)", c)
  60. }
  61. // Un agente NATIVO sin kind sigue listándose como native.
  62. _, tokenN, _ := createAgentToken(t, ts, "living")
  63. payloadN := strings.Replace(strings.Replace(payload, `"patio"`, `"living"`, 1), `"kind":"netgrip",`, ``, 1)
  64. resN := ingest(t, ts, tokenN, "10.0.3.8", payloadN)
  65. resN.Body.Close()
  66. res = get(t, ts.URL, "/api/agents", ts.cookie)
  67. body = readJSON(t, res)
  68. agents, _ = body["agents"].([]any)
  69. for _, a := range agents {
  70. m, _ := a.(map[string]any)
  71. if m["slug"] == "living" && m["kind"] != "native" {
  72. t.Fatalf("living kind: %v (want native)", m["kind"])
  73. }
  74. }
  75. }