topology_lldp_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. package adapters
  2. import (
  3. "reflect"
  4. "testing"
  5. )
  6. // Tests de la fase LLDP-first (issue #300): los vecinos LLDP son ground truth
  7. // de los enlaces de infraestructura (router↔switch y switch↔switch), con el
  8. // FDB como fallback cuando no hay lldpd.
  9. const (
  10. macGS308E = "28:C6:8E:1D:90:44"
  11. macLGS352 = "AA:BB:CC:DD:EE:0A"
  12. )
  13. // distSummary es la forma canónica de comparación de un DistributionNode en
  14. // los tests: solo los campos que la inferencia debe fijar.
  15. type distSummary struct {
  16. id, kind, routerID, port, parent, mac, name, ip string
  17. macCount int
  18. }
  19. func summarizeDists(dists []DistributionNode) []distSummary {
  20. if len(dists) == 0 {
  21. return nil
  22. }
  23. out := make([]distSummary, 0, len(dists))
  24. for _, d := range dists {
  25. out = append(out, distSummary{
  26. id: d.ID, kind: d.Kind, routerID: d.RouterID, port: d.Port,
  27. parent: d.Parent, mac: d.Mac, name: d.Name, ip: d.Ip,
  28. macCount: d.MacCount,
  29. })
  30. }
  31. return out
  32. }
  33. func TestInferTopologyLldpGroundTruth(t *testing.T) {
  34. tests := []struct {
  35. name string
  36. polled map[string]*routerPolled
  37. devices []Device
  38. want []distSummary
  39. }{
  40. {
  41. // Gateway → SwitchA (GS308E) → SwitchB (LGS352C): cadena LLDP.
  42. // SwitchA es un router managed-switch sondeado (con lldp); su
  43. // distnode cuelga del gateway y el de SwitchB cuelga de SwitchA.
  44. // La MAC del propio SwitchA está excluida del FDB del gateway
  45. // (es su bridge MAC), así que el matching es por identidad.
  46. name: "cadena switch→switch",
  47. polled: map[string]*routerPolled{
  48. "flint2": {cfg: RouterConfig{ID: "flint2", IsGateway: true}, brMac: "94:83:C4:00:00:01",
  49. fdb: map[string]string{
  50. macGS308E: "lan3", "04:D4:C4:8B:30:A7": "lan3", "DC:A6:32:4F:77:02": "lan3",
  51. },
  52. lldp: []LldpNeighbor{{Port: "lan3", Chassis: "GS308E", ChassisMac: macGS308E, Mgmt: "192.168.8.13", Caps: []string{"Bridge"}, PortDesc: "ge5"}},
  53. },
  54. "swA": {cfg: RouterConfig{ID: "swA", Name: "GS308E", Type: "managed-switch", AgentOnly: true}, brMac: macGS308E,
  55. fdb: map[string]string{
  56. macLGS352: "ge5", "10:20:30:40:50:60": "ge5",
  57. },
  58. lldp: []LldpNeighbor{{Port: "ge5", Chassis: "LGS352C", ChassisMac: macLGS352, Mgmt: "192.168.8.20", Caps: []string{"Bridge"}, PortDesc: "ge1"}},
  59. },
  60. },
  61. devices: []Device{
  62. dev("04:D4:C4:8B:30:A7", "flint2", "cable"),
  63. dev("DC:A6:32:4F:77:02", "flint2", "cable"),
  64. dev(macLGS352, "swA", "cable"),
  65. dev("10:20:30:40:50:60", "swA", "cable"),
  66. },
  67. want: []distSummary{
  68. {id: "dist-flint2-lan3", kind: "managed", routerID: "flint2", port: "lan3", parent: "", mac: macGS308E, name: "GS308E", ip: "192.168.8.13", macCount: 2},
  69. {id: "dist-swA-ge5", kind: "managed", routerID: "swA", port: "ge5", parent: "dist-flint2-lan3", mac: macLGS352, name: "LGS352C", ip: "192.168.8.20", macCount: 2},
  70. },
  71. },
  72. {
  73. // Switch identificado SOLO por LLDP (chassis-MAC en el FDB, sin
  74. // router managed-switch en config): nodo managed sin Parent.
  75. name: "switch identificado por LLDP",
  76. polled: map[string]*routerPolled{
  77. "flint2": {cfg: RouterConfig{ID: "flint2", IsGateway: true}, brMac: "94:83:C4:00:00:01",
  78. fdb: map[string]string{
  79. macGS308E: "lan3", "04:D4:C4:8B:30:A7": "lan3",
  80. },
  81. lldp: []LldpNeighbor{{Port: "lan3", Chassis: "GS308E", ChassisMac: macGS308E, Mgmt: "192.168.8.13", Caps: []string{"Bridge"}, PortDesc: "ge5"}},
  82. },
  83. },
  84. devices: []Device{
  85. dev(macGS308E, "flint2", "cable"),
  86. dev("04:D4:C4:8B:30:A7", "flint2", "cable"),
  87. },
  88. want: []distSummary{
  89. {id: "dist-flint2-lan3", kind: "managed", routerID: "flint2", port: "lan3", parent: "", mac: macGS308E, name: "GS308E", ip: "192.168.8.13", macCount: 2},
  90. },
  91. },
  92. {
  93. // Un vecino LLDP que es un ROUTER regular (openwrt) no genera nodo
  94. // managed: es un enlace router↔router (uplink), no un switch.
  95. name: "router regular no genera nodo managed",
  96. polled: map[string]*routerPolled{
  97. "flint2": {cfg: RouterConfig{ID: "flint2", IsGateway: true}, brMac: "94:83:C4:00:00:01",
  98. fdb: map[string]string{
  99. "04:D4:C4:8B:30:A7": "lan1", "DC:A6:32:4F:77:02": "lan1",
  100. },
  101. lldp: []LldpNeighbor{{Port: "lan1", Chassis: "Living", ChassisMac: "94:83:C4:00:00:02", Mgmt: "192.168.8.2", Caps: []string{"Bridge", "Router"}, PortDesc: "lan3"}},
  102. },
  103. "living": {cfg: RouterConfig{ID: "living", Type: "openwrt"}, brMac: "94:83:C4:00:00:02", fdb: map[string]string{}},
  104. },
  105. devices: []Device{
  106. dev("04:D4:C4:8B:30:A7", "flint2", "cable"),
  107. dev("DC:A6:32:4F:77:02", "flint2", "cable"),
  108. },
  109. want: nil,
  110. },
  111. {
  112. // Sin LLDP (lldpd ausente): el FDB sigue siendo el fallback → nodo
  113. // "inferred" exactamente como antes.
  114. name: "FDB fallback sin LLDP",
  115. polled: map[string]*routerPolled{
  116. "flint2": {cfg: RouterConfig{ID: "flint2", IsGateway: true}, brMac: "94:83:C4:00:00:01",
  117. fdb: map[string]string{
  118. "78:2B:CB:AA:01:01": "lan3", "8C:EA:48:AA:02:02": "lan3",
  119. }},
  120. },
  121. devices: []Device{
  122. dev("78:2B:CB:AA:01:01", "flint2", "cable"),
  123. dev("8C:EA:48:AA:02:02", "flint2", "cable"),
  124. },
  125. want: []distSummary{
  126. {id: "dist-flint2-lan3", kind: "inferred", routerID: "flint2", port: "lan3", macCount: 2},
  127. },
  128. },
  129. {
  130. // Hipervisor con LLDP: la evidencia de hipervisor manda sobre la de
  131. // LLDP (no se convierte en nodo managed).
  132. name: "hipervisor prevalece sobre LLDP",
  133. polled: map[string]*routerPolled{
  134. "flint2": {cfg: RouterConfig{ID: "flint2", IsGateway: true}, brMac: "94:83:C4:00:00:01",
  135. fdb: map[string]string{
  136. "3C:52:82:10:20:30": "lan2", "BC:24:11:00:20:10": "lan2", "BC:24:11:00:20:11": "lan2",
  137. },
  138. lldp: []LldpNeighbor{{Port: "lan2", Chassis: "pve", ChassisMac: "3C:52:82:10:20:30", Caps: []string{"Station"}}},
  139. },
  140. },
  141. devices: []Device{
  142. dev("3C:52:82:10:20:30", "flint2", "cable"),
  143. dev("BC:24:11:00:20:10", "flint2", "cable"),
  144. dev("BC:24:11:00:20:11", "flint2", "cable"),
  145. },
  146. want: []distSummary{
  147. {id: "dist-flint2-lan2", kind: "hypervisor", routerID: "flint2", port: "lan2", macCount: 3},
  148. },
  149. },
  150. }
  151. for _, tt := range tests {
  152. t.Run(tt.name, func(t *testing.T) {
  153. _, dists := inferTopology(tt.polled, tt.devices)
  154. got := summarizeDists(dists)
  155. if !reflect.DeepEqual(got, tt.want) {
  156. t.Fatalf("dists:\n got: %+v\nwant: %+v", got, tt.want)
  157. }
  158. })
  159. }
  160. }
  161. // El distnode de un switch LLDP encadena a sus clientes FDB y sella al propio
  162. // switch como managed-switch (D1/D2), también en una cadena switch→switch.
  163. func TestInferTopologyLldpCadenaClientes(t *testing.T) {
  164. polled := map[string]*routerPolled{
  165. "flint2": {cfg: RouterConfig{ID: "flint2", IsGateway: true}, brMac: "94:83:C4:00:00:01",
  166. fdb: map[string]string{
  167. macGS308E: "lan3", "04:D4:C4:8B:30:A7": "lan3", "DC:A6:32:4F:77:02": "lan3",
  168. },
  169. lldp: []LldpNeighbor{{Port: "lan3", Chassis: "GS308E", ChassisMac: macGS308E, Mgmt: "192.168.8.13", Caps: []string{"Bridge"}, PortDesc: "ge5"}},
  170. },
  171. "swA": {cfg: RouterConfig{ID: "swA", Name: "GS308E", Type: "managed-switch", AgentOnly: true}, brMac: macGS308E,
  172. fdb: map[string]string{
  173. macLGS352: "ge5", "10:20:30:40:50:60": "ge5",
  174. },
  175. lldp: []LldpNeighbor{{Port: "ge5", Chassis: "LGS352C", ChassisMac: macLGS352, Mgmt: "192.168.8.20", Caps: []string{"Bridge"}, PortDesc: "ge1"}},
  176. },
  177. }
  178. devices := []Device{
  179. dev("04:D4:C4:8B:30:A7", "flint2", "cable"),
  180. dev("DC:A6:32:4F:77:02", "flint2", "cable"),
  181. dev(macLGS352, "swA", "cable"),
  182. dev("10:20:30:40:50:60", "swA", "cable"),
  183. }
  184. devices, dists := inferTopology(polled, devices)
  185. if len(dists) != 2 {
  186. t.Fatalf("distnodes: %+v", dists)
  187. }
  188. // Los PC tras el GS308E cuelgan del nodo del GS308E.
  189. if devices[0].AttachTo != "dist-flint2-lan3" || devices[1].AttachTo != "dist-flint2-lan3" {
  190. t.Fatalf("PCs tras GS308E: %+v %+v", devices[0], devices[1])
  191. }
  192. // El PC tras el LGS352C cuelga del nodo del LGS352C (segundo eslabón).
  193. if devices[3].AttachTo != "dist-swA-ge5" {
  194. t.Fatalf("PC tras LGS352C: %+v", devices[3])
  195. }
  196. // El LGS352C (switch B) queda sellado managed-switch; los clientes no.
  197. if devices[2].Infra != "managed-switch" {
  198. t.Fatalf("sellado del LGS352C: %+v", devices[2])
  199. }
  200. if devices[0].Infra != "" || devices[1].Infra != "" || devices[3].Infra != "" {
  201. t.Fatalf("clientes sin infra: %+v %+v %+v", devices[0], devices[1], devices[3])
  202. }
  203. }
  204. // BuildTopoSemantics: la cadena switch→switch emite un enlace dist distnode→
  205. // distnode (From = Parent), además del router→raíz.
  206. func TestTopoSemanticsCadenaLldp(t *testing.T) {
  207. routers := []Router{
  208. {ID: "flint2", Name: "Gateway", RoleBadge: "Principal", Status: "online"},
  209. {ID: "swA", Name: "GS308E", RoleBadge: "SW", Status: "online"},
  210. }
  211. dists := []DistributionNode{
  212. {ID: "dist-flint2-lan3", Kind: "managed", RouterID: "flint2", Port: "lan3", Mac: macGS308E},
  213. {ID: "dist-swA-ge5", Kind: "managed", RouterID: "swA", Port: "ge5", Parent: "dist-flint2-lan3", Mac: macLGS352},
  214. }
  215. devices := []Device{
  216. {RouterID: "flint2", Band: "cable", Port: "lan3", ID: "pc1", MAC: "04:D4:C4:8B:30:A7", AttachTo: "dist-flint2-lan3", Online: true},
  217. {RouterID: "swA", Band: "cable", Port: "ge5", ID: "pc2", MAC: "DC:A6:32:4F:77:02", AttachTo: "dist-swA-ge5", Online: true},
  218. }
  219. sem := BuildTopoSemantics(routers, devices, WireGuardStats{}, dists, "", nil, nil, nil)
  220. var gotDist []TopoLink
  221. for _, l := range sem.Links {
  222. if l.Kind == "dist" {
  223. gotDist = append(gotDist, l)
  224. }
  225. }
  226. wantDist := []TopoLink{
  227. {From: "flint2", To: "dist-flint2-lan3", Kind: "dist", Port: "lan3"},
  228. {From: "dist-flint2-lan3", To: "dist-swA-ge5", Kind: "dist", Port: "ge5"},
  229. }
  230. if !reflect.DeepEqual(gotDist, wantDist) {
  231. t.Fatalf("dist links:\n got: %+v\nwant: %+v", gotDist, wantDist)
  232. }
  233. }
  234. // #1325: un switch gestionado (distnode managed) con un puerto NO-uplink que
  235. // aprende >= 2 clientes cableados -> nodo inferido hoja anclado al switch
  236. // (Parent). El uplink (root port), los puertos ya resueltos por LLDP y las
  237. // MACs de la propia flota no generan nodo hoja. Los clientes se atribuyen al
  238. // gateway (DHCP), no al switch: la fase FDB (keptMacsForPort) no los veía por
  239. // su filtro de RouterID; la nueva fase usa leafSwitchClients, sin ese filtro.
  240. func TestInferTopologySwitchHojaTrasGestionado(t *testing.T) {
  241. gw := &routerPolled{cfg: RouterConfig{ID: "flint2", IsGateway: true}, brMac: "94:83:C4:00:00:01",
  242. fdb: map[string]string{
  243. macGS308E: "lan3", "04:D4:C4:8B:30:A7": "lan3", "DC:A6:32:4F:77:02": "lan3",
  244. },
  245. lldp: []LldpNeighbor{{Port: "lan3", Chassis: "GS308E", ChassisMac: macGS308E, Mgmt: "192.168.8.13", Caps: []string{"Bridge"}, PortDesc: "ge5"}},
  246. }
  247. gwDevices := []Device{
  248. dev("04:D4:C4:8B:30:A7", "flint2", "cable"),
  249. dev("DC:A6:32:4F:77:02", "flint2", "cable"),
  250. }
  251. swCfg := RouterConfig{ID: "swA", Name: "GS308E", Type: "managed-switch", AgentOnly: true}
  252. managedRoot := distSummary{id: "dist-flint2-lan3", kind: "managed", routerID: "flint2", port: "lan3", mac: macGS308E, name: "GS308E", ip: "192.168.8.13", macCount: 2}
  253. tests := []struct {
  254. name string
  255. swFdb map[string]string
  256. swLldp []LldpNeighbor
  257. devices []Device
  258. want []distSummary
  259. attachTo map[string]string
  260. }{
  261. {
  262. name: "hoja en puerto no-uplink",
  263. swFdb: map[string]string{"94:83:C4:00:00:01": "ge1", "10:20:30:40:50:60": "ge3", "10:20:30:40:50:61": "ge3"},
  264. devices: []Device{
  265. dev("10:20:30:40:50:60", "flint2", "cable"),
  266. dev("10:20:30:40:50:61", "flint2", "cable"),
  267. },
  268. want: []distSummary{
  269. managedRoot,
  270. {id: "dist-swA-ge3", kind: "inferred", routerID: "swA", port: "ge3", parent: "dist-flint2-lan3", macCount: 2},
  271. },
  272. attachTo: map[string]string{
  273. "10:20:30:40:50:60": "dist-swA-ge3",
  274. "10:20:30:40:50:61": "dist-swA-ge3",
  275. },
  276. },
  277. {
  278. // El uplink (root port) también aprende clientes en tránsito, pero
  279. // es el puerto hacia el núcleo: no es un switch hoja.
  280. name: "uplink del switch no genera hoja",
  281. swFdb: map[string]string{"94:83:C4:00:00:01": "ge1", "10:20:30:40:50:60": "ge1", "10:20:30:40:50:61": "ge1"},
  282. devices: []Device{
  283. dev("10:20:30:40:50:60", "flint2", "cable"),
  284. dev("10:20:30:40:50:61", "flint2", "cable"),
  285. },
  286. want: []distSummary{managedRoot},
  287. },
  288. {
  289. // El puerto ya tiene distnode managed (switch→switch por LLDP): no
  290. // se duplica con un nodo hoja.
  291. name: "puerto ya resuelto por LLDP no duplica",
  292. swFdb: map[string]string{"94:83:C4:00:00:01": "ge1", macLGS352: "ge5", "10:20:30:40:50:60": "ge5"},
  293. swLldp: []LldpNeighbor{{Port: "ge5", Chassis: "LGS352C", ChassisMac: macLGS352, Mgmt: "192.168.8.20", Caps: []string{"Bridge"}, PortDesc: "ge1"}},
  294. devices: []Device{
  295. dev(macLGS352, "swA", "cable"),
  296. dev("10:20:30:40:50:60", "swA", "cable"),
  297. },
  298. want: []distSummary{
  299. managedRoot,
  300. {id: "dist-swA-ge5", kind: "managed", routerID: "swA", port: "ge5", parent: "dist-flint2-lan3", mac: macLGS352, name: "LGS352C", ip: "192.168.8.20", macCount: 2},
  301. },
  302. },
  303. {
  304. // La bridge-MAC de otro router de la flota no es un cliente: con un
  305. // solo cliente cableado el umbral (>= 2) no se alcanza.
  306. name: "MACs de flota no cuentan",
  307. swFdb: map[string]string{"94:83:C4:00:00:01": "ge1", "94:83:C4:00:00:02": "ge3", "10:20:30:40:50:60": "ge3"},
  308. devices: []Device{
  309. dev("10:20:30:40:50:60", "flint2", "cable"),
  310. },
  311. want: []distSummary{managedRoot},
  312. },
  313. }
  314. for _, tt := range tests {
  315. t.Run(tt.name, func(t *testing.T) {
  316. polled := map[string]*routerPolled{
  317. "flint2": gw,
  318. "swA": {cfg: swCfg, brMac: macGS308E, fdb: tt.swFdb, lldp: tt.swLldp},
  319. }
  320. if tt.name == "MACs de flota no cuentan" {
  321. polled["ap1"] = &routerPolled{cfg: RouterConfig{ID: "ap1"}, brMac: "94:83:C4:00:00:02", fdb: map[string]string{}}
  322. }
  323. devices := append(append([]Device{}, gwDevices...), tt.devices...)
  324. gotDevices, dists := inferTopology(polled, devices)
  325. got := summarizeDists(dists)
  326. if !reflect.DeepEqual(got, tt.want) {
  327. t.Fatalf("dists:\n got: %+v\nwant: %+v", got, tt.want)
  328. }
  329. for mac, wantAttach := range tt.attachTo {
  330. found := false
  331. for _, d := range gotDevices {
  332. if d.MAC != mac {
  333. continue
  334. }
  335. found = true
  336. if d.AttachTo != wantAttach {
  337. t.Fatalf("%s AttachTo = %q, want %q", mac, d.AttachTo, wantAttach)
  338. }
  339. }
  340. if !found {
  341. t.Fatalf("%s no aparece en los devices", mac)
  342. }
  343. }
  344. })
  345. }
  346. }