base_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. tnow := time.Now()
  15. tcs := []struct {
  16. name string
  17. prevState m.AlertStateType
  18. newState m.AlertStateType
  19. sendReminder bool
  20. frequency time.Duration
  21. journals *m.AlertNotificationState
  22. expect bool
  23. }{
  24. {
  25. name: "pending -> ok should not trigger an notification",
  26. newState: m.AlertStatePending,
  27. prevState: m.AlertStateOK,
  28. sendReminder: false,
  29. journals: &m.AlertNotificationState{},
  30. expect: false,
  31. },
  32. {
  33. name: "ok -> alerting should trigger an notification",
  34. newState: m.AlertStateOK,
  35. prevState: m.AlertStateAlerting,
  36. sendReminder: false,
  37. journals: &m.AlertNotificationState{},
  38. expect: true,
  39. },
  40. {
  41. name: "ok -> pending should not trigger an notification",
  42. newState: m.AlertStateOK,
  43. prevState: m.AlertStatePending,
  44. sendReminder: false,
  45. journals: &m.AlertNotificationState{},
  46. expect: false,
  47. },
  48. {
  49. name: "ok -> ok should not trigger an notification",
  50. newState: m.AlertStateOK,
  51. prevState: m.AlertStateOK,
  52. sendReminder: false,
  53. journals: &m.AlertNotificationState{},
  54. expect: false,
  55. },
  56. {
  57. name: "ok -> alerting should trigger an notification",
  58. newState: m.AlertStateOK,
  59. prevState: m.AlertStateAlerting,
  60. sendReminder: true,
  61. journals: &m.AlertNotificationState{},
  62. expect: true,
  63. },
  64. {
  65. name: "ok -> ok with reminder should not trigger an notification",
  66. newState: m.AlertStateOK,
  67. prevState: m.AlertStateOK,
  68. sendReminder: true,
  69. journals: &m.AlertNotificationState{},
  70. expect: false,
  71. },
  72. {
  73. name: "alerting -> alerting with reminder and no journaling should trigger",
  74. newState: m.AlertStateAlerting,
  75. prevState: m.AlertStateAlerting,
  76. frequency: time.Minute * 10,
  77. sendReminder: true,
  78. journals: &m.AlertNotificationState{},
  79. expect: true,
  80. },
  81. {
  82. name: "alerting -> alerting with reminder and successful recent journal event should not trigger",
  83. newState: m.AlertStateAlerting,
  84. prevState: m.AlertStateAlerting,
  85. frequency: time.Minute * 10,
  86. sendReminder: true,
  87. journals: &m.AlertNotificationState{SentAt: tnow.Add(-time.Minute).Unix()},
  88. expect: false,
  89. },
  90. {
  91. name: "alerting -> alerting with reminder and failed recent journal event should trigger",
  92. newState: m.AlertStateAlerting,
  93. prevState: m.AlertStateAlerting,
  94. frequency: time.Minute * 10,
  95. sendReminder: true,
  96. expect: true,
  97. journals: &m.AlertNotificationState{SentAt: tnow.Add(-time.Hour).Unix()},
  98. },
  99. }
  100. for _, tc := range tcs {
  101. evalContext := alerting.NewEvalContext(context.TODO(), &alerting.Rule{
  102. State: tc.newState,
  103. })
  104. evalContext.Rule.State = tc.prevState
  105. if defaultShouldNotify(evalContext, true, tc.frequency, tc.journals) != tc.expect {
  106. t.Errorf("failed test %s.\n expected \n%+v \nto return: %v", tc.name, tc, tc.expect)
  107. }
  108. }
  109. }
  110. func TestShouldNotifyWhenNoJournalingIsFound(t *testing.T) {
  111. Convey("base notifier", t, func() {
  112. bus.ClearBusHandlers()
  113. notifier := NewNotifierBase(&m.AlertNotification{
  114. Id: 1,
  115. Name: "name",
  116. Type: "email",
  117. Settings: simplejson.New(),
  118. })
  119. evalContext := alerting.NewEvalContext(context.TODO(), &alerting.Rule{})
  120. Convey("should not notify query returns error", func() {
  121. bus.AddHandlerCtx("", func(ctx context.Context, q *m.GetNotificationStateQuery) error {
  122. return errors.New("some kind of error unknown error")
  123. })
  124. if notifier.ShouldNotify(context.Background(), evalContext) {
  125. t.Errorf("should not send notifications when query returns error")
  126. }
  127. })
  128. })
  129. }
  130. func TestBaseNotifier(t *testing.T) {
  131. Convey("default constructor for notifiers", t, func() {
  132. bJson := simplejson.New()
  133. model := &m.AlertNotification{
  134. Id: 1,
  135. Name: "name",
  136. Type: "email",
  137. Settings: bJson,
  138. }
  139. Convey("can parse false value", func() {
  140. bJson.Set("uploadImage", false)
  141. base := NewNotifierBase(model)
  142. So(base.UploadImage, ShouldBeFalse)
  143. })
  144. Convey("can parse true value", func() {
  145. bJson.Set("uploadImage", true)
  146. base := NewNotifierBase(model)
  147. So(base.UploadImage, ShouldBeTrue)
  148. })
  149. Convey("default value should be true for backwards compatibility", func() {
  150. base := NewNotifierBase(model)
  151. So(base.UploadImage, ShouldBeTrue)
  152. })
  153. })
  154. }