2016-11-04 05:28:12 -05:00
|
|
|
package alerting
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-03-21 14:48:29 -05:00
|
|
|
"fmt"
|
2016-11-04 05:28:12 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
)
|
|
|
|
|
2018-05-25 09:12:34 -05:00
|
|
|
func TestStateIsUpdatedWhenNeeded(t *testing.T) {
|
|
|
|
ctx := NewEvalContext(context.TODO(), &Rule{Conditions: []Condition{&conditionStub{firing: true}}})
|
|
|
|
|
|
|
|
t.Run("ok -> alerting", func(t *testing.T) {
|
|
|
|
ctx.PrevAlertState = models.AlertStateOK
|
|
|
|
ctx.Rule.State = models.AlertStateAlerting
|
|
|
|
|
|
|
|
if !ctx.ShouldUpdateAlertState() {
|
|
|
|
t.Fatalf("expected should updated to be true")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ok -> ok", func(t *testing.T) {
|
|
|
|
ctx.PrevAlertState = models.AlertStateOK
|
|
|
|
ctx.Rule.State = models.AlertStateOK
|
|
|
|
|
|
|
|
if ctx.ShouldUpdateAlertState() {
|
|
|
|
t.Fatalf("expected should updated to be false")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-11-04 05:28:12 -05:00
|
|
|
func TestAlertingEvalContext(t *testing.T) {
|
2018-05-25 09:12:34 -05:00
|
|
|
Convey("Should compute and replace properly new rule state", t, func() {
|
2016-11-04 05:28:12 -05:00
|
|
|
ctx := NewEvalContext(context.TODO(), &Rule{Conditions: []Condition{&conditionStub{firing: true}}})
|
2018-05-25 09:12:34 -05:00
|
|
|
dummieError := fmt.Errorf("dummie error")
|
2016-11-04 05:28:12 -05:00
|
|
|
|
2018-05-25 09:12:34 -05:00
|
|
|
Convey("ok -> alerting", func() {
|
|
|
|
ctx.PrevAlertState = models.AlertStateOK
|
|
|
|
ctx.Firing = true
|
2016-11-04 05:28:12 -05:00
|
|
|
|
2018-05-25 09:12:34 -05:00
|
|
|
ctx.Rule.State = ctx.GetNewState()
|
|
|
|
So(ctx.Rule.State, ShouldEqual, models.AlertStateAlerting)
|
|
|
|
})
|
2016-11-04 05:28:12 -05:00
|
|
|
|
2018-05-25 09:12:34 -05:00
|
|
|
Convey("ok -> error(alerting)", func() {
|
|
|
|
ctx.PrevAlertState = models.AlertStateOK
|
|
|
|
ctx.Error = dummieError
|
|
|
|
ctx.Rule.ExecutionErrorState = models.ExecutionErrorSetAlerting
|
|
|
|
|
|
|
|
ctx.Rule.State = ctx.GetNewState()
|
|
|
|
So(ctx.Rule.State, ShouldEqual, models.AlertStateAlerting)
|
|
|
|
})
|
2016-11-04 05:28:12 -05:00
|
|
|
|
2018-05-25 09:12:34 -05:00
|
|
|
Convey("ok -> error(keep_last)", func() {
|
|
|
|
ctx.PrevAlertState = models.AlertStateOK
|
|
|
|
ctx.Error = dummieError
|
|
|
|
ctx.Rule.ExecutionErrorState = models.ExecutionErrorKeepState
|
2016-11-04 05:28:12 -05:00
|
|
|
|
2018-05-25 09:12:34 -05:00
|
|
|
ctx.Rule.State = ctx.GetNewState()
|
|
|
|
So(ctx.Rule.State, ShouldEqual, models.AlertStateOK)
|
2016-11-04 05:28:12 -05:00
|
|
|
})
|
2018-03-21 14:48:29 -05:00
|
|
|
|
2018-05-25 09:12:34 -05:00
|
|
|
Convey("pending -> error(keep_last)", func() {
|
|
|
|
ctx.PrevAlertState = models.AlertStatePending
|
|
|
|
ctx.Error = dummieError
|
|
|
|
ctx.Rule.ExecutionErrorState = models.ExecutionErrorKeepState
|
|
|
|
|
|
|
|
ctx.Rule.State = ctx.GetNewState()
|
|
|
|
So(ctx.Rule.State, ShouldEqual, models.AlertStatePending)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("ok -> no_data(alerting)", func() {
|
|
|
|
ctx.PrevAlertState = models.AlertStateOK
|
|
|
|
ctx.Rule.NoDataState = models.NoDataSetAlerting
|
|
|
|
ctx.NoDataFound = true
|
|
|
|
|
|
|
|
ctx.Rule.State = ctx.GetNewState()
|
|
|
|
So(ctx.Rule.State, ShouldEqual, models.AlertStateAlerting)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("ok -> no_data(keep_last)", func() {
|
|
|
|
ctx.PrevAlertState = models.AlertStateOK
|
|
|
|
ctx.Rule.NoDataState = models.NoDataKeepState
|
|
|
|
ctx.NoDataFound = true
|
|
|
|
|
|
|
|
ctx.Rule.State = ctx.GetNewState()
|
|
|
|
So(ctx.Rule.State, ShouldEqual, models.AlertStateOK)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("pending -> no_data(keep_last)", func() {
|
|
|
|
ctx.PrevAlertState = models.AlertStatePending
|
|
|
|
ctx.Rule.NoDataState = models.NoDataKeepState
|
|
|
|
ctx.NoDataFound = true
|
|
|
|
|
|
|
|
ctx.Rule.State = ctx.GetNewState()
|
|
|
|
So(ctx.Rule.State, ShouldEqual, models.AlertStatePending)
|
2018-03-21 14:48:29 -05:00
|
|
|
})
|
2016-11-04 05:28:12 -05:00
|
|
|
})
|
|
|
|
}
|