base_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. if defaultShouldNotify(evalContext, true, 0, time.Now()) != tc.expected {
  65. t.Errorf("failed %s. expected %+v to return %v", tc.name, tc, tc.expected)
  66. }
  67. }
  68. }
  69. func TestBaseNotifier(t *testing.T) {
  70. Convey("default constructor for notifiers", t, func() {
  71. bJson := simplejson.New()
  72. model := &m.AlertNotification{
  73. Id: 1,
  74. Name: "name",
  75. Type: "email",
  76. Settings: bJson,
  77. }
  78. Convey("can parse false value", func() {
  79. bJson.Set("uploadImage", false)
  80. base := NewNotifierBase(model)
  81. So(base.UploadImage, ShouldBeFalse)
  82. })
  83. Convey("can parse true value", func() {
  84. bJson.Set("uploadImage", true)
  85. base := NewNotifierBase(model)
  86. So(base.UploadImage, ShouldBeTrue)
  87. })
  88. Convey("default value should be true for backwards compatibility", func() {
  89. base := NewNotifierBase(model)
  90. So(base.UploadImage, ShouldBeTrue)
  91. })
  92. })
  93. }