|
|
@@ -6,6 +6,8 @@ import (
|
|
|
"time"
|
|
|
|
|
|
"github.com/gnacho/netpulse/server-go/internal/alerts"
|
|
|
+ "github.com/gnacho/netpulse/server-go/internal/db"
|
|
|
+ npsnmp "github.com/gnacho/netpulse/server-go/internal/snmp"
|
|
|
)
|
|
|
|
|
|
// issue #414: cuando no ha pasado el intervalo SNMP configurado, pollRouterSNMP
|
|
|
@@ -100,3 +102,83 @@ func TestPortMonitorGhostSnmpHysteresis(t *testing.T) {
|
|
|
t.Fatalf("ghost alerts after min silence = %d, want 1", n)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+// #661: el mapeo FDB no debe descartar MACs cuyo ifIndex no resuelve a un
|
|
|
+// puerto conocido (antes dejaba el switch con 0 dispositivos conectados).
|
|
|
+func TestSnmpFdbMapNoDrop(t *testing.T) {
|
|
|
+ portIdxToName := map[int]string{3: "lan3"}
|
|
|
+ fdb := []npsnmp.FdbEntry{
|
|
|
+ {MAC: "aa:bb:cc:dd:ee:01", IfIndex: 3, BridgePortIndex: 3},
|
|
|
+ {MAC: "aa:bb:cc:dd:ee:02", IfIndex: 99, BridgePortIndex: 4}, // ifIndex sin puerto → fallback
|
|
|
+ {MAC: "aa:bb:cc:dd:ee:03", IfIndex: 0, BridgePortIndex: 5}, // nada resuelve → nombre genérico
|
|
|
+ }
|
|
|
+ m := snmpFdbMap(fdb, portIdxToName)
|
|
|
+ if len(m) != 3 {
|
|
|
+ t.Fatalf("fdbMap len = %d, want 3 (no drop)", len(m))
|
|
|
+ }
|
|
|
+ if got := m["aa:bb:cc:dd:ee:01"]; got != "lan3" {
|
|
|
+ t.Errorf("ee:01 = %q, want lan3", got)
|
|
|
+ }
|
|
|
+ if got := m["aa:bb:cc:dd:ee:02"]; got != "port-99" {
|
|
|
+ t.Errorf("ee:02 = %q, want port-99 (fallback ifIndex)", got)
|
|
|
+ }
|
|
|
+ if got := m["aa:bb:cc:dd:ee:03"]; got != "port-0" {
|
|
|
+ t.Errorf("ee:03 = %q, want port-0 (generic)", got)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// #661: el agregado de red del switch SNMP es la suma de la tasa de todos los
|
|
|
+// puertos; nil si todo es 0 (primera muestra sin delta).
|
|
|
+func TestSnmpAggregateNet(t *testing.T) {
|
|
|
+ ports := []EthPort{{RxBps: 100, TxBps: 50}, {RxBps: 25, TxBps: 10}}
|
|
|
+ n := snmpAggregateNet(ports)
|
|
|
+ if n == nil {
|
|
|
+ t.Fatal("expected non-nil aggregate")
|
|
|
+ }
|
|
|
+ if *n.RxBps != 125 || *n.TxBps != 60 {
|
|
|
+ t.Errorf("aggregate = (%.0f, %.0f), want (125, 60)", *n.RxBps, *n.TxBps)
|
|
|
+ }
|
|
|
+ if got := snmpAggregateNet([]EthPort{{RxBps: 0, TxBps: 0}}); got != nil {
|
|
|
+ t.Error("expected nil when all ports at 0")
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// #661: recordPortSamples persiste los contadores de bytes de un puerto SNMP
|
|
|
+// (la ruta que antes NO se llamaba en el path SNMP → historial vacío).
|
|
|
+func TestRecordPortSamplesPersistsBytes(t *testing.T) {
|
|
|
+ d, err := db.Open(t.TempDir())
|
|
|
+ if err != nil {
|
|
|
+ t.Fatalf("db: %v", err)
|
|
|
+ }
|
|
|
+ defer d.Close()
|
|
|
+
|
|
|
+ l := NewLive(nil, d, nil, nil)
|
|
|
+ now := time.Now()
|
|
|
+ l.recordPortSamples("sw1", []EthPort{
|
|
|
+ {ID: "snmp-1", RxBytes: 1000, TxBytes: 2000, RxBps: 10, TxBps: 20, Speed: "1 Gbps"},
|
|
|
+ })
|
|
|
+
|
|
|
+ pts, err := d.PortSeries.GetSeries("sw1", "snmp-1", now.Add(-time.Minute), now.Add(time.Minute), "raw")
|
|
|
+ if err != nil {
|
|
|
+ t.Fatalf("GetSeries: %v", err)
|
|
|
+ }
|
|
|
+ if len(pts) != 1 {
|
|
|
+ t.Fatalf("points = %d, want 1", len(pts))
|
|
|
+ }
|
|
|
+ if pts[0].RxBytes != 1000 || pts[0].TxBytes != 2000 {
|
|
|
+ t.Errorf("bytes = (%d, %d), want (1000, 2000)", pts[0].RxBytes, pts[0].TxBytes)
|
|
|
+ }
|
|
|
+ if pts[0].RxBps != 10 || pts[0].TxBps != 20 {
|
|
|
+ t.Errorf("bps = (%.1f, %.1f), want (10, 20)", pts[0].RxBps, pts[0].TxBps)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// #661: el DTO del router refleja SnmpEnabled para que la UI elija bps (no fps).
|
|
|
+func TestBuildRouterExposeSnmpEnabled(t *testing.T) {
|
|
|
+ l := NewLive(nil, nil, nil, nil)
|
|
|
+ p := &routerPolled{cfg: RouterConfig{ID: "sw1", Host: "192.168.1.10", SnmpEnabled: true}}
|
|
|
+ r := l.buildRouter(p, nil)
|
|
|
+ if !r.SnmpEnabled {
|
|
|
+ t.Error("expected SnmpEnabled=true in built Router")
|
|
|
+ }
|
|
|
+}
|