alert_notification_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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.GetOrCreateNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
  22. err := GetOrCreateAlertNotificationState(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.GetOrCreateNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
  30. err := GetOrCreateAlertNotificationState(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.GetOrCreateNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
  46. err = GetOrCreateAlertNotificationState(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.GetOrCreateNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
  59. err = GetOrCreateAlertNotificationState(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.GetOrCreateNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
  74. err = GetOrCreateAlertNotificationState(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. Convey("Updating existing state to pending with incorrect version since alert rule state update version is higher", func() {
  91. s := *query.Result
  92. cmd := models.SetAlertNotificationStateToPendingCommand{
  93. State: &s,
  94. AlertRuleStateUpdatedVersion: 1000,
  95. }
  96. err := SetAlertNotificationStateToPendingCommand(context.Background(), &cmd)
  97. So(err, ShouldBeNil)
  98. So(cmd.State.Version, ShouldEqual, 1)
  99. So(cmd.State.State, ShouldEqual, models.AlertNotificationStatePending)
  100. })
  101. Convey("different version and same alert state change version should return error", func() {
  102. s := *query.Result
  103. s.Version = 1000
  104. cmd := models.SetAlertNotificationStateToPendingCommand{
  105. State: &s,
  106. }
  107. err := SetAlertNotificationStateToPendingCommand(context.Background(), &cmd)
  108. So(err, ShouldNotBeNil)
  109. })
  110. })
  111. Reset(func() {
  112. timeNow = oldTimeNow
  113. })
  114. })
  115. Convey("Alert notifications should be empty", func() {
  116. cmd := &models.GetAlertNotificationsQuery{
  117. OrgId: 2,
  118. Name: "email",
  119. }
  120. err := GetAlertNotifications(cmd)
  121. So(err, ShouldBeNil)
  122. So(cmd.Result, ShouldBeNil)
  123. })
  124. Convey("Cannot save alert notifier with send reminder = true", func() {
  125. cmd := &models.CreateAlertNotificationCommand{
  126. Name: "ops",
  127. Type: "email",
  128. OrgId: 1,
  129. SendReminder: true,
  130. Settings: simplejson.New(),
  131. }
  132. Convey("and missing frequency", func() {
  133. err := CreateAlertNotificationCommand(cmd)
  134. So(err, ShouldEqual, models.ErrNotificationFrequencyNotFound)
  135. })
  136. Convey("invalid frequency", func() {
  137. cmd.Frequency = "invalid duration"
  138. err := CreateAlertNotificationCommand(cmd)
  139. So(err.Error(), ShouldEqual, "time: invalid duration invalid duration")
  140. })
  141. })
  142. Convey("Cannot update alert notifier with send reminder = false", func() {
  143. cmd := &models.CreateAlertNotificationCommand{
  144. Name: "ops update",
  145. Type: "email",
  146. OrgId: 1,
  147. SendReminder: false,
  148. Settings: simplejson.New(),
  149. }
  150. err := CreateAlertNotificationCommand(cmd)
  151. So(err, ShouldBeNil)
  152. updateCmd := &models.UpdateAlertNotificationCommand{
  153. Id: cmd.Result.Id,
  154. SendReminder: true,
  155. }
  156. Convey("and missing frequency", func() {
  157. err := UpdateAlertNotification(updateCmd)
  158. So(err, ShouldEqual, models.ErrNotificationFrequencyNotFound)
  159. })
  160. Convey("invalid frequency", func() {
  161. updateCmd.Frequency = "invalid duration"
  162. err := UpdateAlertNotification(updateCmd)
  163. So(err, ShouldNotBeNil)
  164. So(err.Error(), ShouldEqual, "time: invalid duration invalid duration")
  165. })
  166. })
  167. Convey("Can save Alert Notification", func() {
  168. cmd := &models.CreateAlertNotificationCommand{
  169. Name: "ops",
  170. Type: "email",
  171. OrgId: 1,
  172. SendReminder: true,
  173. Frequency: "10s",
  174. Settings: simplejson.New(),
  175. }
  176. err := CreateAlertNotificationCommand(cmd)
  177. So(err, ShouldBeNil)
  178. So(cmd.Result.Id, ShouldNotEqual, 0)
  179. So(cmd.Result.OrgId, ShouldNotEqual, 0)
  180. So(cmd.Result.Type, ShouldEqual, "email")
  181. So(cmd.Result.Frequency, ShouldEqual, 10*time.Second)
  182. Convey("Cannot save Alert Notification with the same name", func() {
  183. err = CreateAlertNotificationCommand(cmd)
  184. So(err, ShouldNotBeNil)
  185. })
  186. Convey("Can update alert notification", func() {
  187. newCmd := &models.UpdateAlertNotificationCommand{
  188. Name: "NewName",
  189. Type: "webhook",
  190. OrgId: cmd.Result.OrgId,
  191. SendReminder: true,
  192. Frequency: "60s",
  193. Settings: simplejson.New(),
  194. Id: cmd.Result.Id,
  195. }
  196. err := UpdateAlertNotification(newCmd)
  197. So(err, ShouldBeNil)
  198. So(newCmd.Result.Name, ShouldEqual, "NewName")
  199. So(newCmd.Result.Frequency, ShouldEqual, 60*time.Second)
  200. })
  201. Convey("Can update alert notification to disable sending of reminders", func() {
  202. newCmd := &models.UpdateAlertNotificationCommand{
  203. Name: "NewName",
  204. Type: "webhook",
  205. OrgId: cmd.Result.OrgId,
  206. SendReminder: false,
  207. Settings: simplejson.New(),
  208. Id: cmd.Result.Id,
  209. }
  210. err := UpdateAlertNotification(newCmd)
  211. So(err, ShouldBeNil)
  212. So(newCmd.Result.SendReminder, ShouldBeFalse)
  213. })
  214. })
  215. Convey("Can search using an array of ids", func() {
  216. cmd1 := models.CreateAlertNotificationCommand{Name: "nagios", Type: "webhook", OrgId: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
  217. cmd2 := models.CreateAlertNotificationCommand{Name: "slack", Type: "webhook", OrgId: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
  218. cmd3 := models.CreateAlertNotificationCommand{Name: "ops2", Type: "email", OrgId: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
  219. cmd4 := models.CreateAlertNotificationCommand{IsDefault: true, Name: "default", Type: "email", OrgId: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
  220. otherOrg := models.CreateAlertNotificationCommand{Name: "default", Type: "email", OrgId: 2, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
  221. So(CreateAlertNotificationCommand(&cmd1), ShouldBeNil)
  222. So(CreateAlertNotificationCommand(&cmd2), ShouldBeNil)
  223. So(CreateAlertNotificationCommand(&cmd3), ShouldBeNil)
  224. So(CreateAlertNotificationCommand(&cmd4), ShouldBeNil)
  225. So(CreateAlertNotificationCommand(&otherOrg), ShouldBeNil)
  226. Convey("search", func() {
  227. query := &models.GetAlertNotificationsToSendQuery{
  228. Ids: []int64{cmd1.Result.Id, cmd2.Result.Id, 112341231},
  229. OrgId: 1,
  230. }
  231. err := GetAlertNotificationsToSend(query)
  232. So(err, ShouldBeNil)
  233. So(len(query.Result), ShouldEqual, 3)
  234. })
  235. Convey("all", func() {
  236. query := &models.GetAllAlertNotificationsQuery{
  237. OrgId: 1,
  238. }
  239. err := GetAllAlertNotifications(query)
  240. So(err, ShouldBeNil)
  241. So(len(query.Result), ShouldEqual, 4)
  242. })
  243. })
  244. })
  245. }