base_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 TestBaseNotifier(t *testing.T) {
  12. Convey("Base notifier tests", t, func() {
  13. Convey("default constructor for notifiers", func() {
  14. bJson := simplejson.New()
  15. Convey("can parse false value", func() {
  16. bJson.Set("uploadImage", false)
  17. base := NewNotifierBase(1, false, "name", "email", true, 0, bJson)
  18. So(base.UploadImage, ShouldBeFalse)
  19. })
  20. Convey("can parse true value", func() {
  21. bJson.Set("uploadImage", true)
  22. base := NewNotifierBase(1, false, "name", "email", true, 0, bJson)
  23. So(base.UploadImage, ShouldBeTrue)
  24. })
  25. Convey("default value should be true for backwards compatibility", func() {
  26. base := NewNotifierBase(1, false, "name", "email", true, 0, bJson)
  27. So(base.UploadImage, ShouldBeTrue)
  28. })
  29. })
  30. Convey("should notify", func() {
  31. Convey("pending -> ok", func() {
  32. context := alerting.NewEvalContext(context.TODO(), &alerting.Rule{
  33. State: m.AlertStatePending,
  34. })
  35. context.Rule.State = m.AlertStateOK
  36. timeNow := time.Now()
  37. So(defaultShouldNotify(context, true, 0, &timeNow), ShouldBeFalse)
  38. })
  39. Convey("ok -> alerting", func() {
  40. context := alerting.NewEvalContext(context.TODO(), &alerting.Rule{
  41. State: m.AlertStateOK,
  42. })
  43. context.Rule.State = m.AlertStateAlerting
  44. timeNow := time.Now()
  45. So(defaultShouldNotify(context, true, 0, &timeNow), ShouldBeTrue)
  46. })
  47. })
  48. })
  49. }