eval_context_test.go 2.8 KB

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