base_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 TestBaseNotifier(t *testing.T) {
  109. Convey("default constructor for notifiers", t, func() {
  110. bJson := simplejson.New()
  111. model := &m.AlertNotification{
  112. Id: 1,
  113. Name: "name",
  114. Type: "email",
  115. Settings: bJson,
  116. }
  117. Convey("can parse false value", func() {
  118. bJson.Set("uploadImage", false)
  119. base := NewNotifierBase(model)
  120. So(base.UploadImage, ShouldBeFalse)
  121. })
  122. Convey("can parse true value", func() {
  123. bJson.Set("uploadImage", true)
  124. base := NewNotifierBase(model)
  125. So(base.UploadImage, ShouldBeTrue)
  126. })
  127. Convey("default value should be true for backwards compatibility", func() {
  128. base := NewNotifierBase(model)
  129. So(base.UploadImage, ShouldBeTrue)
  130. })
  131. })
  132. }