eval_context_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package alerting
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/grafana/grafana/pkg/models"
  6. . "github.com/smartystreets/goconvey/convey"
  7. )
  8. func TestAlertingEvalContext(t *testing.T) {
  9. Convey("Eval context", t, func() {
  10. ctx := NewEvalContext(context.TODO(), &Rule{Conditions: []Condition{&conditionStub{firing: true}}})
  11. Convey("Should update alert state", func() {
  12. Convey("ok -> alerting", func() {
  13. ctx.PrevAlertState = models.AlertStateOK
  14. ctx.Rule.State = models.AlertStateAlerting
  15. So(ctx.ShouldUpdateAlertState(), ShouldBeTrue)
  16. })
  17. Convey("ok -> ok", func() {
  18. ctx.PrevAlertState = models.AlertStateOK
  19. ctx.Rule.State = models.AlertStateOK
  20. So(ctx.ShouldUpdateAlertState(), ShouldBeFalse)
  21. })
  22. })
  23. Convey("Should send notifications", func() {
  24. Convey("pending -> ok", func() {
  25. ctx.PrevAlertState = models.AlertStatePending
  26. ctx.Rule.State = models.AlertStateOK
  27. So(ctx.ShouldSendNotification(), ShouldBeFalse)
  28. })
  29. Convey("ok -> alerting", func() {
  30. ctx.PrevAlertState = models.AlertStateOK
  31. ctx.Rule.State = models.AlertStateAlerting
  32. So(ctx.ShouldSendNotification(), ShouldBeTrue)
  33. })
  34. })
  35. })
  36. }