2019-09-27 22:18:24 +08:00
|
|
|
package testdatasource
|
2019-03-16 02:07:20 +08:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
|
|
|
"github.com/grafana/grafana/pkg/tsdb"
|
2020-12-04 04:31:01 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-03-16 02:07:20 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestTestdataScenarios(t *testing.T) {
|
2020-12-04 04:31:01 +08:00
|
|
|
t.Run("random walk ", func(t *testing.T) {
|
2019-09-27 22:18:24 +08:00
|
|
|
scenario := ScenarioRegistry["random_walk"]
|
2019-03-16 03:33:45 +08:00
|
|
|
|
2020-12-04 04:31:01 +08:00
|
|
|
t.Run("Should start at the requested value", func(t *testing.T) {
|
2019-03-16 03:33:45 +08:00
|
|
|
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)
|
2020-12-04 04:31:01 +08:00
|
|
|
require.NotNil(t, result.Series)
|
2019-03-16 03:33:45 +08:00
|
|
|
|
2020-12-04 04:31:01 +08:00
|
|
|
points := result.Series[0].Points
|
|
|
|
|
require.Equal(t, 1.234, points[0][0].Float64)
|
2019-03-16 03:33:45 +08:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2020-12-04 04:31:01 +08:00
|
|
|
t.Run("random walk table", func(t *testing.T) {
|
2019-09-27 22:18:24 +08:00
|
|
|
scenario := ScenarioRegistry["random_walk_table"]
|
2019-03-16 03:33:45 +08:00
|
|
|
|
2020-12-04 04:31:01 +08:00
|
|
|
t.Run("Should return a table that looks like value/min/max", func(t *testing.T) {
|
2019-03-16 03:33:45 +08:00
|
|
|
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]
|
2019-03-16 02:07:20 +08:00
|
|
|
|
2020-12-04 04:31:01 +08:00
|
|
|
require.Greater(t, len(table.Rows), 50)
|
2019-03-16 03:33:45 +08:00
|
|
|
for _, row := range table.Rows {
|
|
|
|
|
value := row[1]
|
|
|
|
|
min := row[2]
|
|
|
|
|
max := row[3]
|
2019-03-16 02:07:20 +08:00
|
|
|
|
2020-12-04 04:31:01 +08:00
|
|
|
require.Less(t, min, value)
|
|
|
|
|
require.Greater(t, max, value)
|
2019-03-16 03:33:45 +08:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2020-12-04 04:31:01 +08:00
|
|
|
t.Run("Should return a table with some nil values", func(t *testing.T) {
|
2019-03-16 03:33:45 +08:00
|
|
|
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
|
|
|
|
|
|
2020-12-04 04:31:01 +08:00
|
|
|
require.Greater(t, len(table.Rows), 50)
|
2019-03-16 03:33:45 +08:00
|
|
|
for _, row := range table.Rows {
|
|
|
|
|
if row[1] == nil {
|
|
|
|
|
nil1 = true
|
|
|
|
|
}
|
|
|
|
|
if row[2] == nil {
|
|
|
|
|
nil2 = true
|
|
|
|
|
}
|
|
|
|
|
if row[3] == nil {
|
|
|
|
|
nil3 = true
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-16 02:07:20 +08:00
|
|
|
|
2020-12-04 04:31:01 +08:00
|
|
|
require.True(t, nil1)
|
|
|
|
|
require.True(t, nil2)
|
|
|
|
|
require.True(t, nil3)
|
2019-03-16 03:33:45 +08:00
|
|
|
})
|
2019-03-16 02:07:20 +08:00
|
|
|
})
|
|
|
|
|
}
|
2019-08-21 22:50:13 +08:00
|
|
|
|
2020-12-04 04:31:01 +08:00
|
|
|
func TestParseLabels(t *testing.T) {
|
|
|
|
|
expectedTags := map[string]string{
|
|
|
|
|
"job": "foo",
|
|
|
|
|
"instance": "bar",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
query1 := tsdb.Query{
|
|
|
|
|
Model: simplejson.NewFromAny(map[string]interface{}{
|
|
|
|
|
"labels": `{job="foo", instance="bar"}`,
|
|
|
|
|
}),
|
|
|
|
|
}
|
|
|
|
|
require.Equal(t, expectedTags, parseLabels(&query1))
|
|
|
|
|
|
|
|
|
|
query2 := tsdb.Query{
|
|
|
|
|
Model: simplejson.NewFromAny(map[string]interface{}{
|
|
|
|
|
"labels": `job=foo, instance=bar`,
|
|
|
|
|
}),
|
|
|
|
|
}
|
|
|
|
|
require.Equal(t, expectedTags, parseLabels(&query2))
|
|
|
|
|
|
|
|
|
|
query3 := tsdb.Query{
|
|
|
|
|
Model: simplejson.NewFromAny(map[string]interface{}{
|
|
|
|
|
"labels": `job = foo,instance = bar`,
|
|
|
|
|
}),
|
|
|
|
|
}
|
|
|
|
|
require.Equal(t, expectedTags, parseLabels(&query3))
|
2019-08-21 22:50:13 +08:00
|
|
|
}
|