alert_notification_test.go 6.9 KB

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