multiwan_alert_test.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // multiwan_alert_test.go — the internet moving from one connection to
  2. // another is news; it bouncing for a few seconds is not.
  3. package adapters
  4. import (
  5. "encoding/json"
  6. "strings"
  7. "testing"
  8. "time"
  9. "github.com/gnacho/netpulse/agent/probe"
  10. )
  11. func uplinkLive(t *testing.T) *Live {
  12. t.Helper()
  13. return NewLive(nil, nil, []RouterConfig{
  14. {ID: "gateway", Host: "192.0.2.1", Name: "Gateway", IsGateway: true, AgentOnly: true},
  15. }, nil)
  16. }
  17. // polledWith builds a poll result reporting two connections, the named one
  18. // carrying traffic.
  19. func polledWith(active, primary string, metered ...string) *routerPolled {
  20. mw := &probe.MultiWanInfo{
  21. Mode: "failover", Managed: true, Active: active, Primary: primary,
  22. Uplinks: []probe.WanUplink{{Name: "fiber", Up: true}, {Name: "cell", Up: true}},
  23. }
  24. for i := range mw.Uplinks {
  25. for _, m := range metered {
  26. if mw.Uplinks[i].Name == m {
  27. mw.Uplinks[i].Metered = true
  28. }
  29. }
  30. mw.Uplinks[i].Active = mw.Uplinks[i].Name == active
  31. }
  32. return &routerPolled{multiWan: mw}
  33. }
  34. func alertTypes(l *Live) []string {
  35. out := []string{}
  36. for _, ev := range l.engine.List() {
  37. out = append(out, ev.Type)
  38. }
  39. return out
  40. }
  41. // The first sighting is recorded, not announced: a server restart must not
  42. // report a failover that happened while it was down.
  43. func TestUplinkFirstObservationIsSilent(t *testing.T) {
  44. l := uplinkLive(t)
  45. now := time.Now()
  46. l.trackUplinkChange(l.routers[0], polledWith("cell", "fiber"), now)
  47. if got := len(l.engine.List()); got != 0 {
  48. t.Fatalf("%d alerts on the first poll, want none", got)
  49. }
  50. }
  51. func TestUplinkSwitchAlertsOnceItHolds(t *testing.T) {
  52. l := uplinkLive(t)
  53. now := time.Now()
  54. cfg := l.routers[0]
  55. l.trackUplinkChange(cfg, polledWith("fiber", "fiber"), now)
  56. // Seen on the other connection, but not yet believed. The settle
  57. // clock starts here, at the first sighting, not at the first poll.
  58. seen := now.Add(5 * time.Second)
  59. l.trackUplinkChange(cfg, polledWith("cell", "fiber", "cell"), seen)
  60. if got := len(l.engine.List()); got != 0 {
  61. t.Fatalf("%d alerts before the change settled, want none", got)
  62. }
  63. l.trackUplinkChange(cfg, polledWith("cell", "fiber", "cell"), seen.Add(uplinkSettle-time.Second))
  64. if got := len(l.engine.List()); got != 0 {
  65. t.Fatalf("%d alerts one second short of the window, want none", got)
  66. }
  67. // Still there past the window: now it counts.
  68. l.trackUplinkChange(cfg, polledWith("cell", "fiber", "cell"), seen.Add(uplinkSettle+time.Second))
  69. list := l.engine.List()
  70. if len(list) != 1 || list[0].Type != "uplink-switched" {
  71. t.Fatalf("alerts = %v", alertTypes(l))
  72. }
  73. ev := list[0]
  74. if ev.Vars["from"] != "fiber" || ev.Vars["to"] != "cell" || ev.Vars["router"] != "Gateway" {
  75. t.Fatalf("vars = %v", ev.Vars)
  76. }
  77. // Moving onto a pay-per-use line says so: it starts costing money.
  78. if ev.Vars["metered"] != "1" {
  79. t.Fatalf("vars = %v, want the destination marked as metered", ev.Vars)
  80. }
  81. if !ev.Urgent {
  82. t.Fatal("the internet category drops anything not urgent by default")
  83. }
  84. }
  85. // A line that drops and comes straight back produces nothing at all —
  86. // neither the switch nor a recovery for it.
  87. func TestUplinkFlapIsNotReported(t *testing.T) {
  88. l := uplinkLive(t)
  89. now := time.Now()
  90. cfg := l.routers[0]
  91. l.trackUplinkChange(cfg, polledWith("fiber", "fiber"), now)
  92. l.trackUplinkChange(cfg, polledWith("cell", "fiber"), now.Add(5*time.Second))
  93. l.trackUplinkChange(cfg, polledWith("fiber", "fiber"), now.Add(20*time.Second))
  94. l.trackUplinkChange(cfg, polledWith("fiber", "fiber"), now.Add(2*time.Minute))
  95. if got := alertTypes(l); len(got) != 0 {
  96. t.Fatalf("alerts = %v, want none for a bounce", got)
  97. }
  98. // And a real move afterwards still alerts.
  99. l.trackUplinkChange(cfg, polledWith("cell", "fiber"), now.Add(3*time.Minute))
  100. l.trackUplinkChange(cfg, polledWith("cell", "fiber"), now.Add(5*time.Minute))
  101. if got := alertTypes(l); len(got) != 1 || got[0] != "uplink-switched" {
  102. t.Fatalf("alerts = %v", got)
  103. }
  104. }
  105. // Coming back to the main connection is its own event: after a failover,
  106. // "it is over" is the part somebody is waiting for.
  107. func TestUplinkReturnToMainIsItsOwnAlert(t *testing.T) {
  108. l := uplinkLive(t)
  109. now := time.Now()
  110. cfg := l.routers[0]
  111. l.trackUplinkChange(cfg, polledWith("fiber", "fiber"), now)
  112. l.trackUplinkChange(cfg, polledWith("cell", "fiber"), now.Add(time.Minute))
  113. l.trackUplinkChange(cfg, polledWith("cell", "fiber"), now.Add(2*time.Minute))
  114. l.trackUplinkChange(cfg, polledWith("fiber", "fiber"), now.Add(10*time.Minute))
  115. l.trackUplinkChange(cfg, polledWith("fiber", "fiber"), now.Add(11*time.Minute))
  116. got := alertTypes(l)
  117. if len(got) != 2 || got[1] != "uplink-switched" || got[0] != "uplink-restored" {
  118. t.Fatalf("alerts = %v, want a switch then a recovery", got)
  119. }
  120. // Both survive the engine's dedup window, which keys on the title: the
  121. // destination is in there for exactly this reason.
  122. if l.engine.List()[0].Title == l.engine.List()[1].Title {
  123. t.Fatal("the two events must not share a title")
  124. }
  125. }
  126. // Nothing to fail over between, nothing to say — and the router is
  127. // forgotten, so a second line added later starts from silence.
  128. func TestUplinkSingleConnectionIsForgotten(t *testing.T) {
  129. l := uplinkLive(t)
  130. now := time.Now()
  131. cfg := l.routers[0]
  132. l.trackUplinkChange(cfg, polledWith("fiber", "fiber"), now)
  133. one := &routerPolled{multiWan: &probe.MultiWanInfo{
  134. Mode: "off", Uplinks: []probe.WanUplink{{Name: "fiber", Up: true, Active: true}},
  135. }}
  136. l.trackUplinkChange(cfg, one, now.Add(time.Minute))
  137. if _, ok := l.uplinkWatch[cfg.ID]; ok {
  138. t.Fatal("a router with one connection should not be watched")
  139. }
  140. l.trackUplinkChange(cfg, polledWith("cell", "fiber"), now.Add(2*time.Minute))
  141. l.trackUplinkChange(cfg, polledWith("cell", "fiber"), now.Add(3*time.Minute))
  142. if got := alertTypes(l); len(got) != 0 {
  143. t.Fatalf("alerts = %v, want none: the history was dropped", got)
  144. }
  145. }
  146. // A router that reports nothing, or a policy steering nothing, is not a
  147. // change and must not disturb what is already known.
  148. func TestUplinkNoDataChangesNothing(t *testing.T) {
  149. l := uplinkLive(t)
  150. now := time.Now()
  151. cfg := l.routers[0]
  152. l.trackUplinkChange(cfg, polledWith("fiber", "fiber"), now)
  153. l.trackUplinkChange(cfg, nil, now.Add(time.Minute))
  154. l.trackUplinkChange(cfg, &routerPolled{}, now.Add(2*time.Minute))
  155. nothingSteering := &routerPolled{multiWan: &probe.MultiWanInfo{
  156. Mode: "off", Uplinks: []probe.WanUplink{{Name: "fiber"}, {Name: "cell"}},
  157. }}
  158. l.trackUplinkChange(cfg, nothingSteering, now.Add(3*time.Minute))
  159. if got := alertTypes(l); len(got) != 0 {
  160. t.Fatalf("alerts = %v, want none", got)
  161. }
  162. if w := l.uplinkWatch[cfg.ID]; w == nil || w.active != "fiber" {
  163. t.Fatalf("watch = %+v, want the known state kept", w)
  164. }
  165. }
  166. // A router can push whatever it likes, so the two shapes that could reach
  167. // the page as something other than data are pinned here.
  168. func TestMultiWanFromAnUnknownAgentIsSafeToServe(t *testing.T) {
  169. // An older agent sends no section at all: the field is absent from the
  170. // response entirely, and the page renders without the panel.
  171. var old probe.Payload
  172. if err := json.Unmarshal([]byte(`{"router":"gw","ts":1,"data":{"system":{}}}`), &old); err != nil {
  173. t.Fatal(err)
  174. }
  175. if old.Data.MultiWan != nil {
  176. t.Fatalf("expected no section, got %+v", old.Data.MultiWan)
  177. }
  178. body, err := json.Marshal(RouterDetail{MultiWan: normalizeMultiWan(old.Data.MultiWan)})
  179. if err != nil {
  180. t.Fatal(err)
  181. }
  182. if strings.Contains(string(body), "multiWan") {
  183. t.Fatalf("an absent section must not appear in the response: %s", body)
  184. }
  185. // A section without its list would otherwise go out as a null, and the
  186. // page reads a length from it.
  187. var partial probe.Payload
  188. if err := json.Unmarshal([]byte(`{"router":"gw","ts":1,"data":{"multiWan":{"mode":"off"}}}`), &partial); err != nil {
  189. t.Fatal(err)
  190. }
  191. body, err = json.Marshal(RouterDetail{MultiWan: normalizeMultiWan(partial.Data.MultiWan)})
  192. if err != nil {
  193. t.Fatal(err)
  194. }
  195. if strings.Contains(string(body), `"uplinks":null`) {
  196. t.Fatalf("the list must never be served as null: %s", body)
  197. }
  198. if !strings.Contains(string(body), `"uplinks":[]`) {
  199. t.Fatalf("the list must be an empty array: %s", body)
  200. }
  201. }
  202. // And the tracker must not act on any of it.
  203. func TestUplinkTrackerIgnoresAMalformedSection(t *testing.T) {
  204. l := uplinkLive(t)
  205. cfg := l.routers[0]
  206. now := time.Now()
  207. l.trackUplinkChange(cfg, polledWith("fiber", "fiber"), now)
  208. l.trackUplinkChange(cfg, &routerPolled{multiWan: &probe.MultiWanInfo{Mode: "off"}}, now.Add(time.Minute))
  209. l.trackUplinkChange(cfg, &routerPolled{multiWan: &probe.MultiWanInfo{Active: "ghost"}}, now.Add(2*time.Minute))
  210. if got := alertTypes(l); len(got) != 0 {
  211. t.Fatalf("alerts = %v, want none from a section with no connections", got)
  212. }
  213. }