alert_rule_test.go 5.6 KB

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