eval_handler_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. So(context.FiringEval, ShouldEqual, "true = true")
  27. })
  28. Convey("Show return false with not passing asdf", func() {
  29. context := NewEvalContext(context.TODO(), &Rule{
  30. Conditions: []Condition{
  31. &conditionStub{firing: true, matches: []*EvalMatch{&EvalMatch{}, &EvalMatch{}}},
  32. &conditionStub{firing: false},
  33. },
  34. })
  35. handler.Eval(context)
  36. So(context.Firing, ShouldEqual, false)
  37. So(context.FiringEval, ShouldEqual, "[true AND false] = false")
  38. })
  39. Convey("Show return true if any of the condition is passing with OR operator", func() {
  40. context := NewEvalContext(context.TODO(), &Rule{
  41. Conditions: []Condition{
  42. &conditionStub{firing: true, operator: "and"},
  43. &conditionStub{firing: false, operator: "or"},
  44. },
  45. })
  46. handler.Eval(context)
  47. So(context.Firing, ShouldEqual, true)
  48. So(context.FiringEval, ShouldEqual, "[true OR false] = true")
  49. })
  50. Convey("Show return false if any of the condition is failing with AND operator", func() {
  51. context := NewEvalContext(context.TODO(), &Rule{
  52. Conditions: []Condition{
  53. &conditionStub{firing: true, operator: "and"},
  54. &conditionStub{firing: false, operator: "and"},
  55. },
  56. })
  57. handler.Eval(context)
  58. So(context.Firing, ShouldEqual, false)
  59. So(context.FiringEval, ShouldEqual, "[true AND false] = false")
  60. })
  61. Convey("Show return true if one condition is failing with nested OR operator", func() {
  62. context := NewEvalContext(context.TODO(), &Rule{
  63. Conditions: []Condition{
  64. &conditionStub{firing: true, operator: "and"},
  65. &conditionStub{firing: true, operator: "and"},
  66. &conditionStub{firing: false, operator: "or"},
  67. },
  68. })
  69. handler.Eval(context)
  70. So(context.Firing, ShouldEqual, true)
  71. So(context.FiringEval, ShouldEqual, "[[true AND true] OR false] = true")
  72. })
  73. Convey("Show return false if one condition is passing with nested OR operator", func() {
  74. context := NewEvalContext(context.TODO(), &Rule{
  75. Conditions: []Condition{
  76. &conditionStub{firing: true, operator: "and"},
  77. &conditionStub{firing: false, operator: "and"},
  78. &conditionStub{firing: false, operator: "or"},
  79. },
  80. })
  81. handler.Eval(context)
  82. So(context.Firing, ShouldEqual, false)
  83. So(context.FiringEval, ShouldEqual, "[[true AND false] OR false] = false")
  84. })
  85. Convey("Show return false if a condition is failing with nested AND operator", func() {
  86. context := NewEvalContext(context.TODO(), &Rule{
  87. Conditions: []Condition{
  88. &conditionStub{firing: true, operator: "and"},
  89. &conditionStub{firing: false, operator: "and"},
  90. &conditionStub{firing: true, operator: "and"},
  91. },
  92. })
  93. handler.Eval(context)
  94. So(context.Firing, ShouldEqual, false)
  95. So(context.FiringEval, ShouldEqual, "[[true AND false] AND true] = false")
  96. })
  97. Convey("Show return true if a condition is passing with nested OR operator", func() {
  98. context := NewEvalContext(context.TODO(), &Rule{
  99. Conditions: []Condition{
  100. &conditionStub{firing: true, operator: "and"},
  101. &conditionStub{firing: false, operator: "or"},
  102. &conditionStub{firing: true, operator: "or"},
  103. },
  104. })
  105. handler.Eval(context)
  106. So(context.Firing, ShouldEqual, true)
  107. So(context.FiringEval, ShouldEqual, "[[true OR false] OR true] = true")
  108. })
  109. })
  110. }