2017-11-15 10:53:02 -06:00
|
|
|
package notifiers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-06-26 07:13:45 -05:00
|
|
|
"errors"
|
2017-11-15 10:53:02 -06:00
|
|
|
"testing"
|
2018-05-20 11:12:10 -05:00
|
|
|
"time"
|
2017-11-15 10:53:02 -06:00
|
|
|
|
2018-06-26 07:13:45 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
|
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) {
|
|
|
|
tcs := []struct {
|
2018-06-15 09:27:20 -05:00
|
|
|
name string
|
|
|
|
prevState m.AlertStateType
|
|
|
|
newState m.AlertStateType
|
|
|
|
expected bool
|
|
|
|
sendReminder bool
|
2018-06-15 08:30:17 -05:00
|
|
|
}{
|
|
|
|
{
|
2018-06-15 09:27:20 -05:00
|
|
|
name: "pending -> ok should not trigger an notification",
|
2018-06-15 08:30:17 -05:00
|
|
|
newState: m.AlertStatePending,
|
|
|
|
prevState: m.AlertStateOK,
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
2018-06-15 09:27:20 -05:00
|
|
|
name: "ok -> alerting should trigger an notification",
|
2018-06-15 08:30:17 -05:00
|
|
|
newState: m.AlertStateOK,
|
|
|
|
prevState: m.AlertStateAlerting,
|
|
|
|
expected: true,
|
|
|
|
},
|
2018-06-15 09:27:20 -05:00
|
|
|
{
|
|
|
|
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,
|
|
|
|
},
|
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-06-15 17:03:13 -05:00
|
|
|
if defaultShouldNotify(evalContext, true, 0, time.Now()) != tc.expected {
|
2018-06-15 09:27:20 -05:00
|
|
|
t.Errorf("failed %s. expected %+v to return %v", tc.name, tc, tc.expected)
|
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() {
|
|
|
|
bus.ClearBusHandlers()
|
|
|
|
|
|
|
|
notifier := NewNotifierBase(&m.AlertNotification{
|
|
|
|
Id: 1,
|
|
|
|
Name: "name",
|
|
|
|
Type: "email",
|
|
|
|
Settings: simplejson.New(),
|
|
|
|
})
|
|
|
|
evalContext := alerting.NewEvalContext(context.TODO(), &alerting.Rule{})
|
|
|
|
|
|
|
|
Convey("should notify if no journaling is found", func() {
|
|
|
|
bus.AddHandlerCtx("", func(ctx context.Context, q *m.GetLatestNotificationQuery) error {
|
|
|
|
return m.ErrJournalingNotFound
|
|
|
|
})
|
|
|
|
|
2018-06-29 08:15:31 -05:00
|
|
|
if !notifier.ShouldNotify(context.Background(), evalContext) {
|
2018-06-26 07:13:45 -05:00
|
|
|
t.Errorf("should send notifications when ErrJournalingNotFound is returned")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("should not notify query returns error", func() {
|
|
|
|
bus.AddHandlerCtx("", func(ctx context.Context, q *m.GetLatestNotificationQuery) error {
|
|
|
|
return errors.New("some kind of error unknown error")
|
|
|
|
})
|
|
|
|
|
2018-06-29 08:15:31 -05:00
|
|
|
if notifier.ShouldNotify(context.Background(), evalContext) {
|
2018-06-26 07:13:45 -05:00
|
|
|
t.Errorf("should not send notifications when query returns error")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|