The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/pkg/services/alerting/conditions_test.go

64 lines
1.6 KiB

package alerting
import (
"testing"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/simplejson"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/tsdb"
. "github.com/smartystreets/goconvey/convey"
)
func TestQueryCondition(t *testing.T) {
Convey("when evaluating query condition", t, func() {
bus.AddHandler("test", func(query *m.GetDataSourceByIdQuery) error {
query.Result = &m.DataSource{Id: 1, Type: "graphite"}
return nil
})
Convey("Given avg() and > 100", func() {
jsonModel, err := simplejson.NewJson([]byte(`{
"type": "query",
"query": {
"params": ["A", "5m", "now"],
"datasourceId": 1,
"model": {"target": "aliasByNode(statsd.fakesite.counters.session_start.mobile.count, 4)"}
},
"reducer": {"type": "avg", "params": []},
"evaluator": {"type": ">", "params": [100]}
}`))
So(err, ShouldBeNil)
condition, err := NewQueryCondition(jsonModel)
So(err, ShouldBeNil)
Convey("Should set result to triggered when avg is above 100", func() {
context := &AlertResultContext{
Rule: &AlertRule{},
}
condition.HandleRequest = func(req *tsdb.Request) (*tsdb.Response, error) {
return &tsdb.Response{
Results: map[string]*tsdb.QueryResult{
"A": &tsdb.QueryResult{
Series: tsdb.TimeSeriesSlice{
tsdb.NewTimeSeries("test1", [][2]float64{{120, 0}}),
},
},
},
}, nil
}
condition.Eval(context)
So(context.Error, ShouldBeNil)
So(context.Triggered, ShouldBeTrue)
})
})
})
}