notifier_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 (n *FakeNotifier) GetNotifierId() int64 {
  19. return 0
  20. }
  21. func (n *FakeNotifier) GetIsDefault() bool {
  22. return false
  23. }
  24. func (fn *FakeNotifier) Notify(alertResult *EvalContext) error { return nil }
  25. func (fn *FakeNotifier) PassesFilter(rule *Rule) bool {
  26. return fn.FakeMatchResult
  27. }
  28. func TestAlertNotificationExtraction(t *testing.T) {
  29. Convey("Notifier tests", t, func() {
  30. Convey("none firing alerts", func() {
  31. ctx := &EvalContext{
  32. Firing: false,
  33. Rule: &Rule{
  34. State: m.AlertStateAlerting,
  35. },
  36. }
  37. notifier := &FakeNotifier{FakeMatchResult: false}
  38. So(shouldUseNotification(notifier, ctx), ShouldBeTrue)
  39. })
  40. Convey("execution error cannot be ignored", func() {
  41. ctx := &EvalContext{
  42. Firing: true,
  43. Error: fmt.Errorf("I used to be a programmer just like you"),
  44. Rule: &Rule{
  45. State: m.AlertStateOK,
  46. },
  47. }
  48. notifier := &FakeNotifier{FakeMatchResult: false}
  49. So(shouldUseNotification(notifier, ctx), ShouldBeTrue)
  50. })
  51. Convey("firing alert that match", func() {
  52. ctx := &EvalContext{
  53. Firing: true,
  54. Rule: &Rule{
  55. State: models.AlertStateAlerting,
  56. },
  57. }
  58. notifier := &FakeNotifier{FakeMatchResult: true}
  59. So(shouldUseNotification(notifier, ctx), ShouldBeTrue)
  60. })
  61. Convey("firing alert that dont match", func() {
  62. ctx := &EvalContext{
  63. Firing: true,
  64. Rule: &Rule{State: m.AlertStateOK},
  65. }
  66. notifier := &FakeNotifier{FakeMatchResult: false}
  67. So(shouldUseNotification(notifier, ctx), ShouldBeFalse)
  68. })
  69. })
  70. }