switchporttraffic_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package adapters
  2. import (
  3. "math"
  4. "testing"
  5. )
  6. // applySnmpSwitchPortTraffic raises a switch client's traffic to its FDB port's
  7. // byte-counter rate (switch-local flows the gateway's per-MAC nlbwmon misses),
  8. // splits a port across its clients, and never lowers an existing higher figure.
  9. func TestApplySnmpSwitchPortTraffic(t *testing.T) {
  10. polled := map[string]*routerPolled{
  11. "sw": {
  12. cfg: RouterConfig{ID: "sw", SnmpEnabled: true},
  13. ports: []EthPort{
  14. {ID: "snmp-3", Label: "1/g3", RxBps: 14e6, TxBps: 0.14e6},
  15. {ID: "snmp-4", Label: "1/g4", RxBps: 5e6, TxBps: 0},
  16. },
  17. fdb: map[string]string{
  18. "AA:01": "1/g3",
  19. "AA:02": "1/g3",
  20. "AA:03": "1/g4",
  21. "AA:04": "1/g3",
  22. },
  23. },
  24. }
  25. devices := []Device{
  26. // Two cameras sharing port 1/g3 (14.14 Mbps total → 7.07 each). Lowercase
  27. // MACs also prove the FDB uppercased-key lookup.
  28. {MAC: "aa:01", Online: true, RouterID: "sw", TrafficMbps: 0.0008},
  29. {MAC: "aa:02", Online: true, RouterID: "sw", TrafficMbps: 0},
  30. // One camera on 1/g4 (5 Mbps); its existing 20 must not be lowered.
  31. {MAC: "aa:03", Online: true, RouterID: "sw", TrafficMbps: 20},
  32. // Offline client is ignored (not counted toward the port split).
  33. {MAC: "aa:04", Online: false, RouterID: "sw", TrafficMbps: 0},
  34. }
  35. l := &Live{}
  36. l.applySnmpSwitchPortTraffic(polled, devices)
  37. if d := devices[0]; math.Abs(d.TrafficMbps-7.07) > 0.01 {
  38. t.Fatalf("AA:01 = %.4f, want ~7.07 (port 14.14 split over 2 online)", d.TrafficMbps)
  39. }
  40. if d := devices[1]; math.Abs(d.TrafficMbps-7.07) > 0.01 {
  41. t.Fatalf("AA:02 = %.4f, want ~7.07", d.TrafficMbps)
  42. }
  43. if d := devices[2]; d.TrafficMbps != 20 {
  44. t.Fatalf("AA:03 = %.4f, want 20 (never lowered from a higher existing figure)", d.TrafficMbps)
  45. }
  46. if d := devices[3]; d.TrafficMbps != 0 {
  47. t.Fatalf("offline AA:04 = %.4f, want 0", d.TrafficMbps)
  48. }
  49. }
  50. // Non-SNMP routers are untouched: a client on an OpenWrt/agent router keeps
  51. // whatever per-MAC rate it had (no switch port to attribute).
  52. func TestApplySnmpSwitchPortTrafficSkipsAgentRouters(t *testing.T) {
  53. polled := map[string]*routerPolled{
  54. "gw": {
  55. cfg: RouterConfig{ID: "gw"}, // SnmpEnabled false
  56. ports: []EthPort{{ID: "lan1", Label: "LAN 1", RxBps: 100e6}},
  57. fdb: map[string]string{"BB:01": "lan1"},
  58. },
  59. }
  60. devices := []Device{{MAC: "BB:01", Online: true, RouterID: "gw", TrafficMbps: 1}}
  61. l := &Live{}
  62. l.applySnmpSwitchPortTraffic(polled, devices)
  63. if devices[0].TrafficMbps != 1 {
  64. t.Fatalf("agent-router client changed to %.4f, want 1 (helper is SNMP-only)", devices[0].TrafficMbps)
  65. }
  66. }