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

181 lines
4.8 KiB
Go
Raw Normal View History

package notifiers
import (
"context"
"testing"
2018-05-20 11:12:10 -05:00
"time"
"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) {
tnow := time.Now()
tcs := []struct {
2018-06-15 09:27:20 -05:00
name string
prevState m.AlertStateType
newState m.AlertStateType
sendReminder bool
frequency time.Duration
2018-09-26 10:26:02 -05:00
journals *m.AlertNotificationState
expect bool
}{
{
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{},
expect: false,
},
{
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{},
expect: true,
},
2018-06-15 09:27:20 -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{},
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{},
expect: false,
2018-06-15 09:27:20 -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{},
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{},
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{},
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()},
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
},
}
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
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-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 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
})
}
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)
})
})
}