Added uid for alert notifications

This commit is contained in:
Pavel Bakulev
2018-12-14 11:53:50 +02:00
parent 6d1ec19fe9
commit f132e929ce
26 changed files with 533 additions and 357 deletions

View File

@@ -3,11 +3,11 @@ package alert_notifications
import (
"testing"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/models"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
"github.com/grafana/grafana/pkg/services/alerting/notifiers"
"github.com/grafana/grafana/pkg/services/sqlstore"
. "github.com/smartystreets/goconvey/convey"
)
@@ -15,7 +15,8 @@ var (
logger = log.New("fake.log")
correct_properties = "./test-configs/correct-properties"
incorrect_properties = "./test-configs/incorrect-properties"
incorrect_settings = "./test-configs/incorrect-settings"
no_required_fields = "./test-configs/no-required-fields"
correct_properties_with_orgName = "./test-configs/correct-properties-with-orgName"
brokenYaml = "./test-configs/broken-yaml"
doubleNotificationsConfig = "./test-configs/double-default"
@@ -23,19 +24,11 @@ var (
emptyFile = "./test-configs/empty"
twoNotificationsConfig = "./test-configs/two-notifications"
unknownNotifier = "./test-configs/unknown-notifier"
fakeRepo *fakeRepository
)
func TestNotificationAsConfig(t *testing.T) {
Convey("Testing notification as configuration", t, func() {
fakeRepo = &fakeRepository{}
bus.ClearBusHandlers()
bus.AddHandler("test", mockDelete)
bus.AddHandler("test", mockInsert)
bus.AddHandler("test", mockUpdate)
bus.AddHandler("test", mockGet)
bus.AddHandler("test", mockGetOrg)
sqlstore.InitTestDB(t)
alerting.RegisterNotifier(&alerting.NotifierPlugin{
Type: "slack",
@@ -63,6 +56,7 @@ func TestNotificationAsConfig(t *testing.T) {
So(nt.Name, ShouldEqual, "default-slack-notification")
So(nt.Type, ShouldEqual, "slack")
So(nt.OrgId, ShouldEqual, 2)
So(nt.Uid, ShouldEqual, "notifier1")
So(nt.IsDefault, ShouldBeTrue)
So(nt.Settings, ShouldResemble, map[string]interface{}{
"recipient": "XXX", "token": "xoxb", "uploadImage": true, "url": "https://slack.com",
@@ -72,17 +66,20 @@ func TestNotificationAsConfig(t *testing.T) {
So(nt.Name, ShouldEqual, "another-not-default-notification")
So(nt.Type, ShouldEqual, "email")
So(nt.OrgId, ShouldEqual, 3)
So(nt.Uid, ShouldEqual, "notifier2")
So(nt.IsDefault, ShouldBeFalse)
nt = nts[2]
So(nt.Name, ShouldEqual, "check-unset-is_default-is-false")
So(nt.Type, ShouldEqual, "slack")
So(nt.OrgId, ShouldEqual, 3)
So(nt.Uid, ShouldEqual, "notifier3")
So(nt.IsDefault, ShouldBeFalse)
nt = nts[3]
So(nt.Name, ShouldEqual, "Added notification with whitespaces in name")
So(nt.Type, ShouldEqual, "email")
So(nt.Uid, ShouldEqual, "notifier4")
So(nt.OrgId, ShouldEqual, 3)
deleteNts := ntCfg.DeleteNotifications
@@ -90,19 +87,23 @@ func TestNotificationAsConfig(t *testing.T) {
deleteNt := deleteNts[0]
So(deleteNt.Name, ShouldEqual, "default-slack-notification")
So(deleteNt.Uid, ShouldEqual, "notifier1")
So(deleteNt.OrgId, ShouldEqual, 2)
deleteNt = deleteNts[1]
So(deleteNt.Name, ShouldEqual, "deleted-notification-without-orgId")
So(deleteNt.OrgId, ShouldEqual, 1)
So(deleteNt.Uid, ShouldEqual, "notifier2")
deleteNt = deleteNts[2]
So(deleteNt.Name, ShouldEqual, "deleted-notification-with-0-orgId")
So(deleteNt.OrgId, ShouldEqual, 1)
So(deleteNt.Uid, ShouldEqual, "notifier3")
deleteNt = deleteNts[3]
So(deleteNt.Name, ShouldEqual, "Deleted notification with whitespaces in name")
So(deleteNt.OrgId, ShouldEqual, 1)
So(deleteNt.Uid, ShouldEqual, "notifier4")
})
Convey("One configured notification", func() {
@@ -112,23 +113,50 @@ func TestNotificationAsConfig(t *testing.T) {
if err != nil {
t.Fatalf("applyChanges return an error %v", err)
}
So(len(fakeRepo.deleted), ShouldEqual, 0)
So(len(fakeRepo.inserted), ShouldEqual, 2)
So(len(fakeRepo.updated), ShouldEqual, 0)
notificationsQuery := m.GetAllAlertNotificationsQuery{OrgId: 1}
err = sqlstore.GetAllAlertNotifications(&notificationsQuery)
So(err, ShouldBeNil)
So(notificationsQuery.Result, ShouldNotBeNil)
So(len(notificationsQuery.Result), ShouldEqual, 2)
})
Convey("One notification in database with same name", func() {
fakeRepo.loadAll = []*models.AlertNotification{
{Name: "channel1", OrgId: 1, Id: 1},
Convey("One notification in database with same name and uid", func() {
existingNotificationCmd := m.CreateAlertNotificationCommand{
Name: "channel1",
OrgId: 1,
Uid: "notifier1",
Type: "slack",
}
err := sqlstore.CreateAlertNotificationCommand(&existingNotificationCmd)
So(err, ShouldBeNil)
So(existingNotificationCmd.Result, ShouldNotBeNil)
notificationsQuery := m.GetAllAlertNotificationsQuery{OrgId: 1}
err = sqlstore.GetAllAlertNotifications(&notificationsQuery)
So(err, ShouldBeNil)
So(notificationsQuery.Result, ShouldNotBeNil)
So(len(notificationsQuery.Result), ShouldEqual, 1)
Convey("should update one notification", func() {
dc := newNotificationProvisioner(logger)
err := dc.applyChanges(twoNotificationsConfig)
err = dc.applyChanges(twoNotificationsConfig)
if err != nil {
t.Fatalf("applyChanges return an error %v", err)
}
So(len(fakeRepo.deleted), ShouldEqual, 0)
So(len(fakeRepo.inserted), ShouldEqual, 1)
So(len(fakeRepo.updated), ShouldEqual, 1)
err = sqlstore.GetAllAlertNotifications(&notificationsQuery)
So(err, ShouldBeNil)
So(notificationsQuery.Result, ShouldNotBeNil)
So(len(notificationsQuery.Result), ShouldEqual, 2)
nts := notificationsQuery.Result
nt1 := nts[0]
So(nt1.Type, ShouldEqual, "email")
So(nt1.Name, ShouldEqual, "channel1")
So(nt1.Uid, ShouldEqual, "notifier1")
nt2 := nts[1]
So(nt2.Type, ShouldEqual, "slack")
So(nt2.Name, ShouldEqual, "channel2")
So(nt2.Uid, ShouldEqual, "notifier2")
})
})
Convey("Two notifications with is_default", func() {
@@ -136,61 +164,106 @@ func TestNotificationAsConfig(t *testing.T) {
err := dc.applyChanges(doubleNotificationsConfig)
Convey("should both be inserted", func() {
So(err, ShouldBeNil)
So(len(fakeRepo.deleted), ShouldEqual, 0)
So(len(fakeRepo.inserted), ShouldEqual, 2)
So(len(fakeRepo.updated), ShouldEqual, 0)
notificationsQuery := m.GetAllAlertNotificationsQuery{OrgId: 1}
err = sqlstore.GetAllAlertNotifications(&notificationsQuery)
So(err, ShouldBeNil)
So(notificationsQuery.Result, ShouldNotBeNil)
So(len(notificationsQuery.Result), ShouldEqual, 2)
So(notificationsQuery.Result[0].IsDefault, ShouldBeTrue)
So(notificationsQuery.Result[1].IsDefault, ShouldBeTrue)
})
})
})
Convey("Two configured notification", func() {
Convey("two other notifications in database", func() {
fakeRepo.loadAll = []*models.AlertNotification{
{Name: "channel1", OrgId: 1, Id: 1},
{Name: "channel3", OrgId: 1, Id: 2},
existingNotificationCmd := m.CreateAlertNotificationCommand{
Name: "channel0",
OrgId: 1,
Uid: "notifier0",
Type: "slack",
}
err := sqlstore.CreateAlertNotificationCommand(&existingNotificationCmd)
So(err, ShouldBeNil)
existingNotificationCmd = m.CreateAlertNotificationCommand{
Name: "channel3",
OrgId: 1,
Uid: "notifier3",
Type: "slack",
}
err = sqlstore.CreateAlertNotificationCommand(&existingNotificationCmd)
So(err, ShouldBeNil)
notificationsQuery := m.GetAllAlertNotificationsQuery{OrgId: 1}
err = sqlstore.GetAllAlertNotifications(&notificationsQuery)
So(err, ShouldBeNil)
So(notificationsQuery.Result, ShouldNotBeNil)
So(len(notificationsQuery.Result), ShouldEqual, 2)
Convey("should have two new notifications", func() {
dc := newNotificationProvisioner(logger)
err := dc.applyChanges(twoNotificationsConfig)
if err != nil {
t.Fatalf("applyChanges return an error %v", err)
}
So(len(fakeRepo.deleted), ShouldEqual, 0)
So(len(fakeRepo.inserted), ShouldEqual, 1)
So(len(fakeRepo.updated), ShouldEqual, 1)
notificationsQuery = m.GetAllAlertNotificationsQuery{OrgId: 1}
err = sqlstore.GetAllAlertNotifications(&notificationsQuery)
So(err, ShouldBeNil)
So(notificationsQuery.Result, ShouldNotBeNil)
So(len(notificationsQuery.Result), ShouldEqual, 4)
})
})
})
Convey("Can read correct properties with orgName instead of orgId", func() {
fakeRepo.loadAllOrg = []*models.Org{
{Name: "Main Org. 1", Id: 1},
{Name: "Main Org. 2", Id: 2},
}
existingOrg1 := m.CreateOrgCommand{Name: "Main Org. 1"}
err := sqlstore.CreateOrg(&existingOrg1)
So(err, ShouldBeNil)
So(existingOrg1.Result, ShouldNotBeNil)
existingOrg2 := m.CreateOrgCommand{Name: "Main Org. 2"}
err = sqlstore.CreateOrg(&existingOrg2)
So(err, ShouldBeNil)
So(existingOrg2.Result, ShouldNotBeNil)
fakeRepo.loadAll = []*models.AlertNotification{
{Name: "default-slack-notification", OrgId: 1, Id: 1},
{Name: "another-not-default-notification", OrgId: 2, Id: 2},
existingNotificationCmd := m.CreateAlertNotificationCommand{
Name: "default-notification-delete",
OrgId: existingOrg2.Result.Id,
Uid: "notifier2",
Type: "slack",
}
err = sqlstore.CreateAlertNotificationCommand(&existingNotificationCmd)
So(err, ShouldBeNil)
dc := newNotificationProvisioner(logger)
err := dc.applyChanges(correct_properties_with_orgName)
err = dc.applyChanges(correct_properties_with_orgName)
if err != nil {
t.Fatalf("applyChanges return an error %v", err)
}
So(len(fakeRepo.deleted), ShouldEqual, 2)
So(len(fakeRepo.inserted), ShouldEqual, 0)
So(len(fakeRepo.updated), ShouldEqual, 2)
updated := fakeRepo.updated
nt := updated[0]
So(nt.Name, ShouldEqual, "default-slack-notification")
So(nt.OrgId, ShouldEqual, 1)
nt = updated[1]
So(nt.Name, ShouldEqual, "another-not-default-notification")
So(nt.OrgId, ShouldEqual, 2)
notificationsQuery := m.GetAllAlertNotificationsQuery{OrgId: existingOrg2.Result.Id}
err = sqlstore.GetAllAlertNotifications(&notificationsQuery)
So(err, ShouldBeNil)
So(notificationsQuery.Result, ShouldNotBeNil)
So(len(notificationsQuery.Result), ShouldEqual, 1)
nt := notificationsQuery.Result[0]
So(nt.Name, ShouldEqual, "default-notification-create")
So(nt.OrgId, ShouldEqual, existingOrg2.Result.Id)
})
Convey("Config doesn't contain required field", func() {
dc := newNotificationProvisioner(logger)
err := dc.applyChanges(no_required_fields)
So(err, ShouldNotBeNil)
errString := err.Error()
So(errString, ShouldContainSubstring, "Deleted alert notification item 1 in configuration doesn't contain required field uid")
So(errString, ShouldContainSubstring, "Deleted alert notification item 2 in configuration doesn't contain required field name")
So(errString, ShouldContainSubstring, "Added alert notification item 1 in configuration doesn't contain required field name")
So(errString, ShouldContainSubstring, "Added alert notification item 2 in configuration doesn't contain required field uid")
})
Convey("Empty yaml file", func() {
Convey("should have not changed repo", func() {
dc := newNotificationProvisioner(logger)
@@ -198,9 +271,10 @@ func TestNotificationAsConfig(t *testing.T) {
if err != nil {
t.Fatalf("applyChanges return an error %v", err)
}
So(len(fakeRepo.deleted), ShouldEqual, 0)
So(len(fakeRepo.inserted), ShouldEqual, 0)
So(len(fakeRepo.updated), ShouldEqual, 0)
notificationsQuery := m.GetAllAlertNotificationsQuery{OrgId: 1}
err = sqlstore.GetAllAlertNotifications(&notificationsQuery)
So(err, ShouldBeNil)
So(notificationsQuery.Result, ShouldBeEmpty)
})
})
Convey("Broken yaml should return error", func() {
@@ -224,49 +298,10 @@ func TestNotificationAsConfig(t *testing.T) {
Convey("Read incorrect properties", func() {
cfgProvifer := &configReader{log: log.New("test logger")}
_, err := cfgProvifer.readConfig(incorrect_properties)
_, err := cfgProvifer.readConfig(incorrect_settings)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, "Alert validation error: Could not find url property in settings")
})
})
}
type fakeRepository struct {
inserted []*models.CreateAlertNotificationCommand
deleted []*models.DeleteAlertNotificationCommand
updated []*models.UpdateAlertNotificationCommand
loadAll []*models.AlertNotification
loadAllOrg []*models.Org
}
func mockDelete(cmd *models.DeleteAlertNotificationCommand) error {
fakeRepo.deleted = append(fakeRepo.deleted, cmd)
return nil
}
func mockUpdate(cmd *models.UpdateAlertNotificationCommand) error {
fakeRepo.updated = append(fakeRepo.updated, cmd)
return nil
}
func mockInsert(cmd *models.CreateAlertNotificationCommand) error {
fakeRepo.inserted = append(fakeRepo.inserted, cmd)
return nil
}
func mockGet(cmd *models.GetAlertNotificationsQuery) error {
for _, v := range fakeRepo.loadAll {
if cmd.Name == v.Name && cmd.OrgId == v.OrgId {
cmd.Result = v
return nil
}
}
return nil
}
func mockGetOrg(cmd *models.GetOrgByNameQuery) error {
for _, v := range fakeRepo.loadAllOrg {
if cmd.Name == v.Name {
cmd.Result = v
return nil
}
}
return nil
}