alerting_test.go 4.6 KB

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