eval_context_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. err := fmt.Errorf("Dummie error!")
  13. Convey("Should update alert state", func() {
  14. Convey("ok -> alerting", func() {
  15. ctx.PrevAlertState = models.AlertStateOK
  16. ctx.Rule.State = models.AlertStateAlerting
  17. So(ctx.ShouldUpdateAlertState(), ShouldBeTrue)
  18. })
  19. Convey("ok -> ok", func() {
  20. ctx.PrevAlertState = models.AlertStateOK
  21. ctx.Rule.State = models.AlertStateOK
  22. So(ctx.ShouldUpdateAlertState(), ShouldBeFalse)
  23. })
  24. })
  25. Convey("Should send notifications", func() {
  26. Convey("pending -> ok", func() {
  27. ctx.PrevAlertState = models.AlertStatePending
  28. ctx.Rule.State = models.AlertStateOK
  29. So(ctx.ShouldSendNotification(), ShouldBeFalse)
  30. })
  31. Convey("ok -> alerting", func() {
  32. ctx.PrevAlertState = models.AlertStateOK
  33. ctx.Rule.State = models.AlertStateAlerting
  34. So(ctx.ShouldSendNotification(), ShouldBeTrue)
  35. })
  36. Convey("alerting -> ok", func() {
  37. ctx.PrevAlertState = models.AlertStateAlerting
  38. ctx.Rule.State = models.AlertStateOK
  39. So(ctx.ShouldSendNotification(), ShouldBeTrue)
  40. })
  41. Convey("ok -> no_data(alerting)", func() {
  42. ctx.PrevAlertState = models.AlertStateOK
  43. ctx.Rule.NoDataState = models.NoDataSetAlerting
  44. ctx.Rule.State = models.AlertStateAlerting
  45. So(ctx.ShouldSendNotification(), ShouldBeTrue)
  46. })
  47. Convey("ok -> no_data(ok)", func() {
  48. ctx.PrevAlertState = models.AlertStateOK
  49. ctx.Rule.NoDataState = models.NoDataSetOK
  50. ctx.NoDataFound = true
  51. ctx.Rule.State = models.AlertStateNoData
  52. So(ctx.ShouldSendNotification(), ShouldBeFalse)
  53. })
  54. Convey("ok -> no_data(keep_last)", func() {
  55. ctx.PrevAlertState = models.AlertStateOK
  56. ctx.Rule.NoDataState = models.NoDataKeepState
  57. ctx.Rule.State = models.AlertStateNoData
  58. ctx.NoDataFound = true
  59. So(ctx.ShouldSendNotification(), ShouldBeFalse)
  60. })
  61. Convey("ok -> execution_error(alerting)", func() {
  62. ctx.PrevAlertState = models.AlertStateOK
  63. ctx.Rule.State = models.AlertStateExecError
  64. ctx.Rule.ExecutionErrorState = models.NoDataSetAlerting
  65. ctx.Error = err
  66. So(ctx.ShouldSendNotification(), ShouldBeTrue)
  67. })
  68. Convey("ok -> execution_error(ok)", func() {
  69. ctx.PrevAlertState = models.AlertStateOK
  70. ctx.Rule.State = models.AlertStateExecError
  71. ctx.Rule.ExecutionErrorState = models.NoDataSetOK
  72. ctx.Error = err
  73. So(ctx.ShouldSendNotification(), ShouldBeFalse)
  74. })
  75. Convey("ok -> execution_error(keep_last)", func() {
  76. ctx.PrevAlertState = models.AlertStateOK
  77. ctx.Rule.State = models.AlertStateExecError
  78. ctx.Rule.ExecutionErrorState = models.NoDataKeepState
  79. ctx.Error = err
  80. So(ctx.ShouldSendNotification(), ShouldBeFalse)
  81. })
  82. })
  83. })
  84. }