wifi_channelplan_scanerror_1848_test.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // wifi_channelplan_scanerror_1848_test.go — #1848: el plan de canales debe
  2. // decir por que un radio no tiene medidas, no solo que no las tiene.
  3. package httpapi_test
  4. import (
  5. "encoding/json"
  6. "io"
  7. "net/http"
  8. "testing"
  9. "time"
  10. "github.com/gnacho/netpulse/agent/probe"
  11. )
  12. func TestChannelPlanSurfacesScanError(t *testing.T) {
  13. ts := makeTestServer(t)
  14. token, cookie := createAgentAndLogin(t, ts)
  15. const motivo = "command failed: Operation not supported (-95)"
  16. payload := probe.Payload{
  17. Router: "rt1",
  18. Ts: time.Now().Unix(),
  19. Version: "test",
  20. Data: probe.PayloadData{
  21. Wireless: &probe.WirelessData{
  22. Clients: map[string]probe.WirelessClient{},
  23. Radios: []probe.Radio{
  24. {Name: "5 GHz", Channel: 48, WidthMhz: 80, Section: "eth6"},
  25. },
  26. ScanOutcomes: []probe.ScanOutcome{
  27. {Iface: "eth6", Phy: 1, BSS: 0, Error: motivo},
  28. },
  29. },
  30. },
  31. }
  32. res := pushPayload(t, ts, token, "10.0.9.2", payload)
  33. if res.StatusCode != http.StatusAccepted {
  34. b, _ := io.ReadAll(res.Body)
  35. res.Body.Close()
  36. t.Fatalf("ingest: %d %s", res.StatusCode, string(b))
  37. }
  38. res.Body.Close()
  39. req, _ := http.NewRequest("GET", ts.URL+"/api/wifi/channel-plan?routerId=rt1", nil)
  40. req.Header.Set("Cookie", "session="+cookie)
  41. res, err := http.DefaultClient.Do(req)
  42. if err != nil {
  43. t.Fatalf("channel-plan: %v", err)
  44. }
  45. defer res.Body.Close()
  46. if res.StatusCode != http.StatusOK {
  47. b, _ := io.ReadAll(res.Body)
  48. t.Fatalf("channel-plan: %d %s", res.StatusCode, string(b))
  49. }
  50. var body struct {
  51. Radios []struct {
  52. Iface string `json:"iface"`
  53. ScanError string `json:"scanError"`
  54. } `json:"radios"`
  55. ScanOutcomes []struct {
  56. Iface string `json:"iface"`
  57. BSS int `json:"bss"`
  58. Error string `json:"error"`
  59. } `json:"scanOutcomes"`
  60. }
  61. if err := json.NewDecoder(res.Body).Decode(&body); err != nil {
  62. t.Fatalf("decode: %v", err)
  63. }
  64. if len(body.Radios) != 1 {
  65. t.Fatalf("esperaba 1 radio, got %d", len(body.Radios))
  66. }
  67. if body.Radios[0].ScanError != motivo {
  68. t.Errorf("el radio debe llevar el motivo del rechazo: %q", body.Radios[0].ScanError)
  69. }
  70. if len(body.ScanOutcomes) != 1 {
  71. t.Fatalf("esperaba 1 outcome, got %d", len(body.ScanOutcomes))
  72. }
  73. if body.ScanOutcomes[0].Iface != "eth6" || body.ScanOutcomes[0].Error != motivo {
  74. t.Errorf("outcome inesperado: %+v", body.ScanOutcomes[0])
  75. }
  76. }