alert_notification_test.go 11 KB

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