alerting_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package sqlstore
  2. import (
  3. "testing"
  4. m "github.com/grafana/grafana/pkg/models"
  5. . "github.com/smartystreets/goconvey/convey"
  6. )
  7. func TestAlertingDataAccess(t *testing.T) {
  8. Convey("Testing Alerting data access", 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. Query: "Query",
  16. QueryRefId: "A",
  17. WarnLevel: "> 30",
  18. CritLevel: "> 50",
  19. Interval: "10",
  20. Title: "Alerting title",
  21. Description: "Alerting description",
  22. QueryRange: "5m",
  23. Aggregator: "avg",
  24. },
  25. }
  26. cmd := m.SaveAlertsCommand{
  27. Alerts: &items,
  28. DashboardId: testDash.Id,
  29. OrgId: 1,
  30. UserId: 1,
  31. }
  32. err := SaveAlerts(&cmd)
  33. Convey("Can create one alert", func() {
  34. So(err, ShouldBeNil)
  35. })
  36. Convey("Can read properties", func() {
  37. alert, err2 := GetAlertsByDashboardAndPanelId(testDash.Id, 1)
  38. So(err2, ShouldBeNil)
  39. So(alert.Interval, ShouldEqual, "10")
  40. So(alert.WarnLevel, ShouldEqual, "> 30")
  41. So(alert.CritLevel, ShouldEqual, "> 50")
  42. So(alert.Query, ShouldEqual, "Query")
  43. So(alert.QueryRefId, ShouldEqual, "A")
  44. So(alert.Title, ShouldEqual, "Alerting title")
  45. So(alert.Description, ShouldEqual, "Alerting description")
  46. So(alert.QueryRange, ShouldEqual, "5m")
  47. So(alert.Aggregator, ShouldEqual, "avg")
  48. })
  49. Convey("Alerts with same dashboard id and panel id should update", func() {
  50. modifiedItems := items
  51. modifiedItems[0].Query = "Updated Query"
  52. modifiedCmd := m.SaveAlertsCommand{
  53. DashboardId: testDash.Id,
  54. OrgId: 1,
  55. UserId: 1,
  56. Alerts: &modifiedItems,
  57. }
  58. err := SaveAlerts(&modifiedCmd)
  59. Convey("Can save alerts with same dashboard and panel id", func() {
  60. So(err, ShouldBeNil)
  61. })
  62. Convey("Alerts should be updated", func() {
  63. alerts, err2 := GetAlertsByDashboardId(testDash.Id)
  64. So(err2, ShouldBeNil)
  65. So(len(alerts), ShouldEqual, 1)
  66. So(alerts[0].Query, ShouldEqual, "Updated Query")
  67. })
  68. })
  69. Convey("Multiple alerts per dashboard", func() {
  70. multipleItems := []m.Alert{
  71. {
  72. DashboardId: testDash.Id,
  73. PanelId: 1,
  74. Query: "1",
  75. },
  76. {
  77. DashboardId: testDash.Id,
  78. PanelId: 2,
  79. Query: "2",
  80. },
  81. {
  82. DashboardId: testDash.Id,
  83. PanelId: 3,
  84. Query: "3",
  85. },
  86. }
  87. cmd.Alerts = &multipleItems
  88. err = SaveAlerts(&cmd)
  89. Convey("Should save 3 dashboards", func() {
  90. So(err, ShouldBeNil)
  91. alerts, err2 := GetAlertsByDashboardId(testDash.Id)
  92. So(err2, ShouldBeNil)
  93. So(len(alerts), ShouldEqual, 3)
  94. })
  95. Convey("should updated two dashboards and delete one", func() {
  96. missingOneAlert := multipleItems[:2]
  97. cmd.Alerts = &missingOneAlert
  98. err = SaveAlerts(&cmd)
  99. Convey("should delete the missing alert", func() {
  100. alerts, err2 := GetAlertsByDashboardId(testDash.Id)
  101. So(err2, ShouldBeNil)
  102. So(len(alerts), ShouldEqual, 2)
  103. })
  104. })
  105. })
  106. Convey("When dashboard is removed", func() {
  107. items := []m.Alert{
  108. {
  109. PanelId: 1,
  110. DashboardId: testDash.Id,
  111. Query: "Query",
  112. QueryRefId: "A",
  113. WarnLevel: "> 30",
  114. CritLevel: "> 50",
  115. Interval: "10",
  116. Title: "Alerting title",
  117. Description: "Alerting description",
  118. QueryRange: "5m",
  119. Aggregator: "avg",
  120. },
  121. }
  122. cmd := m.SaveAlertsCommand{
  123. Alerts: &items,
  124. DashboardId: testDash.Id,
  125. OrgId: 1,
  126. UserId: 1,
  127. }
  128. SaveAlerts(&cmd)
  129. err = DeleteDashboard(&m.DeleteDashboardCommand{
  130. OrgId: 1,
  131. Slug: testDash.Slug,
  132. })
  133. /* Uncomment this once we know why inTransaction2 is failing in unit tests
  134. So(err, ShouldBeNil)
  135. Convey("Alerts should be removed", func() {
  136. alerts, err2 := GetAlertsByDashboardId(testDash.Id)
  137. So(testDash.Id, ShouldEqual, 1)
  138. So(err2, ShouldBeNil)
  139. So(len(alerts), ShouldEqual, 0)
  140. })
  141. */
  142. })
  143. })
  144. }