evaluator_test.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package conditions
  15. import (
  16. "testing"
  17. . "github.com/smartystreets/goconvey/convey"
  18. "yunion.io/x/onecloud/pkg/apis/monitor"
  19. )
  20. func evalutorScenario(typ string, params []float64, reducedValue float64) bool {
  21. evaluator, err := NewAlertEvaluator(&monitor.Condition{Type: typ, Params: params})
  22. So(err, ShouldBeNil)
  23. return evaluator.Eval(&reducedValue)
  24. }
  25. func TestEvalutors(t *testing.T) {
  26. Convey("greater than", t, func() {
  27. So(evalutorScenario("gt", []float64{1}, 3), ShouldBeTrue)
  28. So(evalutorScenario("gt", []float64{3}, 1), ShouldBeFalse)
  29. })
  30. Convey("less than", t, func() {
  31. So(evalutorScenario("lt", []float64{1}, 3), ShouldBeFalse)
  32. So(evalutorScenario("lt", []float64{3}, 1), ShouldBeTrue)
  33. })
  34. Convey("within_range", t, func() {
  35. So(evalutorScenario("within_range", []float64{1, 100}, 3), ShouldBeTrue)
  36. So(evalutorScenario("within_range", []float64{1, 100}, 300), ShouldBeFalse)
  37. So(evalutorScenario("within_range", []float64{100, 1}, 3), ShouldBeTrue)
  38. So(evalutorScenario("within_range", []float64{100, 1}, 300), ShouldBeFalse)
  39. })
  40. Convey("outside_range", t, func() {
  41. So(evalutorScenario("outside_range", []float64{1, 100}, 1000), ShouldBeTrue)
  42. So(evalutorScenario("outside_range", []float64{1, 100}, 50), ShouldBeFalse)
  43. So(evalutorScenario("outside_range", []float64{100, 1}, 1000), ShouldBeTrue)
  44. So(evalutorScenario("outside_range", []float64{100, 1}, 50), ShouldBeFalse)
  45. })
  46. Convey("no_value", t, func() {
  47. Convey("should be false if series have values", func() {
  48. So(evalutorScenario("no_value", nil, 50), ShouldBeFalse)
  49. })
  50. Convey("should be true when the series have no value", func() {
  51. evaluator, err := NewAlertEvaluator(&monitor.Condition{Type: "no_value"})
  52. So(err, ShouldBeNil)
  53. So(evaluator.Eval(nil), ShouldBeTrue)
  54. })
  55. })
  56. }