eval_context_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 TestStateIsUpdatedWhenNeeded(t *testing.T) {
  10. ctx := NewEvalContext(context.TODO(), &Rule{Conditions: []Condition{&conditionStub{firing: true}}})
  11. t.Run("ok -> alerting", func(t *testing.T) {
  12. ctx.PrevAlertState = models.AlertStateOK
  13. ctx.Rule.State = models.AlertStateAlerting
  14. if !ctx.ShouldUpdateAlertState() {
  15. t.Fatalf("expected should updated to be true")
  16. }
  17. })
  18. t.Run("ok -> ok", func(t *testing.T) {
  19. ctx.PrevAlertState = models.AlertStateOK
  20. ctx.Rule.State = models.AlertStateOK
  21. if ctx.ShouldUpdateAlertState() {
  22. t.Fatalf("expected should updated to be false")
  23. }
  24. })
  25. }
  26. func TestAlertingEvalContext(t *testing.T) {
  27. Convey("Should compute and replace properly new rule state", t, func() {
  28. ctx := NewEvalContext(context.TODO(), &Rule{Conditions: []Condition{&conditionStub{firing: true}}})
  29. dummieError := fmt.Errorf("dummie error")
  30. Convey("ok -> alerting", func() {
  31. ctx.PrevAlertState = models.AlertStateOK
  32. ctx.Firing = true
  33. ctx.Rule.State = ctx.GetNewState()
  34. So(ctx.Rule.State, ShouldEqual, models.AlertStateAlerting)
  35. })
  36. Convey("ok -> error(alerting)", func() {
  37. ctx.PrevAlertState = models.AlertStateOK
  38. ctx.Error = dummieError
  39. ctx.Rule.ExecutionErrorState = models.ExecutionErrorSetAlerting
  40. ctx.Rule.State = ctx.GetNewState()
  41. So(ctx.Rule.State, ShouldEqual, models.AlertStateAlerting)
  42. })
  43. Convey("ok -> error(keep_last)", func() {
  44. ctx.PrevAlertState = models.AlertStateOK
  45. ctx.Error = dummieError
  46. ctx.Rule.ExecutionErrorState = models.ExecutionErrorKeepState
  47. ctx.Rule.State = ctx.GetNewState()
  48. So(ctx.Rule.State, ShouldEqual, models.AlertStateOK)
  49. })
  50. Convey("pending -> error(keep_last)", func() {
  51. ctx.PrevAlertState = models.AlertStatePending
  52. ctx.Error = dummieError
  53. ctx.Rule.ExecutionErrorState = models.ExecutionErrorKeepState
  54. ctx.Rule.State = ctx.GetNewState()
  55. So(ctx.Rule.State, ShouldEqual, models.AlertStatePending)
  56. })
  57. Convey("ok -> no_data(alerting)", func() {
  58. ctx.PrevAlertState = models.AlertStateOK
  59. ctx.Rule.NoDataState = models.NoDataSetAlerting
  60. ctx.NoDataFound = true
  61. ctx.Rule.State = ctx.GetNewState()
  62. So(ctx.Rule.State, ShouldEqual, models.AlertStateAlerting)
  63. })
  64. Convey("ok -> no_data(keep_last)", func() {
  65. ctx.PrevAlertState = models.AlertStateOK
  66. ctx.Rule.NoDataState = models.NoDataKeepState
  67. ctx.NoDataFound = true
  68. ctx.Rule.State = ctx.GetNewState()
  69. So(ctx.Rule.State, ShouldEqual, models.AlertStateOK)
  70. })
  71. Convey("pending -> no_data(keep_last)", func() {
  72. ctx.PrevAlertState = models.AlertStatePending
  73. ctx.Rule.NoDataState = models.NoDataKeepState
  74. ctx.NoDataFound = true
  75. ctx.Rule.State = ctx.GetNewState()
  76. So(ctx.Rule.State, ShouldEqual, models.AlertStatePending)
  77. })
  78. })
  79. }