enrich_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package adapters
  2. import "testing"
  3. // #1150: con switches encadenados (gateway -> sw1 -> sw2) la boca de una
  4. // unidad aprende las MACs de bridge de VARIAS unidades, y la primera MAC
  5. // ordenada puede nombrar al switch LEJANO. El vecino LLDP anuncia al par
  6. // DIRECTO: su chassis MAC (unidad conocida) o su sysName manda.
  7. func TestEnrichEthPortRouterLinkPrefersLldpNeighbor(t *testing.T) {
  8. deps := portEnrichDeps{
  9. routerByMac: map[string]string{
  10. "AA:AA:AA:00:00:02": "sw2", // ordena ANTES que sw1: el bug la elegía
  11. "BB:BB:BB:00:00:01": "sw1",
  12. },
  13. portMacs: map[string][]string{
  14. "lan1": {"AA:AA:AA:00:00:02", "BB:BB:BB:00:00:01"},
  15. },
  16. lldp: []LldpNeighbor{
  17. {Port: "lan1", ChassisMac: "bb:bb:bb:00:00:01", Mgmt: "192.168.1.11"},
  18. },
  19. }
  20. port := EthPort{ID: "lan1", Label: "lan1", Up: true}
  21. got := enrichEthPort(port, "lan1", deps)
  22. if got.ConnectedTo != "sw1" {
  23. t.Fatalf("ConnectedTo = %q, want sw1 (vecino directo por LLDP)", got.ConnectedTo)
  24. }
  25. if got.PeerKind != "router-link-lldp" {
  26. t.Fatalf("PeerKind = %q, want router-link-lldp", got.PeerKind)
  27. }
  28. if got.DeviceMac != "BB:BB:BB:00:00:01" {
  29. t.Fatalf("DeviceMac = %q, want la MAC del chassis vecino", got.DeviceMac)
  30. }
  31. if got.Detail != "192.168.1.11 · LLDP" {
  32. t.Fatalf("Detail = %q, want mgmt-ip · LLDP", got.Detail)
  33. }
  34. }
  35. // #1976: sin LLDP y con dos unidades visibles en la misma boca no hay forma
  36. // de saber cuál está enfrente. Antes se nombraba la primera MAC ordenada
  37. // (sw2 por orden alfabético) y el mapa enseñaba un cable que no existe. Sin
  38. // datos para desambiguar, la boca se queda sin par.
  39. func TestEnrichEthPortRouterLinkAmbiguousStaysEmpty(t *testing.T) {
  40. deps := portEnrichDeps{
  41. routerByMac: map[string]string{
  42. "AA:AA:AA:00:00:02": "sw2",
  43. "BB:BB:BB:00:00:01": "sw1",
  44. },
  45. portMacs: map[string][]string{
  46. "lan1": {"AA:AA:AA:00:00:02", "BB:BB:BB:00:00:01"},
  47. },
  48. }
  49. port := EthPort{ID: "lan1", Label: "lan1", Up: true}
  50. got := enrichEthPort(port, "lan1", deps)
  51. if got.ConnectedTo != "" || got.PeerKind != "" || got.DeviceMac != "" {
  52. t.Fatalf("boca ambigua con par inventado: %+v", got)
  53. }
  54. if got.Detail != "" {
  55. t.Fatalf("Detail = %q, want empty", got.Detail)
  56. }
  57. }
  58. // #1976: con el FDB de las candidatas sí se puede desambiguar. sw1 aprende a
  59. // test-gw en un puerto dedicado; sw2 lo aprende en el uplink que también
  60. // lleva a sw1. El par directo es sw1.
  61. func TestEnrichEthPortRouterLinkPicksDedicatedPort(t *testing.T) {
  62. const (
  63. self = "CC:CC:CC:00:00:01"
  64. sw1 = "BB:BB:BB:00:00:01"
  65. sw2 = "AA:AA:AA:00:00:02"
  66. )
  67. deps := portEnrichDeps{
  68. routerByMac: map[string]string{sw2: "sw2", sw1: "sw1"},
  69. portMacs: map[string][]string{"eth0": {sw2, sw1}},
  70. unitByMac: map[string]string{sw1: "sw1", sw2: "sw2"},
  71. selfBrMac: self,
  72. fdbByUnit: map[string]map[string]string{
  73. // Puerto dedicado: solo test-gw.
  74. "sw1": {self: "port1"},
  75. // Uplink hacia sw1: lleva a test-gw y a sw1 a la vez.
  76. "sw2": {self: "uplink", sw1: "uplink"},
  77. },
  78. }
  79. got := enrichEthPort(EthPort{ID: "eth0", Label: "ETH 0", Up: true}, "eth0", deps)
  80. if got.ConnectedTo != "sw1" {
  81. t.Fatalf("ConnectedTo = %q, want sw1 (puerto dedicado)", got.ConnectedTo)
  82. }
  83. if got.DeviceMac != sw1 || got.PeerKind != "router-link" {
  84. t.Fatalf("par = %q %q, want %s router-link", got.ConnectedTo, got.PeerKind, sw1)
  85. }
  86. }
  87. // Si las dos candidatas lo aprenden en un puerto dedicado, el empate no se
  88. // rompe adivinando.
  89. func TestEnrichEthPortRouterLinkTieStaysEmpty(t *testing.T) {
  90. const (
  91. self = "CC:CC:CC:00:00:01"
  92. sw1 = "BB:BB:BB:00:00:01"
  93. sw2 = "AA:AA:AA:00:00:02"
  94. )
  95. deps := portEnrichDeps{
  96. routerByMac: map[string]string{sw2: "sw2", sw1: "sw1"},
  97. portMacs: map[string][]string{"eth0": {sw2, sw1}},
  98. unitByMac: map[string]string{sw1: "sw1", sw2: "sw2"},
  99. selfBrMac: self,
  100. fdbByUnit: map[string]map[string]string{
  101. "sw1": {self: "port1"},
  102. "sw2": {self: "port2"},
  103. },
  104. }
  105. got := enrichEthPort(EthPort{ID: "eth0", Label: "ETH 0", Up: true}, "eth0", deps)
  106. if got.ConnectedTo != "" || got.PeerKind != "" {
  107. t.Fatalf("empate resuelto a ciegas: %+v", got)
  108. }
  109. }
  110. // El vecino LLDP no es una unidad conocida (chassis sin match): manda su
  111. // sysName anunciado sobre la MAC ordenada.
  112. func TestEnrichEthPortRouterLinkLldpUnknownChassis(t *testing.T) {
  113. deps := portEnrichDeps{
  114. routerByMac: map[string]string{
  115. "AA:AA:AA:00:00:02": "sw2",
  116. },
  117. portMacs: map[string][]string{
  118. "eth5": {"AA:AA:AA:00:00:02"},
  119. },
  120. lldp: []LldpNeighbor{
  121. {Port: "eth5", Chassis: "downstream-sw", Mgmt: "10.0.0.9"},
  122. },
  123. }
  124. port := EthPort{ID: "eth5", Label: "eth5", Up: true}
  125. got := enrichEthPort(port, "eth5", deps)
  126. if got.ConnectedTo != "downstream-sw" {
  127. t.Fatalf("ConnectedTo = %q, want downstream-sw (sysName LLDP)", got.ConnectedTo)
  128. }
  129. if got.Detail != "10.0.0.9 · LLDP" {
  130. t.Fatalf("Detail = %q, want mgmt-ip · LLDP", got.Detail)
  131. }
  132. }