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

@ -78,6 +78,10 @@ func (n *NotifierBase) ShouldNotify(c *alerting.EvalContext) bool {
return false
}
// this currently serves two purposes.
// 1. make sure failed notifications try again
// 2. make sure we send notifications if no previous exist
// this should be refactored //Carl Bergquist
if !cmd.Result.Success {
return true
}

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)
}
}
}

View File

@ -262,7 +262,7 @@ func GetLatestNotification(ctx context.Context, cmd *m.GetLatestNotificationQuer
func CleanNotificationJournal(ctx context.Context, cmd *m.CleanNotificationJournalCommand) error {
return inTransactionCtx(ctx, func(sess *DBSession) error {
sql := "DELETE FROM alert_notification_journal WHERE notification_journal.org_id = ? AND alert_notification_journal.alert_id = ? AND alert_notification_journal.notifier_id = ?"
sql := "DELETE FROM alert_notification_journal WHERE alert_notification_journal.org_id = ? AND alert_notification_journal.alert_id = ? AND alert_notification_journal.notifier_id = ?"
_, err := sess.Exec(sql, cmd.OrgId, cmd.AlertId, cmd.NotifierId)
return err
})