alert_notification_test.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. oldTimeNow := timeNow
  18. now := time.Date(2018, 9, 30, 0, 0, 0, 0, time.UTC)
  19. timeNow = func() time.Time { return now }
  20. Convey("Get no existing state should create a new state", func() {
  21. query := &models.GetNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
  22. err := GetAlertNotificationState(context.Background(), query)
  23. So(err, ShouldBeNil)
  24. So(query.Result, ShouldNotBeNil)
  25. So(query.Result.State, ShouldEqual, "unknown")
  26. So(query.Result.Version, ShouldEqual, 0)
  27. So(query.Result.UpdatedAt, ShouldEqual, now.Unix())
  28. Convey("Get existing state should not create a new state", func() {
  29. query2 := &models.GetNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
  30. err := GetAlertNotificationState(context.Background(), query2)
  31. So(err, ShouldBeNil)
  32. So(query2.Result, ShouldNotBeNil)
  33. So(query2.Result.Id, ShouldEqual, query.Result.Id)
  34. So(query2.Result.UpdatedAt, ShouldEqual, now.Unix())
  35. })
  36. Convey("Update existing state to pending with correct version should update database", func() {
  37. s := *query.Result
  38. cmd := models.SetAlertNotificationStateToPendingCommand{
  39. State: &s,
  40. }
  41. err := SetAlertNotificationStateToPendingCommand(context.Background(), &cmd)
  42. So(err, ShouldBeNil)
  43. So(cmd.State.Version, ShouldEqual, 1)
  44. So(cmd.State.State, ShouldEqual, models.AlertNotificationStatePending)
  45. query2 := &models.GetNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
  46. err = GetAlertNotificationState(context.Background(), query2)
  47. So(err, ShouldBeNil)
  48. So(query2.Result.Version, ShouldEqual, 1)
  49. So(query2.Result.State, ShouldEqual, models.AlertNotificationStatePending)
  50. So(query2.Result.UpdatedAt, ShouldEqual, now.Unix())
  51. Convey("Update existing state to completed should update database", func() {
  52. s := *cmd.State
  53. cmd := models.SetAlertNotificationStateToCompleteCommand{
  54. State: &s,
  55. }
  56. err := SetAlertNotificationStateToCompleteCommand(context.Background(), &cmd)
  57. So(err, ShouldBeNil)
  58. query3 := &models.GetNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
  59. err = GetAlertNotificationState(context.Background(), query3)
  60. So(err, ShouldBeNil)
  61. So(query3.Result.Version, ShouldEqual, 2)
  62. So(query3.Result.State, ShouldEqual, models.AlertNotificationStateCompleted)
  63. So(query3.Result.UpdatedAt, ShouldEqual, now.Unix())
  64. })
  65. Convey("Update existing state to completed should update database, but return version mismatch", func() {
  66. cmd.State.Version = 1000
  67. s := *cmd.State
  68. cmd := models.SetAlertNotificationStateToCompleteCommand{
  69. State: &s,
  70. }
  71. err := SetAlertNotificationStateToCompleteCommand(context.Background(), &cmd)
  72. So(err, ShouldEqual, models.ErrAlertNotificationStateVersionConflict)
  73. query3 := &models.GetNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
  74. err = GetAlertNotificationState(context.Background(), query3)
  75. So(err, ShouldBeNil)
  76. So(query3.Result.Version, ShouldEqual, 1001)
  77. So(query3.Result.State, ShouldEqual, models.AlertNotificationStateCompleted)
  78. So(query3.Result.UpdatedAt, ShouldEqual, now.Unix())
  79. })
  80. })
  81. Convey("Update existing state to pending with incorrect version should return version mismatch error", func() {
  82. s := *query.Result
  83. s.Version = 1000
  84. cmd := models.SetAlertNotificationStateToPendingCommand{
  85. State: &s,
  86. }
  87. err := SetAlertNotificationStateToPendingCommand(context.Background(), &cmd)
  88. So(err, ShouldEqual, models.ErrAlertNotificationStateVersionConflict)
  89. })
  90. })
  91. Reset(func() {
  92. timeNow = oldTimeNow
  93. })
  94. })
  95. Convey("Alert notifications should be empty", func() {
  96. cmd := &models.GetAlertNotificationsQuery{
  97. OrgId: 2,
  98. Name: "email",
  99. }
  100. err := GetAlertNotifications(cmd)
  101. So(err, ShouldBeNil)
  102. So(cmd.Result, ShouldBeNil)
  103. })
  104. Convey("Cannot save alert notifier with send reminder = true", func() {
  105. cmd := &models.CreateAlertNotificationCommand{
  106. Name: "ops",
  107. Type: "email",
  108. OrgId: 1,
  109. SendReminder: true,
  110. Settings: simplejson.New(),
  111. }
  112. Convey("and missing frequency", func() {
  113. err := CreateAlertNotificationCommand(cmd)
  114. So(err, ShouldEqual, models.ErrNotificationFrequencyNotFound)
  115. })
  116. Convey("invalid frequency", func() {
  117. cmd.Frequency = "invalid duration"
  118. err := CreateAlertNotificationCommand(cmd)
  119. So(err.Error(), ShouldEqual, "time: invalid duration invalid duration")
  120. })
  121. })
  122. Convey("Cannot update alert notifier with send reminder = false", func() {
  123. cmd := &models.CreateAlertNotificationCommand{
  124. Name: "ops update",
  125. Type: "email",
  126. OrgId: 1,
  127. SendReminder: false,
  128. Settings: simplejson.New(),
  129. }
  130. err := CreateAlertNotificationCommand(cmd)
  131. So(err, ShouldBeNil)
  132. updateCmd := &models.UpdateAlertNotificationCommand{
  133. Id: cmd.Result.Id,
  134. SendReminder: true,
  135. }
  136. Convey("and missing frequency", func() {
  137. err := UpdateAlertNotification(updateCmd)
  138. So(err, ShouldEqual, models.ErrNotificationFrequencyNotFound)
  139. })
  140. Convey("invalid frequency", func() {
  141. updateCmd.Frequency = "invalid duration"
  142. err := UpdateAlertNotification(updateCmd)
  143. So(err, ShouldNotBeNil)
  144. So(err.Error(), ShouldEqual, "time: invalid duration invalid duration")
  145. })
  146. })
  147. Convey("Can save Alert Notification", func() {
  148. cmd := &models.CreateAlertNotificationCommand{
  149. Name: "ops",
  150. Type: "email",
  151. OrgId: 1,
  152. SendReminder: true,
  153. Frequency: "10s",
  154. Settings: simplejson.New(),
  155. }
  156. err := CreateAlertNotificationCommand(cmd)
  157. So(err, ShouldBeNil)
  158. So(cmd.Result.Id, ShouldNotEqual, 0)
  159. So(cmd.Result.OrgId, ShouldNotEqual, 0)
  160. So(cmd.Result.Type, ShouldEqual, "email")
  161. So(cmd.Result.Frequency, ShouldEqual, 10*time.Second)
  162. Convey("Cannot save Alert Notification with the same name", func() {
  163. err = CreateAlertNotificationCommand(cmd)
  164. So(err, ShouldNotBeNil)
  165. })
  166. Convey("Can update alert notification", func() {
  167. newCmd := &models.UpdateAlertNotificationCommand{
  168. Name: "NewName",
  169. Type: "webhook",
  170. OrgId: cmd.Result.OrgId,
  171. SendReminder: true,
  172. Frequency: "60s",
  173. Settings: simplejson.New(),
  174. Id: cmd.Result.Id,
  175. }
  176. err := UpdateAlertNotification(newCmd)
  177. So(err, ShouldBeNil)
  178. So(newCmd.Result.Name, ShouldEqual, "NewName")
  179. So(newCmd.Result.Frequency, ShouldEqual, 60*time.Second)
  180. })
  181. Convey("Can update alert notification to disable sending of reminders", func() {
  182. newCmd := &models.UpdateAlertNotificationCommand{
  183. Name: "NewName",
  184. Type: "webhook",
  185. OrgId: cmd.Result.OrgId,
  186. SendReminder: false,
  187. Settings: simplejson.New(),
  188. Id: cmd.Result.Id,
  189. }
  190. err := UpdateAlertNotification(newCmd)
  191. So(err, ShouldBeNil)
  192. So(newCmd.Result.SendReminder, ShouldBeFalse)
  193. })
  194. })
  195. Convey("Can search using an array of ids", func() {
  196. cmd1 := models.CreateAlertNotificationCommand{Name: "nagios", Type: "webhook", OrgId: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
  197. cmd2 := models.CreateAlertNotificationCommand{Name: "slack", Type: "webhook", OrgId: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
  198. cmd3 := models.CreateAlertNotificationCommand{Name: "ops2", Type: "email", OrgId: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
  199. cmd4 := models.CreateAlertNotificationCommand{IsDefault: true, Name: "default", Type: "email", OrgId: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
  200. otherOrg := models.CreateAlertNotificationCommand{Name: "default", Type: "email", OrgId: 2, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
  201. So(CreateAlertNotificationCommand(&cmd1), ShouldBeNil)
  202. So(CreateAlertNotificationCommand(&cmd2), ShouldBeNil)
  203. So(CreateAlertNotificationCommand(&cmd3), ShouldBeNil)
  204. So(CreateAlertNotificationCommand(&cmd4), ShouldBeNil)
  205. So(CreateAlertNotificationCommand(&otherOrg), ShouldBeNil)
  206. Convey("search", func() {
  207. query := &models.GetAlertNotificationsToSendQuery{
  208. Ids: []int64{cmd1.Result.Id, cmd2.Result.Id, 112341231},
  209. OrgId: 1,
  210. }
  211. err := GetAlertNotificationsToSend(query)
  212. So(err, ShouldBeNil)
  213. So(len(query.Result), ShouldEqual, 3)
  214. })
  215. Convey("all", func() {
  216. query := &models.GetAllAlertNotificationsQuery{
  217. OrgId: 1,
  218. }
  219. err := GetAllAlertNotifications(query)
  220. So(err, ShouldBeNil)
  221. So(len(query.Result), ShouldEqual, 4)
  222. })
  223. })
  224. })
  225. }