snmp_lldp_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package adapters
  2. import (
  3. "context"
  4. "reflect"
  5. "testing"
  6. "time"
  7. npsnmp "github.com/gnacho/netpulse/server-go/internal/snmp"
  8. )
  9. // #931: la conversión LLDP-MIB → LldpNeighbor nombra el puerto como el FDB
  10. // (DisplayName del ifTable) para que inferLldpLinks case el vecino con las
  11. // MACs aprendidas; sin mapeo cae al bridgePort y a un nombre generado
  12. // (paridad con snmpFdbMap, #661).
  13. func TestSnmpLldpNeighborsNaming(t *testing.T) {
  14. rem := []npsnmp.LldpRemEntry{
  15. {LocalPortNum: 5, IfIndex: 5, ChassisMac: "78:55:36:02:58:BA", Chassis: "ryzen-ai", Mgmt: "192.168.1.80", Caps: []string{"Wlan"}, PortDesc: "enp195s0"},
  16. // Sin DisplayName para el ifIndex: cae al LocalPortNum si tiene nombre.
  17. {LocalPortNum: 7, IfIndex: 70, ChassisMac: "D8:EC:5E:60:C7:82", Chassis: "LGS310C-2"},
  18. // Sin nombre en ninguno de los dos: nombre generado.
  19. {LocalPortNum: 9, IfIndex: 90, Chassis: "otro"},
  20. }
  21. names := map[int]string{5: "Slot0/5", 7: "Slot0/7"}
  22. got := snmpLldpNeighbors(rem, names)
  23. if len(got) != 3 {
  24. t.Fatalf("esperaba 3 vecinos, obtuve %d", len(got))
  25. }
  26. want0 := LldpNeighbor{Port: "Slot0/5", ChassisMac: "78:55:36:02:58:BA", Chassis: "ryzen-ai", Mgmt: "192.168.1.80", Caps: []string{"Wlan"}, PortDesc: "enp195s0"}
  27. if !reflect.DeepEqual(got[0], want0) {
  28. t.Fatalf("vecino 0 inesperado:\n got %+v\nwant %+v", got[0], want0)
  29. }
  30. if got[1].Port != "Slot0/7" {
  31. t.Fatalf("vecino 1 puerto: %q, esperaba Slot0/7 (fallback a LocalPortNum)", got[1].Port)
  32. }
  33. if got[2].Port != "port-9" {
  34. t.Fatalf("vecino 2 puerto: %q, esperaba port-9 (nombre generado)", got[2].Port)
  35. }
  36. }
  37. func TestSnmpLldpNeighborsVacio(t *testing.T) {
  38. if got := snmpLldpNeighbors(nil, map[int]string{5: "Slot0/5"}); got != nil {
  39. t.Fatalf("sin vecinos debe devolver nil, obtuve %+v", got)
  40. }
  41. }
  42. // #1279 (reapertura): el flag ChassisFromLocDesc viaja del LLDP-MIB al
  43. // contrato LldpNeighbor para que la resolución de uplinks pueda excluir la
  44. // identidad sintetizada de la anotación local del admin.
  45. func TestSnmpLldpNeighborsPropagaLocDesc(t *testing.T) {
  46. rem := []npsnmp.LldpRemEntry{
  47. {LocalPortNum: 21, IfIndex: 21, Chassis: "ap1", ChassisFromLocDesc: true},
  48. {LocalPortNum: 22, IfIndex: 22, Chassis: "sw2"},
  49. }
  50. got := snmpLldpNeighbors(rem, map[int]string{21: "1/0/21", 22: "1/0/22"})
  51. if !got[0].ChassisFromLocDesc {
  52. t.Fatalf("vecino 0: el flag ChassisFromLocDesc debe propagarse: %+v", got[0])
  53. }
  54. if got[1].ChassisFromLocDesc {
  55. t.Fatalf("vecino 1: identidad remota real, flag a false: %+v", got[1])
  56. }
  57. }
  58. // #931: en una boca con UN solo equipo aprendido y sin hostname DHCP ni
  59. // alias, el sysName anunciado por LLDP es la mejor etiqueta (antes salía la
  60. // MAC o la label del puerto). La mgmt-ip viaja al detalle.
  61. func TestGetRouterDetailLldpNombraEquipoSolitario(t *testing.T) {
  62. l := NewLive(nil, nil, nil, nil)
  63. cfg := RouterConfig{ID: "sw1", Name: "LGS310C", Host: "192.168.1.217", Type: "managed-switch", SnmpEnabled: true}
  64. l.routers = []RouterConfig{cfg}
  65. l.lastPolled["sw1"] = &routerPolled{
  66. cfg: cfg,
  67. ports: []EthPort{
  68. {ID: "snmp-5", Label: "Slot0/5", Iface: "Slot0/5", Up: true, Snmp: true},
  69. {ID: "snmp-6", Label: "Slot0/6", Iface: "Slot0/6", Up: true, Snmp: true},
  70. },
  71. fdb: map[string]string{
  72. "78:55:36:02:58:BA": "Slot0/5",
  73. "AA:BB:CC:DD:EE:FF": "Slot0/6",
  74. },
  75. lldp: []LldpNeighbor{
  76. {Port: "Slot0/5", Chassis: "ryzen-ai", ChassisMac: "78:55:36:02:58:BA", Mgmt: "192.168.1.80", Caps: []string{"Wlan"}},
  77. },
  78. polledAt: time.Now().UnixMilli(),
  79. }
  80. det, err := l.GetRouterDetail(context.Background(), "sw1")
  81. if err != nil || det == nil {
  82. t.Fatalf("detalle: %v %v", det, err)
  83. }
  84. ports := map[string]EthPort{}
  85. for _, p := range det.Ports {
  86. ports[p.ID] = p
  87. }
  88. p5 := ports["snmp-5"]
  89. if p5.ConnectedTo != "ryzen-ai" {
  90. t.Fatalf("snmp-5 connectedTo: %q, esperaba ryzen-ai (LLDP sysName)", p5.ConnectedTo)
  91. }
  92. if p5.Detail != "192.168.1.80 · LLDP" {
  93. t.Fatalf("snmp-5 detail: %q, esperaba mgmt-ip · LLDP", p5.Detail)
  94. }
  95. if p5.DeviceMac != "78:55:36:02:58:BA" {
  96. t.Fatalf("snmp-5 deviceMac: %q", p5.DeviceMac)
  97. }
  98. // Puerto sin LLDP: comportamiento previo intacto (cae a la label).
  99. p6 := ports["snmp-6"]
  100. if p6.ConnectedTo != "Slot0/6" {
  101. t.Fatalf("snmp-6 connectedTo: %q, esperaba Slot0/6 (sin LLDP, sin cambios)", p6.ConnectedTo)
  102. }
  103. }
  104. // #931: un hostname DHCP sigue ganando al sysName LLDP (precedencia
  105. // lease > LLDP > MAC).
  106. func TestGetRouterDetailLldpNoPisaHostnameDhcp(t *testing.T) {
  107. l := NewLive(nil, nil, nil, nil)
  108. cfg := RouterConfig{ID: "sw1", Name: "LGS310C", Host: "192.168.1.217", Type: "managed-switch", SnmpEnabled: true}
  109. l.routers = []RouterConfig{cfg}
  110. l.lastPolled["sw1"] = &routerPolled{
  111. cfg: cfg,
  112. ports: []EthPort{
  113. {ID: "snmp-5", Label: "Slot0/5", Iface: "Slot0/5", Up: true, Snmp: true},
  114. },
  115. fdb: map[string]string{"78:55:36:02:58:BA": "Slot0/5"},
  116. lldp: []LldpNeighbor{{Port: "Slot0/5", Chassis: "ryzen-ai", Mgmt: "192.168.1.80"}},
  117. leases: []DhcpLease{{MAC: "78:55:36:02:58:BA", Hostname: "ryzen9AI", IP: "192.168.1.80"}},
  118. polledAt: time.Now().UnixMilli(),
  119. }
  120. det, err := l.GetRouterDetail(context.Background(), "sw1")
  121. if err != nil || det == nil {
  122. t.Fatalf("detalle: %v %v", det, err)
  123. }
  124. for _, p := range det.Ports {
  125. if p.ID == "snmp-5" && p.ConnectedTo != "ryzen9AI" {
  126. t.Fatalf("connectedTo: %q, esperaba ryzen9AI (hostname DHCP gana a LLDP)", p.ConnectedTo)
  127. }
  128. }
  129. }