alert_notification_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package sqlstore
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. "github.com/grafana/grafana/pkg/components/simplejson"
  7. m "github.com/grafana/grafana/pkg/models"
  8. . "github.com/smartystreets/goconvey/convey"
  9. )
  10. func TestAlertNotificationSQLAccess(t *testing.T) {
  11. Convey("Testing Alert notification sql access", t, func() {
  12. InitTestDB(t)
  13. Convey("Alert notification journal", func() {
  14. var alertId int64 = 7
  15. var orgId int64 = 5
  16. var notifierId int64 = 10
  17. Convey("Getting last journal should raise error if no one exists", func() {
  18. query := &m.GetLatestNotificationQuery{AlertId: alertId, OrgId: orgId, NotifierId: notifierId}
  19. GetLatestNotification(context.Background(), query)
  20. So(len(query.Result), ShouldEqual, 0)
  21. // recording an journal entry in another org to make sure org filter works as expected.
  22. journalInOtherOrg := &m.RecordNotificationJournalCommand{AlertId: alertId, NotifierId: notifierId, OrgId: 10, Success: true, SentAt: 1}
  23. err := RecordNotificationJournal(context.Background(), journalInOtherOrg)
  24. So(err, ShouldBeNil)
  25. Convey("should be able to record two journaling events", func() {
  26. createCmd := &m.RecordNotificationJournalCommand{AlertId: alertId, NotifierId: notifierId, OrgId: orgId, Success: true, SentAt: 1}
  27. err := RecordNotificationJournal(context.Background(), createCmd)
  28. So(err, ShouldBeNil)
  29. createCmd.SentAt += 1000 //increase epoch
  30. err = RecordNotificationJournal(context.Background(), createCmd)
  31. So(err, ShouldBeNil)
  32. Convey("get last journaling event", func() {
  33. err := GetLatestNotification(context.Background(), query)
  34. So(err, ShouldBeNil)
  35. So(len(query.Result), ShouldEqual, 2)
  36. last := query.Result[0]
  37. So(last.SentAt, ShouldEqual, 1001)
  38. Convey("be able to clear all journaling for an notifier", func() {
  39. cmd := &m.CleanNotificationJournalCommand{AlertId: alertId, NotifierId: notifierId, OrgId: orgId}
  40. err := CleanNotificationJournal(context.Background(), cmd)
  41. So(err, ShouldBeNil)
  42. Convey("querying for last journaling should return no journal entries", func() {
  43. query := &m.GetLatestNotificationQuery{AlertId: alertId, OrgId: orgId, NotifierId: notifierId}
  44. err := GetLatestNotification(context.Background(), query)
  45. So(err, ShouldBeNil)
  46. So(len(query.Result), ShouldEqual, 0)
  47. })
  48. })
  49. })
  50. })
  51. })
  52. })
  53. Convey("Alert notifications should be empty", func() {
  54. cmd := &m.GetAlertNotificationsQuery{
  55. OrgId: 2,
  56. Name: "email",
  57. }
  58. err := GetAlertNotifications(cmd)
  59. So(err, ShouldBeNil)
  60. So(cmd.Result, ShouldBeNil)
  61. })
  62. Convey("Cannot save alert notifier with send reminder = true", func() {
  63. cmd := &m.CreateAlertNotificationCommand{
  64. Name: "ops",
  65. Type: "email",
  66. OrgId: 1,
  67. SendReminder: true,
  68. Settings: simplejson.New(),
  69. }
  70. Convey("and missing frequency", func() {
  71. err := CreateAlertNotificationCommand(cmd)
  72. So(err, ShouldEqual, m.ErrNotificationFrequencyNotFound)
  73. })
  74. Convey("invalid frequency", func() {
  75. cmd.Frequency = "invalid duration"
  76. err := CreateAlertNotificationCommand(cmd)
  77. So(err.Error(), ShouldEqual, "time: invalid duration invalid duration")
  78. })
  79. })
  80. Convey("Cannot update alert notifier with send reminder = false", func() {
  81. cmd := &m.CreateAlertNotificationCommand{
  82. Name: "ops update",
  83. Type: "email",
  84. OrgId: 1,
  85. SendReminder: false,
  86. Settings: simplejson.New(),
  87. }
  88. err := CreateAlertNotificationCommand(cmd)
  89. So(err, ShouldBeNil)
  90. updateCmd := &m.UpdateAlertNotificationCommand{
  91. Id: cmd.Result.Id,
  92. SendReminder: true,
  93. }
  94. Convey("and missing frequency", func() {
  95. err := UpdateAlertNotification(updateCmd)
  96. So(err, ShouldEqual, m.ErrNotificationFrequencyNotFound)
  97. })
  98. Convey("invalid frequency", func() {
  99. updateCmd.Frequency = "invalid duration"
  100. err := UpdateAlertNotification(updateCmd)
  101. So(err, ShouldNotBeNil)
  102. So(err.Error(), ShouldEqual, "time: invalid duration invalid duration")
  103. })
  104. })
  105. Convey("Can save Alert Notification", func() {
  106. cmd := &m.CreateAlertNotificationCommand{
  107. Name: "ops",
  108. Type: "email",
  109. OrgId: 1,
  110. SendReminder: true,
  111. Frequency: "10s",
  112. Settings: simplejson.New(),
  113. }
  114. err := CreateAlertNotificationCommand(cmd)
  115. So(err, ShouldBeNil)
  116. So(cmd.Result.Id, ShouldNotEqual, 0)
  117. So(cmd.Result.OrgId, ShouldNotEqual, 0)
  118. So(cmd.Result.Type, ShouldEqual, "email")
  119. So(cmd.Result.Frequency, ShouldEqual, 10*time.Second)
  120. Convey("Cannot save Alert Notification with the same name", func() {
  121. err = CreateAlertNotificationCommand(cmd)
  122. So(err, ShouldNotBeNil)
  123. })
  124. Convey("Can update alert notification", func() {
  125. newCmd := &m.UpdateAlertNotificationCommand{
  126. Name: "NewName",
  127. Type: "webhook",
  128. OrgId: cmd.Result.OrgId,
  129. SendReminder: true,
  130. Frequency: "60s",
  131. Settings: simplejson.New(),
  132. Id: cmd.Result.Id,
  133. }
  134. err := UpdateAlertNotification(newCmd)
  135. So(err, ShouldBeNil)
  136. So(newCmd.Result.Name, ShouldEqual, "NewName")
  137. So(newCmd.Result.Frequency, ShouldEqual, 60*time.Second)
  138. })
  139. Convey("Can update alert notification to disable sending of reminders", func() {
  140. newCmd := &m.UpdateAlertNotificationCommand{
  141. Name: "NewName",
  142. Type: "webhook",
  143. OrgId: cmd.Result.OrgId,
  144. SendReminder: false,
  145. Settings: simplejson.New(),
  146. Id: cmd.Result.Id,
  147. }
  148. err := UpdateAlertNotification(newCmd)
  149. So(err, ShouldBeNil)
  150. So(newCmd.Result.SendReminder, ShouldBeFalse)
  151. })
  152. })
  153. Convey("Can search using an array of ids", func() {
  154. cmd1 := m.CreateAlertNotificationCommand{Name: "nagios", Type: "webhook", OrgId: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
  155. cmd2 := m.CreateAlertNotificationCommand{Name: "slack", Type: "webhook", OrgId: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
  156. cmd3 := m.CreateAlertNotificationCommand{Name: "ops2", Type: "email", OrgId: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
  157. cmd4 := m.CreateAlertNotificationCommand{IsDefault: true, Name: "default", Type: "email", OrgId: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
  158. otherOrg := m.CreateAlertNotificationCommand{Name: "default", Type: "email", OrgId: 2, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
  159. So(CreateAlertNotificationCommand(&cmd1), ShouldBeNil)
  160. So(CreateAlertNotificationCommand(&cmd2), ShouldBeNil)
  161. So(CreateAlertNotificationCommand(&cmd3), ShouldBeNil)
  162. So(CreateAlertNotificationCommand(&cmd4), ShouldBeNil)
  163. So(CreateAlertNotificationCommand(&otherOrg), ShouldBeNil)
  164. Convey("search", func() {
  165. query := &m.GetAlertNotificationsToSendQuery{
  166. Ids: []int64{cmd1.Result.Id, cmd2.Result.Id, 112341231},
  167. OrgId: 1,
  168. }
  169. err := GetAlertNotificationsToSend(query)
  170. So(err, ShouldBeNil)
  171. So(len(query.Result), ShouldEqual, 3)
  172. })
  173. Convey("all", func() {
  174. query := &m.GetAllAlertNotificationsQuery{
  175. OrgId: 1,
  176. }
  177. err := GetAllAlertNotifications(query)
  178. So(err, ShouldBeNil)
  179. So(len(query.Result), ShouldEqual, 4)
  180. })
  181. })
  182. })
  183. }