netrate_2048_test.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package adapters
  2. import (
  3. "testing"
  4. "github.com/gnacho/netpulse/agent/probe"
  5. )
  6. func rate(rx, tx float64) probe.IfRate {
  7. return probe.IfRate{RxBps: netFptr(rx), TxBps: netFptr(tx)}
  8. }
  9. // #2048: el agregado de todas las bocas cuenta la bajada dos veces (entra por
  10. // la WAN y sale por la LAN). Con la WAN identificada se publica la línea.
  11. func TestPickRouterNetPrefersTheWan(t *testing.T) {
  12. agg := &NetDevBps{RxBps: netFptr(70.6e6), TxBps: netFptr(69.4e6)}
  13. rates := map[string]probe.IfRate{
  14. "eth1": rate(70.6e6, 8.1e6), // WAN: 70 de bajada, 8 de subida
  15. "eth0": rate(69.4e6, 70.5e6), // LAN: lo que el router reenvía
  16. }
  17. got := pickRouterNet(agg, "eth1", rates)
  18. if got.RxBps == nil || got.TxBps == nil {
  19. t.Fatal("sin tasas")
  20. }
  21. if *got.RxBps != 70.6e6 || *got.TxBps != 8.1e6 {
  22. t.Fatalf("tasa = (%v,%v); want la de la WAN (70.6,8.1) Mbps", *got.RxBps, *got.TxBps)
  23. }
  24. if *got.TxBps == *agg.TxBps {
  25. t.Fatal("se está publicando la subida del agregado, que es el doble conteo")
  26. }
  27. }
  28. // Una WAN declarada que no aparece en /proc/net/dev (pppoe-wan, túnel) no
  29. // puede publicar ceros: se cae al agregado, que es lo que había antes.
  30. func TestPickRouterNetFallsBackWithoutWanRates(t *testing.T) {
  31. agg := &NetDevBps{RxBps: netFptr(10), TxBps: netFptr(2)}
  32. if got := pickRouterNet(agg, "pppoe-wan", map[string]probe.IfRate{}); got != agg {
  33. t.Fatal("sin tasas de la WAN debe devolver el agregado")
  34. }
  35. // Rates presentes pero sin bps calculados (primera muestra).
  36. rates := map[string]probe.IfRate{"eth1": {}}
  37. if got := pickRouterNet(agg, "eth1", rates); got != agg {
  38. t.Fatal("una primera muestra sin bps debe caer al agregado")
  39. }
  40. // Solo un lado calculado: tampoco sirve.
  41. half := map[string]probe.IfRate{"eth1": {RxBps: netFptr(5)}}
  42. if got := pickRouterNet(agg, "eth1", half); got != agg {
  43. t.Fatal("con un solo lado no se publica media tasa")
  44. }
  45. }
  46. // AP y unidades sin WAN: el agregado sigue siendo el dato bueno.
  47. func TestPickRouterNetWithoutWan(t *testing.T) {
  48. agg := &NetDevBps{RxBps: netFptr(3), TxBps: netFptr(1)}
  49. if got := pickRouterNet(agg, "", map[string]probe.IfRate{"eth0": rate(9, 9)}); got != agg {
  50. t.Fatal("sin WAN identificable se mantiene el agregado")
  51. }
  52. if got := pickRouterNet(nil, "", nil); got == nil {
  53. t.Fatal("nunca se devuelve nil")
  54. }
  55. // Agregado ausente pero WAN con tasas: se publica la WAN igualmente.
  56. got := pickRouterNet(nil, "eth1", map[string]probe.IfRate{"eth1": rate(7, 1)})
  57. if got.RxBps == nil || *got.RxBps != 7 {
  58. t.Fatalf("sin agregado y con WAN válida = %+v; want la WAN", got)
  59. }
  60. }
  61. func netFptr(v float64) *float64 { return &v }