alert_rule_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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.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. Convey("Can create one alert", func() {
  35. So(err, ShouldBeNil)
  36. query := &m.GetAlertChangesQuery{OrgId: 1}
  37. er := GetAlertRuleChanges(query)
  38. So(er, ShouldBeNil)
  39. So(len(query.Result), ShouldEqual, 1)
  40. })
  41. Convey("Can read properties", func() {
  42. alert, err2 := GetAlertsByDashboardAndPanelId(testDash.Id, 1)
  43. So(err2, ShouldBeNil)
  44. So(alert.Interval, ShouldEqual, "10")
  45. So(alert.WarnLevel, ShouldEqual, "> 30")
  46. So(alert.CritLevel, ShouldEqual, "> 50")
  47. So(alert.Query, ShouldEqual, "Query")
  48. So(alert.QueryRefId, ShouldEqual, "A")
  49. So(alert.Title, ShouldEqual, "Alerting title")
  50. So(alert.Description, ShouldEqual, "Alerting description")
  51. So(alert.QueryRange, ShouldEqual, "5m")
  52. So(alert.Aggregator, ShouldEqual, "avg")
  53. })
  54. Convey("Alerts with same dashboard id and panel id should update", func() {
  55. modifiedItems := items
  56. modifiedItems[0].Query = "Updated Query"
  57. modifiedCmd := m.SaveAlertsCommand{
  58. DashboardId: testDash.Id,
  59. OrgId: 1,
  60. UserId: 1,
  61. Alerts: &modifiedItems,
  62. }
  63. err := SaveAlerts(&modifiedCmd)
  64. Convey("Can save alerts with same dashboard and panel id", func() {
  65. So(err, ShouldBeNil)
  66. })
  67. Convey("Alerts should be updated", func() {
  68. alerts, err2 := GetAlertsByDashboardId(testDash.Id)
  69. So(err2, ShouldBeNil)
  70. So(len(alerts), ShouldEqual, 1)
  71. So(alerts[0].Query, ShouldEqual, "Updated Query")
  72. })
  73. Convey("Updates without changes should be ignored", func() {
  74. err3 := SaveAlerts(&modifiedCmd)
  75. So(err3, ShouldBeNil)
  76. query := &m.GetAlertChangesQuery{OrgId: 1}
  77. er := GetAlertRuleChanges(query)
  78. So(er, ShouldBeNil)
  79. So(len(query.Result), ShouldEqual, 2)
  80. })
  81. })
  82. Convey("Multiple alerts per dashboard", func() {
  83. multipleItems := []m.AlertRule{
  84. {
  85. DashboardId: testDash.Id,
  86. PanelId: 1,
  87. Query: "1",
  88. OrgId: 1,
  89. },
  90. {
  91. DashboardId: testDash.Id,
  92. PanelId: 2,
  93. Query: "2",
  94. OrgId: 1,
  95. },
  96. {
  97. DashboardId: testDash.Id,
  98. PanelId: 3,
  99. Query: "3",
  100. OrgId: 1,
  101. },
  102. }
  103. cmd.Alerts = &multipleItems
  104. err = SaveAlerts(&cmd)
  105. Convey("Should save 3 dashboards", func() {
  106. So(err, ShouldBeNil)
  107. alerts, err2 := GetAlertsByDashboardId(testDash.Id)
  108. So(err2, ShouldBeNil)
  109. So(len(alerts), ShouldEqual, 3)
  110. query := &m.GetAlertChangesQuery{OrgId: 1}
  111. er := GetAlertRuleChanges(query)
  112. So(er, ShouldBeNil)
  113. So(len(query.Result), ShouldEqual, 4)
  114. })
  115. Convey("should updated two dashboards and delete one", func() {
  116. missingOneAlert := multipleItems[:2]
  117. cmd.Alerts = &missingOneAlert
  118. err = SaveAlerts(&cmd)
  119. Convey("should delete the missing alert", func() {
  120. alerts, err2 := GetAlertsByDashboardId(testDash.Id)
  121. So(err2, ShouldBeNil)
  122. So(len(alerts), ShouldEqual, 2)
  123. })
  124. Convey("should add one more alert_rule_change", func() {
  125. query := &m.GetAlertChangesQuery{OrgId: 1}
  126. er := GetAlertRuleChanges(query)
  127. So(er, ShouldBeNil)
  128. So(len(query.Result), ShouldEqual, 6)
  129. })
  130. })
  131. })
  132. Convey("When dashboard is removed", func() {
  133. items := []m.AlertRule{
  134. {
  135. PanelId: 1,
  136. DashboardId: testDash.Id,
  137. Query: "Query",
  138. QueryRefId: "A",
  139. WarnLevel: "> 30",
  140. CritLevel: "> 50",
  141. Interval: "10",
  142. Title: "Alerting title",
  143. Description: "Alerting description",
  144. QueryRange: "5m",
  145. Aggregator: "avg",
  146. },
  147. }
  148. cmd := m.SaveAlertsCommand{
  149. Alerts: &items,
  150. DashboardId: testDash.Id,
  151. OrgId: 1,
  152. UserId: 1,
  153. }
  154. SaveAlerts(&cmd)
  155. err = DeleteDashboard(&m.DeleteDashboardCommand{
  156. OrgId: 1,
  157. Slug: testDash.Slug,
  158. })
  159. So(err, ShouldBeNil)
  160. Convey("Alerts should be removed", func() {
  161. alerts, err2 := GetAlertsByDashboardId(testDash.Id)
  162. So(testDash.Id, ShouldEqual, 1)
  163. So(err2, ShouldBeNil)
  164. So(len(alerts), ShouldEqual, 0)
  165. })
  166. })
  167. })
  168. }