notifier_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package alerting
  2. import (
  3. "testing"
  4. "fmt"
  5. "github.com/grafana/grafana/pkg/models"
  6. m "github.com/grafana/grafana/pkg/models"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. type FakeNotifier struct {
  10. FakeMatchResult bool
  11. }
  12. func (fn *FakeNotifier) GetType() string {
  13. return "FakeNotifier"
  14. }
  15. func (fn *FakeNotifier) NeedsImage() bool {
  16. return true
  17. }
  18. func (fn *FakeNotifier) Notify(alertResult *EvalContext) error { return nil }
  19. func (fn *FakeNotifier) PassesFilter(rule *Rule) bool {
  20. return fn.FakeMatchResult
  21. }
  22. func TestAlertNotificationExtraction(t *testing.T) {
  23. Convey("Notifier tests", t, func() {
  24. Convey("none firing alerts", func() {
  25. ctx := &EvalContext{
  26. Firing: false,
  27. Rule: &Rule{
  28. State: m.AlertStateAlerting,
  29. },
  30. }
  31. notifier := &FakeNotifier{FakeMatchResult: false}
  32. So(shouldUseNotification(notifier, ctx), ShouldBeTrue)
  33. })
  34. Convey("execution error cannot be ignored", func() {
  35. ctx := &EvalContext{
  36. Firing: true,
  37. Error: fmt.Errorf("I used to be a programmer just like you"),
  38. Rule: &Rule{
  39. State: m.AlertStateOK,
  40. },
  41. }
  42. notifier := &FakeNotifier{FakeMatchResult: false}
  43. So(shouldUseNotification(notifier, ctx), ShouldBeTrue)
  44. })
  45. Convey("firing alert that match", func() {
  46. ctx := &EvalContext{
  47. Firing: true,
  48. Rule: &Rule{
  49. State: models.AlertStateAlerting,
  50. },
  51. }
  52. notifier := &FakeNotifier{FakeMatchResult: true}
  53. So(shouldUseNotification(notifier, ctx), ShouldBeTrue)
  54. })
  55. Convey("firing alert that dont match", func() {
  56. ctx := &EvalContext{
  57. Firing: true,
  58. Rule: &Rule{State: m.AlertStateOK},
  59. }
  60. notifier := &FakeNotifier{FakeMatchResult: false}
  61. So(shouldUseNotification(notifier, ctx), ShouldBeFalse)
  62. })
  63. })
  64. }