2017-11-15 10:53:02 -06:00
|
|
|
package notifiers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
2018-05-20 11:12:10 -05:00
|
|
|
"time"
|
2017-11-15 10:53:02 -06:00
|
|
|
|
2018-03-27 02:19:14 -05:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2017-11-15 10:53:02 -06:00
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
"github.com/grafana/grafana/pkg/services/alerting"
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
)
|
|
|
|
|
2018-06-15 08:30:17 -05:00
|
|
|
func TestShouldSendAlertNotification(t *testing.T) {
|
2018-09-24 09:16:10 -05:00
|
|
|
tnow := time.Now()
|
|
|
|
|
2018-06-15 08:30:17 -05:00
|
|
|
tcs := []struct {
|
2018-06-15 09:27:20 -05:00
|
|
|
name string
|
|
|
|
prevState m.AlertStateType
|
|
|
|
newState m.AlertStateType
|
|
|
|
sendReminder bool
|
2018-09-24 09:16:10 -05:00
|
|
|
frequency time.Duration
|
2018-09-28 11:34:20 -05:00
|
|
|
state *m.AlertNotificationState
|
2018-09-24 09:16:10 -05:00
|
|
|
|
|
|
|
expect bool
|
2018-06-15 08:30:17 -05:00
|
|
|
}{
|
|
|
|
{
|
2018-09-24 09:16:10 -05:00
|
|
|
name: "pending -> ok should not trigger an notification",
|
2018-09-28 11:34:20 -05:00
|
|
|
newState: m.AlertStateOK,
|
|
|
|
prevState: m.AlertStatePending,
|
2018-09-24 09:16:10 -05:00
|
|
|
sendReminder: false,
|
2018-09-28 11:34:20 -05:00
|
|
|
state: &m.AlertNotificationState{},
|
2018-09-24 09:16:10 -05:00
|
|
|
|
|
|
|
expect: false,
|
2018-06-15 08:30:17 -05:00
|
|
|
},
|
|
|
|
{
|
2018-09-24 09:16:10 -05:00
|
|
|
name: "ok -> alerting should trigger an notification",
|
2018-09-28 11:34:20 -05:00
|
|
|
newState: m.AlertStateAlerting,
|
|
|
|
prevState: m.AlertStateOK,
|
2018-09-24 09:16:10 -05:00
|
|
|
sendReminder: false,
|
2018-09-28 11:34:20 -05:00
|
|
|
state: &m.AlertNotificationState{},
|
2018-09-24 09:16:10 -05:00
|
|
|
|
|
|
|
expect: true,
|
2018-06-15 08:30:17 -05:00
|
|
|
},
|
2018-06-15 09:27:20 -05:00
|
|
|
{
|
2018-09-24 09:16:10 -05:00
|
|
|
name: "ok -> pending should not trigger an notification",
|
2018-09-28 11:34:20 -05:00
|
|
|
newState: m.AlertStatePending,
|
|
|
|
prevState: m.AlertStateOK,
|
2018-09-24 09:16:10 -05:00
|
|
|
sendReminder: false,
|
2018-09-28 11:34:20 -05:00
|
|
|
state: &m.AlertNotificationState{},
|
2018-09-24 09:16:10 -05:00
|
|
|
|
|
|
|
expect: false,
|
2018-06-15 09:27:20 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ok -> ok should not trigger an notification",
|
|
|
|
newState: m.AlertStateOK,
|
|
|
|
prevState: m.AlertStateOK,
|
|
|
|
sendReminder: false,
|
2018-09-28 11:34:20 -05:00
|
|
|
state: &m.AlertNotificationState{},
|
2018-09-24 09:16:10 -05:00
|
|
|
|
|
|
|
expect: false,
|
2018-06-15 09:27:20 -05:00
|
|
|
},
|
|
|
|
{
|
2018-09-28 11:34:20 -05:00
|
|
|
name: "ok -> ok with reminder should not trigger an notification",
|
2018-06-15 09:27:20 -05:00
|
|
|
newState: m.AlertStateOK,
|
2018-09-28 11:34:20 -05:00
|
|
|
prevState: m.AlertStateOK,
|
2018-06-15 09:27:20 -05:00
|
|
|
sendReminder: true,
|
2018-09-28 11:34:20 -05:00
|
|
|
state: &m.AlertNotificationState{},
|
|
|
|
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "alerting -> ok should trigger an notification",
|
|
|
|
newState: m.AlertStateOK,
|
|
|
|
prevState: m.AlertStateAlerting,
|
|
|
|
sendReminder: false,
|
|
|
|
state: &m.AlertNotificationState{},
|
2018-09-24 09:16:10 -05:00
|
|
|
|
|
|
|
expect: true,
|
2018-06-15 09:27:20 -05:00
|
|
|
},
|
|
|
|
{
|
2018-09-28 11:34:20 -05:00
|
|
|
name: "alerting -> ok should trigger an notification when reminders enabled",
|
2018-06-15 09:27:20 -05:00
|
|
|
newState: m.AlertStateOK,
|
2018-09-28 11:34:20 -05:00
|
|
|
prevState: m.AlertStateAlerting,
|
|
|
|
frequency: time.Minute * 10,
|
2018-06-15 09:27:20 -05:00
|
|
|
sendReminder: true,
|
2018-09-28 11:34:20 -05:00
|
|
|
state: &m.AlertNotificationState{SentAt: tnow.Add(-time.Minute).Unix()},
|
2018-09-24 09:16:10 -05:00
|
|
|
|
2018-09-28 11:34:20 -05:00
|
|
|
expect: true,
|
2018-09-24 09:16:10 -05:00
|
|
|
},
|
|
|
|
{
|
2018-09-28 11:34:20 -05:00
|
|
|
name: "alerting -> alerting with reminder and no state should trigger",
|
2018-09-24 09:16:10 -05:00
|
|
|
newState: m.AlertStateAlerting,
|
|
|
|
prevState: m.AlertStateAlerting,
|
|
|
|
frequency: time.Minute * 10,
|
|
|
|
sendReminder: true,
|
2018-09-28 11:34:20 -05:00
|
|
|
state: &m.AlertNotificationState{},
|
2018-09-24 09:16:10 -05:00
|
|
|
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2018-09-28 11:34:20 -05:00
|
|
|
name: "alerting -> alerting with reminder and last notification sent 1 minute ago should not trigger",
|
2018-09-24 09:16:10 -05:00
|
|
|
newState: m.AlertStateAlerting,
|
|
|
|
prevState: m.AlertStateAlerting,
|
|
|
|
frequency: time.Minute * 10,
|
|
|
|
sendReminder: true,
|
2018-09-28 11:34:20 -05:00
|
|
|
state: &m.AlertNotificationState{SentAt: tnow.Add(-time.Minute).Unix()},
|
2018-09-24 09:16:10 -05:00
|
|
|
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
2018-09-28 11:34:20 -05:00
|
|
|
name: "alerting -> alerting with reminder and last notifciation sent 11 minutes ago should trigger",
|
2018-09-24 09:16:10 -05:00
|
|
|
newState: m.AlertStateAlerting,
|
|
|
|
prevState: m.AlertStateAlerting,
|
|
|
|
frequency: time.Minute * 10,
|
|
|
|
sendReminder: true,
|
2018-09-28 11:34:20 -05:00
|
|
|
state: &m.AlertNotificationState{SentAt: tnow.Add(-11 * time.Minute).Unix()},
|
|
|
|
|
2018-09-30 14:52:50 -05:00
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "OK -> alerting with notifciation state pending and updated 30 seconds ago should not trigger",
|
|
|
|
newState: m.AlertStateAlerting,
|
|
|
|
prevState: m.AlertStateOK,
|
|
|
|
state: &m.AlertNotificationState{State: m.AlertNotificationStatePending, UpdatedAt: tnow.Add(-30 * time.Second).Unix()},
|
|
|
|
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "OK -> alerting with notifciation state pending and updated 2 minutes ago should trigger",
|
|
|
|
newState: m.AlertStateAlerting,
|
|
|
|
prevState: m.AlertStateOK,
|
|
|
|
state: &m.AlertNotificationState{State: m.AlertNotificationStatePending, UpdatedAt: tnow.Add(-2 * time.Minute).Unix()},
|
|
|
|
|
2018-09-28 11:34:20 -05:00
|
|
|
expect: true,
|
2018-06-15 09:27:20 -05:00
|
|
|
},
|
2018-06-15 08:30:17 -05:00
|
|
|
}
|
2018-06-05 03:27:29 -05:00
|
|
|
|
2018-06-15 08:30:17 -05:00
|
|
|
for _, tc := range tcs {
|
2018-06-15 09:27:20 -05:00
|
|
|
evalContext := alerting.NewEvalContext(context.TODO(), &alerting.Rule{
|
2018-09-28 11:34:20 -05:00
|
|
|
State: tc.prevState,
|
2018-06-15 08:30:17 -05:00
|
|
|
})
|
2018-06-26 07:13:45 -05:00
|
|
|
|
2018-09-28 11:34:20 -05:00
|
|
|
evalContext.Rule.State = tc.newState
|
|
|
|
if defaultShouldNotify(evalContext, tc.sendReminder, tc.frequency, tc.state) != tc.expect {
|
2018-09-24 09:16:10 -05:00
|
|
|
t.Errorf("failed test %s.\n expected \n%+v \nto return: %v", tc.name, tc, tc.expect)
|
2018-06-15 08:30:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-27 02:19:14 -05:00
|
|
|
|
2018-06-15 08:30:17 -05:00
|
|
|
func TestBaseNotifier(t *testing.T) {
|
|
|
|
Convey("default constructor for notifiers", t, func() {
|
|
|
|
bJson := simplejson.New()
|
2018-03-27 02:19:14 -05:00
|
|
|
|
2018-06-15 08:30:17 -05:00
|
|
|
model := &m.AlertNotification{
|
|
|
|
Id: 1,
|
|
|
|
Name: "name",
|
|
|
|
Type: "email",
|
|
|
|
Settings: bJson,
|
|
|
|
}
|
2018-03-27 02:19:14 -05:00
|
|
|
|
2018-06-15 08:30:17 -05:00
|
|
|
Convey("can parse false value", func() {
|
|
|
|
bJson.Set("uploadImage", false)
|
2018-03-27 02:19:14 -05:00
|
|
|
|
2018-06-15 08:30:17 -05:00
|
|
|
base := NewNotifierBase(model)
|
|
|
|
So(base.UploadImage, ShouldBeFalse)
|
2018-03-27 02:19:14 -05:00
|
|
|
})
|
|
|
|
|
2018-06-15 08:30:17 -05:00
|
|
|
Convey("can parse true value", func() {
|
|
|
|
bJson.Set("uploadImage", true)
|
|
|
|
|
|
|
|
base := NewNotifierBase(model)
|
|
|
|
So(base.UploadImage, ShouldBeTrue)
|
|
|
|
})
|
2017-11-15 10:53:02 -06:00
|
|
|
|
2018-06-15 08:30:17 -05:00
|
|
|
Convey("default value should be true for backwards compatibility", func() {
|
|
|
|
base := NewNotifierBase(model)
|
|
|
|
So(base.UploadImage, ShouldBeTrue)
|
2017-11-15 10:53:02 -06:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|