openwrt_wireless_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package adapters
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. "github.com/gnacho/netpulse/agent/probe"
  7. )
  8. // fakeWirelessRunner es un poolRunner que responde por comando exacto y
  9. // registra las llamadas para poder afirmar qué se ejecutó (y qué NO).
  10. type fakeWirelessRunner struct {
  11. calls []string
  12. responses map[string]string
  13. errs map[string]error
  14. }
  15. func (f *fakeWirelessRunner) Run(host, cmd string, timeout time.Duration) (string, error) {
  16. f.calls = append(f.calls, cmd)
  17. if err := f.errs[cmd]; err != nil {
  18. return "", err
  19. }
  20. return f.responses[cmd], nil
  21. }
  22. func (f *fakeWirelessRunner) RunCtx(ctx context.Context, host, cmd string, timeout time.Duration) (string, error) {
  23. return f.Run(host, cmd, timeout)
  24. }
  25. func (f *fakeWirelessRunner) ran(cmd string) bool {
  26. for _, c := range f.calls {
  27. if c == cmd {
  28. return true
  29. }
  30. }
  31. return false
  32. }
  33. const hostapdTwoAPOut = `==AP==hostapd.phy0-ap0
  34. {
  35. "freq": 2437,
  36. "clients": {
  37. "AA:BB:CC:DD:EE:FF": { "auth": true, "assoc": true, "authorized": true, "signal": -55 }
  38. }
  39. }
  40. ==AP==hostapd.phy1-ap0
  41. {
  42. "freq": 5180,
  43. "clients": {
  44. "AA:BB:CC:DD:EE:01": { "auth": true, "assoc": true, "authorized": true, "signal": -42 }
  45. }
  46. }`
  47. // ubus-first (#373 en SSH): con objetos hostapd ubus, GetWirelessClients
  48. // devuelve los clientes SIN ejecutar iwinfo (ni combined ni assoclist).
  49. func TestGetWirelessClientsUbusFirstNoIwinfo(t *testing.T) {
  50. r := &fakeWirelessRunner{responses: map[string]string{probe.CmdHostapdClients: hostapdTwoAPOut}}
  51. c := &OpenWrtClient{Host: "192.168.1.9", pool: r}
  52. m := c.GetWirelessClients()
  53. if len(m) != 2 {
  54. t.Fatalf("clientes: %d (want 2)", len(m))
  55. }
  56. if r.ran(probe.CmdWirelessCombined) || r.ran(probe.CmdIwinfoAssoc) {
  57. t.Fatalf("ubus-first tocó iwinfo: %v", r.calls)
  58. }
  59. }
  60. // AP vacío con hostapd ubus: 0 clientes es un resultado REAL, no debe caer al
  61. // fallback iwinfo en cada poll de 5 s (el bug de CPUs justas #368).
  62. func TestGetWirelessClientsUbusEmptyAPNoIwinfo(t *testing.T) {
  63. empty := `==AP==hostapd.phy0-ap0
  64. {
  65. "freq": 2437,
  66. "clients": {}
  67. }`
  68. r := &fakeWirelessRunner{responses: map[string]string{probe.CmdHostapdClients: empty}}
  69. c := &OpenWrtClient{Host: "192.168.1.9", pool: r}
  70. m := c.GetWirelessClients()
  71. if len(m) != 0 {
  72. t.Fatalf("clientes: %+v (want 0)", m)
  73. }
  74. if r.ran(probe.CmdWirelessCombined) || r.ran(probe.CmdIwinfoAssoc) {
  75. t.Fatalf("AP vacío con ubus tocó iwinfo: %v", r.calls)
  76. }
  77. }
  78. // Sin objetos hostapd ubus (p.ej. wpad básico): cae al iwinfo combinado en UNA
  79. // pasada, que además puebla la caché de radios para GetRadios.
  80. func TestGetWirelessClientsFallbackCombined(t *testing.T) {
  81. combined := "R|2.437|6|HE20|22|2\nC|AA:BB:CC:DD:EE:FF|-55|2.437\n"
  82. r := &fakeWirelessRunner{responses: map[string]string{
  83. probe.CmdHostapdClients: "",
  84. probe.CmdWirelessCombined: combined,
  85. probe.CmdIwinfoAssoc: "AA:BB:CC:DD:EE:FF -55 2.4\n",
  86. }}
  87. c := &OpenWrtClient{Host: "192.168.1.9", pool: r}
  88. m := c.GetWirelessClients()
  89. if len(m) != 1 {
  90. t.Fatalf("clientes: %d (want 1)", len(m))
  91. }
  92. if r.ran(probe.CmdIwinfoAssoc) {
  93. t.Fatalf("el combinado bastó; no debe tocar el par legacy: %v", r.calls)
  94. }
  95. c.radiosMu.Lock()
  96. cached := c.radiosCache
  97. c.radiosMu.Unlock()
  98. if len(cached) != 1 {
  99. t.Fatalf("caché de radios no poblada por el combinado: %+v", cached)
  100. }
  101. }
  102. // Sin ubus ni combined: último recurso legacy (CmdIwinfoAssoc).
  103. func TestGetWirelessClientsLegacyFallback(t *testing.T) {
  104. r := &fakeWirelessRunner{responses: map[string]string{
  105. probe.CmdHostapdClients: "ubus: not found\n",
  106. probe.CmdWirelessCombined: "",
  107. probe.CmdIwinfoAssoc: "AA:BB:CC:DD:EE:FF -55 5\n",
  108. }}
  109. c := &OpenWrtClient{Host: "192.168.1.9", pool: r}
  110. m := c.GetWirelessClients()
  111. if len(m) != 1 {
  112. t.Fatalf("clientes: %d (want 1)", len(m))
  113. }
  114. if !r.ran(probe.CmdIwinfoAssoc) {
  115. t.Fatalf("legacy no se usó: %v", r.calls)
  116. }
  117. }
  118. // GetRadios refresca al arranque (caché vacía) y sirve de la caché en las
  119. // llamadas siguientes SIN volver a tocar el router (radiosTTL).
  120. func TestGetRadiosCacheTTL(t *testing.T) {
  121. combined := "R|5.240|48|HE80|23|1\nC|AA:BB:CC:DD:EE:01|-40|5.240\n"
  122. r := &fakeWirelessRunner{responses: map[string]string{
  123. probe.CmdHostapdClients: "",
  124. probe.CmdWirelessCombined: combined,
  125. probe.CmdRadios: "5.24|48|HE80|23|1\n",
  126. }}
  127. c := &OpenWrtClient{Host: "192.168.1.9", pool: r}
  128. rs1 := c.GetRadios()
  129. if len(rs1) != 1 || rs1[0].Channel != 48 {
  130. t.Fatalf("primera llamada: %+v", rs1)
  131. }
  132. n1 := len(r.calls)
  133. rs2 := c.GetRadios()
  134. if len(rs2) != 1 {
  135. t.Fatalf("segunda llamada: %+v", rs2)
  136. }
  137. if len(r.calls) != n1 {
  138. t.Fatalf("la caché fresca no debe re-sondear: %v", r.calls)
  139. }
  140. // Caducada: al envejecer radiosAt, la siguiente llamada refresca.
  141. c.radiosMu.Lock()
  142. c.radiosAt = time.Now().Add(-radiosTTL - time.Second)
  143. c.radiosMu.Unlock()
  144. rs3 := c.GetRadios()
  145. if len(rs3) != 1 {
  146. t.Fatalf("refresco tras TTL: %+v", rs3)
  147. }
  148. if len(r.calls) <= n1 {
  149. t.Fatalf("TTL caducado debe refrescar: %v", r.calls)
  150. }
  151. }