alert_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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, 0, false, "alert")
  12. items := []*m.Alert{
  13. {
  14. PanelId: 1,
  15. DashboardId: testDash.Id,
  16. OrgId: testDash.OrgId,
  17. Name: "Alerting title",
  18. Message: "Alerting message",
  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. })
  33. Convey("Can set new states", func() {
  34. Convey("new state ok", func() {
  35. cmd := &m.SetAlertStateCommand{
  36. AlertId: 1,
  37. State: m.AlertStateOK,
  38. }
  39. err = SetAlertState(cmd)
  40. So(err, ShouldBeNil)
  41. })
  42. Convey("can pause alert", func() {
  43. cmd := &m.PauseAllAlertCommand{
  44. Paused: true,
  45. }
  46. err = PauseAllAlerts(cmd)
  47. So(err, ShouldBeNil)
  48. Convey("cannot updated paused alert", func() {
  49. cmd := &m.SetAlertStateCommand{
  50. AlertId: 1,
  51. State: m.AlertStateOK,
  52. }
  53. err = SetAlertState(cmd)
  54. So(err, ShouldNotBeNil)
  55. })
  56. })
  57. })
  58. Convey("Can read properties", func() {
  59. alertQuery := m.GetAlertsQuery{DashboardId: testDash.Id, PanelId: 1, OrgId: 1}
  60. err2 := HandleAlertsQuery(&alertQuery)
  61. alert := alertQuery.Result[0]
  62. So(err2, ShouldBeNil)
  63. So(alert.Name, ShouldEqual, "Alerting title")
  64. So(alert.Message, ShouldEqual, "Alerting message")
  65. So(alert.State, ShouldEqual, "pending")
  66. So(alert.Frequency, ShouldEqual, 1)
  67. })
  68. Convey("Alerts with same dashboard id and panel id should update", func() {
  69. modifiedItems := items
  70. modifiedItems[0].Name = "Name"
  71. modifiedCmd := m.SaveAlertsCommand{
  72. DashboardId: testDash.Id,
  73. OrgId: 1,
  74. UserId: 1,
  75. Alerts: modifiedItems,
  76. }
  77. err := SaveAlerts(&modifiedCmd)
  78. Convey("Can save alerts with same dashboard and panel id", func() {
  79. So(err, ShouldBeNil)
  80. })
  81. Convey("Alerts should be updated", func() {
  82. query := m.GetAlertsQuery{DashboardId: testDash.Id, OrgId: 1}
  83. err2 := HandleAlertsQuery(&query)
  84. So(err2, ShouldBeNil)
  85. So(len(query.Result), ShouldEqual, 1)
  86. So(query.Result[0].Name, ShouldEqual, "Name")
  87. Convey("Alert state should not be updated", func() {
  88. So(query.Result[0].State, ShouldEqual, "pending")
  89. })
  90. })
  91. Convey("Updates without changes should be ignored", func() {
  92. err3 := SaveAlerts(&modifiedCmd)
  93. So(err3, ShouldBeNil)
  94. })
  95. })
  96. Convey("Multiple alerts per dashboard", func() {
  97. multipleItems := []*m.Alert{
  98. {
  99. DashboardId: testDash.Id,
  100. PanelId: 1,
  101. Name: "1",
  102. OrgId: 1,
  103. Settings: simplejson.New(),
  104. },
  105. {
  106. DashboardId: testDash.Id,
  107. PanelId: 2,
  108. Name: "2",
  109. OrgId: 1,
  110. Settings: simplejson.New(),
  111. },
  112. {
  113. DashboardId: testDash.Id,
  114. PanelId: 3,
  115. Name: "3",
  116. OrgId: 1,
  117. Settings: simplejson.New(),
  118. },
  119. }
  120. cmd.Alerts = multipleItems
  121. err = SaveAlerts(&cmd)
  122. Convey("Should save 3 dashboards", func() {
  123. So(err, ShouldBeNil)
  124. queryForDashboard := m.GetAlertsQuery{DashboardId: testDash.Id, OrgId: 1}
  125. err2 := HandleAlertsQuery(&queryForDashboard)
  126. So(err2, ShouldBeNil)
  127. So(len(queryForDashboard.Result), ShouldEqual, 3)
  128. })
  129. Convey("should updated two dashboards and delete one", func() {
  130. missingOneAlert := multipleItems[:2]
  131. cmd.Alerts = missingOneAlert
  132. err = SaveAlerts(&cmd)
  133. Convey("should delete the missing alert", func() {
  134. query := m.GetAlertsQuery{DashboardId: testDash.Id, OrgId: 1}
  135. err2 := HandleAlertsQuery(&query)
  136. So(err2, ShouldBeNil)
  137. So(len(query.Result), ShouldEqual, 2)
  138. })
  139. })
  140. })
  141. Convey("When dashboard is removed", func() {
  142. items := []*m.Alert{
  143. {
  144. PanelId: 1,
  145. DashboardId: testDash.Id,
  146. Name: "Alerting title",
  147. Message: "Alerting message",
  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. Id: testDash.Id,
  160. })
  161. So(err, ShouldBeNil)
  162. Convey("Alerts should be removed", func() {
  163. query := m.GetAlertsQuery{DashboardId: testDash.Id, OrgId: 1}
  164. err2 := HandleAlertsQuery(&query)
  165. So(testDash.Id, ShouldEqual, 1)
  166. So(err2, ShouldBeNil)
  167. So(len(query.Result), ShouldEqual, 0)
  168. })
  169. })
  170. })
  171. }