vlan_names_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package adapters
  2. // FORK: #1757 - the VLAN numbers from `bridge vlan show` get the names LuCI
  3. // keeps for them.
  4. import (
  5. "testing"
  6. "github.com/gnacho/netpulse/agent/probe"
  7. )
  8. func TestVlanPortsCarryTheLuCINames(t *testing.T) {
  9. ports := []VlanPort{
  10. {Port: "lan1", Vlans: []VlanEntry{{ID: 1, PVID: true}, {ID: 20, Tagged: true}}},
  11. {Port: "lan2", Vlans: []VlanEntry{{ID: 30}}},
  12. }
  13. luci := &probe.LuCILabels{VlanLabels: map[string]string{"1": "LAN", "20": "IoT"}}
  14. got := vlanPortsWithNames(ports, luci)
  15. if len(got) != 2 || len(got[0].Vlans) != 2 {
  16. t.Fatalf("shape: %+v", got)
  17. }
  18. if got[0].Vlans[0].Name != "LAN" || got[0].Vlans[1].Name != "IoT" {
  19. t.Errorf("names: %+v", got[0].Vlans)
  20. }
  21. // A VLAN without a label keeps no name: an invented one would be worse
  22. // than the number alone.
  23. if got[1].Vlans[0].Name != "" {
  24. t.Errorf("unlabelled VLAN got %q", got[1].Vlans[0].Name)
  25. }
  26. // The flags and the id survive the copy.
  27. if !got[0].Vlans[0].PVID || !got[0].Vlans[1].Tagged || got[0].Vlans[1].ID != 20 {
  28. t.Errorf("flags: %+v", got[0].Vlans)
  29. }
  30. // The payload the registry holds is not written to.
  31. if ports[0].Vlans[0].Name != "" {
  32. t.Fatalf("the payload was mutated: %+v", ports[0].Vlans)
  33. }
  34. // Without labels there is nothing to join, so the same slice comes back.
  35. if same := vlanPortsWithNames(ports, nil); &same[0] != &ports[0] {
  36. t.Error("no labels: a copy was made for nothing")
  37. }
  38. if same := vlanPortsWithNames(ports, &probe.LuCILabels{}); &same[0] != &ports[0] {
  39. t.Error("empty labels: a copy was made for nothing")
  40. }
  41. }