| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- package sqlstore
- import (
- "testing"
- "github.com/grafana/grafana/pkg/components/simplejson"
- m "github.com/grafana/grafana/pkg/models"
- . "github.com/smartystreets/goconvey/convey"
- )
- func TestAlertingDataAccess(t *testing.T) {
- Convey("Testing Alerting data access", t, func() {
- InitTestDB(t)
- testDash := insertTestDashboard("dashboard with alerts", 1, "alert")
- items := []*m.Alert{
- {
- PanelId: 1,
- DashboardId: testDash.Id,
- OrgId: testDash.OrgId,
- Name: "Alerting title",
- Description: "Alerting description",
- Settings: simplejson.New(),
- },
- }
- cmd := m.SaveAlertsCommand{
- Alerts: items,
- DashboardId: testDash.Id,
- OrgId: 1,
- UserId: 1,
- }
- err := SaveAlerts(&cmd)
- 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() {
- alertQuery := m.GetAlertsQuery{DashboardId: testDash.Id, PanelId: 1, OrgId: 1}
- err2 := HandleAlertsQuery(&alertQuery)
- alert := alertQuery.Result[0]
- So(err2, ShouldBeNil)
- So(alert.Name, ShouldEqual, "Alerting title")
- So(alert.Description, ShouldEqual, "Alerting description")
- So(alert.State, ShouldEqual, "OK")
- })
- Convey("Alerts with same dashboard id and panel id should update", func() {
- modifiedItems := items
- modifiedItems[0].Name = "Name"
- modifiedCmd := m.SaveAlertsCommand{
- DashboardId: testDash.Id,
- OrgId: 1,
- UserId: 1,
- Alerts: modifiedItems,
- }
- err := SaveAlerts(&modifiedCmd)
- Convey("Can save alerts with same dashboard and panel id", func() {
- So(err, ShouldBeNil)
- })
- Convey("Alerts should be updated", func() {
- query := m.GetAlertsQuery{DashboardId: testDash.Id, OrgId: 1}
- err2 := HandleAlertsQuery(&query)
- So(err2, ShouldBeNil)
- So(len(query.Result), ShouldEqual, 1)
- So(query.Result[0].Name, ShouldEqual, "Name")
- Convey("Alert state should not be updated", func() {
- So(query.Result[0].State, ShouldEqual, "OK")
- })
- })
- 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)
- })
- })
- Convey("Multiple alerts per dashboard", func() {
- multipleItems := []*m.Alert{
- {
- DashboardId: testDash.Id,
- PanelId: 1,
- Name: "1",
- OrgId: 1,
- Settings: simplejson.New(),
- },
- {
- DashboardId: testDash.Id,
- PanelId: 2,
- Name: "2",
- OrgId: 1,
- Settings: simplejson.New(),
- },
- {
- DashboardId: testDash.Id,
- PanelId: 3,
- Name: "3",
- OrgId: 1,
- Settings: simplejson.New(),
- },
- }
- cmd.Alerts = multipleItems
- err = SaveAlerts(&cmd)
- Convey("Should save 3 dashboards", func() {
- So(err, ShouldBeNil)
- queryForDashboard := m.GetAlertsQuery{DashboardId: testDash.Id, OrgId: 1}
- err2 := HandleAlertsQuery(&queryForDashboard)
- 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() {
- missingOneAlert := multipleItems[:2]
- cmd.Alerts = missingOneAlert
- err = SaveAlerts(&cmd)
- Convey("should delete the missing alert", func() {
- query := m.GetAlertsQuery{DashboardId: testDash.Id, OrgId: 1}
- err2 := HandleAlertsQuery(&query)
- 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)
- })
- })
- })
- Convey("When dashboard is removed", func() {
- items := []*m.Alert{
- {
- PanelId: 1,
- DashboardId: testDash.Id,
- Name: "Alerting title",
- Description: "Alerting description",
- },
- }
- cmd := m.SaveAlertsCommand{
- Alerts: items,
- DashboardId: testDash.Id,
- OrgId: 1,
- UserId: 1,
- }
- SaveAlerts(&cmd)
- err = DeleteDashboard(&m.DeleteDashboardCommand{
- OrgId: 1,
- Slug: testDash.Slug,
- })
- So(err, ShouldBeNil)
- Convey("Alerts should be removed", func() {
- query := m.GetAlertsQuery{DashboardId: testDash.Id, OrgId: 1}
- err2 := HandleAlertsQuery(&query)
- So(testDash.Id, ShouldEqual, 1)
- So(err2, ShouldBeNil)
- So(len(query.Result), ShouldEqual, 0)
- })
- })
- })
- }
|