alert_notification_test.go 8.7 KB

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