snmp_merge_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package adapters
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. // #1726: SNMP como segunda fuente en unidades con pusher. Rellena lo que el
  7. // payload no trae y no pisa nada de lo que sí viene.
  8. func TestApplySNMPSurfaceFillsOnlyWhatPayloadLacks(t *testing.T) {
  9. entry := snmpSurfaceEntry{
  10. ok: true,
  11. byIface: map[string]EthPort{
  12. "sfp-sfpplus1": {Iface: "sfp-sfpplus1", Up: true, Speed: "10 Gbps", RxErrs: 5, TxErrs: 3},
  13. "ether1": {Iface: "ether1", Up: true, Speed: "1 Gbps", RxErrs: 9, TxErrs: 0},
  14. },
  15. lldp: []LldpNeighbor{{Port: "sfp-sfpplus1", Chassis: "XS708T"}},
  16. }
  17. p := &routerPolled{
  18. ports: []EthPort{
  19. // El pusher de RouterOS no da velocidad de enlace: se rellena.
  20. {ID: "sfp-sfpplus1", Iface: "sfp-sfpplus1", Up: true},
  21. // Aquí el payload sí trae velocidad y un contador de errores: no se pisa.
  22. {ID: "ether1", Iface: "ether1", Up: true, Speed: "1 Gbps", RxErrs: 2},
  23. },
  24. lldpUnavailable: true,
  25. }
  26. applySNMPSurface(p, entry)
  27. if got := p.ports[0].Speed; got != "10 Gbps" {
  28. t.Fatalf("velocidad de la boca sin dato: quiero %q, tengo %q", "10 Gbps", got)
  29. }
  30. if p.ports[0].RxErrs != 5 || p.ports[0].TxErrs != 3 {
  31. t.Fatalf("contadores de error: quiero 5/3, tengo %d/%d", p.ports[0].RxErrs, p.ports[0].TxErrs)
  32. }
  33. if got := p.ports[1].Speed; got != "1 Gbps" {
  34. t.Fatalf("no debe pisar la velocidad del payload: tengo %q", got)
  35. }
  36. if p.ports[1].RxErrs != 2 {
  37. t.Fatalf("no debe pisar errores del payload: tengo %d", p.ports[1].RxErrs)
  38. }
  39. if len(p.lldp) != 1 || p.lldp[0].Chassis != "XS708T" {
  40. t.Fatalf("los vecinos LLDP de SNMP deben entrar cuando el payload no trae: %+v", p.lldp)
  41. }
  42. if p.lldpUnavailable {
  43. t.Fatal("con vecinos LLDP la sonda no debe quedar marcada como no disponible")
  44. }
  45. }
  46. func TestApplySNMPSurfaceIgnoresDownLinkSpeed(t *testing.T) {
  47. entry := snmpSurfaceEntry{
  48. ok: true,
  49. byIface: map[string]EthPort{
  50. // ifHighSpeed=0 en un puerto down: no es una velocidad.
  51. "sfp-sfpplus3": {Iface: "sfp-sfpplus3", Speed: ""},
  52. },
  53. }
  54. p := &routerPolled{ports: []EthPort{{ID: "sfp-sfpplus3", Iface: "sfp-sfpplus3"}}}
  55. applySNMPSurface(p, entry)
  56. if got := p.ports[0].Speed; got != "" {
  57. t.Fatalf("una boca sin enlace no debe heredar velocidad: tengo %q", got)
  58. }
  59. }
  60. func TestApplySNMPSurfaceKeepsPayloadLldp(t *testing.T) {
  61. entry := snmpSurfaceEntry{
  62. ok: true,
  63. byIface: map[string]EthPort{},
  64. lldp: []LldpNeighbor{{Port: "sfp-sfpplus1", Chassis: "desde-snmp"}},
  65. }
  66. p := &routerPolled{lldp: []LldpNeighbor{{Port: "sfp-sfpplus1", Chassis: "desde-payload"}}}
  67. applySNMPSurface(p, entry)
  68. if len(p.lldp) != 1 || p.lldp[0].Chassis != "desde-payload" {
  69. t.Fatalf("los vecinos del payload mandan: %+v", p.lldp)
  70. }
  71. }
  72. // Una entrada fresca en caché se aplica sin volver a sondear SNMP: el host de
  73. // prueba es inalcanzable, así que un sondeo real dejaría el puerto sin datos.
  74. func TestMergeSNMPSurfaceUsesFreshCache(t *testing.T) {
  75. l := NewLive(nil, nil, nil, nil)
  76. cfg := RouterConfig{ID: "crs305", Host: "127.0.0.1", SnmpEnabled: true, SnmpPort: 1}
  77. l.snmpSurface[cfg.ID] = snmpSurfaceEntry{
  78. at: time.Now(),
  79. ok: true,
  80. byIface: map[string]EthPort{"sfp-sfpplus1": {Iface: "sfp-sfpplus1", Speed: "10 Gbps"}},
  81. }
  82. p := &routerPolled{cfg: cfg, ports: []EthPort{{ID: "sfp-sfpplus1", Iface: "sfp-sfpplus1"}}}
  83. l.mergeSNMPSurface(cfg, p)
  84. if got := p.ports[0].Speed; got != "10 Gbps" {
  85. t.Fatalf("la caché fresca debe aplicarse sin sondear: tengo %q", got)
  86. }
  87. }
  88. // Sin snmp_enabled no se toca nada ni se siembra la caché.
  89. func TestMergeSNMPSurfaceSkipsWhenDisabled(t *testing.T) {
  90. l := NewLive(nil, nil, nil, nil)
  91. cfg := RouterConfig{ID: "crs305", Host: "127.0.0.1", SnmpPort: 1}
  92. p := &routerPolled{cfg: cfg, ports: []EthPort{{ID: "sfp-sfpplus1", Iface: "sfp-sfpplus1"}}}
  93. l.mergeSNMPSurface(cfg, p)
  94. if _, ok := l.snmpSurface[cfg.ID]; ok {
  95. t.Fatal("sin snmp_enabled no debe sondearse ni cachearse nada")
  96. }
  97. if p.ports[0].Speed != "" {
  98. t.Fatalf("sin SNMP la boca debe quedar como vino: %q", p.ports[0].Speed)
  99. }
  100. }