alert_rule_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. Name: "Alerting title",
  17. Description: "Alerting description",
  18. },
  19. }
  20. cmd := m.SaveAlertsCommand{
  21. Alerts: items,
  22. DashboardId: testDash.Id,
  23. OrgId: 1,
  24. UserId: 1,
  25. }
  26. err := SaveAlerts(&cmd)
  27. Convey("Can create one alert", func() {
  28. So(err, ShouldBeNil)
  29. query := &m.GetAlertChangesQuery{OrgId: 1}
  30. er := GetAlertRuleChanges(query)
  31. So(er, ShouldBeNil)
  32. So(len(query.Result), ShouldEqual, 1)
  33. })
  34. Convey("Can read properties", func() {
  35. alertQuery := m.GetAlertsQuery{DashboardId: testDash.Id, PanelId: 1, OrgId: 1}
  36. err2 := HandleAlertsQuery(&alertQuery)
  37. alert := alertQuery.Result[0]
  38. So(err2, ShouldBeNil)
  39. So(alert.Name, ShouldEqual, "Alerting title")
  40. So(alert.Description, ShouldEqual, "Alerting description")
  41. So(alert.State, ShouldEqual, "OK")
  42. })
  43. Convey("Alerts with same dashboard id and panel id should update", func() {
  44. modifiedItems := items
  45. modifiedItems[0].Name = "New name"
  46. //modifiedItems[0].State = "ALERT"
  47. modifiedCmd := m.SaveAlertsCommand{
  48. DashboardId: testDash.Id,
  49. OrgId: 1,
  50. UserId: 1,
  51. Alerts: modifiedItems,
  52. }
  53. err := SaveAlerts(&modifiedCmd)
  54. Convey("Can save alerts with same dashboard and panel id", func() {
  55. So(err, ShouldBeNil)
  56. })
  57. Convey("Alerts should be updated", func() {
  58. query := m.GetAlertsQuery{DashboardId: testDash.Id, OrgId: 1}
  59. err2 := HandleAlertsQuery(&query)
  60. So(err2, ShouldBeNil)
  61. So(len(query.Result), ShouldEqual, 1)
  62. So(query.Result[0].Name, ShouldEqual, "Name")
  63. Convey("Alert state should not be updated", func() {
  64. So(query.Result[0].State, ShouldEqual, "OK")
  65. })
  66. })
  67. Convey("Updates without changes should be ignored", func() {
  68. err3 := SaveAlerts(&modifiedCmd)
  69. So(err3, ShouldBeNil)
  70. query := &m.GetAlertChangesQuery{OrgId: 1}
  71. er := GetAlertRuleChanges(query)
  72. So(er, ShouldBeNil)
  73. So(len(query.Result), ShouldEqual, 2)
  74. })
  75. })
  76. Convey("Multiple alerts per dashboard", func() {
  77. multipleItems := []*m.AlertRule{
  78. {
  79. DashboardId: testDash.Id,
  80. PanelId: 1,
  81. Name: "1",
  82. OrgId: 1,
  83. },
  84. {
  85. DashboardId: testDash.Id,
  86. PanelId: 2,
  87. Name: "2",
  88. OrgId: 1,
  89. },
  90. {
  91. DashboardId: testDash.Id,
  92. PanelId: 3,
  93. Name: "3",
  94. OrgId: 1,
  95. },
  96. }
  97. cmd.Alerts = multipleItems
  98. err = SaveAlerts(&cmd)
  99. Convey("Should save 3 dashboards", func() {
  100. So(err, ShouldBeNil)
  101. queryForDashboard := m.GetAlertsQuery{DashboardId: testDash.Id, OrgId: 1}
  102. err2 := HandleAlertsQuery(&queryForDashboard)
  103. So(err2, ShouldBeNil)
  104. So(len(queryForDashboard.Result), ShouldEqual, 3)
  105. query := &m.GetAlertChangesQuery{OrgId: 1}
  106. er := GetAlertRuleChanges(query)
  107. So(er, ShouldBeNil)
  108. So(len(query.Result), ShouldEqual, 4)
  109. })
  110. Convey("should updated two dashboards and delete one", func() {
  111. missingOneAlert := multipleItems[:2]
  112. cmd.Alerts = missingOneAlert
  113. err = SaveAlerts(&cmd)
  114. Convey("should delete the missing alert", func() {
  115. query := m.GetAlertsQuery{DashboardId: testDash.Id, OrgId: 1}
  116. err2 := HandleAlertsQuery(&query)
  117. So(err2, ShouldBeNil)
  118. So(len(query.Result), ShouldEqual, 2)
  119. })
  120. Convey("should add one more alert_rule_change", func() {
  121. query := &m.GetAlertChangesQuery{OrgId: 1}
  122. er := GetAlertRuleChanges(query)
  123. So(er, ShouldBeNil)
  124. So(len(query.Result), 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. Name: "Alerting title",
  134. Description: "Alerting description",
  135. },
  136. }
  137. cmd := m.SaveAlertsCommand{
  138. Alerts: items,
  139. DashboardId: testDash.Id,
  140. OrgId: 1,
  141. UserId: 1,
  142. }
  143. SaveAlerts(&cmd)
  144. err = DeleteDashboard(&m.DeleteDashboardCommand{
  145. OrgId: 1,
  146. Slug: testDash.Slug,
  147. })
  148. So(err, ShouldBeNil)
  149. Convey("Alerts should be removed", func() {
  150. query := m.GetAlertsQuery{DashboardId: testDash.Id, OrgId: 1}
  151. err2 := HandleAlertsQuery(&query)
  152. So(testDash.Id, ShouldEqual, 1)
  153. So(err2, ShouldBeNil)
  154. So(len(query.Result), ShouldEqual, 0)
  155. })
  156. })
  157. })
  158. }