base_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. model := &m.AlertNotification{
  16. Id: 1,
  17. Name: "name",
  18. Type: "email",
  19. Settings: bJson,
  20. }
  21. Convey("can parse false value", func() {
  22. bJson.Set("uploadImage", false)
  23. base := NewNotifierBase(model)
  24. So(base.UploadImage, ShouldBeFalse)
  25. })
  26. Convey("can parse true value", func() {
  27. bJson.Set("uploadImage", true)
  28. base := NewNotifierBase(model)
  29. So(base.UploadImage, ShouldBeTrue)
  30. })
  31. Convey("default value should be true for backwards compatibility", func() {
  32. base := NewNotifierBase(model)
  33. So(base.UploadImage, ShouldBeTrue)
  34. })
  35. })
  36. Convey("should notify", func() {
  37. Convey("pending -> ok", func() {
  38. context := alerting.NewEvalContext(context.TODO(), &alerting.Rule{
  39. State: m.AlertStatePending,
  40. })
  41. context.Rule.State = m.AlertStateOK
  42. timeNow := time.Now()
  43. So(defaultShouldNotify(context, true, 0, &timeNow), ShouldBeFalse)
  44. })
  45. Convey("ok -> alerting", func() {
  46. context := alerting.NewEvalContext(context.TODO(), &alerting.Rule{
  47. State: m.AlertStateOK,
  48. })
  49. context.Rule.State = m.AlertStateAlerting
  50. timeNow := time.Now()
  51. So(defaultShouldNotify(context, true, 0, &timeNow), ShouldBeTrue)
  52. })
  53. })
  54. })
  55. }