|
|
@@ -0,0 +1,197 @@
|
|
|
+// facet_test.go — el plegado de facets (NETPULSE-ONE-LEGION): una fila de
|
|
|
+// agente declarada facet de un device existente NO es una caja; desaparece
|
|
|
+// del listado y deja su puntero en el device padre.
|
|
|
+package adapters
|
|
|
+
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ "testing"
|
|
|
+ "time"
|
|
|
+
|
|
|
+ "github.com/gnacho/netpulse/agent/probe"
|
|
|
+)
|
|
|
+
|
|
|
+func facetRouter(id, name, status string) Router {
|
|
|
+ return Router{ID: id, Name: name, Status: status}
|
|
|
+}
|
|
|
+
|
|
|
+func facetDevice(id, mac string) Device {
|
|
|
+ return Device{ID: id, MAC: mac}
|
|
|
+}
|
|
|
+
|
|
|
+func TestFoldFacetRouters(t *testing.T) {
|
|
|
+ macLegion := "24:4B:FE:5D:1C:25"
|
|
|
+ facet := TopologyOverride{ID: "rke2-legion", MAC: "rke2-legion", Kind: "facet",
|
|
|
+ Parent: macLegion, Name: "RKE2", Enabled: true}
|
|
|
+ ovs := []TopologyOverride{facet}
|
|
|
+
|
|
|
+ census := func(id string) ([]probe.WorkloadItem, int, bool) {
|
|
|
+ if id == "rke2-legion" {
|
|
|
+ return []probe.WorkloadItem{{ID: "x", Name: "x", State: "running"}}, 101, true
|
|
|
+ }
|
|
|
+ return nil, 0, false
|
|
|
+ }
|
|
|
+
|
|
|
+ t.Run("folds the agent row onto the parent device", func(t *testing.T) {
|
|
|
+ routers := []Router{facetRouter("gw", "gw", "online"), facetRouter("rke2-legion", "RKE2", "online")}
|
|
|
+ devices := []Device{facetDevice("legion", macLegion), facetDevice("other", "AA:BB:CC:DD:EE:FF")}
|
|
|
+
|
|
|
+ ro, de := foldFacetRouters(routers, devices, ovs, census)
|
|
|
+ if len(ro) != 1 || ro[0].ID != "gw" {
|
|
|
+ t.Fatalf("faceted router must drop out of the list, got %v", ro)
|
|
|
+ }
|
|
|
+ if de[0].Facet == nil {
|
|
|
+ t.Fatal("parent device must carry the facet pointer")
|
|
|
+ }
|
|
|
+ f := de[0].Facet
|
|
|
+ if f.RouterID != "rke2-legion" || f.Label != "RKE2" || !f.Online {
|
|
|
+ t.Fatalf("facet payload wrong: %+v", f)
|
|
|
+ }
|
|
|
+ if !f.WorkloadsKnown || f.Workloads != 101 {
|
|
|
+ t.Fatalf("census stamp wrong: %+v", f)
|
|
|
+ }
|
|
|
+ if de[1].Facet != nil {
|
|
|
+ t.Fatal("non-parent device must stay facet-free")
|
|
|
+ }
|
|
|
+ if devices[0].Facet != nil {
|
|
|
+ t.Fatal("inputs must not be rewritten (shared backing array, #1378)")
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ t.Run("offline agent folds with the down flag, no ghost box", func(t *testing.T) {
|
|
|
+ routers := []Router{facetRouter("rke2-legion", "RKE2", "offline")}
|
|
|
+ devices := []Device{facetDevice("legion", macLegion)}
|
|
|
+ ro, de := foldFacetRouters(routers, devices, ovs, nil)
|
|
|
+ if len(ro) != 0 {
|
|
|
+ t.Fatalf("offline facet must NOT resurrect the box, got %v", ro)
|
|
|
+ }
|
|
|
+ if de[0].Facet == nil || de[0].Facet.Online {
|
|
|
+ t.Fatalf("facet must report agent down: %+v", de[0].Facet)
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ t.Run("missing parent device keeps the box", func(t *testing.T) {
|
|
|
+ routers := []Router{facetRouter("rke2-legion", "RKE2", "online")}
|
|
|
+ devices := []Device{facetDevice("other", "AA:BB:CC:DD:EE:FF")}
|
|
|
+ ro, de := foldFacetRouters(routers, devices, ovs, census)
|
|
|
+ if len(ro) != 1 {
|
|
|
+ t.Fatal("a facet whose host is absent must keep its box (never swallow a row)")
|
|
|
+ }
|
|
|
+ if de[0].Facet != nil {
|
|
|
+ t.Fatal("no fold without the parent")
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ t.Run("label falls back to the agent row name", func(t *testing.T) {
|
|
|
+ ov := TopologyOverride{MAC: "rke2-legion", Kind: "facet", Parent: macLegion, Enabled: true}
|
|
|
+ routers := []Router{facetRouter("rke2-legion", "RKE2", "online")}
|
|
|
+ devices := []Device{facetDevice("legion", macLegion)}
|
|
|
+ _, de := foldFacetRouters(routers, devices, []TopologyOverride{ov}, nil)
|
|
|
+ if de[0].Facet == nil || de[0].Facet.Label != "RKE2" {
|
|
|
+ t.Fatalf("override without a name must borrow the row's display name: %+v", de[0].Facet)
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ t.Run("disabled facet and other kinds are inert", func(t *testing.T) {
|
|
|
+ off := facet
|
|
|
+ off.Enabled = false
|
|
|
+ attach := TopologyOverride{MAC: "rke2-legion", Kind: "attach", Parent: macLegion, Enabled: true}
|
|
|
+ routers := []Router{facetRouter("rke2-legion", "RKE2", "online")}
|
|
|
+ devices := []Device{facetDevice("legion", macLegion)}
|
|
|
+ ro, de := foldFacetRouters(routers, devices, []TopologyOverride{off, attach}, census)
|
|
|
+ if len(ro) != 1 || de[0].Facet != nil {
|
|
|
+ t.Fatal("only enabled facet rows fold")
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ t.Run("router-list-only fold (nil devices)", func(t *testing.T) {
|
|
|
+ routers := []Router{facetRouter("gw", "gw", "online"), facetRouter("rke2-legion", "RKE2", "online")}
|
|
|
+ ro, de := foldFacetRouters(routers, nil, ovs, census)
|
|
|
+ if len(ro) != 1 || ro[0].ID != "gw" {
|
|
|
+ t.Fatalf("nil-device fold must drop the faceted row, got %v", ro)
|
|
|
+ }
|
|
|
+ if de != nil {
|
|
|
+ t.Fatal("nil devices stay nil")
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ t.Run("workload items ride the facet, capped with the needle preserved", func(t *testing.T) {
|
|
|
+ // 100 running + 1 STOPPED at the tail of the census: a naive
|
|
|
+ // head-trim would drop the needle past the cap and the canvas
|
|
|
+ // would lie ("all green").
|
|
|
+ items := make([]probe.WorkloadItem, 0, 101)
|
|
|
+ for i := range 100 {
|
|
|
+ items = append(items, probe.WorkloadItem{ID: fmt.Sprintf("p%d", i), Name: fmt.Sprintf("pod-%d", i), Kind: "pod", State: "running"})
|
|
|
+ }
|
|
|
+ items = append(items, probe.WorkloadItem{ID: "p100", Name: "kube-flannel-x", Kind: "pod", State: "stopped"})
|
|
|
+ c := func(string) ([]probe.WorkloadItem, int, bool) { return items, len(items), true }
|
|
|
+
|
|
|
+ routers := []Router{facetRouter("rke2-legion", "RKE2", "online")}
|
|
|
+ devices := []Device{facetDevice("legion", macLegion)}
|
|
|
+ _, de := foldFacetRouters(routers, devices, ovs, c)
|
|
|
+ f := de[0].Facet
|
|
|
+ if f == nil || !f.WorkloadsKnown || f.Workloads != 101 {
|
|
|
+ t.Fatalf("true total must survive the cap: %+v", f)
|
|
|
+ }
|
|
|
+ if len(f.WorkloadItems) != facetWorkloadItemCap {
|
|
|
+ t.Fatalf("items must be capped at %d, got %d", facetWorkloadItemCap, len(f.WorkloadItems))
|
|
|
+ }
|
|
|
+ var stopped int
|
|
|
+ for _, w := range f.WorkloadItems {
|
|
|
+ if w.State == "stopped" {
|
|
|
+ stopped++
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if stopped != 1 {
|
|
|
+ t.Fatal("the not-running needle must always survive the cap")
|
|
|
+ }
|
|
|
+ // The sample must not alias the polled backing array (#1378).
|
|
|
+ items[0].Name = "MUTATED"
|
|
|
+ if f.WorkloadItems[0].Name == "MUTATED" && f.WorkloadItems[0].ID == "p0" {
|
|
|
+ // first slot is now a running pod p0 (order: stopped first?
|
|
|
+ // no: stopped is appended after? verify order contract below)
|
|
|
+ t.Log("sample slot 0 aliases? check copy", f.WorkloadItems[0])
|
|
|
+ }
|
|
|
+ for _, w := range f.WorkloadItems {
|
|
|
+ if w.Name == "MUTATED" {
|
|
|
+ t.Fatal("sample must be a copy, not an alias of live state")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ t.Run("small census ships whole", func(t *testing.T) {
|
|
|
+ items := []probe.WorkloadItem{{ID: "a", Name: "a", State: "running"}, {ID: "b", Name: "b", State: "stopped"}}
|
|
|
+ c := func(string) ([]probe.WorkloadItem, int, bool) { return items, len(items), true }
|
|
|
+ routers := []Router{facetRouter("rke2-legion", "RKE2", "online")}
|
|
|
+ devices := []Device{facetDevice("legion", macLegion)}
|
|
|
+ _, de := foldFacetRouters(routers, devices, ovs, c)
|
|
|
+ if len(de[0].Facet.WorkloadItems) != 2 {
|
|
|
+ t.Fatalf("census under the cap must ship whole, got %v", de[0].Facet.WorkloadItems)
|
|
|
+ }
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+// #2236 freshness applied to the facet fold (gnacho on #2233): both census
|
|
|
+// readers must report ok=false past the 15-minute TTL, so a dead pusher
|
|
|
+// keeps its offline badge but stops painting counts and CT chips.
|
|
|
+func TestCensusOfLastPolledFreshness(t *testing.T) {
|
|
|
+ l := NewLive(nil, nil, nil, nil)
|
|
|
+ now := time.Now().UnixMilli()
|
|
|
+ items := []probe.WorkloadItem{{ID: "a", Name: "a", State: "running"}}
|
|
|
+ l.lastPolled["fresh"] = &routerPolled{polledAt: now - 60_000, workloads: &probe.Workloads{Items: items}}
|
|
|
+ l.lastPolled["stale"] = &routerPolled{polledAt: now - int64(16*time.Minute/time.Millisecond), workloads: &probe.Workloads{Items: items}}
|
|
|
+ l.lastPolled["zero-stamp"] = &routerPolled{workloads: &probe.Workloads{Items: items}}
|
|
|
+
|
|
|
+ if _, n, ok := l.censusOfLastPolled("fresh"); !ok || n != 1 {
|
|
|
+ t.Fatalf("fresh census must report ok with its count, got ok=%v n=%d", ok, n)
|
|
|
+ }
|
|
|
+ if _, n, ok := l.censusOfLastPolled("stale"); ok || n != 0 {
|
|
|
+ t.Fatalf("census past the TTL must report ok=false, got ok=%v n=%d", ok, n)
|
|
|
+ }
|
|
|
+ if _, _, ok := l.censusOfLastPolled("zero-stamp"); ok {
|
|
|
+ t.Fatal("a never-polled zero stamp is a fossil, not a fresh census")
|
|
|
+ }
|
|
|
+ if _, _, ok := l.censusOfLastPolled("missing"); ok {
|
|
|
+ t.Fatal("missing agent must report ok=false")
|
|
|
+ }
|
|
|
+}
|