alert_state_test.go 2.2 KB

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