alert_test.go 4.8 KB

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