base_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package notifiers
  2. import (
  3. "context"
  4. "errors"
  5. "testing"
  6. "time"
  7. "github.com/grafana/grafana/pkg/bus"
  8. "github.com/grafana/grafana/pkg/components/simplejson"
  9. m "github.com/grafana/grafana/pkg/models"
  10. "github.com/grafana/grafana/pkg/services/alerting"
  11. . "github.com/smartystreets/goconvey/convey"
  12. )
  13. func TestShouldSendAlertNotification(t *testing.T) {
  14. tcs := []struct {
  15. name string
  16. prevState m.AlertStateType
  17. newState m.AlertStateType
  18. expected bool
  19. sendReminder bool
  20. }{
  21. {
  22. name: "pending -> ok should not trigger an notification",
  23. newState: m.AlertStatePending,
  24. prevState: m.AlertStateOK,
  25. expected: false,
  26. },
  27. {
  28. name: "ok -> alerting should trigger an notification",
  29. newState: m.AlertStateOK,
  30. prevState: m.AlertStateAlerting,
  31. expected: true,
  32. },
  33. {
  34. name: "ok -> pending should not trigger an notification",
  35. newState: m.AlertStateOK,
  36. prevState: m.AlertStatePending,
  37. expected: false,
  38. },
  39. {
  40. name: "ok -> ok should not trigger an notification",
  41. newState: m.AlertStateOK,
  42. prevState: m.AlertStateOK,
  43. expected: false,
  44. sendReminder: false,
  45. },
  46. {
  47. name: "ok -> alerting should not trigger an notification",
  48. newState: m.AlertStateOK,
  49. prevState: m.AlertStateAlerting,
  50. expected: true,
  51. sendReminder: true,
  52. },
  53. {
  54. name: "ok -> ok with reminder should not trigger an notification",
  55. newState: m.AlertStateOK,
  56. prevState: m.AlertStateOK,
  57. expected: false,
  58. sendReminder: true,
  59. },
  60. }
  61. for _, tc := range tcs {
  62. evalContext := alerting.NewEvalContext(context.TODO(), &alerting.Rule{
  63. State: tc.newState,
  64. })
  65. evalContext.Rule.State = tc.prevState
  66. if defaultShouldNotify(evalContext, true, 0, time.Now()) != tc.expected {
  67. t.Errorf("failed %s. expected %+v to return %v", tc.name, tc, tc.expected)
  68. }
  69. }
  70. }
  71. func TestShouldNotifyWhenNoJournalingIsFound(t *testing.T) {
  72. Convey("base notifier", t, func() {
  73. bus.ClearBusHandlers()
  74. notifier := NewNotifierBase(&m.AlertNotification{
  75. Id: 1,
  76. Name: "name",
  77. Type: "email",
  78. Settings: simplejson.New(),
  79. })
  80. evalContext := alerting.NewEvalContext(context.TODO(), &alerting.Rule{})
  81. Convey("should notify if no journaling is found", func() {
  82. bus.AddHandlerCtx("", func(ctx context.Context, q *m.GetLatestNotificationQuery) error {
  83. return m.ErrJournalingNotFound
  84. })
  85. if !notifier.ShouldNotify(context.Background(), evalContext) {
  86. t.Errorf("should send notifications when ErrJournalingNotFound is returned")
  87. }
  88. })
  89. Convey("should not notify query returns error", func() {
  90. bus.AddHandlerCtx("", func(ctx context.Context, q *m.GetLatestNotificationQuery) error {
  91. return errors.New("some kind of error unknown error")
  92. })
  93. if notifier.ShouldNotify(context.Background(), evalContext) {
  94. t.Errorf("should not send notifications when query returns error")
  95. }
  96. })
  97. })
  98. }
  99. func TestBaseNotifier(t *testing.T) {
  100. Convey("default constructor for notifiers", t, func() {
  101. bJson := simplejson.New()
  102. model := &m.AlertNotification{
  103. Id: 1,
  104. Name: "name",
  105. Type: "email",
  106. Settings: bJson,
  107. }
  108. Convey("can parse false value", func() {
  109. bJson.Set("uploadImage", false)
  110. base := NewNotifierBase(model)
  111. So(base.UploadImage, ShouldBeFalse)
  112. })
  113. Convey("can parse true value", func() {
  114. bJson.Set("uploadImage", true)
  115. base := NewNotifierBase(model)
  116. So(base.UploadImage, ShouldBeTrue)
  117. })
  118. Convey("default value should be true for backwards compatibility", func() {
  119. base := NewNotifierBase(model)
  120. So(base.UploadImage, ShouldBeTrue)
  121. })
  122. })
  123. }