alert_notification_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package sqlstore
  2. import (
  3. "testing"
  4. "github.com/grafana/grafana/pkg/components/simplejson"
  5. m "github.com/grafana/grafana/pkg/models"
  6. . "github.com/smartystreets/goconvey/convey"
  7. )
  8. func TestAlertNotificationSQLAccess(t *testing.T) {
  9. Convey("Testing Alert notification sql access", t, func() {
  10. InitTestDB(t)
  11. Convey("Alert notifications should be empty", func() {
  12. cmd := &m.GetAlertNotificationsQuery{
  13. OrgId: 2,
  14. Name: "email",
  15. }
  16. err := GetAlertNotifications(cmd)
  17. So(err, ShouldBeNil)
  18. So(cmd.Result, ShouldBeNil)
  19. })
  20. Convey("Cannot save alert notifier with send reminder = true", func() {
  21. cmd := &m.CreateAlertNotificationCommand{
  22. Name: "ops",
  23. Type: "email",
  24. OrgId: 1,
  25. SendReminder: true,
  26. Settings: simplejson.New(),
  27. }
  28. Convey("and missing frequency", func() {
  29. err := CreateAlertNotificationCommand(cmd)
  30. So(err, ShouldEqual, m.ErrNotificationFrequencyNotFound)
  31. })
  32. Convey("invalid frequency", func() {
  33. cmd.Frequency = "invalid duration"
  34. err := CreateAlertNotificationCommand(cmd)
  35. So(err.Error(), ShouldEqual, "time: invalid duration invalid duration")
  36. })
  37. })
  38. Convey("Cannot update alert notifier with notitfyonce = false", func() {
  39. cmd := &m.CreateAlertNotificationCommand{
  40. Name: "ops update",
  41. Type: "email",
  42. OrgId: 1,
  43. SendReminder: false,
  44. Settings: simplejson.New(),
  45. }
  46. err := CreateAlertNotificationCommand(cmd)
  47. So(err, ShouldBeNil)
  48. updateCmd := &m.UpdateAlertNotificationCommand{
  49. Id: cmd.Result.Id,
  50. SendReminder: true,
  51. }
  52. Convey("and missing frequency", func() {
  53. err := UpdateAlertNotification(updateCmd)
  54. So(err, ShouldEqual, m.ErrNotificationFrequencyNotFound)
  55. })
  56. Convey("invalid frequency", func() {
  57. updateCmd.Frequency = "invalid duration"
  58. err := UpdateAlertNotification(updateCmd)
  59. So(err, ShouldNotBeNil)
  60. So(err.Error(), ShouldEqual, "time: invalid duration invalid duration")
  61. })
  62. })
  63. Convey("Can save Alert Notification", func() {
  64. cmd := &m.CreateAlertNotificationCommand{
  65. Name: "ops",
  66. Type: "email",
  67. OrgId: 1,
  68. SendReminder: true,
  69. Frequency: "10s",
  70. Settings: simplejson.New(),
  71. }
  72. err := CreateAlertNotificationCommand(cmd)
  73. So(err, ShouldBeNil)
  74. So(cmd.Result.Id, ShouldNotEqual, 0)
  75. So(cmd.Result.OrgId, ShouldNotEqual, 0)
  76. So(cmd.Result.Type, ShouldEqual, "email")
  77. Convey("Cannot save Alert Notification with the same name", func() {
  78. err = CreateAlertNotificationCommand(cmd)
  79. So(err, ShouldNotBeNil)
  80. })
  81. Convey("Can update alert notification", func() {
  82. newCmd := &m.UpdateAlertNotificationCommand{
  83. Name: "NewName",
  84. Type: "webhook",
  85. OrgId: cmd.Result.OrgId,
  86. SendReminder: true,
  87. Frequency: "10s",
  88. Settings: simplejson.New(),
  89. Id: cmd.Result.Id,
  90. }
  91. err := UpdateAlertNotification(newCmd)
  92. So(err, ShouldBeNil)
  93. So(newCmd.Result.Name, ShouldEqual, "NewName")
  94. })
  95. })
  96. Convey("Can search using an array of ids", func() {
  97. cmd1 := m.CreateAlertNotificationCommand{Name: "nagios", Type: "webhook", OrgId: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
  98. cmd2 := m.CreateAlertNotificationCommand{Name: "slack", Type: "webhook", OrgId: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
  99. cmd3 := m.CreateAlertNotificationCommand{Name: "ops2", Type: "email", OrgId: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
  100. cmd4 := m.CreateAlertNotificationCommand{IsDefault: true, Name: "default", Type: "email", OrgId: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
  101. otherOrg := m.CreateAlertNotificationCommand{Name: "default", Type: "email", OrgId: 2, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
  102. So(CreateAlertNotificationCommand(&cmd1), ShouldBeNil)
  103. So(CreateAlertNotificationCommand(&cmd2), ShouldBeNil)
  104. So(CreateAlertNotificationCommand(&cmd3), ShouldBeNil)
  105. So(CreateAlertNotificationCommand(&cmd4), ShouldBeNil)
  106. So(CreateAlertNotificationCommand(&otherOrg), ShouldBeNil)
  107. Convey("search", func() {
  108. query := &m.GetAlertNotificationsToSendQuery{
  109. Ids: []int64{cmd1.Result.Id, cmd2.Result.Id, 112341231},
  110. OrgId: 1,
  111. }
  112. err := GetAlertNotificationsToSend(query)
  113. So(err, ShouldBeNil)
  114. So(len(query.Result), ShouldEqual, 3)
  115. })
  116. Convey("all", func() {
  117. query := &m.GetAllAlertNotificationsQuery{
  118. OrgId: 1,
  119. }
  120. err := GetAllAlertNotifications(query)
  121. So(err, ShouldBeNil)
  122. So(len(query.Result), ShouldEqual, 4)
  123. })
  124. })
  125. })
  126. }