alert_state_test.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. WarnOperator: ">",
  21. CritOperator: ">",
  22. Interval: "10",
  23. Title: "Alerting title",
  24. Description: "Alerting description",
  25. QueryRange: "5m",
  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.GetAlertsStateLogCommand{
  71. AlertId: 1,
  72. OrgId: 1,
  73. }
  74. err := GetAlertStateLogByAlertId(query)
  75. So(err, ShouldBeNil)
  76. So(len(*query.Result), ShouldEqual, 2)
  77. })
  78. })
  79. })
  80. })
  81. }