alert_notification_test.go 7.2 KB

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