tests for defaultShouldNotify

This commit is contained in:
bergquist
2018-06-15 16:27:20 +02:00
parent f4b089d551
commit 12bf5c225a
3 changed files with 43 additions and 8 deletions

View File

@@ -13,30 +13,61 @@ import (
func TestShouldSendAlertNotification(t *testing.T) {
tcs := []struct {
prevState m.AlertStateType
newState m.AlertStateType
expected bool
name string
prevState m.AlertStateType
newState m.AlertStateType
expected bool
sendReminder bool
}{
{
name: "pending -> ok should not trigger an notification",
newState: m.AlertStatePending,
prevState: m.AlertStateOK,
expected: false,
},
{
name: "ok -> alerting should trigger an notification",
newState: m.AlertStateOK,
prevState: m.AlertStateAlerting,
expected: true,
},
{
name: "ok -> pending should not trigger an notification",
newState: m.AlertStateOK,
prevState: m.AlertStatePending,
expected: false,
},
{
name: "ok -> ok should not trigger an notification",
newState: m.AlertStateOK,
prevState: m.AlertStateOK,
expected: false,
sendReminder: false,
},
{
name: "ok -> alerting should not trigger an notification",
newState: m.AlertStateOK,
prevState: m.AlertStateAlerting,
expected: true,
sendReminder: true,
},
{
name: "ok -> ok with reminder should not trigger an notification",
newState: m.AlertStateOK,
prevState: m.AlertStateOK,
expected: false,
sendReminder: true,
},
}
for _, tc := range tcs {
context := alerting.NewEvalContext(context.TODO(), &alerting.Rule{
evalContext := alerting.NewEvalContext(context.TODO(), &alerting.Rule{
State: tc.newState,
})
context.Rule.State = tc.prevState
evalContext.Rule.State = tc.prevState
timeNow := time.Now()
if defaultShouldNotify(context, true, 0, &timeNow) != tc.expected {
t.Errorf("expected %v to return %v", tc, tc.expected)
if defaultShouldNotify(evalContext, true, 0, &timeNow) != tc.expected {
t.Errorf("failed %s. expected %+v to return %v", tc.name, tc, tc.expected)
}
}
}