alert_state_test.go 1.8 KB

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