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-26 10:26:02 -05:00
|
|
|
journals *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",
|
|
|
|
newState: m.AlertStatePending,
|
|
|
|
prevState: m.AlertStateOK,
|
|
|
|
sendReminder: false,
|
2018-09-26 10:26:02 -05:00
|
|
|
journals: &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",
|
|
|
|
newState: m.AlertStateOK,
|
|
|
|
prevState: m.AlertStateAlerting,
|
|
|
|
sendReminder: false,
|
2018-09-26 10:26:02 -05:00
|
|
|
journals: &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",
|
|
|
|
newState: m.AlertStateOK,
|
|
|
|
prevState: m.AlertStatePending,
|
|
|
|
sendReminder: false,
|
2018-09-26 10:26:02 -05:00
|
|
|
journals: &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-26 10:26:02 -05:00
|
|
|
journals: &m.AlertNotificationState{},
|
2018-09-24 09:16:10 -05:00
|
|
|
|
|
|
|
expect: false,
|
2018-06-15 09:27:20 -05:00
|
|
|
},
|
|
|
|
{
|
2018-09-24 09:16:10 -05:00
|
|
|
name: "ok -> alerting should trigger an notification",
|
2018-06-15 09:27:20 -05:00
|
|
|
newState: m.AlertStateOK,
|
|
|
|
prevState: m.AlertStateAlerting,
|
|
|
|
sendReminder: true,
|
2018-09-26 10:26:02 -05:00
|
|
|
journals: &m.AlertNotificationState{},
|
2018-09-24 09:16:10 -05:00
|
|
|
|
|
|
|
expect: true,
|
2018-06-15 09:27:20 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ok -> ok with reminder should not trigger an notification",
|
|
|
|
newState: m.AlertStateOK,
|
|
|
|
prevState: m.AlertStateOK,
|
|
|
|
sendReminder: true,
|
2018-09-26 10:26:02 -05:00
|
|
|
journals: &m.AlertNotificationState{},
|
2018-09-24 09:16:10 -05:00
|
|
|
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "alerting -> alerting with reminder and no journaling should trigger",
|
|
|
|
newState: m.AlertStateAlerting,
|
|
|
|
prevState: m.AlertStateAlerting,
|
|
|
|
frequency: time.Minute * 10,
|
|
|
|
sendReminder: true,
|
2018-09-26 10:26:02 -05:00
|
|
|
journals: &m.AlertNotificationState{},
|
2018-09-24 09:16:10 -05:00
|
|
|
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "alerting -> alerting with reminder and successful recent journal event should not trigger",
|
|
|
|
newState: m.AlertStateAlerting,
|
|
|
|
prevState: m.AlertStateAlerting,
|
|
|
|
frequency: time.Minute * 10,
|
|
|
|
sendReminder: true,
|
2018-09-26 10:26:02 -05:00
|
|
|
journals: &m.AlertNotificationState{SentAt: tnow.Add(-time.Minute).Unix()},
|
2018-09-24 09:16:10 -05:00
|
|
|
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "alerting -> alerting with reminder and failed recent journal event should trigger",
|
|
|
|
newState: m.AlertStateAlerting,
|
|
|
|
prevState: m.AlertStateAlerting,
|
|
|
|
frequency: time.Minute * 10,
|
|
|
|
sendReminder: true,
|
|
|
|
expect: true,
|
2018-09-26 10:26:02 -05:00
|
|
|
journals: &m.AlertNotificationState{SentAt: tnow.Add(-time.Hour).Unix()},
|
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-06-15 08:30:17 -05:00
|
|
|
State: tc.newState,
|
|
|
|
})
|
2018-06-26 07:13:45 -05:00
|
|
|
|
2018-06-15 09:27:20 -05:00
|
|
|
evalContext.Rule.State = tc.prevState
|
2018-09-24 09:16:10 -05:00
|
|
|
if defaultShouldNotify(evalContext, true, tc.frequency, tc.journals) != tc.expect {
|
|
|
|
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-26 07:13:45 -05:00
|
|
|
func TestShouldNotifyWhenNoJournalingIsFound(t *testing.T) {
|
|
|
|
Convey("base notifier", t, func() {
|
2018-09-28 03:48:08 -05:00
|
|
|
//bus.ClearBusHandlers()
|
|
|
|
//
|
|
|
|
//notifier := NewNotifierBase(&m.AlertNotification{
|
|
|
|
// Id: 1,
|
|
|
|
// Name: "name",
|
|
|
|
// Type: "email",
|
|
|
|
// Settings: simplejson.New(),
|
|
|
|
//})
|
|
|
|
//evalContext := alerting.NewEvalContext(context.TODO(), &alerting.Rule{})
|
|
|
|
//
|
|
|
|
//Convey("should not notify query returns error", func() {
|
|
|
|
// bus.AddHandlerCtx("", func(ctx context.Context, q *m.GetNotificationStateQuery) error {
|
|
|
|
// return errors.New("some kind of error unknown error")
|
|
|
|
// })
|
|
|
|
//
|
|
|
|
// if notifier.ShouldNotify(context.Background(), evalContext) {
|
|
|
|
// t.Errorf("should not send notifications when query returns error")
|
|
|
|
// }
|
|
|
|
//})
|
|
|
|
|
|
|
|
t.Error("might not need this anymore, at least not like this, control flow has changedd")
|
2018-06-26 07:13:45 -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
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|