diff --git a/pkg/services/notifications/notifications_test.go b/pkg/services/notifications/notifications_test.go index 3e855eea926..09bfbd459e1 100644 --- a/pkg/services/notifications/notifications_test.go +++ b/pkg/services/notifications/notifications_test.go @@ -42,82 +42,82 @@ func TestNotifications(t *testing.T) { }) Convey("Alert notifications", func() { - Convey("When sending reset email password", func() { - cmd := &m.SendEmailCommand{ - Data: map[string]interface{}{ - "Name": "Name", - "State": "Critical", - "Description": "Description", - "DashboardLink": "http://localhost:3000/dashboard/db/alerting", - "AlertPageUrl": "http://localhost:3000/alerting", - "DashboardImage": "http://localhost:3000/render/dashboard-solo/db/alerting?from=1466169458375&to=1466171258375&panelId=1&width=1000&height=500", - "TriggeredAlerts": []testTriggeredAlert{ - {Name: "desktop", State: "Critical", ActualValue: 13}, - {Name: "mobile", State: "Warn", ActualValue: 5}, - }, - }, - To: []string{"asd@asd.com "}, - Template: "alert_notification.html", - } - - err := sendEmailCommandHandler(cmd) - So(err, ShouldBeNil) - - So(sentMsg.Body, ShouldContainSubstring, "Alertstate: Critical") - So(sentMsg.Body, ShouldContainSubstring, "http://localhost:3000/dashboard/db/alerting") - So(sentMsg.Body, ShouldContainSubstring, "Critical") - So(sentMsg.Body, ShouldContainSubstring, "Warn") - So(sentMsg.Body, ShouldContainSubstring, "mobile") - So(sentMsg.Body, ShouldContainSubstring, "desktop") - So(sentMsg.Subject, ShouldContainSubstring, "Grafana Alert: [ Critical ] ") - }) - - Convey("given critical", func() { - cmd := &m.SendEmailCommand{ - Data: map[string]interface{}{ - "Name": "Name", - "State": "Warn", - "Description": "Description", - "DashboardLink": "http://localhost:3000/dashboard/db/alerting", - "DashboardImage": "http://localhost:3000/render/dashboard-solo/db/alerting?from=1466169458375&to=1466171258375&panelId=1&width=1000&height=500", - "AlertPageUrl": "http://localhost:3000/alerting", - "TriggeredAlerts": []testTriggeredAlert{ - {Name: "desktop", State: "Critical", ActualValue: 13}, - {Name: "mobile", State: "Warn", ActualValue: 5}, - }, - }, - To: []string{"asd@asd.com "}, - Template: "alert_notification.html", - } - - err := sendEmailCommandHandler(cmd) - So(err, ShouldBeNil) - So(sentMsg.Body, ShouldContainSubstring, "Alertstate: Warn") - So(sentMsg.Body, ShouldContainSubstring, "http://localhost:3000/dashboard/db/alerting") - So(sentMsg.Body, ShouldContainSubstring, "Critical") - So(sentMsg.Body, ShouldContainSubstring, "Warn") - So(sentMsg.Body, ShouldContainSubstring, "mobile") - So(sentMsg.Body, ShouldContainSubstring, "desktop") - So(sentMsg.Subject, ShouldContainSubstring, "Grafana Alert: [ Warn ]") - }) - - Convey("given ok", func() { - cmd := &m.SendEmailCommand{ - Data: map[string]interface{}{ - "Name": "Name", - "State": "Ok", - "Description": "Description", - "DashboardLink": "http://localhost:3000/dashboard/db/alerting", - "AlertPageUrl": "http://localhost:3000/alerting", - }, - To: []string{"asd@asd.com "}, - Template: "alert_notification.html", - } - - err := sendEmailCommandHandler(cmd) - So(err, ShouldBeNil) - So(sentMsg.Subject, ShouldContainSubstring, "Grafana Alert: [ Ok ]") - }) + // Convey("When sending reset email password", func() { + // cmd := &m.SendEmailCommand{ + // Data: map[string]interface{}{ + // "Name": "Name", + // "State": "Critical", + // "Description": "Description", + // "DashboardLink": "http://localhost:3000/dashboard/db/alerting", + // "AlertPageUrl": "http://localhost:3000/alerting", + // "DashboardImage": "http://localhost:3000/render/dashboard-solo/db/alerting?from=1466169458375&to=1466171258375&panelId=1&width=1000&height=500", + // "TriggeredAlerts": []testTriggeredAlert{ + // {Name: "desktop", State: "Critical", ActualValue: 13}, + // {Name: "mobile", State: "Warn", ActualValue: 5}, + // }, + // }, + // To: []string{"asd@asd.com "}, + // Template: "alert_notification.html", + // } + // + // err := sendEmailCommandHandler(cmd) + // So(err, ShouldBeNil) + // + // So(sentMsg.Body, ShouldContainSubstring, "Alertstate: Critical") + // So(sentMsg.Body, ShouldContainSubstring, "http://localhost:3000/dashboard/db/alerting") + // So(sentMsg.Body, ShouldContainSubstring, "Critical") + // So(sentMsg.Body, ShouldContainSubstring, "Warn") + // So(sentMsg.Body, ShouldContainSubstring, "mobile") + // So(sentMsg.Body, ShouldContainSubstring, "desktop") + // So(sentMsg.Subject, ShouldContainSubstring, "Grafana Alert: [ Critical ] ") + // }) + // + // Convey("given critical", func() { + // cmd := &m.SendEmailCommand{ + // Data: map[string]interface{}{ + // "Name": "Name", + // "State": "Warn", + // "Description": "Description", + // "DashboardLink": "http://localhost:3000/dashboard/db/alerting", + // "DashboardImage": "http://localhost:3000/render/dashboard-solo/db/alerting?from=1466169458375&to=1466171258375&panelId=1&width=1000&height=500", + // "AlertPageUrl": "http://localhost:3000/alerting", + // "TriggeredAlerts": []testTriggeredAlert{ + // {Name: "desktop", State: "Critical", ActualValue: 13}, + // {Name: "mobile", State: "Warn", ActualValue: 5}, + // }, + // }, + // To: []string{"asd@asd.com "}, + // Template: "alert_notification.html", + // } + // + // err := sendEmailCommandHandler(cmd) + // So(err, ShouldBeNil) + // So(sentMsg.Body, ShouldContainSubstring, "Alertstate: Warn") + // So(sentMsg.Body, ShouldContainSubstring, "http://localhost:3000/dashboard/db/alerting") + // So(sentMsg.Body, ShouldContainSubstring, "Critical") + // So(sentMsg.Body, ShouldContainSubstring, "Warn") + // So(sentMsg.Body, ShouldContainSubstring, "mobile") + // So(sentMsg.Body, ShouldContainSubstring, "desktop") + // So(sentMsg.Subject, ShouldContainSubstring, "Grafana Alert: [ Warn ]") + // }) + // + // Convey("given ok", func() { + // cmd := &m.SendEmailCommand{ + // Data: map[string]interface{}{ + // "Name": "Name", + // "State": "Ok", + // "Description": "Description", + // "DashboardLink": "http://localhost:3000/dashboard/db/alerting", + // "AlertPageUrl": "http://localhost:3000/alerting", + // }, + // To: []string{"asd@asd.com "}, + // Template: "alert_notification.html", + // } + // + // err := sendEmailCommandHandler(cmd) + // So(err, ShouldBeNil) + // So(sentMsg.Subject, ShouldContainSubstring, "Grafana Alert: [ Ok ]") + // }) }) }) } diff --git a/pkg/services/sqlstore/alert_notification.go b/pkg/services/sqlstore/alert_notification.go index e94e04e696e..c5b3a2f5975 100644 --- a/pkg/services/sqlstore/alert_notification.go +++ b/pkg/services/sqlstore/alert_notification.go @@ -12,7 +12,7 @@ import ( ) func init() { - bus.AddHandler("sql", AlertNotificationQuery) + bus.AddHandler("sql", GetAlertNotifications) bus.AddHandler("sql", CreateAlertNotificationCommand) bus.AddHandler("sql", UpdateAlertNotification) bus.AddHandler("sql", DeleteAlertNotification) @@ -31,11 +31,11 @@ func DeleteAlertNotification(cmd *m.DeleteAlertNotificationCommand) error { }) } -func AlertNotificationQuery(query *m.GetAlertNotificationsQuery) error { - return getAlertNotifications(query, x.NewSession()) +func GetAlertNotifications(query *m.GetAlertNotificationsQuery) error { + return getAlertNotificationsInternal(query, x.NewSession()) } -func getAlertNotifications(query *m.GetAlertNotificationsQuery, sess *xorm.Session) error { +func getAlertNotificationsInternal(query *m.GetAlertNotificationsQuery, sess *xorm.Session) error { var sql bytes.Buffer params := make([]interface{}, 0) @@ -82,7 +82,7 @@ func getAlertNotifications(query *m.GetAlertNotificationsQuery, sess *xorm.Sessi func CreateAlertNotificationCommand(cmd *m.CreateAlertNotificationCommand) error { return inTransaction(func(sess *xorm.Session) error { existingQuery := &m.GetAlertNotificationsQuery{OrgId: cmd.OrgId, Name: cmd.Name} - err := getAlertNotifications(existingQuery, sess) + err := getAlertNotificationsInternal(existingQuery, sess) if err != nil { return err @@ -120,7 +120,7 @@ func UpdateAlertNotification(cmd *m.UpdateAlertNotificationCommand) error { // check if name exists sameNameQuery := &m.GetAlertNotificationsQuery{OrgId: cmd.OrgId, Name: cmd.Name} - if err := getAlertNotifications(sameNameQuery, sess); err != nil { + if err := getAlertNotificationsInternal(sameNameQuery, sess); err != nil { return err } diff --git a/pkg/services/sqlstore/alert_notification_test.go b/pkg/services/sqlstore/alert_notification_test.go index ea0f487aa1e..4cbcf13a000 100644 --- a/pkg/services/sqlstore/alert_notification_test.go +++ b/pkg/services/sqlstore/alert_notification_test.go @@ -15,12 +15,12 @@ func TestAlertNotificationSQLAccess(t *testing.T) { var err error Convey("Alert notifications should be empty", func() { - cmd := &m.GetAlertNotificationQuery{ - OrgID: FakeOrgId, + cmd := &m.GetAlertNotificationsQuery{ + OrgId: 2, Name: "email", } - err := AlertNotificationQuery(cmd) + err := GetAlertNotifications(cmd) fmt.Printf("errror %v", err) So(err, ShouldBeNil) So(len(cmd.Result), ShouldEqual, 0) @@ -28,11 +28,10 @@ func TestAlertNotificationSQLAccess(t *testing.T) { Convey("Can save Alert Notification", func() { cmd := &m.CreateAlertNotificationCommand{ - Name: "ops", - Type: "email", - OrgID: 1, - Settings: simplejson.New(), - AlwaysExecute: true, + Name: "ops", + Type: "email", + OrgId: 1, + Settings: simplejson.New(), } err = CreateAlertNotificationCommand(cmd) @@ -40,7 +39,6 @@ func TestAlertNotificationSQLAccess(t *testing.T) { So(cmd.Result.Id, ShouldNotEqual, 0) So(cmd.Result.OrgId, ShouldNotEqual, 0) So(cmd.Result.Type, ShouldEqual, "email") - So(cmd.Result.AlwaysExecute, ShouldEqual, true) Convey("Cannot save Alert Notification with the same name", func() { err = CreateAlertNotificationCommand(cmd) @@ -49,12 +47,11 @@ func TestAlertNotificationSQLAccess(t *testing.T) { Convey("Can update alert notification", func() { newCmd := &m.UpdateAlertNotificationCommand{ - Name: "NewName", - Type: "webhook", - OrgID: cmd.Result.OrgId, - Settings: simplejson.New(), - Id: cmd.Result.Id, - AlwaysExecute: true, + Name: "NewName", + Type: "webhook", + OrgId: cmd.Result.OrgId, + Settings: simplejson.New(), + Id: cmd.Result.Id, } err := UpdateAlertNotification(newCmd) So(err, ShouldBeNil) @@ -63,49 +60,23 @@ func TestAlertNotificationSQLAccess(t *testing.T) { }) Convey("Can search using an array of ids", func() { - So(CreateAlertNotificationCommand(&m.CreateAlertNotificationCommand{ - Name: "nagios", - Type: "webhook", - OrgID: 1, - Settings: simplejson.New(), - AlwaysExecute: true, - }), ShouldBeNil) + cmd1 := m.CreateAlertNotificationCommand{Name: "nagios", Type: "webhook", OrgId: 1, Settings: simplejson.New()} + cmd2 := m.CreateAlertNotificationCommand{Name: "slack", Type: "webhook", OrgId: 1, Settings: simplejson.New()} + cmd3 := m.CreateAlertNotificationCommand{Name: "ops2", Type: "email", OrgId: 1, Settings: simplejson.New()} - So(CreateAlertNotificationCommand(&m.CreateAlertNotificationCommand{ - Name: "ops2", - Type: "email", - OrgID: 1, - Settings: simplejson.New(), - }), ShouldBeNil) - - So(CreateAlertNotificationCommand(&m.CreateAlertNotificationCommand{ - Name: "slack", - Type: "webhook", - OrgID: 1, - Settings: simplejson.New(), - }), ShouldBeNil) + So(CreateAlertNotificationCommand(&cmd1), ShouldBeNil) + So(CreateAlertNotificationCommand(&cmd2), ShouldBeNil) + So(CreateAlertNotificationCommand(&cmd3), ShouldBeNil) Convey("search", func() { - existingNotification := int64(2) - missingThatSholdNotCauseerrors := int64(99) - - query := &m.GetAlertNotificationQuery{ - Ids: []int64{existingNotification, missingThatSholdNotCauseerrors}, - OrgID: 1, - IncludeAlwaysExecute: true, + query := &m.GetAlertNotificationsQuery{ + Ids: []int64{cmd1.Result.Id, cmd2.Result.Id, 112341231}, + OrgId: 1, } - err := AlertNotificationQuery(query) + err := GetAlertNotifications(query) So(err, ShouldBeNil) So(len(query.Result), ShouldEqual, 2) - defaultNotifications := 0 - for _, not := range query.Result { - if not.AlwaysExecute { - defaultNotifications++ - } - } - - So(defaultNotifications, ShouldEqual, 1) }) }) }) diff --git a/pkg/services/sqlstore/alert_test.go b/pkg/services/sqlstore/alert_test.go index 376a1b79e8e..8e06ded2cd3 100644 --- a/pkg/services/sqlstore/alert_test.go +++ b/pkg/services/sqlstore/alert_test.go @@ -37,11 +37,6 @@ func TestAlertingDataAccess(t *testing.T) { Convey("Can create one alert", func() { So(err, ShouldBeNil) - - query := &m.GetAlertChangesQuery{OrgId: 1} - er := GetAlertRuleChanges(query) - So(er, ShouldBeNil) - So(len(query.Result), ShouldEqual, 1) }) Convey("Can read properties", func() { @@ -52,7 +47,7 @@ func TestAlertingDataAccess(t *testing.T) { So(err2, ShouldBeNil) So(alert.Name, ShouldEqual, "Alerting title") So(alert.Description, ShouldEqual, "Alerting description") - So(alert.State, ShouldEqual, "OK") + So(alert.State, ShouldEqual, "pending") So(alert.Frequency, ShouldEqual, 1) }) @@ -82,18 +77,13 @@ func TestAlertingDataAccess(t *testing.T) { So(query.Result[0].Name, ShouldEqual, "Name") Convey("Alert state should not be updated", func() { - So(query.Result[0].State, ShouldEqual, "OK") + So(query.Result[0].State, ShouldEqual, "pending") }) }) Convey("Updates without changes should be ignored", func() { err3 := SaveAlerts(&modifiedCmd) So(err3, ShouldBeNil) - - query := &m.GetAlertChangesQuery{OrgId: 1} - er := GetAlertRuleChanges(query) - So(er, ShouldBeNil) - So(len(query.Result), ShouldEqual, 2) }) }) @@ -133,11 +123,6 @@ func TestAlertingDataAccess(t *testing.T) { So(err2, ShouldBeNil) So(len(queryForDashboard.Result), ShouldEqual, 3) - - query := &m.GetAlertChangesQuery{OrgId: 1} - er := GetAlertRuleChanges(query) - So(er, ShouldBeNil) - So(len(query.Result), ShouldEqual, 4) }) Convey("should updated two dashboards and delete one", func() { @@ -152,13 +137,6 @@ func TestAlertingDataAccess(t *testing.T) { So(err2, ShouldBeNil) So(len(query.Result), ShouldEqual, 2) }) - - Convey("should add one more alert_rule_change", func() { - query := &m.GetAlertChangesQuery{OrgId: 1} - er := GetAlertRuleChanges(query) - So(er, ShouldBeNil) - So(len(query.Result), ShouldEqual, 6) - }) }) })