alert_rule_test.go 5.5 KB

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