eval_handler_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package alerting
  2. import (
  3. "context"
  4. "testing"
  5. . "github.com/smartystreets/goconvey/convey"
  6. )
  7. type conditionStub struct {
  8. firing bool
  9. operator string
  10. matches []*EvalMatch
  11. }
  12. func (c *conditionStub) Eval(context *EvalContext) (*ConditionResult, error) {
  13. return &ConditionResult{Firing: c.firing, EvalMatches: c.matches, Operator: c.operator}, nil
  14. }
  15. func TestAlertingExecutor(t *testing.T) {
  16. Convey("Test alert execution", t, func() {
  17. handler := NewEvalHandler()
  18. Convey("Show return triggered with single passing condition", func() {
  19. context := NewEvalContext(context.TODO(), &Rule{
  20. Conditions: []Condition{&conditionStub{
  21. firing: true,
  22. }},
  23. })
  24. handler.Eval(context)
  25. So(context.Firing, ShouldEqual, true)
  26. })
  27. Convey("Show return false with not passing asdf", func() {
  28. context := NewEvalContext(context.TODO(), &Rule{
  29. Conditions: []Condition{
  30. &conditionStub{firing: true, matches: []*EvalMatch{&EvalMatch{}, &EvalMatch{}}},
  31. &conditionStub{firing: false},
  32. },
  33. })
  34. handler.Eval(context)
  35. So(context.Firing, ShouldEqual, false)
  36. })
  37. Convey("Show return true if any of the condition is passing with OR operator", func() {
  38. context := NewEvalContext(context.TODO(), &Rule{
  39. Conditions: []Condition{
  40. &conditionStub{firing: true, operator: "and"},
  41. &conditionStub{firing: false, operator: "or"},
  42. },
  43. })
  44. handler.Eval(context)
  45. So(context.Firing, ShouldEqual, true)
  46. })
  47. Convey("Show return false if any of the condition is failing with AND operator", func() {
  48. context := NewEvalContext(context.TODO(), &Rule{
  49. Conditions: []Condition{
  50. &conditionStub{firing: true, operator: "and"},
  51. &conditionStub{firing: false, operator: "and"},
  52. },
  53. })
  54. handler.Eval(context)
  55. So(context.Firing, ShouldEqual, false)
  56. })
  57. Convey("Show return true if one condition is failing with nested OR operator", func() {
  58. context := NewEvalContext(context.TODO(), &Rule{
  59. Conditions: []Condition{
  60. &conditionStub{firing: true, operator: "and"},
  61. &conditionStub{firing: true, operator: "and"},
  62. &conditionStub{firing: false, operator: "or"},
  63. },
  64. })
  65. handler.Eval(context)
  66. So(context.Firing, ShouldEqual, true)
  67. })
  68. Convey("Show return false if one condition is passing with nested OR operator", func() {
  69. context := NewEvalContext(context.TODO(), &Rule{
  70. Conditions: []Condition{
  71. &conditionStub{firing: true, operator: "and"},
  72. &conditionStub{firing: false, operator: "and"},
  73. &conditionStub{firing: false, operator: "or"},
  74. },
  75. })
  76. handler.Eval(context)
  77. So(context.Firing, ShouldEqual, false)
  78. })
  79. Convey("Show return false if a condition is failing with nested AND operator", func() {
  80. context := NewEvalContext(context.TODO(), &Rule{
  81. Conditions: []Condition{
  82. &conditionStub{firing: true, operator: "and"},
  83. &conditionStub{firing: false, operator: "and"},
  84. &conditionStub{firing: true, operator: "and"},
  85. },
  86. })
  87. handler.Eval(context)
  88. So(context.Firing, ShouldEqual, false)
  89. })
  90. Convey("Show return true if a condition is passing with nested OR operator", func() {
  91. context := NewEvalContext(context.TODO(), &Rule{
  92. Conditions: []Condition{
  93. &conditionStub{firing: true, operator: "and"},
  94. &conditionStub{firing: false, operator: "or"},
  95. &conditionStub{firing: true, operator: "or"},
  96. },
  97. })
  98. handler.Eval(context)
  99. So(context.Firing, ShouldEqual, true)
  100. })
  101. })
  102. }