alert_rule_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package sqlstore
  2. import (
  3. "testing"
  4. "github.com/grafana/grafana/pkg/components/simplejson"
  5. m "github.com/grafana/grafana/pkg/models"
  6. . "github.com/smartystreets/goconvey/convey"
  7. )
  8. func TestAlertingDataAccess(t *testing.T) {
  9. Convey("Testing Alerting data access", t, func() {
  10. InitTestDB(t)
  11. testDash := insertTestDashboard("dashboard with alerts", 1, "alert")
  12. items := []*m.Alert{
  13. {
  14. PanelId: 1,
  15. DashboardId: testDash.Id,
  16. OrgId: testDash.OrgId,
  17. Name: "Alerting title",
  18. Description: "Alerting description",
  19. Settings: simplejson.New(),
  20. },
  21. }
  22. cmd := m.SaveAlertsCommand{
  23. Alerts: items,
  24. DashboardId: testDash.Id,
  25. OrgId: 1,
  26. UserId: 1,
  27. }
  28. err := SaveAlerts(&cmd)
  29. Convey("Can create one alert", func() {
  30. So(err, ShouldBeNil)
  31. query := &m.GetAlertChangesQuery{OrgId: 1}
  32. er := GetAlertRuleChanges(query)
  33. So(er, ShouldBeNil)
  34. So(len(query.Result), ShouldEqual, 1)
  35. })
  36. Convey("Can read properties", func() {
  37. alertQuery := m.GetAlertsQuery{DashboardId: testDash.Id, PanelId: 1, OrgId: 1}
  38. err2 := HandleAlertsQuery(&alertQuery)
  39. alert := alertQuery.Result[0]
  40. So(err2, ShouldBeNil)
  41. So(alert.Name, ShouldEqual, "Alerting title")
  42. So(alert.Description, ShouldEqual, "Alerting description")
  43. So(alert.State, ShouldEqual, "OK")
  44. })
  45. Convey("Alerts with same dashboard id and panel id should update", func() {
  46. modifiedItems := items
  47. modifiedItems[0].Name = "Name"
  48. modifiedCmd := m.SaveAlertsCommand{
  49. DashboardId: testDash.Id,
  50. OrgId: 1,
  51. UserId: 1,
  52. Alerts: modifiedItems,
  53. }
  54. err := SaveAlerts(&modifiedCmd)
  55. Convey("Can save alerts with same dashboard and panel id", func() {
  56. So(err, ShouldBeNil)
  57. })
  58. Convey("Alerts should be updated", func() {
  59. query := m.GetAlertsQuery{DashboardId: testDash.Id, OrgId: 1}
  60. err2 := HandleAlertsQuery(&query)
  61. So(err2, ShouldBeNil)
  62. So(len(query.Result), ShouldEqual, 1)
  63. So(query.Result[0].Name, ShouldEqual, "Name")
  64. Convey("Alert state should not be updated", func() {
  65. So(query.Result[0].State, ShouldEqual, "OK")
  66. })
  67. })
  68. Convey("Updates without changes should be ignored", func() {
  69. err3 := SaveAlerts(&modifiedCmd)
  70. So(err3, ShouldBeNil)
  71. query := &m.GetAlertChangesQuery{OrgId: 1}
  72. er := GetAlertRuleChanges(query)
  73. So(er, ShouldBeNil)
  74. So(len(query.Result), ShouldEqual, 2)
  75. })
  76. })
  77. Convey("Multiple alerts per dashboard", func() {
  78. multipleItems := []*m.Alert{
  79. {
  80. DashboardId: testDash.Id,
  81. PanelId: 1,
  82. Name: "1",
  83. OrgId: 1,
  84. Settings: simplejson.New(),
  85. },
  86. {
  87. DashboardId: testDash.Id,
  88. PanelId: 2,
  89. Name: "2",
  90. OrgId: 1,
  91. Settings: simplejson.New(),
  92. },
  93. {
  94. DashboardId: testDash.Id,
  95. PanelId: 3,
  96. Name: "3",
  97. OrgId: 1,
  98. Settings: simplejson.New(),
  99. },
  100. }
  101. cmd.Alerts = multipleItems
  102. err = SaveAlerts(&cmd)
  103. Convey("Should save 3 dashboards", func() {
  104. So(err, ShouldBeNil)
  105. queryForDashboard := m.GetAlertsQuery{DashboardId: testDash.Id, OrgId: 1}
  106. err2 := HandleAlertsQuery(&queryForDashboard)
  107. So(err2, ShouldBeNil)
  108. So(len(queryForDashboard.Result), ShouldEqual, 3)
  109. query := &m.GetAlertChangesQuery{OrgId: 1}
  110. er := GetAlertRuleChanges(query)
  111. So(er, ShouldBeNil)
  112. So(len(query.Result), ShouldEqual, 4)
  113. })
  114. Convey("should updated two dashboards and delete one", func() {
  115. missingOneAlert := multipleItems[:2]
  116. cmd.Alerts = missingOneAlert
  117. err = SaveAlerts(&cmd)
  118. Convey("should delete the missing alert", func() {
  119. query := m.GetAlertsQuery{DashboardId: testDash.Id, OrgId: 1}
  120. err2 := HandleAlertsQuery(&query)
  121. So(err2, ShouldBeNil)
  122. So(len(query.Result), ShouldEqual, 2)
  123. })
  124. Convey("should add one more alert_rule_change", func() {
  125. query := &m.GetAlertChangesQuery{OrgId: 1}
  126. er := GetAlertRuleChanges(query)
  127. So(er, ShouldBeNil)
  128. So(len(query.Result), ShouldEqual, 6)
  129. })
  130. })
  131. })
  132. Convey("When dashboard is removed", func() {
  133. items := []*m.Alert{
  134. {
  135. PanelId: 1,
  136. DashboardId: testDash.Id,
  137. Name: "Alerting title",
  138. Description: "Alerting description",
  139. },
  140. }
  141. cmd := m.SaveAlertsCommand{
  142. Alerts: items,
  143. DashboardId: testDash.Id,
  144. OrgId: 1,
  145. UserId: 1,
  146. }
  147. SaveAlerts(&cmd)
  148. err = DeleteDashboard(&m.DeleteDashboardCommand{
  149. OrgId: 1,
  150. Slug: testDash.Slug,
  151. })
  152. So(err, ShouldBeNil)
  153. Convey("Alerts should be removed", func() {
  154. query := m.GetAlertsQuery{DashboardId: testDash.Id, OrgId: 1}
  155. err2 := HandleAlertsQuery(&query)
  156. So(testDash.Id, ShouldEqual, 1)
  157. So(err2, ShouldBeNil)
  158. So(len(query.Result), ShouldEqual, 0)
  159. })
  160. })
  161. })
  162. }