Alerting: Add tests for Evaluate (#66739)

This commit is contained in:
George Robinson 2023-04-20 11:24:40 +01:00 committed by GitHub
parent a7e74f6d6d
commit 883dcc81c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -9,6 +9,7 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/expr"
@ -547,6 +548,62 @@ func TestValidate(t *testing.T) {
}
}
func TestEvaluate(t *testing.T) {
cases := []struct {
name string
cond models.Condition
resp backend.QueryDataResponse
expected Results
error string
}{{
name: "is no data with no frames",
cond: models.Condition{
Data: []models.AlertQuery{{
RefID: "A",
DatasourceUID: "test",
}},
},
resp: backend.QueryDataResponse{
Responses: backend.Responses{
"A": {Frames: nil},
},
},
expected: Results{{
State: NoData,
Instance: data.Labels{
"datasource_uid": "test",
"ref_id": "A",
},
}},
}}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
ev := conditionEvaluator{
pipeline: nil,
expressionService: &fakeExpressionService{
hook: func(ctx context.Context, now time.Time, pipeline expr.DataPipeline) (*backend.QueryDataResponse, error) {
return &tc.resp, nil
},
},
condition: tc.cond,
}
results, err := ev.Evaluate(context.Background(), time.Now())
if tc.error != "" {
require.EqualError(t, err, tc.error)
} else {
require.NoError(t, err)
require.Len(t, results, len(tc.expected))
for i := range results {
tc.expected[i].EvaluatedAt = results[i].EvaluatedAt
tc.expected[i].EvaluationDuration = results[i].EvaluationDuration
assert.Equal(t, tc.expected[i], results[i])
}
}
})
}
}
func TestEvaluateRaw(t *testing.T) {
t.Run("should timeout if request takes too long", func(t *testing.T) {
unexpectedResponse := &backend.QueryDataResponse{}