2016-07-27 09:18:10 -05:00
|
|
|
package conditions
|
2016-07-20 02:30:31 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2016-07-20 07:28:02 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2016-07-20 02:30:31 -05:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2016-07-20 07:28:02 -05:00
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
2016-07-27 09:18:10 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/alerting"
|
2016-07-20 07:28:02 -05:00
|
|
|
"github.com/grafana/grafana/pkg/tsdb"
|
2016-07-20 02:30:31 -05:00
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestQueryCondition(t *testing.T) {
|
|
|
|
|
|
|
|
Convey("when evaluating query condition", t, func() {
|
|
|
|
|
2016-07-21 09:19:28 -05:00
|
|
|
queryConditionScenario("Given avg() and > 100", func(ctx *queryConditionTestContext) {
|
|
|
|
|
|
|
|
ctx.reducer = `{"type": "avg"}`
|
2016-08-12 06:19:06 -05:00
|
|
|
ctx.evaluator = `{"type": "gt "params": [100]}`
|
2016-07-21 09:19:28 -05:00
|
|
|
|
2016-07-27 09:18:10 -05:00
|
|
|
Convey("Can read query condition from json model", func() {
|
|
|
|
ctx.exec()
|
|
|
|
|
|
|
|
So(ctx.condition.Query.From, ShouldEqual, "5m")
|
|
|
|
So(ctx.condition.Query.To, ShouldEqual, "now")
|
|
|
|
So(ctx.condition.Query.DatasourceId, ShouldEqual, 1)
|
|
|
|
|
|
|
|
Convey("Can read query reducer", func() {
|
|
|
|
reducer, ok := ctx.condition.Reducer.(*SimpleReducer)
|
|
|
|
So(ok, ShouldBeTrue)
|
|
|
|
So(reducer.Type, ShouldEqual, "avg")
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Can read evaluator", func() {
|
|
|
|
evaluator, ok := ctx.condition.Evaluator.(*DefaultAlertEvaluator)
|
|
|
|
So(ok, ShouldBeTrue)
|
|
|
|
So(evaluator.Type, ShouldEqual, ">")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-07-22 06:14:09 -05:00
|
|
|
Convey("should fire when avg is above 100", func() {
|
2016-07-21 09:19:28 -05:00
|
|
|
ctx.series = tsdb.TimeSeriesSlice{tsdb.NewTimeSeries("test1", [][2]float64{{120, 0}})}
|
|
|
|
ctx.exec()
|
|
|
|
|
|
|
|
So(ctx.result.Error, ShouldBeNil)
|
2016-07-22 06:14:09 -05:00
|
|
|
So(ctx.result.Firing, ShouldBeTrue)
|
2016-07-21 09:19:28 -05:00
|
|
|
})
|
|
|
|
|
2016-07-22 06:14:09 -05:00
|
|
|
Convey("Should not fire when avg is below 100", func() {
|
2016-07-21 09:19:28 -05:00
|
|
|
ctx.series = tsdb.TimeSeriesSlice{tsdb.NewTimeSeries("test1", [][2]float64{{90, 0}})}
|
|
|
|
ctx.exec()
|
|
|
|
|
|
|
|
So(ctx.result.Error, ShouldBeNil)
|
2016-07-22 06:14:09 -05:00
|
|
|
So(ctx.result.Firing, ShouldBeFalse)
|
2016-07-21 09:19:28 -05:00
|
|
|
})
|
2016-07-20 07:28:02 -05:00
|
|
|
})
|
2016-07-21 09:19:28 -05:00
|
|
|
})
|
|
|
|
}
|
2016-07-20 07:28:02 -05:00
|
|
|
|
2016-07-21 09:19:28 -05:00
|
|
|
type queryConditionTestContext struct {
|
|
|
|
reducer string
|
|
|
|
evaluator string
|
|
|
|
series tsdb.TimeSeriesSlice
|
2016-07-27 09:29:28 -05:00
|
|
|
result *alerting.EvalContext
|
2016-07-27 09:18:10 -05:00
|
|
|
condition *QueryCondition
|
2016-07-21 09:19:28 -05:00
|
|
|
}
|
2016-07-20 02:30:31 -05:00
|
|
|
|
2016-07-21 09:19:28 -05:00
|
|
|
type queryConditionScenarioFunc func(c *queryConditionTestContext)
|
|
|
|
|
|
|
|
func (ctx *queryConditionTestContext) exec() {
|
|
|
|
jsonModel, err := simplejson.NewJson([]byte(`{
|
2016-07-20 02:30:31 -05:00
|
|
|
"type": "query",
|
|
|
|
"query": {
|
|
|
|
"params": ["A", "5m", "now"],
|
|
|
|
"datasourceId": 1,
|
|
|
|
"model": {"target": "aliasByNode(statsd.fakesite.counters.session_start.mobile.count, 4)"}
|
|
|
|
},
|
2016-07-21 09:19:28 -05:00
|
|
|
"reducer":` + ctx.reducer + `,
|
|
|
|
"evaluator":` + ctx.evaluator + `
|
2016-07-20 02:30:31 -05:00
|
|
|
}`))
|
2016-07-21 09:19:28 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2016-07-21 14:54:12 -05:00
|
|
|
condition, err := NewQueryCondition(jsonModel, 0)
|
2016-07-21 09:19:28 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2016-07-27 09:18:10 -05:00
|
|
|
ctx.condition = condition
|
|
|
|
|
2016-07-21 09:19:28 -05:00
|
|
|
condition.HandleRequest = func(req *tsdb.Request) (*tsdb.Response, error) {
|
|
|
|
return &tsdb.Response{
|
|
|
|
Results: map[string]*tsdb.QueryResult{
|
|
|
|
"A": {Series: ctx.series},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
condition.Eval(ctx.result)
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryConditionScenario(desc string, fn queryConditionScenarioFunc) {
|
|
|
|
Convey(desc, func() {
|
|
|
|
|
|
|
|
bus.AddHandler("test", func(query *m.GetDataSourceByIdQuery) error {
|
|
|
|
query.Result = &m.DataSource{Id: 1, Type: "graphite"}
|
|
|
|
return nil
|
2016-07-20 02:30:31 -05:00
|
|
|
})
|
|
|
|
|
2016-07-21 09:19:28 -05:00
|
|
|
ctx := &queryConditionTestContext{}
|
2016-07-27 09:29:28 -05:00
|
|
|
ctx.result = &alerting.EvalContext{
|
|
|
|
Rule: &alerting.Rule{},
|
2016-07-21 09:19:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn(ctx)
|
2016-07-20 02:30:31 -05:00
|
|
|
})
|
|
|
|
}
|