2016-07-20 02:30:31 -05:00
|
|
|
package alerting
|
|
|
|
|
|
|
|
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"
|
|
|
|
"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"}`
|
|
|
|
ctx.evaluator = `{"type": ">", "params": [100]}`
|
|
|
|
|
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
|
|
|
|
result *AlertResultContext
|
|
|
|
}
|
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)
|
|
|
|
|
|
|
|
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{}
|
|
|
|
ctx.result = &AlertResultContext{
|
|
|
|
Rule: &AlertRule{},
|
|
|
|
}
|
|
|
|
|
|
|
|
fn(ctx)
|
2016-07-20 02:30:31 -05:00
|
|
|
})
|
|
|
|
}
|