2019-09-27 09:18:24 -05:00
|
|
|
package testdatasource
|
2019-03-15 13:07:20 -05:00
|
|
|
|
|
|
|
import (
|
2021-01-29 11:33:23 -06:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2023-05-15 16:47:40 -05:00
|
|
|
"math/rand"
|
2019-03-15 13:07:20 -05:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-01-29 11:33:23 -06:00
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-12-03 14:31:01 -06:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-03-15 13:07:20 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTestdataScenarios(t *testing.T) {
|
2021-11-01 04:53:33 -05:00
|
|
|
s := &Service{}
|
2019-03-15 14:33:45 -05:00
|
|
|
|
2021-01-29 11:33:23 -06:00
|
|
|
t.Run("random walk ", func(t *testing.T) {
|
2020-12-03 14:31:01 -06:00
|
|
|
t.Run("Should start at the requested value", func(t *testing.T) {
|
2023-09-21 03:39:28 -05:00
|
|
|
from := time.Now()
|
|
|
|
to := from.Add(5 * time.Minute)
|
2021-01-29 11:33:23 -06:00
|
|
|
|
|
|
|
query := backend.DataQuery{
|
|
|
|
RefID: "A",
|
|
|
|
TimeRange: backend.TimeRange{
|
2023-09-21 03:39:28 -05:00
|
|
|
From: from,
|
|
|
|
To: to,
|
2019-03-15 14:33:45 -05:00
|
|
|
},
|
2021-01-29 11:33:23 -06:00
|
|
|
Interval: 100 * time.Millisecond,
|
|
|
|
MaxDataPoints: 100,
|
2023-07-11 08:13:09 -05:00
|
|
|
JSON: []byte(`{"startValue": 1.234}`),
|
2019-03-15 14:33:45 -05:00
|
|
|
}
|
|
|
|
|
2021-01-29 11:33:23 -06:00
|
|
|
req := &backend.QueryDataRequest{
|
|
|
|
PluginContext: backend.PluginContext{},
|
|
|
|
Queries: []backend.DataQuery{query},
|
|
|
|
}
|
2019-03-15 14:33:45 -05:00
|
|
|
|
2021-11-01 04:53:33 -05:00
|
|
|
resp, err := s.handleRandomWalkScenario(context.Background(), req)
|
2021-01-29 11:33:23 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, resp)
|
|
|
|
|
|
|
|
dResp, exists := resp.Responses[query.RefID]
|
|
|
|
require.True(t, exists)
|
|
|
|
require.NoError(t, dResp.Error)
|
|
|
|
|
|
|
|
require.Len(t, dResp.Frames, 1)
|
|
|
|
frame := dResp.Frames[0]
|
|
|
|
require.Len(t, frame.Fields, 2)
|
|
|
|
require.Equal(t, "time", frame.Fields[0].Name)
|
|
|
|
require.Equal(t, "A-series", frame.Fields[1].Name)
|
|
|
|
val, ok := frame.Fields[1].ConcreteAt(0)
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, 1.234, val)
|
2019-03-15 14:33:45 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-12-03 14:31:01 -06:00
|
|
|
t.Run("random walk table", func(t *testing.T) {
|
|
|
|
t.Run("Should return a table that looks like value/min/max", func(t *testing.T) {
|
2023-09-21 03:39:28 -05:00
|
|
|
from := time.Now()
|
|
|
|
to := from.Add(5 * time.Minute)
|
2021-01-29 11:33:23 -06:00
|
|
|
|
|
|
|
query := backend.DataQuery{
|
|
|
|
RefID: "A",
|
|
|
|
TimeRange: backend.TimeRange{
|
2023-09-21 03:39:28 -05:00
|
|
|
From: from,
|
|
|
|
To: to,
|
2019-03-15 14:33:45 -05:00
|
|
|
},
|
2021-01-29 11:33:23 -06:00
|
|
|
Interval: 100 * time.Millisecond,
|
|
|
|
MaxDataPoints: 100,
|
2023-07-11 08:13:09 -05:00
|
|
|
JSON: []byte(`{}`),
|
2019-03-15 14:33:45 -05:00
|
|
|
}
|
|
|
|
|
2021-01-29 11:33:23 -06:00
|
|
|
req := &backend.QueryDataRequest{
|
|
|
|
PluginContext: backend.PluginContext{},
|
|
|
|
Queries: []backend.DataQuery{query},
|
|
|
|
}
|
2019-03-15 13:07:20 -05:00
|
|
|
|
2021-11-01 04:53:33 -05:00
|
|
|
resp, err := s.handleRandomWalkTableScenario(context.Background(), req)
|
2021-01-29 11:33:23 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, resp)
|
|
|
|
|
|
|
|
dResp, exists := resp.Responses[query.RefID]
|
|
|
|
require.True(t, exists)
|
|
|
|
require.NoError(t, dResp.Error)
|
|
|
|
|
|
|
|
require.Len(t, dResp.Frames, 1)
|
|
|
|
frame := dResp.Frames[0]
|
|
|
|
require.Greater(t, frame.Rows(), 50)
|
2023-03-03 15:37:56 -06:00
|
|
|
require.Len(t, frame.Fields, 6)
|
2021-01-29 11:33:23 -06:00
|
|
|
require.Equal(t, "Time", frame.Fields[0].Name)
|
|
|
|
require.Equal(t, "Value", frame.Fields[1].Name)
|
|
|
|
require.Equal(t, "Min", frame.Fields[2].Name)
|
|
|
|
require.Equal(t, "Max", frame.Fields[3].Name)
|
|
|
|
require.Equal(t, "Info", frame.Fields[4].Name)
|
|
|
|
|
|
|
|
for i := 0; i < frame.Rows(); i++ {
|
|
|
|
value, ok := frame.ConcreteAt(1, i)
|
|
|
|
require.True(t, ok)
|
|
|
|
min, ok := frame.ConcreteAt(2, i)
|
|
|
|
require.True(t, ok)
|
|
|
|
max, ok := frame.ConcreteAt(3, i)
|
|
|
|
require.True(t, ok)
|
2019-03-15 13:07:20 -05:00
|
|
|
|
2020-12-03 14:31:01 -06:00
|
|
|
require.Less(t, min, value)
|
|
|
|
require.Greater(t, max, value)
|
2019-03-15 14:33:45 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-12-03 14:31:01 -06:00
|
|
|
t.Run("Should return a table with some nil values", func(t *testing.T) {
|
2023-09-21 03:39:28 -05:00
|
|
|
from := time.Now()
|
|
|
|
to := from.Add(5 * time.Minute)
|
2021-01-29 11:33:23 -06:00
|
|
|
|
|
|
|
query := backend.DataQuery{
|
|
|
|
RefID: "A",
|
|
|
|
TimeRange: backend.TimeRange{
|
2023-09-21 03:39:28 -05:00
|
|
|
From: from,
|
|
|
|
To: to,
|
2019-03-15 14:33:45 -05:00
|
|
|
},
|
2021-01-29 11:33:23 -06:00
|
|
|
Interval: 100 * time.Millisecond,
|
|
|
|
MaxDataPoints: 100,
|
2023-07-11 08:13:09 -05:00
|
|
|
JSON: []byte(`{"withNil": true}`),
|
2019-03-15 14:33:45 -05:00
|
|
|
}
|
|
|
|
|
2021-01-29 11:33:23 -06:00
|
|
|
req := &backend.QueryDataRequest{
|
|
|
|
PluginContext: backend.PluginContext{},
|
|
|
|
Queries: []backend.DataQuery{query},
|
|
|
|
}
|
2019-03-15 14:33:45 -05:00
|
|
|
|
2021-11-01 04:53:33 -05:00
|
|
|
resp, err := s.handleRandomWalkTableScenario(context.Background(), req)
|
2021-01-29 11:33:23 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, resp)
|
|
|
|
|
|
|
|
dResp, exists := resp.Responses[query.RefID]
|
|
|
|
require.True(t, exists)
|
|
|
|
require.NoError(t, dResp.Error)
|
|
|
|
|
|
|
|
require.Len(t, dResp.Frames, 1)
|
|
|
|
frame := dResp.Frames[0]
|
|
|
|
require.Greater(t, frame.Rows(), 50)
|
2023-03-03 15:37:56 -06:00
|
|
|
require.Len(t, frame.Fields, 6)
|
2021-01-29 11:33:23 -06:00
|
|
|
require.Equal(t, "Time", frame.Fields[0].Name)
|
|
|
|
require.Equal(t, "Value", frame.Fields[1].Name)
|
|
|
|
require.Equal(t, "Min", frame.Fields[2].Name)
|
|
|
|
require.Equal(t, "Max", frame.Fields[3].Name)
|
|
|
|
require.Equal(t, "Info", frame.Fields[4].Name)
|
2023-03-03 15:37:56 -06:00
|
|
|
require.Equal(t, "State", frame.Fields[5].Name)
|
2021-01-29 11:33:23 -06:00
|
|
|
|
|
|
|
valNil := false
|
|
|
|
minNil := false
|
|
|
|
maxNil := false
|
|
|
|
|
|
|
|
for i := 0; i < frame.Rows(); i++ {
|
|
|
|
_, ok := frame.ConcreteAt(1, i)
|
|
|
|
if !ok {
|
|
|
|
valNil = true
|
2019-03-15 14:33:45 -05:00
|
|
|
}
|
2021-01-29 11:33:23 -06:00
|
|
|
|
|
|
|
_, ok = frame.ConcreteAt(2, i)
|
|
|
|
if !ok {
|
|
|
|
minNil = true
|
2019-03-15 14:33:45 -05:00
|
|
|
}
|
2021-01-29 11:33:23 -06:00
|
|
|
|
|
|
|
_, ok = frame.ConcreteAt(3, i)
|
|
|
|
if !ok {
|
|
|
|
maxNil = true
|
2019-03-15 14:33:45 -05:00
|
|
|
}
|
|
|
|
}
|
2019-03-15 13:07:20 -05:00
|
|
|
|
2021-01-29 11:33:23 -06:00
|
|
|
require.True(t, valNil)
|
|
|
|
require.True(t, minNil)
|
|
|
|
require.True(t, maxNil)
|
2019-03-15 14:33:45 -05:00
|
|
|
})
|
2019-03-15 13:07:20 -05:00
|
|
|
})
|
|
|
|
}
|
2019-08-21 09:50:13 -05:00
|
|
|
|
2020-12-03 14:31:01 -06:00
|
|
|
func TestParseLabels(t *testing.T) {
|
2021-01-29 11:33:23 -06:00
|
|
|
expectedTags := data.Labels{
|
2020-12-03 14:31:01 -06:00
|
|
|
"job": "foo",
|
|
|
|
"instance": "bar",
|
|
|
|
}
|
2023-05-15 16:47:40 -05:00
|
|
|
seriesIndex := rand.Int()
|
2020-12-03 14:31:01 -06:00
|
|
|
|
2023-05-15 16:47:40 -05:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
2023-07-11 08:13:09 -05:00
|
|
|
model JSONModel
|
2023-05-15 16:47:40 -05:00
|
|
|
expected data.Labels
|
2021-01-29 11:33:23 -06:00
|
|
|
}{
|
2023-05-15 16:47:40 -05:00
|
|
|
{
|
|
|
|
name: "wrapped in {} and quoted value ",
|
2023-07-11 08:13:09 -05:00
|
|
|
model: JSONModel{Labels: `{job="foo", instance="bar"}`},
|
2023-05-15 16:47:40 -05:00
|
|
|
expected: expectedTags,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "comma-separated non-quoted",
|
2023-07-11 08:13:09 -05:00
|
|
|
model: JSONModel{Labels: `job=foo, instance=bar`},
|
2023-05-15 16:47:40 -05:00
|
|
|
expected: expectedTags,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "comma-separated quoted",
|
2023-07-11 08:13:09 -05:00
|
|
|
model: JSONModel{Labels: `job="foo"", instance="bar"`},
|
2023-05-15 16:47:40 -05:00
|
|
|
expected: expectedTags,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "comma-separated with spaces, non quoted",
|
2023-07-11 08:13:09 -05:00
|
|
|
model: JSONModel{Labels: `job = foo,instance = bar`},
|
2023-05-15 16:47:40 -05:00
|
|
|
expected: expectedTags,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "expands $seriesIndex",
|
2023-07-11 08:13:09 -05:00
|
|
|
model: JSONModel{Labels: `job=series-$seriesIndex,instance=bar`},
|
2023-05-15 16:47:40 -05:00
|
|
|
expected: data.Labels{
|
|
|
|
"job": fmt.Sprintf("series-%d", seriesIndex),
|
|
|
|
"instance": "bar",
|
|
|
|
},
|
|
|
|
},
|
2020-12-03 14:31:01 -06:00
|
|
|
}
|
|
|
|
|
2023-05-15 16:47:40 -05:00
|
|
|
for i, tc := range tests {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2023-07-11 08:13:09 -05:00
|
|
|
assert.Equal(t, tc.expected, parseLabels(tc.model, seriesIndex), fmt.Sprintf("Actual tags in test case %d doesn't match expected tags", i+1))
|
2023-05-15 16:47:40 -05:00
|
|
|
})
|
2020-12-03 14:31:01 -06:00
|
|
|
}
|
2019-08-21 09:50:13 -05:00
|
|
|
}
|