alert_notification_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package sqlstore
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. "github.com/grafana/grafana/pkg/components/simplejson"
  7. "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 state", func() {
  14. var alertId int64 = 7
  15. var orgId int64 = 5
  16. var notifierId int64 = 10
  17. Convey("Getting no existant state returns error", func() {
  18. query := &models.GetNotificationStateQuery{AlertId: alertId, OrgId: orgId, NotifierId: notifierId}
  19. err := GetAlertNotificationState(context.Background(), query)
  20. So(err, ShouldEqual, models.ErrAlertNotificationStateNotFound)
  21. })
  22. Convey("Can insert new state for alert notifier", func() {
  23. createCmd := &models.InsertAlertNotificationCommand{
  24. AlertId: alertId,
  25. NotifierId: notifierId,
  26. OrgId: orgId,
  27. SentAt: 1,
  28. State: models.AlertNotificationStateCompleted,
  29. }
  30. err := InsertAlertNotificationState(context.Background(), createCmd)
  31. So(err, ShouldBeNil)
  32. err = InsertAlertNotificationState(context.Background(), createCmd)
  33. So(err, ShouldEqual, models.ErrAlertNotificationStateAlreadyExist)
  34. Convey("should be able to update alert notifier state", func() {
  35. updateCmd := &models.SetAlertNotificationStateToPendingCommand{
  36. Id: 1,
  37. SentAt: 1,
  38. Version: 0,
  39. }
  40. err := SetAlertNotificationStateToPendingCommand(context.Background(), updateCmd)
  41. So(err, ShouldBeNil)
  42. Convey("should not be able to set pending on old version", func() {
  43. err = SetAlertNotificationStateToPendingCommand(context.Background(), updateCmd)
  44. So(err, ShouldEqual, models.ErrAlertNotificationStateVersionConflict)
  45. })
  46. Convey("should be able to set state to completed", func() {
  47. cmd := &models.SetAlertNotificationStateToCompleteCommand{Id: 1}
  48. err = SetAlertNotificationStateToCompleteCommand(context.Background(), cmd)
  49. So(err, ShouldBeNil)
  50. })
  51. })
  52. })
  53. })
  54. Convey("Alert notifications should be empty", func() {
  55. cmd := &models.GetAlertNotificationsQuery{
  56. OrgId: 2,
  57. Name: "email",
  58. }
  59. err := GetAlertNotifications(cmd)
  60. So(err, ShouldBeNil)
  61. So(cmd.Result, ShouldBeNil)
  62. })
  63. Convey("Cannot save alert notifier with send reminder = true", func() {
  64. cmd := &models.CreateAlertNotificationCommand{
  65. Name: "ops",
  66. Type: "email",
  67. OrgId: 1,
  68. SendReminder: true,
  69. Settings: simplejson.New(),
  70. }
  71. Convey("and missing frequency", func() {
  72. err := CreateAlertNotificationCommand(cmd)
  73. So(err, ShouldEqual, models.ErrNotificationFrequencyNotFound)
  74. })
  75. Convey("invalid frequency", func() {
  76. cmd.Frequency = "invalid duration"
  77. err := CreateAlertNotificationCommand(cmd)
  78. So(err.Error(), ShouldEqual, "time: invalid duration invalid duration")
  79. })
  80. })
  81. Convey("Cannot update alert notifier with send reminder = false", func() {
  82. cmd := &models.CreateAlertNotificationCommand{
  83. Name: "ops update",
  84. Type: "email",
  85. OrgId: 1,
  86. SendReminder: false,
  87. Settings: simplejson.New(),
  88. }
  89. err := CreateAlertNotificationCommand(cmd)
  90. So(err, ShouldBeNil)
  91. updateCmd := &models.UpdateAlertNotificationCommand{
  92. Id: cmd.Result.Id,
  93. SendReminder: true,
  94. }
  95. Convey("and missing frequency", func() {
  96. err := UpdateAlertNotification(updateCmd)
  97. So(err, ShouldEqual, models.ErrNotificationFrequencyNotFound)
  98. })
  99. Convey("invalid frequency", func() {
  100. updateCmd.Frequency = "invalid duration"
  101. err := UpdateAlertNotification(updateCmd)
  102. So(err, ShouldNotBeNil)
  103. So(err.Error(), ShouldEqual, "time: invalid duration invalid duration")
  104. })
  105. })
  106. Convey("Can save Alert Notification", func() {
  107. cmd := &models.CreateAlertNotificationCommand{
  108. Name: "ops",
  109. Type: "email",
  110. OrgId: 1,
  111. SendReminder: true,
  112. Frequency: "10s",
  113. Settings: simplejson.New(),
  114. }
  115. err := CreateAlertNotificationCommand(cmd)
  116. So(err, ShouldBeNil)
  117. So(cmd.Result.Id, ShouldNotEqual, 0)
  118. So(cmd.Result.OrgId, ShouldNotEqual, 0)
  119. So(cmd.Result.Type, ShouldEqual, "email")
  120. So(cmd.Result.Frequency, ShouldEqual, 10*time.Second)
  121. Convey("Cannot save Alert Notification with the same name", func() {
  122. err = CreateAlertNotificationCommand(cmd)
  123. So(err, ShouldNotBeNil)
  124. })
  125. Convey("Can update alert notification", func() {
  126. newCmd := &models.UpdateAlertNotificationCommand{
  127. Name: "NewName",
  128. Type: "webhook",
  129. OrgId: cmd.Result.OrgId,
  130. SendReminder: true,
  131. Frequency: "60s",
  132. Settings: simplejson.New(),
  133. Id: cmd.Result.Id,
  134. }
  135. err := UpdateAlertNotification(newCmd)
  136. So(err, ShouldBeNil)
  137. So(newCmd.Result.Name, ShouldEqual, "NewName")
  138. So(newCmd.Result.Frequency, ShouldEqual, 60*time.Second)
  139. })
  140. Convey("Can update alert notification to disable sending of reminders", func() {
  141. newCmd := &models.UpdateAlertNotificationCommand{
  142. Name: "NewName",
  143. Type: "webhook",
  144. OrgId: cmd.Result.OrgId,
  145. SendReminder: false,
  146. Settings: simplejson.New(),
  147. Id: cmd.Result.Id,
  148. }
  149. err := UpdateAlertNotification(newCmd)
  150. So(err, ShouldBeNil)
  151. So(newCmd.Result.SendReminder, ShouldBeFalse)
  152. })
  153. })
  154. Convey("Can search using an array of ids", func() {
  155. cmd1 := models.CreateAlertNotificationCommand{Name: "nagios", Type: "webhook", OrgId: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
  156. cmd2 := models.CreateAlertNotificationCommand{Name: "slack", Type: "webhook", OrgId: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
  157. cmd3 := models.CreateAlertNotificationCommand{Name: "ops2", Type: "email", OrgId: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
  158. cmd4 := models.CreateAlertNotificationCommand{IsDefault: true, Name: "default", Type: "email", OrgId: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
  159. otherOrg := models.CreateAlertNotificationCommand{Name: "default", Type: "email", OrgId: 2, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
  160. So(CreateAlertNotificationCommand(&cmd1), ShouldBeNil)
  161. So(CreateAlertNotificationCommand(&cmd2), ShouldBeNil)
  162. So(CreateAlertNotificationCommand(&cmd3), ShouldBeNil)
  163. So(CreateAlertNotificationCommand(&cmd4), ShouldBeNil)
  164. So(CreateAlertNotificationCommand(&otherOrg), ShouldBeNil)
  165. Convey("search", func() {
  166. query := &models.GetAlertNotificationsToSendQuery{
  167. Ids: []int64{cmd1.Result.Id, cmd2.Result.Id, 112341231},
  168. OrgId: 1,
  169. }
  170. err := GetAlertNotificationsToSend(query)
  171. So(err, ShouldBeNil)
  172. So(len(query.Result), ShouldEqual, 3)
  173. })
  174. Convey("all", func() {
  175. query := &models.GetAllAlertNotificationsQuery{
  176. OrgId: 1,
  177. }
  178. err := GetAllAlertNotifications(query)
  179. So(err, ShouldBeNil)
  180. So(len(query.Result), ShouldEqual, 4)
  181. })
  182. })
  183. })
  184. }