result_handler_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package alerting
  2. import (
  3. "context"
  4. "testing"
  5. "fmt"
  6. "github.com/grafana/grafana/pkg/models"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. func TestAlertingResultHandler(t *testing.T) {
  10. Convey("Result handler", t, func() {
  11. ctx := NewEvalContext(context.TODO(), &Rule{Conditions: []Condition{&conditionStub{firing: true}}})
  12. dummieError := fmt.Errorf("dummie")
  13. handler := NewResultHandler()
  14. Convey("Should update alert state", func() {
  15. Convey("ok -> alerting", func() {
  16. ctx.PrevAlertState = models.AlertStateOK
  17. ctx.Firing = true
  18. So(handler.GetStateFromEvaluation(ctx), ShouldEqual, models.AlertStateAlerting)
  19. So(ctx.ShouldUpdateAlertState(), ShouldBeTrue)
  20. })
  21. Convey("ok -> error(alerting)", func() {
  22. ctx.PrevAlertState = models.AlertStateOK
  23. ctx.Error = dummieError
  24. ctx.Rule.ExecutionErrorState = models.ExecutionErrorSetAlerting
  25. ctx.Rule.State = handler.GetStateFromEvaluation(ctx)
  26. So(ctx.Rule.State, ShouldEqual, models.AlertStateAlerting)
  27. So(ctx.ShouldUpdateAlertState(), ShouldBeTrue)
  28. })
  29. Convey("ok -> error(keep_last)", func() {
  30. ctx.PrevAlertState = models.AlertStateOK
  31. ctx.Error = dummieError
  32. ctx.Rule.ExecutionErrorState = models.ExecutionErrorKeepState
  33. ctx.Rule.State = handler.GetStateFromEvaluation(ctx)
  34. So(ctx.Rule.State, ShouldEqual, models.AlertStateOK)
  35. So(ctx.ShouldUpdateAlertState(), ShouldBeFalse)
  36. })
  37. Convey("pending -> error(keep_last)", func() {
  38. ctx.PrevAlertState = models.AlertStatePending
  39. ctx.Error = dummieError
  40. ctx.Rule.ExecutionErrorState = models.ExecutionErrorKeepState
  41. ctx.Rule.State = handler.GetStateFromEvaluation(ctx)
  42. So(ctx.Rule.State, ShouldEqual, models.AlertStatePending)
  43. So(ctx.ShouldUpdateAlertState(), ShouldBeFalse)
  44. })
  45. Convey("ok -> no_data(alerting)", func() {
  46. ctx.PrevAlertState = models.AlertStateOK
  47. ctx.Rule.NoDataState = models.NoDataSetAlerting
  48. ctx.NoDataFound = true
  49. ctx.Rule.State = handler.GetStateFromEvaluation(ctx)
  50. So(ctx.Rule.State, ShouldEqual, models.AlertStateAlerting)
  51. So(ctx.ShouldUpdateAlertState(), ShouldBeTrue)
  52. })
  53. Convey("ok -> no_data(keep_last)", func() {
  54. ctx.PrevAlertState = models.AlertStateOK
  55. ctx.Rule.NoDataState = models.NoDataKeepState
  56. ctx.NoDataFound = true
  57. ctx.Rule.State = handler.GetStateFromEvaluation(ctx)
  58. So(ctx.Rule.State, ShouldEqual, models.AlertStateOK)
  59. So(ctx.ShouldUpdateAlertState(), ShouldBeFalse)
  60. })
  61. Convey("pending -> no_data(keep_last)", func() {
  62. ctx.PrevAlertState = models.AlertStatePending
  63. ctx.Rule.NoDataState = models.NoDataKeepState
  64. ctx.NoDataFound = true
  65. ctx.Rule.State = handler.GetStateFromEvaluation(ctx)
  66. So(ctx.Rule.State, ShouldEqual, models.AlertStatePending)
  67. So(ctx.ShouldUpdateAlertState(), ShouldBeFalse)
  68. })
  69. })
  70. })
  71. }