virtual_ports_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package adapters
  2. import (
  3. "testing"
  4. "github.com/gnacho/netpulse/agent/probe"
  5. )
  6. func TestAppendVirtualPortsListsWhatNetIfCarries(t *testing.T) {
  7. ports := []EthPort{{ID: "eth0", Iface: "eth0", Label: "WAN", Up: true}}
  8. ifaces := map[string]probe.IfCounters{
  9. "eth0": {Rx: 1000, Tx: 2000},
  10. "br-lan": {Rx: 500, Tx: 700},
  11. "vlan10": {Rx: 0, Tx: 0},
  12. "wg0": {Rx: 10, Tx: 20},
  13. "lo": {Rx: 999999, Tx: 999999},
  14. "br-wan2": {Rx: 1, Tx: 0},
  15. }
  16. rxBps, txBps := 800.0, 200.0
  17. rates := map[string]probe.IfRate{
  18. "br-lan": {IfCounters: ifaces["br-lan"], RxBps: &rxBps, TxBps: &txBps},
  19. }
  20. out := appendVirtualPorts(ports, ifaces, rates)
  21. if len(out) != 5 {
  22. t.Fatalf("expected 4 physical plus 4 virtual ports, got %d: %+v", len(out), out)
  23. }
  24. // The physical port is left as it was and not duplicated.
  25. if out[0].ID != "eth0" || out[0].Label != "WAN" || !out[0].Up {
  26. t.Fatalf("physical port changed: %+v", out[0])
  27. }
  28. byName := map[string]EthPort{}
  29. for _, p := range out[1:] {
  30. byName[p.Label] = p
  31. }
  32. if _, ok := byName["lo"]; ok {
  33. t.Fatal("loopback must not be listed")
  34. }
  35. if _, ok := byName["eth0"]; ok {
  36. t.Fatal("a physical port must not be added twice")
  37. }
  38. if p := byName["br-lan"]; p.Family != "bridge" || p.ID != "if-br-lan" || p.Iface != "br-lan" {
  39. t.Fatalf("bridge port wrong: %+v", p)
  40. }
  41. if p := byName["br-lan"]; !p.Up || p.RxBps != 800 || p.TxBps != 200 {
  42. t.Fatalf("bridge traffic wrong: %+v", p)
  43. }
  44. if p := byName["vlan10"]; p.Family != "vlan" || p.Up {
  45. t.Fatalf("idle vlan should be listed and not up: %+v", p)
  46. }
  47. if p := byName["wg0"]; p.Family != "tunnel" || !p.Up {
  48. t.Fatalf("tunnel wrong: %+v", p)
  49. }
  50. // Rates only reach the interface they belong to.
  51. if p := byName["wg0"]; p.RxBps != 0 || p.TxBps != 0 {
  52. t.Fatalf("rates leaked into another interface: %+v", p)
  53. }
  54. }
  55. func TestAppendVirtualPortsWithoutNetIfChangesNothing(t *testing.T) {
  56. ports := []EthPort{{ID: "eth0", Iface: "eth0"}}
  57. if out := appendVirtualPorts(ports, nil, nil); len(out) != 1 || out[0].ID != "eth0" {
  58. t.Fatalf("expected the same ports, got %+v", out)
  59. }
  60. }
  61. func TestVirtualIfaceFamilyNamesTheFamilies(t *testing.T) {
  62. cases := map[string]string{
  63. "br-lan": "bridge",
  64. "bridge": "bridge",
  65. "vlan10": "vlan",
  66. "eth0.10": "vlan",
  67. "bond0": "lag",
  68. "lagg0": "lag",
  69. "wg0": "tunnel",
  70. "tun0": "tunnel",
  71. "pppoe-wan": "tunnel",
  72. "gre1": "tunnel",
  73. // #1757: las interfaces AP del agente (OpenWrt mac80211 y wlan<N>).
  74. "phy0-ap0": "wifi",
  75. "phy1-ap2": "wifi",
  76. "wlan0": "wifi",
  77. "eth1": "virtual",
  78. "foo0": "virtual",
  79. }
  80. for name, want := range cases {
  81. if got := virtualIfaceFamily(name); got != want {
  82. t.Errorf("virtualIfaceFamily(%q) = %q, want %q", name, got, want)
  83. }
  84. }
  85. }
  86. func TestLinkPortsKeepsOnlyPhysicalLinks(t *testing.T) {
  87. ports := []EthPort{
  88. {ID: "eth0", Iface: "eth0"},
  89. {ID: "if-br-lan", Iface: "br-lan", Family: "bridge"},
  90. {ID: "snmp-2", Iface: "eth1", Family: "ethernet"},
  91. {ID: "if-vlan10", Iface: "vlan10", Family: "vlan"},
  92. }
  93. out := linkPorts(ports)
  94. if len(out) != 2 {
  95. t.Fatalf("expected the two physical links, got %+v", out)
  96. }
  97. if out[0].ID != "eth0" || out[1].ID != "snmp-2" {
  98. t.Fatalf("wrong ports kept: %+v", out)
  99. }
  100. if same := linkPorts(out); len(same) != 2 || &same[0] != &out[0] {
  101. t.Fatal("with nothing to drop the same slice should come back")
  102. }
  103. }