notifier_test.go 1.7 KB

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