grafana/pkg/services/alerting/notifiers/base_test.go

143 lines
3.6 KiB
Go
Raw Normal View History

package notifiers
import (
"context"
2018-06-26 07:13:45 -05:00
"errors"
"testing"
2018-05-20 11:12:10 -05:00
"time"
2018-06-26 07:13:45 -05:00
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/simplejson"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
. "github.com/smartystreets/goconvey/convey"
)
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 09:27:20 -05:00
name: "pending -> ok should not trigger an notification",
newState: m.AlertStatePending,
prevState: m.AlertStateOK,
expected: false,
},
{
2018-06-15 09:27:20 -05:00
name: "ok -> alerting should trigger an notification",
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,
},
}
for _, tc := range tcs {
2018-06-15 09:27:20 -05:00
evalContext := alerting.NewEvalContext(context.TODO(), &alerting.Rule{
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-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
})
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")
})
if notifier.ShouldNotify(context.Background(), evalContext) {
2018-06-26 07:13:45 -05:00
t.Errorf("should not send notifications when query returns error")
}
})
})
}
func TestBaseNotifier(t *testing.T) {
Convey("default constructor for notifiers", t, func() {
bJson := simplejson.New()
model := &m.AlertNotification{
Id: 1,
Name: "name",
Type: "email",
Settings: bJson,
}
Convey("can parse false value", func() {
bJson.Set("uploadImage", false)
base := NewNotifierBase(model)
So(base.UploadImage, ShouldBeFalse)
})
Convey("can parse true value", func() {
bJson.Set("uploadImage", true)
base := NewNotifierBase(model)
So(base.UploadImage, ShouldBeTrue)
})
Convey("default value should be true for backwards compatibility", func() {
base := NewNotifierBase(model)
So(base.UploadImage, ShouldBeTrue)
})
})
}