facet_test.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // facet_test.go - el plegado de facets (NETPULSE-ONE-LEGION): una fila de
  2. // agente declarada facet de un device existente NO es una caja; desaparece
  3. // del listado y deja su puntero en el device padre.
  4. package adapters
  5. import (
  6. "fmt"
  7. "testing"
  8. "time"
  9. "github.com/gnacho/netpulse/agent/probe"
  10. )
  11. func facetRouter(id, name, status string) Router {
  12. return Router{ID: id, Name: name, Status: status}
  13. }
  14. func facetDevice(id, mac string) Device {
  15. return Device{ID: id, MAC: mac}
  16. }
  17. func TestFoldFacetRouters(t *testing.T) {
  18. macLegion := "24:4B:FE:5D:1C:25"
  19. facet := TopologyOverride{ID: "rke2-legion", MAC: "rke2-legion", Kind: "facet",
  20. Parent: macLegion, Name: "RKE2", Enabled: true}
  21. ovs := []TopologyOverride{facet}
  22. census := func(id string) ([]probe.WorkloadItem, int, bool) {
  23. if id == "rke2-legion" {
  24. return []probe.WorkloadItem{{ID: "x", Name: "x", State: "running"}}, 101, true
  25. }
  26. return nil, 0, false
  27. }
  28. t.Run("folds the agent row onto the parent device", func(t *testing.T) {
  29. routers := []Router{facetRouter("gw", "gw", "online"), facetRouter("rke2-legion", "RKE2", "online")}
  30. devices := []Device{facetDevice("legion", macLegion), facetDevice("other", "AA:BB:CC:DD:EE:FF")}
  31. ro, de := foldFacetRouters(routers, devices, ovs, census)
  32. if len(ro) != 1 || ro[0].ID != "gw" {
  33. t.Fatalf("faceted router must drop out of the list, got %v", ro)
  34. }
  35. if de[0].Facet == nil {
  36. t.Fatal("parent device must carry the facet pointer")
  37. }
  38. f := de[0].Facet
  39. if f.RouterID != "rke2-legion" || f.Label != "RKE2" || !f.Online {
  40. t.Fatalf("facet payload wrong: %+v", f)
  41. }
  42. if !f.WorkloadsKnown || f.Workloads != 101 {
  43. t.Fatalf("census stamp wrong: %+v", f)
  44. }
  45. if de[1].Facet != nil {
  46. t.Fatal("non-parent device must stay facet-free")
  47. }
  48. if devices[0].Facet != nil {
  49. t.Fatal("inputs must not be rewritten (shared backing array, #1378)")
  50. }
  51. })
  52. t.Run("offline agent folds with the down flag, no ghost box", func(t *testing.T) {
  53. routers := []Router{facetRouter("rke2-legion", "RKE2", "offline")}
  54. devices := []Device{facetDevice("legion", macLegion)}
  55. ro, de := foldFacetRouters(routers, devices, ovs, nil)
  56. if len(ro) != 0 {
  57. t.Fatalf("offline facet must NOT resurrect the box, got %v", ro)
  58. }
  59. if de[0].Facet == nil || de[0].Facet.Online {
  60. t.Fatalf("facet must report agent down: %+v", de[0].Facet)
  61. }
  62. })
  63. t.Run("missing parent device keeps the box", func(t *testing.T) {
  64. routers := []Router{facetRouter("rke2-legion", "RKE2", "online")}
  65. devices := []Device{facetDevice("other", "AA:BB:CC:DD:EE:FF")}
  66. ro, de := foldFacetRouters(routers, devices, ovs, census)
  67. if len(ro) != 1 {
  68. t.Fatal("a facet whose host is absent must keep its box (never swallow a row)")
  69. }
  70. if de[0].Facet != nil {
  71. t.Fatal("no fold without the parent")
  72. }
  73. })
  74. t.Run("label falls back to the agent row name", func(t *testing.T) {
  75. ov := TopologyOverride{MAC: "rke2-legion", Kind: "facet", Parent: macLegion, Enabled: true}
  76. routers := []Router{facetRouter("rke2-legion", "RKE2", "online")}
  77. devices := []Device{facetDevice("legion", macLegion)}
  78. _, de := foldFacetRouters(routers, devices, []TopologyOverride{ov}, nil)
  79. if de[0].Facet == nil || de[0].Facet.Label != "RKE2" {
  80. t.Fatalf("override without a name must borrow the row's display name: %+v", de[0].Facet)
  81. }
  82. })
  83. t.Run("disabled facet and other kinds are inert", func(t *testing.T) {
  84. off := facet
  85. off.Enabled = false
  86. attach := TopologyOverride{MAC: "rke2-legion", Kind: "attach", Parent: macLegion, Enabled: true}
  87. routers := []Router{facetRouter("rke2-legion", "RKE2", "online")}
  88. devices := []Device{facetDevice("legion", macLegion)}
  89. ro, de := foldFacetRouters(routers, devices, []TopologyOverride{off, attach}, census)
  90. if len(ro) != 1 || de[0].Facet != nil {
  91. t.Fatal("only enabled facet rows fold")
  92. }
  93. })
  94. t.Run("router-list-only fold (nil devices)", func(t *testing.T) {
  95. routers := []Router{facetRouter("gw", "gw", "online"), facetRouter("rke2-legion", "RKE2", "online")}
  96. ro, de := foldFacetRouters(routers, nil, ovs, census)
  97. if len(ro) != 1 || ro[0].ID != "gw" {
  98. t.Fatalf("nil-device fold must drop the faceted row, got %v", ro)
  99. }
  100. if de != nil {
  101. t.Fatal("nil devices stay nil")
  102. }
  103. })
  104. t.Run("workload items ride the facet, capped with the needle preserved", func(t *testing.T) {
  105. // 100 running + 1 STOPPED at the tail of the census: a naive
  106. // head-trim would drop the needle past the cap and the canvas
  107. // would lie ("all green").
  108. items := make([]probe.WorkloadItem, 0, 101)
  109. for i := range 100 {
  110. items = append(items, probe.WorkloadItem{ID: fmt.Sprintf("p%d", i), Name: fmt.Sprintf("pod-%d", i), Kind: "pod", State: "running"})
  111. }
  112. items = append(items, probe.WorkloadItem{ID: "p100", Name: "kube-flannel-x", Kind: "pod", State: "stopped"})
  113. c := func(string) ([]probe.WorkloadItem, int, bool) { return items, len(items), true }
  114. routers := []Router{facetRouter("rke2-legion", "RKE2", "online")}
  115. devices := []Device{facetDevice("legion", macLegion)}
  116. _, de := foldFacetRouters(routers, devices, ovs, c)
  117. f := de[0].Facet
  118. if f == nil || !f.WorkloadsKnown || f.Workloads != 101 {
  119. t.Fatalf("true total must survive the cap: %+v", f)
  120. }
  121. if len(f.WorkloadItems) != facetWorkloadItemCap {
  122. t.Fatalf("items must be capped at %d, got %d", facetWorkloadItemCap, len(f.WorkloadItems))
  123. }
  124. var stopped int
  125. for _, w := range f.WorkloadItems {
  126. if w.State == "stopped" {
  127. stopped++
  128. }
  129. }
  130. if stopped != 1 {
  131. t.Fatal("the not-running needle must always survive the cap")
  132. }
  133. // The sample must not alias the polled backing array (#1378).
  134. items[0].Name = "MUTATED"
  135. if f.WorkloadItems[0].Name == "MUTATED" && f.WorkloadItems[0].ID == "p0" {
  136. // first slot is now a running pod p0 (order: stopped first?
  137. // no: stopped is appended after? verify order contract below)
  138. t.Log("sample slot 0 aliases? check copy", f.WorkloadItems[0])
  139. }
  140. for _, w := range f.WorkloadItems {
  141. if w.Name == "MUTATED" {
  142. t.Fatal("sample must be a copy, not an alias of live state")
  143. }
  144. }
  145. })
  146. t.Run("small census ships whole", func(t *testing.T) {
  147. items := []probe.WorkloadItem{{ID: "a", Name: "a", State: "running"}, {ID: "b", Name: "b", State: "stopped"}}
  148. c := func(string) ([]probe.WorkloadItem, int, bool) { return items, len(items), true }
  149. routers := []Router{facetRouter("rke2-legion", "RKE2", "online")}
  150. devices := []Device{facetDevice("legion", macLegion)}
  151. _, de := foldFacetRouters(routers, devices, ovs, c)
  152. if len(de[0].Facet.WorkloadItems) != 2 {
  153. t.Fatalf("census under the cap must ship whole, got %v", de[0].Facet.WorkloadItems)
  154. }
  155. })
  156. }
  157. // #2236 freshness applied to the facet fold (gnacho on #2233): both census
  158. // readers must report ok=false past the 15-minute TTL, so a dead pusher
  159. // keeps its offline badge but stops painting counts and CT chips.
  160. func TestCensusOfLastPolledFreshness(t *testing.T) {
  161. l := NewLive(nil, nil, nil, nil)
  162. now := time.Now().UnixMilli()
  163. items := []probe.WorkloadItem{{ID: "a", Name: "a", State: "running"}}
  164. l.lastPolled["fresh"] = &routerPolled{polledAt: now - 60_000, workloads: &probe.Workloads{Items: items}}
  165. l.lastPolled["stale"] = &routerPolled{polledAt: now - int64(16*time.Minute/time.Millisecond), workloads: &probe.Workloads{Items: items}}
  166. l.lastPolled["zero-stamp"] = &routerPolled{workloads: &probe.Workloads{Items: items}}
  167. if _, n, ok := l.censusOfLastPolled("fresh"); !ok || n != 1 {
  168. t.Fatalf("fresh census must report ok with its count, got ok=%v n=%d", ok, n)
  169. }
  170. if _, n, ok := l.censusOfLastPolled("stale"); ok || n != 0 {
  171. t.Fatalf("census past the TTL must report ok=false, got ok=%v n=%d", ok, n)
  172. }
  173. if _, _, ok := l.censusOfLastPolled("zero-stamp"); ok {
  174. t.Fatal("a never-polled zero stamp is a fossil, not a fresh census")
  175. }
  176. if _, _, ok := l.censusOfLastPolled("missing"); ok {
  177. t.Fatal("missing agent must report ok=false")
  178. }
  179. }