alert_notification_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 notitfyonce = false", func() {
  21. cmd := &m.CreateAlertNotificationCommand{
  22. Name: "ops",
  23. Type: "email",
  24. OrgId: 1,
  25. NotifyOnce: false,
  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. NotifyOnce: true,
  44. Settings: simplejson.New(),
  45. }
  46. err := CreateAlertNotificationCommand(cmd)
  47. So(err, ShouldBeNil)
  48. updateCmd := &m.UpdateAlertNotificationCommand{
  49. Id: cmd.Result.Id,
  50. NotifyOnce: false,
  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.Error(), ShouldEqual, "time: invalid duration invalid duration")
  60. })
  61. })
  62. Convey("Can save Alert Notification", func() {
  63. cmd := &m.CreateAlertNotificationCommand{
  64. Name: "ops",
  65. Type: "email",
  66. OrgId: 1,
  67. NotifyOnce: true,
  68. Frequency: "10s",
  69. Settings: simplejson.New(),
  70. }
  71. err := CreateAlertNotificationCommand(cmd)
  72. So(err, ShouldBeNil)
  73. So(cmd.Result.Id, ShouldNotEqual, 0)
  74. So(cmd.Result.OrgId, ShouldNotEqual, 0)
  75. So(cmd.Result.Type, ShouldEqual, "email")
  76. Convey("Cannot save Alert Notification with the same name", func() {
  77. err = CreateAlertNotificationCommand(cmd)
  78. So(err, ShouldNotBeNil)
  79. })
  80. Convey("Can update alert notification", func() {
  81. newCmd := &m.UpdateAlertNotificationCommand{
  82. Name: "NewName",
  83. Type: "webhook",
  84. OrgId: cmd.Result.OrgId,
  85. NotifyOnce: true,
  86. Frequency: "10s",
  87. Settings: simplejson.New(),
  88. Id: cmd.Result.Id,
  89. }
  90. err := UpdateAlertNotification(newCmd)
  91. So(err, ShouldBeNil)
  92. So(newCmd.Result.Name, ShouldEqual, "NewName")
  93. })
  94. })
  95. Convey("Can search using an array of ids", func() {
  96. cmd1 := m.CreateAlertNotificationCommand{Name: "nagios", Type: "webhook", OrgId: 1, NotifyOnce: true, Frequency: "10s", Settings: simplejson.New()}
  97. cmd2 := m.CreateAlertNotificationCommand{Name: "slack", Type: "webhook", OrgId: 1, NotifyOnce: true, Frequency: "10s", Settings: simplejson.New()}
  98. cmd3 := m.CreateAlertNotificationCommand{Name: "ops2", Type: "email", OrgId: 1, NotifyOnce: true, Frequency: "10s", Settings: simplejson.New()}
  99. cmd4 := m.CreateAlertNotificationCommand{IsDefault: true, Name: "default", Type: "email", OrgId: 1, NotifyOnce: true, Frequency: "10s", Settings: simplejson.New()}
  100. otherOrg := m.CreateAlertNotificationCommand{Name: "default", Type: "email", OrgId: 2, NotifyOnce: true, Frequency: "10s", Settings: simplejson.New()}
  101. So(CreateAlertNotificationCommand(&cmd1), ShouldBeNil)
  102. So(CreateAlertNotificationCommand(&cmd2), ShouldBeNil)
  103. So(CreateAlertNotificationCommand(&cmd3), ShouldBeNil)
  104. So(CreateAlertNotificationCommand(&cmd4), ShouldBeNil)
  105. So(CreateAlertNotificationCommand(&otherOrg), ShouldBeNil)
  106. Convey("search", func() {
  107. query := &m.GetAlertNotificationsToSendQuery{
  108. Ids: []int64{cmd1.Result.Id, cmd2.Result.Id, 112341231},
  109. OrgId: 1,
  110. }
  111. err := GetAlertNotificationsToSend(query)
  112. So(err, ShouldBeNil)
  113. So(len(query.Result), ShouldEqual, 3)
  114. })
  115. Convey("all", func() {
  116. query := &m.GetAllAlertNotificationsQuery{
  117. OrgId: 1,
  118. }
  119. err := GetAllAlertNotifications(query)
  120. So(err, ShouldBeNil)
  121. So(len(query.Result), ShouldEqual, 4)
  122. })
  123. })
  124. })
  125. }