handler_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package alerting
  2. import (
  3. "testing"
  4. . "github.com/smartystreets/goconvey/convey"
  5. )
  6. type conditionStub struct {
  7. firing bool
  8. }
  9. func (c *conditionStub) Eval(context *AlertResultContext) {
  10. context.Firing = c.firing
  11. }
  12. func TestAlertingExecutor(t *testing.T) {
  13. Convey("Test alert execution", t, func() {
  14. handler := NewHandler()
  15. Convey("Show return triggered with single passing condition", func() {
  16. context := NewAlertResultContext(&AlertRule{
  17. Conditions: []AlertCondition{&conditionStub{
  18. firing: true,
  19. }},
  20. })
  21. handler.eval(context)
  22. So(context.Firing, ShouldEqual, true)
  23. })
  24. Convey("Show return false with not passing condition", func() {
  25. context := NewAlertResultContext(&AlertRule{
  26. Conditions: []AlertCondition{
  27. &conditionStub{firing: true},
  28. &conditionStub{firing: false},
  29. },
  30. })
  31. handler.eval(context)
  32. So(context.Firing, ShouldEqual, false)
  33. })
  34. // Convey("Show return critical since below 2", func() {
  35. // rule := &AlertRule{
  36. // Critical: Level{Value: 10, Operator: "<"},
  37. // Transformer: transformers.NewAggregationTransformer("avg"),
  38. // }
  39. //
  40. // timeSeries := []*tsdb.TimeSeries{
  41. // tsdb.NewTimeSeries("test1", [][2]float64{{2, 0}}),
  42. // }
  43. //
  44. // result := executor.evaluateRule(rule, timeSeries)
  45. // So(result.State, ShouldEqual, alertstates.Critical)
  46. // })
  47. //
  48. // Convey("Show return critical since sum is above 10", func() {
  49. // rule := &AlertRule{
  50. // Critical: Level{Value: 10, Operator: ">"},
  51. // Transformer: transformers.NewAggregationTransformer("sum"),
  52. // }
  53. //
  54. // timeSeries := []*tsdb.TimeSeries{
  55. // tsdb.NewTimeSeries("test1", [][2]float64{{9, 0}, {9, 0}}),
  56. // }
  57. //
  58. // result := executor.evaluateRule(rule, timeSeries)
  59. // So(result.State, ShouldEqual, alertstates.Critical)
  60. // })
  61. //
  62. // Convey("Show return ok since avg is below 10", func() {
  63. // rule := &AlertRule{
  64. // Critical: Level{Value: 10, Operator: ">"},
  65. // Transformer: transformers.NewAggregationTransformer("avg"),
  66. // }
  67. //
  68. // timeSeries := []*tsdb.TimeSeries{
  69. // tsdb.NewTimeSeries("test1", [][2]float64{{9, 0}, {9, 0}}),
  70. // }
  71. //
  72. // result := executor.evaluateRule(rule, timeSeries)
  73. // So(result.State, ShouldEqual, alertstates.Ok)
  74. // })
  75. //
  76. // Convey("Show return ok since min is below 10", func() {
  77. // rule := &AlertRule{
  78. // Critical: Level{Value: 10, Operator: ">"},
  79. // Transformer: transformers.NewAggregationTransformer("avg"),
  80. // }
  81. //
  82. // timeSeries := []*tsdb.TimeSeries{
  83. // tsdb.NewTimeSeries("test1", [][2]float64{{11, 0}, {9, 0}}),
  84. // }
  85. //
  86. // result := executor.evaluateRule(rule, timeSeries)
  87. // So(result.State, ShouldEqual, alertstates.Ok)
  88. // })
  89. //
  90. // Convey("Show return ok since max is above 10", func() {
  91. // rule := &AlertRule{
  92. // Critical: Level{Value: 10, Operator: ">"},
  93. // Transformer: transformers.NewAggregationTransformer("max"),
  94. // }
  95. //
  96. // timeSeries := []*tsdb.TimeSeries{
  97. // tsdb.NewTimeSeries("test1", [][2]float64{{6, 0}, {11, 0}}),
  98. // }
  99. //
  100. // result := executor.evaluateRule(rule, timeSeries)
  101. // So(result.State, ShouldEqual, alertstates.Critical)
  102. // })
  103. //
  104. // })
  105. //
  106. // Convey("muliple time series", func() {
  107. // Convey("both are ok", func() {
  108. // rule := &AlertRule{
  109. // Critical: Level{Value: 10, Operator: ">"},
  110. // Transformer: transformers.NewAggregationTransformer("avg"),
  111. // }
  112. //
  113. // timeSeries := []*tsdb.TimeSeries{
  114. // tsdb.NewTimeSeries("test1", [][2]float64{{2, 0}}),
  115. // tsdb.NewTimeSeries("test1", [][2]float64{{2, 0}}),
  116. // }
  117. //
  118. // result := executor.evaluateRule(rule, timeSeries)
  119. // So(result.State, ShouldEqual, alertstates.Ok)
  120. // })
  121. //
  122. // Convey("first serie is good, second is critical", func() {
  123. // rule := &AlertRule{
  124. // Critical: Level{Value: 10, Operator: ">"},
  125. // Transformer: transformers.NewAggregationTransformer("avg"),
  126. // }
  127. //
  128. // timeSeries := []*tsdb.TimeSeries{
  129. // tsdb.NewTimeSeries("test1", [][2]float64{{2, 0}}),
  130. // tsdb.NewTimeSeries("test1", [][2]float64{{11, 0}}),
  131. // }
  132. //
  133. // result := executor.evaluateRule(rule, timeSeries)
  134. // So(result.State, ShouldEqual, alertstates.Critical)
  135. // })
  136. //
  137. // Convey("first serie is warn, second is critical", func() {
  138. // rule := &AlertRule{
  139. // Critical: Level{Value: 10, Operator: ">"},
  140. // Warning: Level{Value: 5, Operator: ">"},
  141. // Transformer: transformers.NewAggregationTransformer("avg"),
  142. // }
  143. //
  144. // timeSeries := []*tsdb.TimeSeries{
  145. // tsdb.NewTimeSeries("test1", [][2]float64{{6, 0}}),
  146. // tsdb.NewTimeSeries("test1", [][2]float64{{11, 0}}),
  147. // }
  148. //
  149. // result := executor.evaluateRule(rule, timeSeries)
  150. // So(result.State, ShouldEqual, alertstates.Critical)
  151. // })
  152. // })
  153. })
  154. }