base_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. tcs := []struct {
  13. name string
  14. prevState m.AlertStateType
  15. newState m.AlertStateType
  16. expected bool
  17. sendReminder bool
  18. }{
  19. {
  20. name: "pending -> ok should not trigger an notification",
  21. newState: m.AlertStatePending,
  22. prevState: m.AlertStateOK,
  23. expected: false,
  24. },
  25. {
  26. name: "ok -> alerting should trigger an notification",
  27. newState: m.AlertStateOK,
  28. prevState: m.AlertStateAlerting,
  29. expected: true,
  30. },
  31. {
  32. name: "ok -> pending should not trigger an notification",
  33. newState: m.AlertStateOK,
  34. prevState: m.AlertStatePending,
  35. expected: false,
  36. },
  37. {
  38. name: "ok -> ok should not trigger an notification",
  39. newState: m.AlertStateOK,
  40. prevState: m.AlertStateOK,
  41. expected: false,
  42. sendReminder: false,
  43. },
  44. {
  45. name: "ok -> alerting should not trigger an notification",
  46. newState: m.AlertStateOK,
  47. prevState: m.AlertStateAlerting,
  48. expected: true,
  49. sendReminder: true,
  50. },
  51. {
  52. name: "ok -> ok with reminder should not trigger an notification",
  53. newState: m.AlertStateOK,
  54. prevState: m.AlertStateOK,
  55. expected: false,
  56. sendReminder: true,
  57. },
  58. }
  59. for _, tc := range tcs {
  60. evalContext := alerting.NewEvalContext(context.TODO(), &alerting.Rule{
  61. State: tc.newState,
  62. })
  63. evalContext.Rule.State = tc.prevState
  64. timeNow := time.Now()
  65. if defaultShouldNotify(evalContext, true, 0, &timeNow) != tc.expected {
  66. t.Errorf("failed %s. expected %+v to return %v", tc.name, tc, tc.expected)
  67. }
  68. }
  69. }
  70. func TestBaseNotifier(t *testing.T) {
  71. Convey("default constructor for notifiers", t, func() {
  72. bJson := simplejson.New()
  73. model := &m.AlertNotification{
  74. Id: 1,
  75. Name: "name",
  76. Type: "email",
  77. Settings: bJson,
  78. }
  79. Convey("can parse false value", func() {
  80. bJson.Set("uploadImage", false)
  81. base := NewNotifierBase(model)
  82. So(base.UploadImage, ShouldBeFalse)
  83. })
  84. Convey("can parse true value", func() {
  85. bJson.Set("uploadImage", true)
  86. base := NewNotifierBase(model)
  87. So(base.UploadImage, ShouldBeTrue)
  88. })
  89. Convey("default value should be true for backwards compatibility", func() {
  90. base := NewNotifierBase(model)
  91. So(base.UploadImage, ShouldBeTrue)
  92. })
  93. })
  94. }