alert_rule_test.go 4.8 KB

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