2016-11-04 05:28:12 -05:00
|
|
|
package alerting
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-11-01 10:04:38 -05:00
|
|
|
"errors"
|
2016-11-04 05:28:12 -05:00
|
|
|
"testing"
|
2018-11-01 10:04:38 -05:00
|
|
|
"time"
|
2016-11-04 05:28:12 -05:00
|
|
|
|
2021-02-03 13:47:45 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/validations"
|
|
|
|
|
2021-01-19 15:02:44 -06:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-05-07 03:34:00 -05:00
|
|
|
|
2016-11-04 05:28:12 -05:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2021-01-19 15:02:44 -06:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-11-04 05:28:12 -05:00
|
|
|
)
|
|
|
|
|
2018-05-25 09:12:34 -05:00
|
|
|
func TestStateIsUpdatedWhenNeeded(t *testing.T) {
|
2021-02-03 13:47:45 -06:00
|
|
|
ctx := NewEvalContext(context.TODO(), &Rule{Conditions: []Condition{&conditionStub{firing: true}}}, &validations.OSSPluginRequestValidator{})
|
2018-05-25 09:12:34 -05:00
|
|
|
|
|
|
|
t.Run("ok -> alerting", func(t *testing.T) {
|
|
|
|
ctx.PrevAlertState = models.AlertStateOK
|
|
|
|
ctx.Rule.State = models.AlertStateAlerting
|
|
|
|
|
2019-05-20 05:13:32 -05:00
|
|
|
if !ctx.shouldUpdateAlertState() {
|
2018-05-25 09:12:34 -05:00
|
|
|
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
|
|
|
|
|
2019-05-20 05:13:32 -05:00
|
|
|
if ctx.shouldUpdateAlertState() {
|
2018-05-25 09:12:34 -05:00
|
|
|
t.Fatalf("expected should updated to be false")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-11-01 10:04:38 -05:00
|
|
|
func TestGetStateFromEvalContext(t *testing.T) {
|
|
|
|
tcs := []struct {
|
|
|
|
name string
|
|
|
|
expected models.AlertStateType
|
|
|
|
applyFn func(ec *EvalContext)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "ok -> alerting",
|
|
|
|
expected: models.AlertStateAlerting,
|
|
|
|
applyFn: func(ec *EvalContext) {
|
|
|
|
ec.Firing = true
|
|
|
|
ec.PrevAlertState = models.AlertStateOK
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ok -> error(alerting)",
|
|
|
|
expected: models.AlertStateAlerting,
|
|
|
|
applyFn: func(ec *EvalContext) {
|
|
|
|
ec.PrevAlertState = models.AlertStateOK
|
|
|
|
ec.Error = errors.New("test error")
|
|
|
|
ec.Rule.ExecutionErrorState = models.ExecutionErrorSetAlerting
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ok -> pending. since its been firing for less than FOR",
|
|
|
|
expected: models.AlertStatePending,
|
|
|
|
applyFn: func(ec *EvalContext) {
|
|
|
|
ec.PrevAlertState = models.AlertStateOK
|
|
|
|
ec.Firing = true
|
|
|
|
ec.Rule.LastStateChange = time.Now().Add(-time.Minute * 2)
|
2018-11-05 04:05:30 -06:00
|
|
|
ec.Rule.For = time.Minute * 5
|
2018-11-01 10:04:38 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2018-11-15 08:37:46 -06:00
|
|
|
name: "ok -> pending. since it has to be pending longer than FOR and prev state is ok",
|
|
|
|
expected: models.AlertStatePending,
|
2018-11-01 10:04:38 -05:00
|
|
|
applyFn: func(ec *EvalContext) {
|
|
|
|
ec.PrevAlertState = models.AlertStateOK
|
|
|
|
ec.Firing = true
|
|
|
|
ec.Rule.LastStateChange = time.Now().Add(-(time.Hour * 5))
|
2018-11-05 04:05:30 -06:00
|
|
|
ec.Rule.For = time.Minute * 2
|
2018-11-01 10:04:38 -05:00
|
|
|
},
|
|
|
|
},
|
2018-11-15 08:37:46 -06:00
|
|
|
{
|
|
|
|
name: "pending -> alerting. since its been firing for more than FOR and prev state is pending",
|
|
|
|
expected: models.AlertStateAlerting,
|
|
|
|
applyFn: func(ec *EvalContext) {
|
|
|
|
ec.PrevAlertState = models.AlertStatePending
|
|
|
|
ec.Firing = true
|
|
|
|
ec.Rule.LastStateChange = time.Now().Add(-(time.Hour * 5))
|
|
|
|
ec.Rule.For = time.Minute * 2
|
|
|
|
},
|
|
|
|
},
|
2018-11-01 10:04:38 -05:00
|
|
|
{
|
|
|
|
name: "alerting -> alerting. should not update regardless of FOR",
|
|
|
|
expected: models.AlertStateAlerting,
|
|
|
|
applyFn: func(ec *EvalContext) {
|
|
|
|
ec.PrevAlertState = models.AlertStateAlerting
|
|
|
|
ec.Firing = true
|
|
|
|
ec.Rule.LastStateChange = time.Now().Add(-time.Minute * 5)
|
2018-11-05 04:05:30 -06:00
|
|
|
ec.Rule.For = time.Minute * 2
|
2018-11-01 10:04:38 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ok -> ok. should not update regardless of FOR",
|
|
|
|
expected: models.AlertStateOK,
|
|
|
|
applyFn: func(ec *EvalContext) {
|
|
|
|
ec.PrevAlertState = models.AlertStateOK
|
|
|
|
ec.Rule.LastStateChange = time.Now().Add(-time.Minute * 5)
|
2018-11-05 04:05:30 -06:00
|
|
|
ec.Rule.For = time.Minute * 2
|
2018-11-01 10:04:38 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ok -> error(keep_last)",
|
|
|
|
expected: models.AlertStateOK,
|
|
|
|
applyFn: func(ec *EvalContext) {
|
|
|
|
ec.PrevAlertState = models.AlertStateOK
|
|
|
|
ec.Error = errors.New("test error")
|
|
|
|
ec.Rule.ExecutionErrorState = models.ExecutionErrorKeepState
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "pending -> error(keep_last)",
|
|
|
|
expected: models.AlertStatePending,
|
|
|
|
applyFn: func(ec *EvalContext) {
|
|
|
|
ec.PrevAlertState = models.AlertStatePending
|
|
|
|
ec.Error = errors.New("test error")
|
|
|
|
ec.Rule.ExecutionErrorState = models.ExecutionErrorKeepState
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ok -> no_data(alerting)",
|
|
|
|
expected: models.AlertStateAlerting,
|
|
|
|
applyFn: func(ec *EvalContext) {
|
|
|
|
ec.PrevAlertState = models.AlertStateOK
|
|
|
|
ec.Rule.NoDataState = models.NoDataSetAlerting
|
|
|
|
ec.NoDataFound = true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ok -> no_data(keep_last)",
|
|
|
|
expected: models.AlertStateOK,
|
|
|
|
applyFn: func(ec *EvalContext) {
|
|
|
|
ec.PrevAlertState = models.AlertStateOK
|
|
|
|
ec.Rule.NoDataState = models.NoDataKeepState
|
|
|
|
ec.NoDataFound = true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "pending -> no_data(keep_last)",
|
|
|
|
expected: models.AlertStatePending,
|
|
|
|
applyFn: func(ec *EvalContext) {
|
|
|
|
ec.PrevAlertState = models.AlertStatePending
|
|
|
|
ec.Rule.NoDataState = models.NoDataKeepState
|
|
|
|
ec.NoDataFound = true
|
|
|
|
},
|
|
|
|
},
|
2018-11-14 16:39:44 -06:00
|
|
|
{
|
|
|
|
name: "pending -> no_data(alerting) with for duration have not passed",
|
|
|
|
expected: models.AlertStatePending,
|
|
|
|
applyFn: func(ec *EvalContext) {
|
|
|
|
ec.PrevAlertState = models.AlertStatePending
|
|
|
|
ec.Rule.NoDataState = models.NoDataSetAlerting
|
|
|
|
ec.NoDataFound = true
|
|
|
|
ec.Rule.For = time.Minute * 5
|
|
|
|
ec.Rule.LastStateChange = time.Now().Add(-time.Minute * 2)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "pending -> no_data(alerting) should set alerting since time passed FOR",
|
|
|
|
expected: models.AlertStateAlerting,
|
|
|
|
applyFn: func(ec *EvalContext) {
|
|
|
|
ec.PrevAlertState = models.AlertStatePending
|
|
|
|
ec.Rule.NoDataState = models.NoDataSetAlerting
|
|
|
|
ec.NoDataFound = true
|
|
|
|
ec.Rule.For = time.Minute * 2
|
|
|
|
ec.Rule.LastStateChange = time.Now().Add(-time.Minute * 5)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "pending -> error(alerting) with for duration have not passed ",
|
|
|
|
expected: models.AlertStatePending,
|
|
|
|
applyFn: func(ec *EvalContext) {
|
|
|
|
ec.PrevAlertState = models.AlertStatePending
|
|
|
|
ec.Rule.ExecutionErrorState = models.ExecutionErrorSetAlerting
|
|
|
|
ec.Error = errors.New("test error")
|
|
|
|
ec.Rule.For = time.Minute * 5
|
|
|
|
ec.Rule.LastStateChange = time.Now().Add(-time.Minute * 2)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "pending -> error(alerting) should set alerting since time passed FOR",
|
|
|
|
expected: models.AlertStateAlerting,
|
|
|
|
applyFn: func(ec *EvalContext) {
|
|
|
|
ec.PrevAlertState = models.AlertStatePending
|
|
|
|
ec.Rule.ExecutionErrorState = models.ExecutionErrorSetAlerting
|
|
|
|
ec.Error = errors.New("test error")
|
|
|
|
ec.Rule.For = time.Minute * 2
|
|
|
|
ec.Rule.LastStateChange = time.Now().Add(-time.Minute * 5)
|
|
|
|
},
|
|
|
|
},
|
2018-11-01 10:04:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tcs {
|
2021-02-03 13:47:45 -06:00
|
|
|
evalContext := NewEvalContext(context.Background(), &Rule{Conditions: []Condition{&conditionStub{firing: true}}}, &validations.OSSPluginRequestValidator{})
|
2016-11-04 05:28:12 -05:00
|
|
|
|
2019-05-07 03:34:00 -05:00
|
|
|
tc.applyFn(evalContext)
|
|
|
|
newState := evalContext.GetNewState()
|
|
|
|
assert.Equal(t, tc.expected, newState, "failed: %s \n expected '%s' have '%s'\n", tc.name, tc.expected, string(newState))
|
2018-11-01 10:04:38 -05:00
|
|
|
}
|
2016-11-04 05:28:12 -05:00
|
|
|
}
|
2021-01-19 15:02:44 -06:00
|
|
|
|
|
|
|
func TestBuildTemplateDataMap(t *testing.T) {
|
|
|
|
tcs := []struct {
|
|
|
|
name string
|
|
|
|
matches []*EvalMatch
|
|
|
|
expected map[string]string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "single match",
|
|
|
|
matches: []*EvalMatch{
|
|
|
|
{
|
|
|
|
Tags: map[string]string{
|
|
|
|
"InstanceId": "i-123456789",
|
|
|
|
"Percentile": "0.999",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: map[string]string{
|
|
|
|
"InstanceId": "i-123456789",
|
|
|
|
"Percentile": "0.999",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "matches with duplicate keys",
|
|
|
|
matches: []*EvalMatch{
|
|
|
|
{
|
|
|
|
Tags: map[string]string{
|
|
|
|
"InstanceId": "i-123456789",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Tags: map[string]string{
|
|
|
|
"InstanceId": "i-987654321",
|
|
|
|
"Percentile": "0.999",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: map[string]string{
|
|
|
|
"InstanceId": "i-123456789, i-987654321",
|
|
|
|
"Percentile": "0.999",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "matches with duplicate keys and values",
|
|
|
|
matches: []*EvalMatch{
|
|
|
|
{
|
|
|
|
Tags: map[string]string{
|
|
|
|
"InstanceId": "i-123456789",
|
|
|
|
"Percentile": "0.999",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Tags: map[string]string{
|
|
|
|
"InstanceId": "i-987654321",
|
|
|
|
"Percentile": "0.995",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Tags: map[string]string{
|
|
|
|
"InstanceId": "i-987654321",
|
|
|
|
"Percentile": "0.999",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: map[string]string{
|
|
|
|
"InstanceId": "i-123456789, i-987654321",
|
|
|
|
"Percentile": "0.999, 0.995",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "a value and its substring for same key",
|
|
|
|
matches: []*EvalMatch{
|
|
|
|
{
|
|
|
|
Tags: map[string]string{
|
|
|
|
"Percentile": "0.9990",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Tags: map[string]string{
|
|
|
|
"Percentile": "0.999",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: map[string]string{
|
|
|
|
"Percentile": "0.9990, 0.999",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tcs {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
result, err := buildTemplateDataMap(tc.matches)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, tc.expected, result, "failed: %s \n expected '%s' have '%s'\n", tc.name, tc.expected, result)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEvaluateTemplate(t *testing.T) {
|
|
|
|
tcs := []struct {
|
|
|
|
name string
|
|
|
|
message string
|
|
|
|
data map[string]string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "matching terms",
|
|
|
|
message: "Degraded ${percentile} latency on ${instance}",
|
|
|
|
data: map[string]string{
|
|
|
|
"instance": "i-123456789",
|
|
|
|
"percentile": "0.95",
|
|
|
|
},
|
|
|
|
expected: "Degraded 0.95 latency on i-123456789",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "non-matching terms",
|
|
|
|
message: "Degraded $percentile latency for endpoint ${ endpoint } on ${instance}",
|
|
|
|
data: map[string]string{
|
|
|
|
"INSTANCE": "i-123456789",
|
|
|
|
"percentile": "0.95",
|
|
|
|
"endpoint": "/api/dashboard/123",
|
|
|
|
},
|
|
|
|
expected: "Degraded $percentile latency for endpoint ${ endpoint } on ${instance}",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range tcs {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
result, err := evaluateTemplate(tc.message, tc.data)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, tc.expected, result, "failed: %s \n expected '%s' have '%s'\n", tc.name, tc.expected, result)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|