alert_state_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. {
  13. PanelId: 1,
  14. DashboardId: testDash.Id,
  15. OrgId: testDash.OrgId,
  16. Query: "Query",
  17. QueryRefId: "A",
  18. WarnLevel: "> 30",
  19. CritLevel: "> 50",
  20. Interval: "10",
  21. Title: "Alerting title",
  22. Description: "Alerting description",
  23. QueryRange: "5m",
  24. Aggregator: "avg",
  25. },
  26. }
  27. cmd := m.SaveAlertsCommand{
  28. Alerts: &items,
  29. DashboardId: testDash.Id,
  30. OrgId: 1,
  31. UserId: 1,
  32. }
  33. err := SaveAlerts(&cmd)
  34. So(err, ShouldBeNil)
  35. Convey("Cannot insert invalid states", func() {
  36. err = SetNewAlertState(&m.UpdateAlertStateCommand{
  37. AlertId: 1,
  38. NewState: "maybe ok",
  39. Info: "Shit just hit the fan",
  40. })
  41. So(err, ShouldNotBeNil)
  42. })
  43. Convey("Changes state to alert", func() {
  44. err = SetNewAlertState(&m.UpdateAlertStateCommand{
  45. AlertId: 1,
  46. NewState: "ALERT",
  47. Info: "Shit just hit the fan",
  48. })
  49. Convey("can get new state for alert", func() {
  50. query := &m.GetAlertByIdQuery{Id: 1}
  51. err := GetAlertById(query)
  52. So(err, ShouldBeNil)
  53. So(query.Result.State, ShouldEqual, "ALERT")
  54. })
  55. Convey("Changes state to ok", func() {
  56. err = SetNewAlertState(&m.UpdateAlertStateCommand{
  57. AlertId: 1,
  58. NewState: "OK",
  59. Info: "Shit just hit the fan",
  60. })
  61. Convey("get ok state for alert", func() {
  62. query := &m.GetAlertByIdQuery{Id: 1}
  63. err := GetAlertById(query)
  64. So(err, ShouldBeNil)
  65. So(query.Result.State, ShouldEqual, "OK")
  66. })
  67. Convey("should have two event state logs", func() {
  68. query := &m.GetAlertsStateLogCommand{
  69. AlertId: 1,
  70. OrgId: 1,
  71. }
  72. err := GetAlertStateLogByAlertId(query)
  73. So(err, ShouldBeNil)
  74. So(len(*query.Result), ShouldEqual, 2)
  75. })
  76. })
  77. })
  78. })
  79. }