| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- 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, 0, false, "alert")
- items := []*m.Alert{
- {
- PanelId: 1,
- DashboardId: testDash.Id,
- OrgId: testDash.OrgId,
- Name: "Alerting title",
- Message: "Alerting message",
- Settings: simplejson.New(),
- Frequency: 1,
- },
- }
- cmd := m.SaveAlertsCommand{
- Alerts: items,
- DashboardId: testDash.Id,
- OrgId: 1,
- UserId: 1,
- }
- err := SaveAlerts(&cmd)
- Convey("Can create one alert", func() {
- So(err, ShouldBeNil)
- })
- Convey("Can set new states", func() {
- Convey("new state ok", func() {
- cmd := &m.SetAlertStateCommand{
- AlertId: 1,
- State: m.AlertStateOK,
- }
- err = SetAlertState(cmd)
- So(err, ShouldBeNil)
- })
- Convey("can pause alert", func() {
- cmd := &m.PauseAllAlertCommand{
- Paused: true,
- }
- err = PauseAllAlerts(cmd)
- So(err, ShouldBeNil)
- Convey("cannot updated paused alert", func() {
- cmd := &m.SetAlertStateCommand{
- AlertId: 1,
- State: m.AlertStateOK,
- }
- err = SetAlertState(cmd)
- So(err, ShouldNotBeNil)
- })
- })
- })
- Convey("Can read properties", func() {
- alertQuery := m.GetAlertsQuery{DashboardId: testDash.Id, PanelId: 1, OrgId: 1, User: &m.SignedInUser{OrgRole: m.ROLE_ADMIN}}
- err2 := HandleAlertsQuery(&alertQuery)
- alert := alertQuery.Result[0]
- So(err2, ShouldBeNil)
- So(alert.Name, ShouldEqual, "Alerting title")
- So(alert.State, ShouldEqual, "pending")
- })
- Convey("Viewer cannot read alerts", func() {
- alertQuery := m.GetAlertsQuery{DashboardId: testDash.Id, PanelId: 1, OrgId: 1, User: &m.SignedInUser{OrgRole: m.ROLE_VIEWER}}
- err2 := HandleAlertsQuery(&alertQuery)
- So(err2, ShouldBeNil)
- So(alertQuery.Result, ShouldHaveLength, 0)
- })
- 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, User: &m.SignedInUser{OrgRole: m.ROLE_ADMIN}}
- 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, "pending")
- })
- })
- Convey("Updates without changes should be ignored", func() {
- err3 := SaveAlerts(&modifiedCmd)
- So(err3, ShouldBeNil)
- })
- })
- 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, User: &m.SignedInUser{OrgRole: m.ROLE_ADMIN}}
- err2 := HandleAlertsQuery(&queryForDashboard)
- So(err2, ShouldBeNil)
- So(len(queryForDashboard.Result), ShouldEqual, 3)
- })
- 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, User: &m.SignedInUser{OrgRole: m.ROLE_ADMIN}}
- err2 := HandleAlertsQuery(&query)
- So(err2, ShouldBeNil)
- So(len(query.Result), ShouldEqual, 2)
- })
- })
- })
- Convey("When dashboard is removed", func() {
- items := []*m.Alert{
- {
- PanelId: 1,
- DashboardId: testDash.Id,
- Name: "Alerting title",
- Message: "Alerting message",
- },
- }
- cmd := m.SaveAlertsCommand{
- Alerts: items,
- DashboardId: testDash.Id,
- OrgId: 1,
- UserId: 1,
- }
- SaveAlerts(&cmd)
- err = DeleteDashboard(&m.DeleteDashboardCommand{
- OrgId: 1,
- Id: testDash.Id,
- })
- So(err, ShouldBeNil)
- Convey("Alerts should be removed", func() {
- query := m.GetAlertsQuery{DashboardId: testDash.Id, OrgId: 1, User: &m.SignedInUser{OrgRole: m.ROLE_ADMIN}}
- err2 := HandleAlertsQuery(&query)
- So(testDash.Id, ShouldEqual, 1)
- So(err2, ShouldBeNil)
- So(len(query.Result), ShouldEqual, 0)
- })
- })
- })
- }
|