base_test.go 4.8 KB

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