agent_wan_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // agent_wan_test.go — #276: el estado WAN viaja en el payload del agente.
  2. // Un router agent_only no tiene cliente SSH, así que la sonda del servidor
  3. // no corre nunca: sin el campo del payload, el panel de conexión del
  4. // gateway se queda con "—" aunque el uplink esté perfectamente arriba.
  5. package adapters
  6. import (
  7. "testing"
  8. "github.com/gnacho/netpulse/agent/probe"
  9. )
  10. func TestPolledFromAgentTomaElWanDelPayload(t *testing.T) {
  11. l := NewLive(nil, nil, []RouterConfig{
  12. {ID: "gateway", Host: "192.0.2.1", Name: "openwrt", IsGateway: true, AgentOnly: true},
  13. }, nil)
  14. payload := &probe.Payload{
  15. Router: "gateway", Ts: 1, Version: "2.28.24",
  16. Data: probe.PayloadData{Wan: &probe.WanInfo{
  17. Proto: "pppoe", Device: "pppoe-isp", Port: "lan1",
  18. IP: "203.0.113.45", Gateway: "198.51.100.1",
  19. DNS: []string{"198.51.100.53"},
  20. }},
  21. }
  22. p := l.polledFromAgent(l.routers[0], payload)
  23. if p.wanInfo.IP != "203.0.113.45" || p.wanInfo.Proto != "pppoe" {
  24. t.Fatalf("wanInfo del payload: %+v", p.wanInfo)
  25. }
  26. if p.wanInfo.Gateway != "198.51.100.1" || p.wanInfo.Port != "lan1" {
  27. t.Fatalf("wanInfo del payload: %+v", p.wanInfo)
  28. }
  29. }
  30. func TestPolledFromAgentSinWanQuedaVacio(t *testing.T) {
  31. // Agente viejo (sin el campo) o AP sin uplink: nada que ingerir, y el
  32. // servidor sigue pudiendo sondear por SSH si el router lo permite.
  33. l := NewLive(nil, nil, []RouterConfig{{ID: "ap1", Host: "192.0.2.2", Name: "ap"}}, nil)
  34. payload := &probe.Payload{Router: "ap1", Ts: 1, Version: "2.28.0"}
  35. p := l.polledFromAgent(l.routers[0], payload)
  36. if p.wanInfo.Proto != "" || p.wanInfo.IP != "" {
  37. t.Fatalf("sin wan en el payload debía quedar vacío: %+v", p.wanInfo)
  38. }
  39. }
  40. // The several-connections section travels the same way the single one
  41. // does, and is the only source for a router polled through its agent.
  42. func TestPolledFromAgentTakesMultiWanFromPayload(t *testing.T) {
  43. l := NewLive(nil, nil, []RouterConfig{
  44. {ID: "gateway", Host: "192.0.2.1", Name: "gateway", IsGateway: true, AgentOnly: true},
  45. }, nil)
  46. payload := &probe.Payload{
  47. Router: "gateway", Ts: 1, Version: "2.29.0",
  48. Data: probe.PayloadData{MultiWan: &probe.MultiWanInfo{
  49. Mode: "failover", Managed: true, Active: "cell", Primary: "fiber",
  50. Uplinks: []probe.WanUplink{
  51. {Name: "fiber", Proto: "pppoe", IP: "203.0.113.45", Up: true, Primary: true},
  52. {Name: "cell", Proto: "qmi", IP: "192.0.2.77", Up: true, Active: true, Metered: true},
  53. },
  54. }},
  55. }
  56. p := l.polledFromAgent(l.routers[0], payload)
  57. if p.multiWan == nil || p.multiWan.Active != "cell" || len(p.multiWan.Uplinks) != 2 {
  58. t.Fatalf("multiWan = %+v", p.multiWan)
  59. }
  60. }
  61. // An event-driven push carries no such section. Dropping it would make the
  62. // panel blink out of the page every time a device associates.
  63. func TestPolledFromAgentKeepsTheLastMultiWanSection(t *testing.T) {
  64. l := NewLive(nil, nil, []RouterConfig{
  65. {ID: "gateway", Host: "192.0.2.1", Name: "gateway", IsGateway: true, AgentOnly: true},
  66. }, nil)
  67. full := &probe.Payload{Router: "gateway", Ts: 1, Data: probe.PayloadData{
  68. MultiWan: &probe.MultiWanInfo{Mode: "balance", Uplinks: []probe.WanUplink{{Name: "a"}, {Name: "b"}}},
  69. Vlans: []probe.VlanPort{{Port: "lan1"}},
  70. }}
  71. l.polledFromAgent(l.routers[0], full)
  72. wirelessOnly := &probe.Payload{Router: "gateway", Ts: 2, Data: probe.PayloadData{}}
  73. p := l.polledFromAgent(l.routers[0], wirelessOnly)
  74. if p.multiWan == nil || p.multiWan.Mode != "balance" {
  75. t.Fatalf("multiWan = %+v, want the last good section", p.multiWan)
  76. }
  77. // Same rule for the VLANs, which travel in the payload and were being
  78. // dropped on this path entirely.
  79. if len(p.vlans) != 1 {
  80. t.Fatalf("vlans = %+v, want the last good section", p.vlans)
  81. }
  82. }
  83. // An older agent reports nothing, and nothing is what the panel gets.
  84. func TestPolledFromAgentWithoutMultiWanStaysNil(t *testing.T) {
  85. l := NewLive(nil, nil, []RouterConfig{
  86. {ID: "gateway", Host: "192.0.2.1", Name: "gateway", IsGateway: true, AgentOnly: true},
  87. }, nil)
  88. p := l.polledFromAgent(l.routers[0], &probe.Payload{Router: "gateway", Ts: 1, Version: "2.28.0"})
  89. if p.multiWan != nil {
  90. t.Fatalf("multiWan = %+v, want nothing", p.multiWan)
  91. }
  92. }
  93. func TestPolledFromAgentTakesReservationsFromPayload(t *testing.T) {
  94. // A client with a fixed address of its own never takes a lease, so the
  95. // reservation is the only name the router has for it.
  96. l := NewLive(nil, nil, []RouterConfig{
  97. {ID: "gateway", Host: "192.0.2.1", Name: "openwrt", IsGateway: true, AgentOnly: true},
  98. }, nil)
  99. payload := &probe.Payload{
  100. Router: "gateway", Ts: 1, Version: "2.28.24",
  101. Data: probe.PayloadData{DHCP: &probe.DHCPData{
  102. Reservations: []probe.DhcpReservation{
  103. {MAC: "02:00:00:00:00:01", Name: "printer", IP: "192.0.2.10"},
  104. },
  105. }},
  106. }
  107. p := l.polledFromAgent(l.routers[0], payload)
  108. if len(p.reservations) != 1 || p.reservations[0].Name != "printer" {
  109. t.Fatalf("reservations from the payload: %+v", p.reservations)
  110. }
  111. }