base_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. state *m.AlertNotificationState
  20. expect bool
  21. }{
  22. {
  23. name: "pending -> ok should not trigger an notification",
  24. newState: m.AlertStateOK,
  25. prevState: m.AlertStatePending,
  26. sendReminder: false,
  27. state: &m.AlertNotificationState{},
  28. expect: false,
  29. },
  30. {
  31. name: "ok -> alerting should trigger an notification",
  32. newState: m.AlertStateAlerting,
  33. prevState: m.AlertStateOK,
  34. sendReminder: false,
  35. state: &m.AlertNotificationState{},
  36. expect: true,
  37. },
  38. {
  39. name: "ok -> pending should not trigger an notification",
  40. newState: m.AlertStatePending,
  41. prevState: m.AlertStateOK,
  42. sendReminder: false,
  43. state: &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. state: &m.AlertNotificationState{},
  52. expect: false,
  53. },
  54. {
  55. name: "ok -> ok with reminder should not trigger an notification",
  56. newState: m.AlertStateOK,
  57. prevState: m.AlertStateOK,
  58. sendReminder: true,
  59. state: &m.AlertNotificationState{},
  60. expect: false,
  61. },
  62. {
  63. name: "alerting -> ok should trigger an notification",
  64. newState: m.AlertStateOK,
  65. prevState: m.AlertStateAlerting,
  66. sendReminder: false,
  67. state: &m.AlertNotificationState{},
  68. expect: true,
  69. },
  70. {
  71. name: "alerting -> ok should trigger an notification when reminders enabled",
  72. newState: m.AlertStateOK,
  73. prevState: m.AlertStateAlerting,
  74. frequency: time.Minute * 10,
  75. sendReminder: true,
  76. state: &m.AlertNotificationState{UpdatedAt: tnow.Add(-time.Minute).Unix()},
  77. expect: true,
  78. },
  79. {
  80. name: "alerting -> alerting with reminder and no state should trigger",
  81. newState: m.AlertStateAlerting,
  82. prevState: m.AlertStateAlerting,
  83. frequency: time.Minute * 10,
  84. sendReminder: true,
  85. state: &m.AlertNotificationState{},
  86. expect: true,
  87. },
  88. {
  89. name: "alerting -> alerting with reminder and last notification sent 1 minute ago should not trigger",
  90. newState: m.AlertStateAlerting,
  91. prevState: m.AlertStateAlerting,
  92. frequency: time.Minute * 10,
  93. sendReminder: true,
  94. state: &m.AlertNotificationState{UpdatedAt: tnow.Add(-time.Minute).Unix()},
  95. expect: false,
  96. },
  97. {
  98. name: "alerting -> alerting with reminder and last notifciation sent 11 minutes ago should trigger",
  99. newState: m.AlertStateAlerting,
  100. prevState: m.AlertStateAlerting,
  101. frequency: time.Minute * 10,
  102. sendReminder: true,
  103. state: &m.AlertNotificationState{UpdatedAt: tnow.Add(-11 * time.Minute).Unix()},
  104. expect: true,
  105. },
  106. {
  107. name: "OK -> alerting with notifciation state pending and updated 30 seconds ago should not trigger",
  108. newState: m.AlertStateAlerting,
  109. prevState: m.AlertStateOK,
  110. state: &m.AlertNotificationState{State: m.AlertNotificationStatePending, UpdatedAt: tnow.Add(-30 * time.Second).Unix()},
  111. expect: false,
  112. },
  113. {
  114. name: "OK -> alerting with notifciation state pending and updated 2 minutes ago should trigger",
  115. newState: m.AlertStateAlerting,
  116. prevState: m.AlertStateOK,
  117. state: &m.AlertNotificationState{State: m.AlertNotificationStatePending, UpdatedAt: tnow.Add(-2 * time.Minute).Unix()},
  118. expect: true,
  119. },
  120. }
  121. for _, tc := range tcs {
  122. evalContext := alerting.NewEvalContext(context.TODO(), &alerting.Rule{
  123. State: tc.prevState,
  124. })
  125. evalContext.Rule.State = tc.newState
  126. nb := &NotifierBase{SendReminder: tc.sendReminder, Frequency: tc.frequency}
  127. if nb.ShouldNotify(evalContext.Ctx, evalContext, tc.state) != tc.expect {
  128. t.Errorf("failed test %s.\n expected \n%+v \nto return: %v", tc.name, tc, tc.expect)
  129. }
  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. }