alert_test.go 4.2 KB

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