notifications: make journaling ctx aware

This commit is contained in:
bergquist
2018-06-15 15:30:17 +02:00
parent 7632983c62
commit f4b089d551
4 changed files with 66 additions and 53 deletions
+51 -43
View File
@@ -11,56 +11,64 @@ import (
. "github.com/smartystreets/goconvey/convey"
)
func TestShouldSendAlertNotification(t *testing.T) {
tcs := []struct {
prevState m.AlertStateType
newState m.AlertStateType
expected bool
}{
{
newState: m.AlertStatePending,
prevState: m.AlertStateOK,
expected: false,
},
{
newState: m.AlertStateOK,
prevState: m.AlertStateAlerting,
expected: true,
},
}
for _, tc := range tcs {
context := alerting.NewEvalContext(context.TODO(), &alerting.Rule{
State: tc.newState,
})
context.Rule.State = tc.prevState
timeNow := time.Now()
if defaultShouldNotify(context, true, 0, &timeNow) != tc.expected {
t.Errorf("expected %v to return %v", tc, tc.expected)
}
}
}
func TestBaseNotifier(t *testing.T) {
Convey("Base notifier tests", t, func() {
Convey("default constructor for notifiers", func() {
bJson := simplejson.New()
Convey("default constructor for notifiers", t, func() {
bJson := simplejson.New()
model := &m.AlertNotification{
Id: 1,
Name: "name",
Type: "email",
Settings: bJson,
}
model := &m.AlertNotification{
Id: 1,
Name: "name",
Type: "email",
Settings: bJson,
}
Convey("can parse false value", func() {
bJson.Set("uploadImage", false)
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)
})
base := NewNotifierBase(model)
So(base.UploadImage, ShouldBeFalse)
})
Convey("should notify", func() {
Convey("pending -> ok", func() {
context := alerting.NewEvalContext(context.TODO(), &alerting.Rule{
State: m.AlertStatePending,
})
context.Rule.State = m.AlertStateOK
timeNow := time.Now()
So(defaultShouldNotify(context, true, 0, &timeNow), ShouldBeFalse)
})
Convey("can parse true value", func() {
bJson.Set("uploadImage", true)
Convey("ok -> alerting", func() {
context := alerting.NewEvalContext(context.TODO(), &alerting.Rule{
State: m.AlertStateOK,
})
context.Rule.State = m.AlertStateAlerting
timeNow := time.Now()
So(defaultShouldNotify(context, true, 0, &timeNow), ShouldBeTrue)
})
base := NewNotifierBase(model)
So(base.UploadImage, ShouldBeTrue)
})
Convey("default value should be true for backwards compatibility", func() {
base := NewNotifierBase(model)
So(base.UploadImage, ShouldBeTrue)
})
})
}
+1 -1
View File
@@ -95,7 +95,7 @@ func (handler *DefaultResultHandler) Handle(evalContext *EvalContext) error {
NotifierId: notifierId,
OrgId: evalContext.Rule.OrgId,
}
if err := bus.Dispatch(cmd); err != nil {
if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
handler.log.Error("Failed to clean up old notification records", "notifier", notifierId, "alert", evalContext.Rule.Id, "Error", err)
}
}