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/tsdb/testdatasource/scenarios_test.go

108 lines
2.7 KiB

package testdatasource
7 years ago
import (
"testing"
"time"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/tsdb"
. "github.com/smartystreets/goconvey/convey"
)
func TestTestdataScenarios(t *testing.T) {
Convey("random walk ", t, func() {
scenario := ScenarioRegistry["random_walk"]
Convey("Should start at the requested value", func() {
req := &tsdb.TsdbQuery{
TimeRange: tsdb.NewFakeTimeRange("5m", "now", time.Now()),
Queries: []*tsdb.Query{
{RefId: "A", IntervalMs: 100, MaxDataPoints: 100, Model: simplejson.New()},
},
}
query := req.Queries[0]
query.Model.Set("startValue", 1.234)
result := scenario.Handler(req.Queries[0], req)
points := result.Series[0].Points
So(result.Series, ShouldNotBeNil)
So(points[0][0].Float64, ShouldEqual, 1.234)
})
})
Convey("random walk table", t, func() {
scenario := ScenarioRegistry["random_walk_table"]
Convey("Should return a table that looks like value/min/max", func() {
req := &tsdb.TsdbQuery{
TimeRange: tsdb.NewFakeTimeRange("5m", "now", time.Now()),
Queries: []*tsdb.Query{
{RefId: "A", IntervalMs: 100, MaxDataPoints: 100, Model: simplejson.New()},
},
}
result := scenario.Handler(req.Queries[0], req)
table := result.Tables[0]
7 years ago
So(len(table.Rows), ShouldBeGreaterThan, 50)
for _, row := range table.Rows {
value := row[1]
min := row[2]
max := row[3]
7 years ago
So(min, ShouldBeLessThan, value)
So(max, ShouldBeGreaterThan, value)
}
})
Convey("Should return a table with some nil values", func() {
req := &tsdb.TsdbQuery{
TimeRange: tsdb.NewFakeTimeRange("5m", "now", time.Now()),
Queries: []*tsdb.Query{
{RefId: "A", IntervalMs: 100, MaxDataPoints: 100, Model: simplejson.New()},
},
}
query := req.Queries[0]
query.Model.Set("withNil", true)
result := scenario.Handler(req.Queries[0], req)
table := result.Tables[0]
nil1 := false
nil2 := false
nil3 := false
So(len(table.Rows), ShouldBeGreaterThan, 50)
for _, row := range table.Rows {
if row[1] == nil {
nil1 = true
}
if row[2] == nil {
nil2 = true
}
if row[3] == nil {
nil3 = true
}
}
7 years ago
So(nil1, ShouldBeTrue)
So(nil2, ShouldBeTrue)
So(nil3, ShouldBeTrue)
})
7 years ago
})
}
func TestToLabels(t *testing.T) {
Convey("read labels", t, func() {
tags := make(map[string]string)
tags["job"] = "foo"
tags["instance"] = "bar"
So(parseLabels(`{job="foo", instance="bar"}`), ShouldResemble, tags)
So(parseLabels(`job="foo", instance="bar"`), ShouldResemble, tags)
So(parseLabels(`job=foo, instance=bar`), ShouldResemble, tags)
So(parseLabels(`job = foo,instance = bar`), ShouldResemble, tags)
})
}