2016-07-27 16:29:28 +02:00
|
|
|
package alerting
|
|
|
|
|
|
|
|
|
|
import (
|
2016-10-03 09:38:03 +02:00
|
|
|
"context"
|
2016-07-27 16:29:28 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type conditionStub struct {
|
2016-11-15 07:54:09 -08:00
|
|
|
firing bool
|
|
|
|
|
operator string
|
|
|
|
|
matches []*EvalMatch
|
2016-11-22 11:44:25 +01:00
|
|
|
noData bool
|
2016-07-27 16:29:28 +02:00
|
|
|
}
|
|
|
|
|
|
2016-11-03 15:26:17 +01:00
|
|
|
func (c *conditionStub) Eval(context *EvalContext) (*ConditionResult, error) {
|
2016-11-22 11:44:25 +01:00
|
|
|
return &ConditionResult{Firing: c.firing, EvalMatches: c.matches, Operator: c.operator, NoDataFound: c.noData}, nil
|
2016-07-27 16:29:28 +02:00
|
|
|
}
|
|
|
|
|
|
2017-01-13 10:24:40 +01:00
|
|
|
func TestAlertingEvaluationHandler(t *testing.T) {
|
|
|
|
|
Convey("Test alert evaluation handler", t, func() {
|
2016-07-27 16:29:28 +02:00
|
|
|
handler := NewEvalHandler()
|
|
|
|
|
|
|
|
|
|
Convey("Show return triggered with single passing condition", func() {
|
2016-10-03 09:38:03 +02:00
|
|
|
context := NewEvalContext(context.TODO(), &Rule{
|
2016-07-27 16:29:28 +02:00
|
|
|
Conditions: []Condition{&conditionStub{
|
|
|
|
|
firing: true,
|
|
|
|
|
}},
|
|
|
|
|
})
|
|
|
|
|
|
2016-10-03 09:38:03 +02:00
|
|
|
handler.Eval(context)
|
2016-07-27 16:29:28 +02:00
|
|
|
So(context.Firing, ShouldEqual, true)
|
2016-11-17 15:48:15 +01:00
|
|
|
So(context.ConditionEvals, ShouldEqual, "true = true")
|
2016-07-27 16:29:28 +02:00
|
|
|
})
|
|
|
|
|
|
2017-12-13 13:37:10 +01:00
|
|
|
Convey("Show return triggered with single passing condition2", func() {
|
|
|
|
|
context := NewEvalContext(context.TODO(), &Rule{
|
|
|
|
|
Conditions: []Condition{&conditionStub{firing: true, operator: "and"}},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
handler.Eval(context)
|
|
|
|
|
So(context.Firing, ShouldEqual, true)
|
|
|
|
|
So(context.ConditionEvals, ShouldEqual, "true = true")
|
|
|
|
|
})
|
|
|
|
|
|
2016-11-03 15:26:17 +01:00
|
|
|
Convey("Show return false with not passing asdf", func() {
|
2016-10-03 09:38:03 +02:00
|
|
|
context := NewEvalContext(context.TODO(), &Rule{
|
2016-07-27 16:29:28 +02:00
|
|
|
Conditions: []Condition{
|
2017-01-27 15:21:02 +01:00
|
|
|
&conditionStub{firing: true, operator: "and", matches: []*EvalMatch{{}, {}}},
|
2016-11-18 10:53:27 +01:00
|
|
|
&conditionStub{firing: false, operator: "and"},
|
2016-07-27 16:29:28 +02:00
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
2016-10-03 09:38:03 +02:00
|
|
|
handler.Eval(context)
|
2016-07-27 16:29:28 +02:00
|
|
|
So(context.Firing, ShouldEqual, false)
|
2016-11-17 15:48:15 +01:00
|
|
|
So(context.ConditionEvals, ShouldEqual, "[true AND false] = false")
|
2016-07-27 16:29:28 +02:00
|
|
|
})
|
2016-11-15 07:54:09 -08:00
|
|
|
|
|
|
|
|
Convey("Show return true if any of the condition is passing with OR operator", func() {
|
|
|
|
|
context := NewEvalContext(context.TODO(), &Rule{
|
|
|
|
|
Conditions: []Condition{
|
|
|
|
|
&conditionStub{firing: true, operator: "and"},
|
|
|
|
|
&conditionStub{firing: false, operator: "or"},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
handler.Eval(context)
|
|
|
|
|
So(context.Firing, ShouldEqual, true)
|
2016-11-17 15:48:15 +01:00
|
|
|
So(context.ConditionEvals, ShouldEqual, "[true OR false] = true")
|
2016-11-15 07:54:09 -08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
Convey("Show return false if any of the condition is failing with AND operator", func() {
|
|
|
|
|
context := NewEvalContext(context.TODO(), &Rule{
|
|
|
|
|
Conditions: []Condition{
|
|
|
|
|
&conditionStub{firing: true, operator: "and"},
|
|
|
|
|
&conditionStub{firing: false, operator: "and"},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
handler.Eval(context)
|
|
|
|
|
So(context.Firing, ShouldEqual, false)
|
2016-11-17 15:48:15 +01:00
|
|
|
So(context.ConditionEvals, ShouldEqual, "[true AND false] = false")
|
2016-11-15 07:54:09 -08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
Convey("Show return true if one condition is failing with nested OR operator", func() {
|
|
|
|
|
context := NewEvalContext(context.TODO(), &Rule{
|
|
|
|
|
Conditions: []Condition{
|
|
|
|
|
&conditionStub{firing: true, operator: "and"},
|
|
|
|
|
&conditionStub{firing: true, operator: "and"},
|
|
|
|
|
&conditionStub{firing: false, operator: "or"},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
handler.Eval(context)
|
|
|
|
|
So(context.Firing, ShouldEqual, true)
|
2016-11-17 15:48:15 +01:00
|
|
|
So(context.ConditionEvals, ShouldEqual, "[[true AND true] OR false] = true")
|
2016-11-15 07:54:09 -08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
Convey("Show return false if one condition is passing with nested OR operator", func() {
|
|
|
|
|
context := NewEvalContext(context.TODO(), &Rule{
|
|
|
|
|
Conditions: []Condition{
|
|
|
|
|
&conditionStub{firing: true, operator: "and"},
|
|
|
|
|
&conditionStub{firing: false, operator: "and"},
|
|
|
|
|
&conditionStub{firing: false, operator: "or"},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
handler.Eval(context)
|
|
|
|
|
So(context.Firing, ShouldEqual, false)
|
2016-11-17 15:48:15 +01:00
|
|
|
So(context.ConditionEvals, ShouldEqual, "[[true AND false] OR false] = false")
|
2016-11-15 07:54:09 -08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
Convey("Show return false if a condition is failing with nested AND operator", func() {
|
|
|
|
|
context := NewEvalContext(context.TODO(), &Rule{
|
|
|
|
|
Conditions: []Condition{
|
|
|
|
|
&conditionStub{firing: true, operator: "and"},
|
|
|
|
|
&conditionStub{firing: false, operator: "and"},
|
|
|
|
|
&conditionStub{firing: true, operator: "and"},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
handler.Eval(context)
|
|
|
|
|
So(context.Firing, ShouldEqual, false)
|
2016-11-17 15:48:15 +01:00
|
|
|
So(context.ConditionEvals, ShouldEqual, "[[true AND false] AND true] = false")
|
2016-11-15 07:54:09 -08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
Convey("Show return true if a condition is passing with nested OR operator", func() {
|
|
|
|
|
context := NewEvalContext(context.TODO(), &Rule{
|
|
|
|
|
Conditions: []Condition{
|
|
|
|
|
&conditionStub{firing: true, operator: "and"},
|
|
|
|
|
&conditionStub{firing: false, operator: "or"},
|
|
|
|
|
&conditionStub{firing: true, operator: "or"},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
handler.Eval(context)
|
|
|
|
|
So(context.Firing, ShouldEqual, true)
|
2016-11-17 15:48:15 +01:00
|
|
|
So(context.ConditionEvals, ShouldEqual, "[[true OR false] OR true] = true")
|
2016-11-15 07:54:09 -08:00
|
|
|
})
|
2016-11-22 11:44:25 +01:00
|
|
|
|
2017-12-13 13:37:10 +01:00
|
|
|
Convey("Should return false if no condition is firing using OR operator", func() {
|
|
|
|
|
context := NewEvalContext(context.TODO(), &Rule{
|
|
|
|
|
Conditions: []Condition{
|
|
|
|
|
&conditionStub{firing: false, operator: "or"},
|
|
|
|
|
&conditionStub{firing: false, operator: "or"},
|
|
|
|
|
&conditionStub{firing: false, operator: "or"},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
handler.Eval(context)
|
|
|
|
|
So(context.Firing, ShouldEqual, false)
|
|
|
|
|
So(context.ConditionEvals, ShouldEqual, "[[false OR false] OR false] = false")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
Convey("Should retuasdfrn no data if one condition has nodata", func() {
|
|
|
|
|
context := NewEvalContext(context.TODO(), &Rule{
|
|
|
|
|
Conditions: []Condition{
|
|
|
|
|
&conditionStub{operator: "or", noData: false},
|
|
|
|
|
&conditionStub{operator: "or", noData: false},
|
|
|
|
|
&conditionStub{operator: "or", noData: false},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
handler.Eval(context)
|
|
|
|
|
So(context.NoDataFound, ShouldBeFalse)
|
|
|
|
|
})
|
|
|
|
|
|
2016-11-22 11:44:25 +01:00
|
|
|
Convey("Should return no data if one condition has nodata", func() {
|
|
|
|
|
context := NewEvalContext(context.TODO(), &Rule{
|
|
|
|
|
Conditions: []Condition{
|
|
|
|
|
&conditionStub{operator: "and", noData: true},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
handler.Eval(context)
|
|
|
|
|
So(context.Firing, ShouldEqual, false)
|
|
|
|
|
So(context.NoDataFound, ShouldBeTrue)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
Convey("Should return no data if both conditions have no data and using AND", func() {
|
|
|
|
|
context := NewEvalContext(context.TODO(), &Rule{
|
|
|
|
|
Conditions: []Condition{
|
|
|
|
|
&conditionStub{operator: "and", noData: true},
|
|
|
|
|
&conditionStub{operator: "and", noData: false},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
handler.Eval(context)
|
|
|
|
|
So(context.NoDataFound, ShouldBeFalse)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
Convey("Should not return no data if both conditions have no data and using OR", func() {
|
|
|
|
|
context := NewEvalContext(context.TODO(), &Rule{
|
|
|
|
|
Conditions: []Condition{
|
|
|
|
|
&conditionStub{operator: "or", noData: true},
|
|
|
|
|
&conditionStub{operator: "or", noData: false},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
handler.Eval(context)
|
|
|
|
|
So(context.NoDataFound, ShouldBeTrue)
|
|
|
|
|
})
|
2016-07-27 16:29:28 +02:00
|
|
|
})
|
|
|
|
|
}
|