alert_state_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package sqlstore
  2. import (
  3. "testing"
  4. m "github.com/grafana/grafana/pkg/models"
  5. . "github.com/smartystreets/goconvey/convey"
  6. )
  7. func TestAlertingStateAccess(t *testing.T) {
  8. Convey("Test alerting state changes", t, func() {
  9. InitTestDB(t)
  10. testDash := insertTestDashboard("dashboard with alerts", 1, "alert")
  11. items := []*m.AlertRule{
  12. &m.AlertRule{
  13. PanelId: 1,
  14. DashboardId: testDash.Id,
  15. OrgId: testDash.OrgId,
  16. Query: "Query",
  17. QueryRefId: "A",
  18. WarnLevel: 30,
  19. CritLevel: 50,
  20. WarnOperator: ">",
  21. CritOperator: ">",
  22. Frequency: 10,
  23. Name: "Alerting title",
  24. Description: "Alerting description",
  25. QueryRange: 3600,
  26. Aggregator: "avg",
  27. },
  28. }
  29. cmd := m.SaveAlertsCommand{
  30. Alerts: items,
  31. DashboardId: testDash.Id,
  32. OrgId: 1,
  33. UserId: 1,
  34. }
  35. err := SaveAlerts(&cmd)
  36. So(err, ShouldBeNil)
  37. Convey("Cannot insert invalid states", func() {
  38. err = SetNewAlertState(&m.UpdateAlertStateCommand{
  39. AlertId: 1,
  40. NewState: "maybe ok",
  41. Info: "Shit just hit the fan",
  42. })
  43. So(err, ShouldNotBeNil)
  44. })
  45. Convey("Changes state to alert", func() {
  46. err = SetNewAlertState(&m.UpdateAlertStateCommand{
  47. AlertId: 1,
  48. NewState: "CRITICAL",
  49. Info: "Shit just hit the fan",
  50. })
  51. Convey("can get new state for alert", func() {
  52. query := &m.GetAlertByIdQuery{Id: 1}
  53. err := GetAlertById(query)
  54. So(err, ShouldBeNil)
  55. So(query.Result.State, ShouldEqual, "CRITICAL")
  56. })
  57. Convey("Changes state to ok", func() {
  58. err = SetNewAlertState(&m.UpdateAlertStateCommand{
  59. AlertId: 1,
  60. NewState: "OK",
  61. Info: "Shit just hit the fan",
  62. })
  63. Convey("get ok state for alert", func() {
  64. query := &m.GetAlertByIdQuery{Id: 1}
  65. err := GetAlertById(query)
  66. So(err, ShouldBeNil)
  67. So(query.Result.State, ShouldEqual, "OK")
  68. })
  69. Convey("should have two event state logs", func() {
  70. query := &m.GetAlertsStateQuery{
  71. AlertId: 1,
  72. OrgId: 1,
  73. }
  74. err := GetAlertStateLogByAlertId(query)
  75. So(err, ShouldBeNil)
  76. So(len(*query.Result), ShouldEqual, 2)
  77. })
  78. Convey("should not get any alerts with critical state", func() {
  79. query := &m.GetAlertsQuery{
  80. OrgId: 1,
  81. State: []string{"Critical", "Warn"},
  82. }
  83. err := HandleAlertsQuery(query)
  84. So(err, ShouldBeNil)
  85. So(len(query.Result), ShouldEqual, 0)
  86. })
  87. })
  88. })
  89. })
  90. }