Chore: Refactor GoConvey in alerting tests (#40845)

This commit is contained in:
Serge Zaitsev 2021-10-26 13:15:09 +02:00 committed by GitHub
parent ce8e569700
commit 5409c88fb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 266 additions and 270 deletions

View File

@ -15,11 +15,11 @@ import (
"github.com/grafana/grafana/pkg/infra/usagestats"
"github.com/grafana/grafana/pkg/services/encryption/ossencryption"
"github.com/grafana/grafana/pkg/setting"
. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/require"
)
func TestEngineTimeouts(t *testing.T) {
Convey("Alerting engine timeout tests", t, func() {
usMock := &usagestats.UsageStatsMock{T: t}
engine := ProvideAlertEngine(nil, nil, nil, nil, usMock, ossencryption.ProvideService(), setting.NewCfg())
setting.AlertingNotificationTimeout = 30 * time.Second
@ -27,8 +27,8 @@ func TestEngineTimeouts(t *testing.T) {
engine.resultHandler = &FakeResultHandler{}
job := &Job{running: true, Rule: &Rule{}}
Convey("Should trigger as many retries as needed", func() {
Convey("pended alert for datasource -> result handler should be worked", func() {
t.Run("Should trigger as many retries as needed", func(t *testing.T) {
t.Run("pended alert for datasource -> result handler should be worked", func(t *testing.T) {
// reduce alert timeout to test quickly
setting.AlertingEvaluationTimeout = 30 * time.Second
transportTimeoutInterval := 2 * time.Second
@ -40,17 +40,16 @@ func TestEngineTimeouts(t *testing.T) {
engine.resultHandler = resultHandler
err := engine.processJobWithRetry(context.TODO(), job)
So(err, ShouldBeNil)
require.Nil(t, err)
So(evalHandler.EvalSucceed, ShouldEqual, true)
So(resultHandler.ResultHandleSucceed, ShouldEqual, true)
require.Equal(t, true, evalHandler.EvalSucceed)
require.Equal(t, true, resultHandler.ResultHandleSucceed)
// initialize for other tests.
setting.AlertingEvaluationTimeout = 2 * time.Second
engine.resultHandler = &FakeResultHandler{}
})
})
})
}
type FakeCommonTimeoutHandler struct {

View File

@ -13,7 +13,8 @@ import (
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/encryption/ossencryption"
"github.com/grafana/grafana/pkg/setting"
. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/require"
)
type FakeEvalHandler struct {
@ -42,7 +43,6 @@ func (handler *FakeResultHandler) handle(evalContext *EvalContext) error {
}
func TestEngineProcessJob(t *testing.T) {
Convey("Alerting engine job processing", t, func() {
bus := bus.New()
usMock := &usagestats.UsageStatsMock{T: t}
engine := ProvideAlertEngine(nil, bus, nil, nil, usMock, ossencryption.ProvideService(), setting.NewCfg())
@ -52,7 +52,7 @@ func TestEngineProcessJob(t *testing.T) {
engine.resultHandler = &FakeResultHandler{}
job := &Job{running: true, Rule: &Rule{}}
Convey("Should register usage metrics func", func() {
t.Run("Should register usage metrics func", func(t *testing.T) {
bus.AddHandler(func(q *models.GetAllAlertsQuery) error {
settings, err := simplejson.NewJson([]byte(`{"conditions": [{"query": { "datasourceId": 1}}]}`))
if err != nil {
@ -68,14 +68,14 @@ func TestEngineProcessJob(t *testing.T) {
})
report, err := usMock.GetUsageReport(context.Background())
So(err, ShouldBeNil)
require.Nil(t, err)
So(report.Metrics["stats.alerting.ds.prometheus.count"], ShouldEqual, 1)
So(report.Metrics["stats.alerting.ds.other.count"], ShouldEqual, 0)
require.Equal(t, 1, report.Metrics["stats.alerting.ds.prometheus.count"])
require.Equal(t, 0, report.Metrics["stats.alerting.ds.other.count"])
})
Convey("Should trigger retry if needed", func() {
Convey("error + not last attempt -> retry", func() {
t.Run("Should trigger retry if needed", func(t *testing.T) {
t.Run("error + not last attempt -> retry", func(t *testing.T) {
engine.evalHandler = NewFakeEvalHandler(0)
for i := 1; i < setting.AlertingMaxAttempts; i++ {
@ -85,13 +85,13 @@ func TestEngineProcessJob(t *testing.T) {
engine.processJob(i, attemptChan, cancelChan, job)
nextAttemptID, more := <-attemptChan
So(nextAttemptID, ShouldEqual, i+1)
So(more, ShouldEqual, true)
So(<-cancelChan, ShouldNotBeNil)
require.Equal(t, i+1, nextAttemptID)
require.Equal(t, true, more)
require.NotNil(t, <-cancelChan)
}
})
Convey("error + last attempt -> no retry", func() {
t.Run("error + last attempt -> no retry", func(t *testing.T) {
engine.evalHandler = NewFakeEvalHandler(0)
attemptChan := make(chan int, 1)
cancelChan := make(chan context.CancelFunc, setting.AlertingMaxAttempts)
@ -99,12 +99,12 @@ func TestEngineProcessJob(t *testing.T) {
engine.processJob(setting.AlertingMaxAttempts, attemptChan, cancelChan, job)
nextAttemptID, more := <-attemptChan
So(nextAttemptID, ShouldEqual, 0)
So(more, ShouldEqual, false)
So(<-cancelChan, ShouldNotBeNil)
require.Equal(t, 0, nextAttemptID)
require.Equal(t, false, more)
require.NotNil(t, <-cancelChan)
})
Convey("no error -> no retry", func() {
t.Run("no error -> no retry", func(t *testing.T) {
engine.evalHandler = NewFakeEvalHandler(1)
attemptChan := make(chan int, 1)
cancelChan := make(chan context.CancelFunc, setting.AlertingMaxAttempts)
@ -112,42 +112,41 @@ func TestEngineProcessJob(t *testing.T) {
engine.processJob(1, attemptChan, cancelChan, job)
nextAttemptID, more := <-attemptChan
So(nextAttemptID, ShouldEqual, 0)
So(more, ShouldEqual, false)
So(<-cancelChan, ShouldNotBeNil)
require.Equal(t, 0, nextAttemptID)
require.Equal(t, false, more)
require.NotNil(t, <-cancelChan)
})
})
Convey("Should trigger as many retries as needed", func() {
Convey("never success -> max retries number", func() {
t.Run("Should trigger as many retries as needed", func(t *testing.T) {
t.Run("never success -> max retries number", func(t *testing.T) {
expectedAttempts := setting.AlertingMaxAttempts
evalHandler := NewFakeEvalHandler(0)
engine.evalHandler = evalHandler
err := engine.processJobWithRetry(context.TODO(), job)
So(err, ShouldBeNil)
So(evalHandler.CallNb, ShouldEqual, expectedAttempts)
require.Nil(t, err)
require.Equal(t, expectedAttempts, evalHandler.CallNb)
})
Convey("always success -> never retry", func() {
t.Run("always success -> never retry", func(t *testing.T) {
expectedAttempts := 1
evalHandler := NewFakeEvalHandler(1)
engine.evalHandler = evalHandler
err := engine.processJobWithRetry(context.TODO(), job)
So(err, ShouldBeNil)
So(evalHandler.CallNb, ShouldEqual, expectedAttempts)
require.Nil(t, err)
require.Equal(t, expectedAttempts, evalHandler.CallNb)
})
Convey("some errors before success -> some retries", func() {
t.Run("some errors before success -> some retries", func(t *testing.T) {
expectedAttempts := int(math.Ceil(float64(setting.AlertingMaxAttempts) / 2))
evalHandler := NewFakeEvalHandler(expectedAttempts)
engine.evalHandler = evalHandler
err := engine.processJobWithRetry(context.TODO(), job)
So(err, ShouldBeNil)
So(evalHandler.CallNb, ShouldEqual, expectedAttempts)
})
require.Nil(t, err)
require.Equal(t, expectedAttempts, evalHandler.CallNb)
})
})
}

View File

@ -7,7 +7,7 @@ import (
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/services/validations"
. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/require"
)
type conditionStub struct {
@ -22,10 +22,9 @@ func (c *conditionStub) Eval(context *EvalContext, reqHandler plugins.DataReques
}
func TestAlertingEvaluationHandler(t *testing.T) {
Convey("Test alert evaluation handler", t, func() {
handler := NewEvalHandler(nil)
Convey("Show return triggered with single passing condition", func() {
t.Run("Show return triggered with single passing condition", func(t *testing.T) {
context := NewEvalContext(context.TODO(), &Rule{
Conditions: []Condition{&conditionStub{
firing: true,
@ -33,21 +32,21 @@ func TestAlertingEvaluationHandler(t *testing.T) {
}, &validations.OSSPluginRequestValidator{})
handler.Eval(context)
So(context.Firing, ShouldEqual, true)
So(context.ConditionEvals, ShouldEqual, "true = true")
require.Equal(t, true, context.Firing)
require.Equal(t, "true = true", context.ConditionEvals)
})
Convey("Show return triggered with single passing condition2", func() {
t.Run("Show return triggered with single passing condition2", func(t *testing.T) {
context := NewEvalContext(context.TODO(), &Rule{
Conditions: []Condition{&conditionStub{firing: true, operator: "and"}},
}, &validations.OSSPluginRequestValidator{})
handler.Eval(context)
So(context.Firing, ShouldEqual, true)
So(context.ConditionEvals, ShouldEqual, "true = true")
require.Equal(t, true, context.Firing)
require.Equal(t, "true = true", context.ConditionEvals)
})
Convey("Show return false with not passing asdf", func() {
t.Run("Show return false with not passing asdf", func(t *testing.T) {
context := NewEvalContext(context.TODO(), &Rule{
Conditions: []Condition{
&conditionStub{firing: true, operator: "and", matches: []*EvalMatch{{}, {}}},
@ -56,11 +55,11 @@ func TestAlertingEvaluationHandler(t *testing.T) {
}, &validations.OSSPluginRequestValidator{})
handler.Eval(context)
So(context.Firing, ShouldEqual, false)
So(context.ConditionEvals, ShouldEqual, "[true AND false] = false")
require.Equal(t, false, context.Firing)
require.Equal(t, "[true AND false] = false", context.ConditionEvals)
})
Convey("Show return true if any of the condition is passing with OR operator", func() {
t.Run("Show return true if any of the condition is passing with OR operator", func(t *testing.T) {
context := NewEvalContext(context.TODO(), &Rule{
Conditions: []Condition{
&conditionStub{firing: true, operator: "and"},
@ -69,11 +68,11 @@ func TestAlertingEvaluationHandler(t *testing.T) {
}, &validations.OSSPluginRequestValidator{})
handler.Eval(context)
So(context.Firing, ShouldEqual, true)
So(context.ConditionEvals, ShouldEqual, "[true OR false] = true")
require.Equal(t, true, context.Firing)
require.Equal(t, "[true OR false] = true", context.ConditionEvals)
})
Convey("Show return false if any of the condition is failing with AND operator", func() {
t.Run("Show return false if any of the condition is failing with AND operator", func(t *testing.T) {
context := NewEvalContext(context.TODO(), &Rule{
Conditions: []Condition{
&conditionStub{firing: true, operator: "and"},
@ -82,11 +81,11 @@ func TestAlertingEvaluationHandler(t *testing.T) {
}, &validations.OSSPluginRequestValidator{})
handler.Eval(context)
So(context.Firing, ShouldEqual, false)
So(context.ConditionEvals, ShouldEqual, "[true AND false] = false")
require.Equal(t, false, context.Firing)
require.Equal(t, "[true AND false] = false", context.ConditionEvals)
})
Convey("Show return true if one condition is failing with nested OR operator", func() {
t.Run("Show return true if one condition is failing with nested OR operator", func(t *testing.T) {
context := NewEvalContext(context.TODO(), &Rule{
Conditions: []Condition{
&conditionStub{firing: true, operator: "and"},
@ -96,11 +95,11 @@ func TestAlertingEvaluationHandler(t *testing.T) {
}, &validations.OSSPluginRequestValidator{})
handler.Eval(context)
So(context.Firing, ShouldEqual, true)
So(context.ConditionEvals, ShouldEqual, "[[true AND true] OR false] = true")
require.Equal(t, true, context.Firing)
require.Equal(t, "[[true AND true] OR false] = true", context.ConditionEvals)
})
Convey("Show return false if one condition is passing with nested OR operator", func() {
t.Run("Show return false if one condition is passing with nested OR operator", func(t *testing.T) {
context := NewEvalContext(context.TODO(), &Rule{
Conditions: []Condition{
&conditionStub{firing: true, operator: "and"},
@ -110,11 +109,11 @@ func TestAlertingEvaluationHandler(t *testing.T) {
}, &validations.OSSPluginRequestValidator{})
handler.Eval(context)
So(context.Firing, ShouldEqual, false)
So(context.ConditionEvals, ShouldEqual, "[[true AND false] OR false] = false")
require.Equal(t, false, context.Firing)
require.Equal(t, "[[true AND false] OR false] = false", context.ConditionEvals)
})
Convey("Show return false if a condition is failing with nested AND operator", func() {
t.Run("Show return false if a condition is failing with nested AND operator", func(t *testing.T) {
context := NewEvalContext(context.TODO(), &Rule{
Conditions: []Condition{
&conditionStub{firing: true, operator: "and"},
@ -124,11 +123,11 @@ func TestAlertingEvaluationHandler(t *testing.T) {
}, &validations.OSSPluginRequestValidator{})
handler.Eval(context)
So(context.Firing, ShouldEqual, false)
So(context.ConditionEvals, ShouldEqual, "[[true AND false] AND true] = false")
require.Equal(t, false, context.Firing)
require.Equal(t, "[[true AND false] AND true] = false", context.ConditionEvals)
})
Convey("Show return true if a condition is passing with nested OR operator", func() {
t.Run("Show return true if a condition is passing with nested OR operator", func(t *testing.T) {
context := NewEvalContext(context.TODO(), &Rule{
Conditions: []Condition{
&conditionStub{firing: true, operator: "and"},
@ -138,11 +137,11 @@ func TestAlertingEvaluationHandler(t *testing.T) {
}, &validations.OSSPluginRequestValidator{})
handler.Eval(context)
So(context.Firing, ShouldEqual, true)
So(context.ConditionEvals, ShouldEqual, "[[true OR false] OR true] = true")
require.Equal(t, true, context.Firing)
require.Equal(t, "[[true OR false] OR true] = true", context.ConditionEvals)
})
Convey("Should return false if no condition is firing using OR operator", func() {
t.Run("Should return false if no condition is firing using OR operator", func(t *testing.T) {
context := NewEvalContext(context.TODO(), &Rule{
Conditions: []Condition{
&conditionStub{firing: false, operator: "or"},
@ -152,12 +151,12 @@ func TestAlertingEvaluationHandler(t *testing.T) {
}, &validations.OSSPluginRequestValidator{})
handler.Eval(context)
So(context.Firing, ShouldEqual, false)
So(context.ConditionEvals, ShouldEqual, "[[false OR false] OR false] = false")
require.Equal(t, false, context.Firing)
require.Equal(t, "[[false OR false] OR false] = false", context.ConditionEvals)
})
// FIXME: What should the actual test case name be here?
Convey("Should not return NoDataFound if all conditions have data and using OR", func() {
t.Run("Should not return NoDataFound if all conditions have data and using OR", func(t *testing.T) {
context := NewEvalContext(context.TODO(), &Rule{
Conditions: []Condition{
&conditionStub{operator: "or", noData: false},
@ -167,10 +166,10 @@ func TestAlertingEvaluationHandler(t *testing.T) {
}, &validations.OSSPluginRequestValidator{})
handler.Eval(context)
So(context.NoDataFound, ShouldBeFalse)
require.False(t, context.NoDataFound)
})
Convey("Should return NoDataFound if one condition has no data", func() {
t.Run("Should return NoDataFound if one condition has no data", func(t *testing.T) {
context := NewEvalContext(context.TODO(), &Rule{
Conditions: []Condition{
&conditionStub{operator: "and", noData: true},
@ -178,11 +177,11 @@ func TestAlertingEvaluationHandler(t *testing.T) {
}, &validations.OSSPluginRequestValidator{})
handler.Eval(context)
So(context.Firing, ShouldEqual, false)
So(context.NoDataFound, ShouldBeTrue)
require.Equal(t, false, context.Firing)
require.True(t, context.NoDataFound)
})
Convey("Should not return no data if at least one condition has no data and using AND", func() {
t.Run("Should not return no data if at least one condition has no data and using AND", func(t *testing.T) {
context := NewEvalContext(context.TODO(), &Rule{
Conditions: []Condition{
&conditionStub{operator: "and", noData: true},
@ -191,10 +190,10 @@ func TestAlertingEvaluationHandler(t *testing.T) {
}, &validations.OSSPluginRequestValidator{})
handler.Eval(context)
So(context.NoDataFound, ShouldBeFalse)
require.False(t, context.NoDataFound)
})
Convey("Should return no data if at least one condition has no data and using OR", func() {
t.Run("Should return no data if at least one condition has no data and using OR", func(t *testing.T) {
context := NewEvalContext(context.TODO(), &Rule{
Conditions: []Condition{
&conditionStub{operator: "or", noData: true},
@ -203,7 +202,6 @@ func TestAlertingEvaluationHandler(t *testing.T) {
}, &validations.OSSPluginRequestValidator{})
handler.Eval(context)
So(context.NoDataFound, ShouldBeTrue)
})
require.True(t, context.NoDataFound)
})
}