netrate_2048_test.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package probe
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. )
  7. // seqNetRunner devuelve una salida distinta de /proc/net/dev en cada llamada y
  8. // vacío para el resto de comandos.
  9. type seqNetRunner struct {
  10. netdev []string
  11. i int
  12. }
  13. func (r *seqNetRunner) Run(_ context.Context, cmd string, _ time.Duration) (string, error) {
  14. if cmd != CmdNetDev {
  15. return "", &fakeErr{cmd}
  16. }
  17. if r.i >= len(r.netdev) {
  18. return r.netdev[len(r.netdev)-1], nil
  19. }
  20. out := r.netdev[r.i]
  21. r.i++
  22. return out, nil
  23. }
  24. // #2048: el agregado de todas las bocas cuenta la bajada dos veces (entra por
  25. // la WAN y sale por la LAN). Cuando el equipo sabe cuál es su WAN, la tasa
  26. // publicada es la suya.
  27. func TestSystemRatesUseTheWanPort(t *testing.T) {
  28. // eth0 = LAN (reenvía la bajada), eth1 = WAN.
  29. first := " eth0: 1000000 0 0 0 0 0 0 0 500000 0 0 0 0 0 0 0\n" +
  30. " eth1: 1000000 0 0 0 0 0 0 0 100000 0 0 0 0 0 0 0\n"
  31. second := " eth0: 2000000 0 0 0 0 0 0 0 1000000 0 0 0 0 0 0 0\n" +
  32. " eth1: 3000000 0 0 0 0 0 0 0 110000 0 0 0 0 0 0 0\n"
  33. p := NewProber(&seqNetRunner{netdev: []string{first, second}}, Options{})
  34. p.lastWan = &WanInfo{Port: "eth1"}
  35. p.probeSystem(context.Background()) // abre la ventana
  36. sd := p.probeSystem(context.Background())
  37. if sd.RxBps == nil || sd.TxBps == nil {
  38. t.Fatal("sin tasas en la segunda muestra")
  39. }
  40. // WAN: rx +2e6, tx +1e4 → tx/rx = 0,005. Agregado: rx +3e6, tx +5e5 → 0,17.
  41. if *sd.TxBps > *sd.RxBps/50 {
  42. t.Fatalf("se publica el agregado: tx/rx = %v (WAN esperada ~0,005)", *sd.TxBps / *sd.RxBps)
  43. }
  44. }
  45. // Un AP sin WAN identificable sigue publicando el agregado, que ahí son dos
  46. // direcciones reales y no un doble conteo.
  47. func TestSystemRatesKeepTheAggregateWithoutWan(t *testing.T) {
  48. first := " eth0: 1000000 0 0 0 0 0 0 0 500000 0 0 0 0 0 0 0\n"
  49. second := " eth0: 2000000 0 0 0 0 0 0 0 1000000 0 0 0 0 0 0 0\n"
  50. p := NewProber(&seqNetRunner{netdev: []string{first, second}}, Options{})
  51. p.probeSystem(context.Background())
  52. sd := p.probeSystem(context.Background())
  53. if sd.RxBps == nil || sd.TxBps == nil {
  54. t.Fatal("sin tasas en la segunda muestra")
  55. }
  56. // Agregado de una boca: rx y tx crecen en la misma proporción (1e6 y 5e5).
  57. if *sd.TxBps < *sd.RxBps/4 {
  58. t.Fatalf("el agregado de una boca debería conservarse: tx/rx = %v", *sd.TxBps / *sd.RxBps)
  59. }
  60. }
  61. // Una WAN declarada que no está en /proc/net/dev no puede dejar la tasa a cero.
  62. func TestSystemRatesFallBackWhenWanPortMissing(t *testing.T) {
  63. first := " eth0: 1000000 0 0 0 0 0 0 0 500000 0 0 0 0 0 0 0\n"
  64. second := " eth0: 2000000 0 0 0 0 0 0 0 1000000 0 0 0 0 0 0 0\n"
  65. p := NewProber(&seqNetRunner{netdev: []string{first, second}}, Options{})
  66. p.lastWan = &WanInfo{Port: "pppoe-wan"}
  67. p.probeSystem(context.Background())
  68. sd := p.probeSystem(context.Background())
  69. if sd.RxBps == nil || *sd.RxBps == 0 {
  70. t.Fatalf("sin la boca WAN debe caer al agregado, sd=%+v", sd)
  71. }
  72. }