alert_notification_test.go 6.3 KB

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