suppression_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package alerts
  2. import (
  3. "testing"
  4. )
  5. func TestSuppressionGraphBasic(t *testing.T) {
  6. g := NewSuppressionGraph()
  7. g.SetTopology(map[string]string{
  8. "ap-sala": "gateway",
  9. "ap-patio": "gateway",
  10. })
  11. if g.SuppressedBy("ap-sala") != "" {
  12. t.Fatal("no parent should suppress when no one is down")
  13. }
  14. g.MarkDown("gateway")
  15. if got := g.SuppressedBy("ap-sala"); got != "gateway" {
  16. t.Fatalf("expected gateway, got %q", got)
  17. }
  18. if got := g.SuppressedBy("ap-patio"); got != "gateway" {
  19. t.Fatalf("expected gateway, got %q", got)
  20. }
  21. if got := g.SuppressedBy("gateway"); got != "" {
  22. t.Fatalf("gateway itself should not be suppressed, got %q", got)
  23. }
  24. g.MarkUp("gateway")
  25. if g.SuppressedBy("ap-sala") != "" {
  26. t.Fatal("after MarkUp, no suppression")
  27. }
  28. }
  29. func TestSuppressionGraphChain(t *testing.T) {
  30. g := NewSuppressionGraph()
  31. g.SetTopology(map[string]string{
  32. "switch-1": "gateway",
  33. "ap-sala": "switch-1",
  34. })
  35. g.MarkDown("gateway")
  36. if got := g.SuppressedBy("ap-sala"); got != "gateway" {
  37. t.Fatalf("chain: expected gateway, got %q", got)
  38. }
  39. g.MarkUp("gateway")
  40. g.MarkDown("switch-1")
  41. if got := g.SuppressedBy("ap-sala"); got != "switch-1" {
  42. t.Fatalf("chain: expected switch-1, got %q", got)
  43. }
  44. }
  45. func TestSuppressionGraphCycleSafe(t *testing.T) {
  46. g := NewSuppressionGraph()
  47. g.SetTopology(map[string]string{
  48. "a": "b",
  49. "b": "a",
  50. })
  51. g.MarkDown("a")
  52. if got := g.SuppressedBy("b"); got != "a" {
  53. t.Fatalf("cycle: expected a, got %q", got)
  54. }
  55. if got := g.SuppressedBy("c"); got != "" {
  56. t.Fatalf("unknown node: expected empty, got %q", got)
  57. }
  58. }
  59. func TestSuppressionInEmit(t *testing.T) {
  60. g := NewSuppressionGraph()
  61. g.SetTopology(map[string]string{"ap": "gw"})
  62. g.MarkDown("gw")
  63. spy := &spyNotifier{}
  64. e := New(nil, spy)
  65. e.SetSuppression(g)
  66. ev := AlertEvent{
  67. ID: "alert-offline-ap", Category: CatRouter, Urgent: true,
  68. Severity: "critical", Title: "AP offline", RouterID: "ap",
  69. }
  70. if !e.Emit(ev) {
  71. t.Fatal("emit should pass (router:urgent + urgent=true)")
  72. }
  73. list := e.List()
  74. if len(list) != 1 {
  75. t.Fatalf("expected 1 event, got %d", len(list))
  76. }
  77. if list[0].SuppressedBy != "gw" {
  78. t.Fatalf("expected SuppressedBy=gw, got %q", list[0].SuppressedBy)
  79. }
  80. if len(spy.got) != 0 {
  81. t.Fatal("suppressed alert should NOT notify")
  82. }
  83. }
  84. func TestSuppressionNilGraph(t *testing.T) {
  85. spy := &spyNotifier{}
  86. e := New(nil, spy)
  87. e.Emit(AlertEvent{
  88. ID: "a1", Category: CatSystem, Urgent: true,
  89. Severity: "warn", Title: "x", RouterID: "r",
  90. })
  91. if len(spy.got) != 1 {
  92. t.Fatal("nil suppression should not block notification")
  93. }
  94. }