| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- // lldp_test.go — parser de `lldpcli -f json show neighbors` (fixture
- // portado de adapters/lldp_test.go al mover el parser a probe, #489) y
- // comportamiento de la sonda probeLldp con runner fake.
- package probe
- import (
- "context"
- "encoding/json"
- "reflect"
- "strings"
- "testing"
- )
- // Fixture realista (lldpd 1.0.x en OpenWrt): switch gestionado GS308E con
- // mgmt-ip (array) y caps Bridge, un AP anunciándose (mgmt-ip como string
- // suelto), un equipo sin mgmt-ip ni descr de puerto.
- const lldpcliFixture = `{
- "lldp": {
- "interface": [
- {
- "name": "lan3",
- "via": "LLDP",
- "rid": "2",
- "age": "0 day, 00:01:12",
- "chassis": {
- "GS308E": {
- "id": {"type": "mac", "value": "28:c6:8e:1d:90:44"},
- "descr": "Netgear GS308E 8-Port Gigabit Smart Managed Plus Switch",
- "mgmt-ip": ["192.168.8.13", "fe80::2ac6:8eff:fe1d:9044"],
- "capability": [
- {"type": "Bridge", "enabled": true},
- {"type": "Router", "enabled": false}
- ]
- }
- },
- "port": {
- "id": {"type": "ifname", "value": "ge5"},
- "descr": "ge5",
- "ttl": "120"
- }
- },
- {
- "name": "lan1",
- "chassis": {
- "AP-patio": {
- "id": {"type": "mac", "value": "aa:bb:cc:dd:ee:ff"},
- "mgmt-ip": "192.168.8.20"
- }
- },
- "port": {"id": {"type": "local", "value": "0.2"}}
- },
- {
- "name": "lan4",
- "chassis": {
- " Equipo-sin-nombre ": {
- "id": {"type": "mac", "value": "11:22:33:44:55:66"}
- }
- }
- }
- ]
- }
- }`
- func TestParseLldpNeighborsFixture(t *testing.T) {
- nbs, err := ParseLldpNeighbors([]byte(lldpcliFixture))
- if err != nil {
- t.Fatalf("parse: %v", err)
- }
- if len(nbs) != 3 {
- t.Fatalf("vecinos: %d", len(nbs))
- }
- // Orden estable: el array llega lan3, lan1, lan4
- got := nbs[0]
- if got.Port != "lan3" || got.Chassis != "GS308E" || got.ChassisMac != "28:C6:8E:1D:90:44" {
- t.Fatalf("n[0]: %+v", got)
- }
- if got.Mgmt != "192.168.8.13" {
- t.Fatalf("mgmt: %q", got.Mgmt)
- }
- if !reflect.DeepEqual(got.Caps, []string{"Bridge"}) {
- t.Fatalf("caps: %v", got.Caps)
- }
- if got.PortDesc != "ge5" {
- t.Fatalf("portDesc: %q", got.PortDesc)
- }
- // mgmt-ip como string suelto + port descr ausente → id del puerto
- if nbs[1].Mgmt != "192.168.8.20" || nbs[1].PortDesc != "0.2" {
- t.Fatalf("n[1]: %+v", nbs[1])
- }
- // sin mgmt-ip ni port
- if nbs[2].Mgmt != "" || nbs[2].PortDesc != "" {
- t.Fatalf("n[2]: %+v", nbs[2])
- }
- }
- func TestParseLldpNeighborsDefensivo(t *testing.T) {
- // Raíz no JSON → error (el caller reintenta; NO es lldpd ausente)
- if _, err := ParseLldpNeighbors([]byte("lldpd is not running")); err == nil {
- t.Fatal("no JSON debería dar error")
- }
- // Formas vacías → sin vecinos, sin error
- for _, in := range []string{`{}`, `{"lldp":{}}`, `{"lldp":{"interface":null}}`, `{"lldp":{"interface":[]}}`, `null`} {
- neighbors, err := ParseLldpNeighbors([]byte(in))
- if err != nil || len(neighbors) != 0 {
- t.Fatalf("%s → %v %v", in, neighbors, err)
- }
- }
- // Forma mapa (lldpd viejo): {"interface": {"lan3": {...}}}; la CLAVE es
- // el puerto local.
- mapForm := `{"lldp":{"interface":{"lan3":{"chassis":{"sw1":{"id":{"type":"mac","value":"AA:BB:CC:DD:EE:FF"}}}}}}}`
- neighbors, err := ParseLldpNeighbors([]byte(mapForm))
- if err != nil || len(neighbors) != 1 {
- t.Fatalf("forma mapa: %v %v", neighbors, err)
- }
- if neighbors[0].Port != "lan3" || neighbors[0].ChassisMac != "AA:BB:CC:DD:EE:FF" || neighbors[0].Chassis != "sw1" {
- t.Fatalf("forma mapa parseada: %+v", neighbors[0])
- }
- // Tipos inesperados por todas partes: nunca panic, campos vacíos
- weird := `{"lldp":{"interface":[
- {"name":"lan1","chassis":"una cadena","port":123},
- {"name":"lan2","chassis":{"x":{"id":"no-objeto","mgmt-ip":42,"capability":"no-array"}},"port":{"id":{}}},
- 42,
- null
- ]}}`
- neighbors, err = ParseLldpNeighbors([]byte(weird))
- if err != nil {
- t.Fatal(err)
- }
- if len(neighbors) != 4 {
- t.Fatalf("entradas raras: %d", len(neighbors))
- }
- if neighbors[0].Port != "lan1" || neighbors[0].Chassis != "" || neighbors[0].PortDesc != "" {
- t.Fatalf("chassis cadena: %+v", neighbors[0])
- }
- if neighbors[1].Port != "lan2" || neighbors[1].ChassisMac != "" || neighbors[1].Mgmt != "" {
- t.Fatalf("subcampos raros: %+v", neighbors[1])
- }
- if neighbors[2].Port != "" || neighbors[2].Chassis != "" || neighbors[2].Caps != nil {
- t.Fatalf("entrada no-objeto: %+v", neighbors[2])
- }
- if neighbors[3].Port != "" || neighbors[3].ChassisMac != "" {
- t.Fatalf("entrada null: %+v", neighbors[3])
- }
- // interface con forma ni array ni mapa → sin datos, no error
- neighbors, err = ParseLldpNeighbors([]byte(`{"lldp":{"interface":42}}`))
- if err != nil || len(neighbors) != 0 {
- t.Fatalf("interface escalar: %v %v", neighbors, err)
- }
- }
- // Fixture real de lldpd 1.0.20 (OpenWrt 25.12, #1370): el array de interface
- // lleva objetos de UNA clave con el nombre de la interfaz, sin campo "name".
- const lldpcliKeyedArrayFixture = `{
- "lldp": {
- "interface": [
- {
- "lan1": {
- "via": "LLDP",
- "rid": "1",
- "age": "0 day, 00:06:34",
- "chassis": {
- "core-switch": {
- "id": {"type": "mac", "value": "78:d8:00:32:31:49"},
- "descr": "Managed switch",
- "mgmt-ip": ["192.168.8.6"],
- "capability": [
- {"type": "Bridge", "enabled": true},
- {"type": "Router", "enabled": false}
- ]
- }
- },
- "port": {
- "id": {"type": "ifname", "value": "3"},
- "ttl": "120"
- }
- }
- },
- {
- "lan1": {
- "via": "LLDP",
- "rid": "2",
- "age": "0 day, 00:00:59",
- "chassis": {
- "ap-atico": {
- "id": {"type": "mac", "value": "9c:9d:7e:54:ef:b8"},
- "capability": [
- {"type": "Wlan", "enabled": true}
- ]
- }
- },
- "port": {
- "id": {"type": "mac", "value": "9c:9d:7e:54:ef:b8"},
- "descr": "lan1",
- "ttl": "120"
- }
- }
- }
- ]
- }
- }`
- func TestParseLldpNeighborsKeyedArray(t *testing.T) {
- nbs, err := ParseLldpNeighbors([]byte(lldpcliKeyedArrayFixture))
- if err != nil {
- t.Fatalf("parse: %v", err)
- }
- if len(nbs) != 2 {
- t.Fatalf("vecinos: %d", len(nbs))
- }
- got := nbs[0]
- if got.Port != "lan1" || got.Chassis != "core-switch" || got.ChassisMac != "78:D8:00:32:31:49" {
- t.Fatalf("n[0]: %+v", got)
- }
- if got.Mgmt != "192.168.8.6" || !reflect.DeepEqual(got.Caps, []string{"Bridge"}) {
- t.Fatalf("n[0] mgmt/caps: %+v", got)
- }
- got = nbs[1]
- if got.Port != "lan1" || got.Chassis != "ap-atico" || got.ChassisMac != "9C:9D:7E:54:EF:B8" || got.PortDesc != "lan1" {
- t.Fatalf("n[1]: %+v", got)
- }
- }
- // TestProberLldp: contrato de disponibilidad de la sonda (#489).
- func TestProberLldp(t *testing.T) {
- ctx := context.Background()
- // lldpd NO instalado: command -v falla → Available=false.
- p := NewProber(fakeRunner{outs: map[string]string{}}, Options{})
- ld := p.probeLldp(ctx)
- if ld == nil || ld.Available || ld.Neighbors != nil {
- t.Fatalf("sin lldpd: %+v", ld)
- }
- // Instalado con vecinos: fixture completo.
- p = NewProber(fakeRunner{outs: map[string]string{
- CmdLldpCheck: "/usr/sbin/lldpcli\n",
- CmdLldpNeighbors: lldpcliFixture,
- }}, Options{})
- ld = p.probeLldp(ctx)
- if ld == nil || !ld.Available || len(ld.Neighbors) != 3 {
- t.Fatalf("con vecinos: %+v", ld)
- }
- // Instalado, lldpd parado: check OK pero lldpcli sin salida →
- // Available=true con Neighbors nil (sonda fallida, no "no instalado").
- p = NewProber(fakeRunner{outs: map[string]string{
- CmdLldpCheck: "/usr/sbin/lldpcli\n",
- }}, Options{})
- ld = p.probeLldp(ctx)
- if ld == nil || !ld.Available || ld.Neighbors != nil {
- t.Fatalf("parado: %+v", ld)
- }
- // Instalado, cero vecinos: interface [] → vacío honesto, no nil.
- p = NewProber(fakeRunner{outs: map[string]string{
- CmdLldpCheck: "/usr/sbin/lldpcli\n",
- CmdLldpNeighbors: `{"lldp":{"interface":[]}}`,
- }}, Options{})
- ld = p.probeLldp(ctx)
- if ld == nil || !ld.Available || ld.Neighbors == nil || len(ld.Neighbors) != 0 {
- t.Fatalf("cero vecinos: %+v", ld)
- }
- }
- // TestPayloadLldpWire: nil vs vacío viaja distinto en el JSON (contrato del
- // anti-parpadeo del server). Sin omitempty en Neighbors a propósito.
- func TestPayloadLldpWire(t *testing.T) {
- mustJSON := func(v any) string {
- b, err := json.Marshal(v)
- if err != nil {
- t.Fatalf("marshal: %v", err)
- }
- return string(b)
- }
- // Sección presente con vecinos
- if s := mustJSON(PayloadData{Lldp: &LldpData{Available: true, Neighbors: []LldpNeighbor{{Port: "lan3"}}}}); !strings.Contains(s, `"neighbors":[{`) {
- t.Fatalf("wire con vecinos: %s", s)
- }
- // Sección presente, sonda fallida: neighbors null (no ausente)
- if s := mustJSON(PayloadData{Lldp: &LldpData{Available: true}}); !strings.Contains(s, `"neighbors":null`) {
- t.Fatalf("wire sonda fallida: %s", s)
- }
- // Sección ausente (push event-driven): nada de lldp en el JSON
- if s := mustJSON(PayloadData{}); strings.Contains(s, "lldp") {
- t.Fatalf("wire sin sección: %s", s)
- }
- // Disponible sin vecinos (vacío honesto): "neighbors":[]
- if s := mustJSON(PayloadData{Lldp: &LldpData{Available: true, Neighbors: []LldpNeighbor{}}}); !strings.Contains(s, `"neighbors":[]`) {
- t.Fatalf("wire vacío honesto: %s", s)
- }
- }
- // #1369: el hint bridge-only sale solo cuando el binding UCI es SOLO bridge.
- func TestLldpdBridgeOnly(t *testing.T) {
- cases := []struct {
- ifaces string
- want bool
- }{
- {"", false}, // sin opción UCI: lldpd escucha en físicas
- {"br-lan", true}, // el caso del issue (stock qualcommax)
- {"br-lan,br-guest", true}, // varios bridges: igual de ciego
- {"lan*,wan", false}, // el fix aplicado en la flota
- {"lan1", false}, // puerto físico suelto
- {"br-lan,lan1", false}, // mixto: ya escucha en físicas
- {" lan1 , wan ", false}, // espacios tolerados
- {"eth0", false}, // Linux genérico con nombre eth
- {",,,", false}, // tokens vacíos: sin binding efectivo
- }
- for _, c := range cases {
- if got := LldpdBridgeOnly(c.ifaces); got != c.want {
- t.Errorf("LldpdBridgeOnly(%q) = %v, want %v", c.ifaces, got, c.want)
- }
- }
- }
|