alert_notification_test.go 10 KB

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