| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- package alerting
- import (
- "testing"
- . "github.com/smartystreets/goconvey/convey"
- )
- type conditionStub struct {
- firing bool
- }
- func (c *conditionStub) Eval(context *AlertResultContext) {
- context.Firing = c.firing
- }
- func TestAlertingExecutor(t *testing.T) {
- Convey("Test alert execution", t, func() {
- handler := NewHandler()
- Convey("Show return triggered with single passing condition", func() {
- context := NewAlertResultContext(&AlertRule{
- Conditions: []AlertCondition{&conditionStub{
- firing: true,
- }},
- })
- handler.eval(context)
- So(context.Firing, ShouldEqual, true)
- })
- Convey("Show return false with not passing condition", func() {
- context := NewAlertResultContext(&AlertRule{
- Conditions: []AlertCondition{
- &conditionStub{firing: true},
- &conditionStub{firing: false},
- },
- })
- handler.eval(context)
- So(context.Firing, ShouldEqual, false)
- })
- // Convey("Show return critical since below 2", func() {
- // rule := &AlertRule{
- // Critical: Level{Value: 10, Operator: "<"},
- // Transformer: transformers.NewAggregationTransformer("avg"),
- // }
- //
- // timeSeries := []*tsdb.TimeSeries{
- // tsdb.NewTimeSeries("test1", [][2]float64{{2, 0}}),
- // }
- //
- // result := executor.evaluateRule(rule, timeSeries)
- // So(result.State, ShouldEqual, alertstates.Critical)
- // })
- //
- // Convey("Show return critical since sum is above 10", func() {
- // rule := &AlertRule{
- // Critical: Level{Value: 10, Operator: ">"},
- // Transformer: transformers.NewAggregationTransformer("sum"),
- // }
- //
- // timeSeries := []*tsdb.TimeSeries{
- // tsdb.NewTimeSeries("test1", [][2]float64{{9, 0}, {9, 0}}),
- // }
- //
- // result := executor.evaluateRule(rule, timeSeries)
- // So(result.State, ShouldEqual, alertstates.Critical)
- // })
- //
- // Convey("Show return ok since avg is below 10", func() {
- // rule := &AlertRule{
- // Critical: Level{Value: 10, Operator: ">"},
- // Transformer: transformers.NewAggregationTransformer("avg"),
- // }
- //
- // timeSeries := []*tsdb.TimeSeries{
- // tsdb.NewTimeSeries("test1", [][2]float64{{9, 0}, {9, 0}}),
- // }
- //
- // result := executor.evaluateRule(rule, timeSeries)
- // So(result.State, ShouldEqual, alertstates.Ok)
- // })
- //
- // Convey("Show return ok since min is below 10", func() {
- // rule := &AlertRule{
- // Critical: Level{Value: 10, Operator: ">"},
- // Transformer: transformers.NewAggregationTransformer("avg"),
- // }
- //
- // timeSeries := []*tsdb.TimeSeries{
- // tsdb.NewTimeSeries("test1", [][2]float64{{11, 0}, {9, 0}}),
- // }
- //
- // result := executor.evaluateRule(rule, timeSeries)
- // So(result.State, ShouldEqual, alertstates.Ok)
- // })
- //
- // Convey("Show return ok since max is above 10", func() {
- // rule := &AlertRule{
- // Critical: Level{Value: 10, Operator: ">"},
- // Transformer: transformers.NewAggregationTransformer("max"),
- // }
- //
- // timeSeries := []*tsdb.TimeSeries{
- // tsdb.NewTimeSeries("test1", [][2]float64{{6, 0}, {11, 0}}),
- // }
- //
- // result := executor.evaluateRule(rule, timeSeries)
- // So(result.State, ShouldEqual, alertstates.Critical)
- // })
- //
- // })
- //
- // Convey("muliple time series", func() {
- // Convey("both are ok", func() {
- // rule := &AlertRule{
- // Critical: Level{Value: 10, Operator: ">"},
- // Transformer: transformers.NewAggregationTransformer("avg"),
- // }
- //
- // timeSeries := []*tsdb.TimeSeries{
- // tsdb.NewTimeSeries("test1", [][2]float64{{2, 0}}),
- // tsdb.NewTimeSeries("test1", [][2]float64{{2, 0}}),
- // }
- //
- // result := executor.evaluateRule(rule, timeSeries)
- // So(result.State, ShouldEqual, alertstates.Ok)
- // })
- //
- // Convey("first serie is good, second is critical", func() {
- // rule := &AlertRule{
- // Critical: Level{Value: 10, Operator: ">"},
- // Transformer: transformers.NewAggregationTransformer("avg"),
- // }
- //
- // timeSeries := []*tsdb.TimeSeries{
- // tsdb.NewTimeSeries("test1", [][2]float64{{2, 0}}),
- // tsdb.NewTimeSeries("test1", [][2]float64{{11, 0}}),
- // }
- //
- // result := executor.evaluateRule(rule, timeSeries)
- // So(result.State, ShouldEqual, alertstates.Critical)
- // })
- //
- // Convey("first serie is warn, second is critical", func() {
- // rule := &AlertRule{
- // Critical: Level{Value: 10, Operator: ">"},
- // Warning: Level{Value: 5, Operator: ">"},
- // Transformer: transformers.NewAggregationTransformer("avg"),
- // }
- //
- // timeSeries := []*tsdb.TimeSeries{
- // tsdb.NewTimeSeries("test1", [][2]float64{{6, 0}}),
- // tsdb.NewTimeSeries("test1", [][2]float64{{11, 0}}),
- // }
- //
- // result := executor.evaluateRule(rule, timeSeries)
- // So(result.State, ShouldEqual, alertstates.Critical)
- // })
- // })
- })
- }
|