base_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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.AlertNotificationJournal
  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.AlertNotificationJournal{},
  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.AlertNotificationJournal{},
  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.AlertNotificationJournal{},
  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.AlertNotificationJournal{},
  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.AlertNotificationJournal{},
  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.AlertNotificationJournal{},
  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.AlertNotificationJournal{},
  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.AlertNotificationJournal{
  88. {SentAt: tnow.Add(-time.Minute).Unix(), Success: true},
  89. },
  90. expect: false,
  91. },
  92. {
  93. name: "alerting -> alerting with reminder and failed recent journal event should trigger",
  94. newState: m.AlertStateAlerting,
  95. prevState: m.AlertStateAlerting,
  96. frequency: time.Minute * 10,
  97. sendReminder: true,
  98. expect: true,
  99. journals: []m.AlertNotificationJournal{
  100. {SentAt: tnow.Add(-time.Minute).Unix(), Success: false}, // recent failed notification
  101. {SentAt: tnow.Add(-time.Hour).Unix(), Success: true}, // old successful notification
  102. },
  103. },
  104. }
  105. for _, tc := range tcs {
  106. evalContext := alerting.NewEvalContext(context.TODO(), &alerting.Rule{
  107. State: tc.newState,
  108. })
  109. evalContext.Rule.State = tc.prevState
  110. if defaultShouldNotify(evalContext, true, tc.frequency, tc.journals) != tc.expect {
  111. t.Errorf("failed test %s.\n expected \n%+v \nto return: %v", tc.name, tc, tc.expect)
  112. }
  113. }
  114. }
  115. func TestShouldNotifyWhenNoJournalingIsFound(t *testing.T) {
  116. Convey("base notifier", t, func() {
  117. bus.ClearBusHandlers()
  118. notifier := NewNotifierBase(&m.AlertNotification{
  119. Id: 1,
  120. Name: "name",
  121. Type: "email",
  122. Settings: simplejson.New(),
  123. })
  124. evalContext := alerting.NewEvalContext(context.TODO(), &alerting.Rule{})
  125. Convey("should not notify query returns error", func() {
  126. bus.AddHandlerCtx("", func(ctx context.Context, q *m.GetLatestNotificationQuery) error {
  127. return errors.New("some kind of error unknown error")
  128. })
  129. if notifier.ShouldNotify(context.Background(), evalContext) {
  130. t.Errorf("should not send notifications when query returns error")
  131. }
  132. })
  133. })
  134. }
  135. func TestBaseNotifier(t *testing.T) {
  136. Convey("default constructor for notifiers", t, func() {
  137. bJson := simplejson.New()
  138. model := &m.AlertNotification{
  139. Id: 1,
  140. Name: "name",
  141. Type: "email",
  142. Settings: bJson,
  143. }
  144. Convey("can parse false value", func() {
  145. bJson.Set("uploadImage", false)
  146. base := NewNotifierBase(model)
  147. So(base.UploadImage, ShouldBeFalse)
  148. })
  149. Convey("can parse true value", func() {
  150. bJson.Set("uploadImage", true)
  151. base := NewNotifierBase(model)
  152. So(base.UploadImage, ShouldBeTrue)
  153. })
  154. Convey("default value should be true for backwards compatibility", func() {
  155. base := NewNotifierBase(model)
  156. So(base.UploadImage, ShouldBeTrue)
  157. })
  158. })
  159. }