2020-11-19 06:17:00 -06:00
|
|
|
package expr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2021-11-10 04:52:16 -06:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2020-11-19 06:17:00 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2021-11-12 05:16:39 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/secrets/fakes"
|
2021-11-24 07:01:44 -06:00
|
|
|
secretsManager "github.com/grafana/grafana/pkg/services/secrets/manager"
|
2021-11-10 04:52:16 -06:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2020-11-19 06:17:00 -06:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestService(t *testing.T) {
|
|
|
|
dsDF := data.NewFrame("test",
|
2021-06-02 11:29:19 -05:00
|
|
|
data.NewField("time", nil, []time.Time{time.Unix(1, 0)}),
|
2020-11-19 06:17:00 -06:00
|
|
|
data.NewField("value", nil, []*float64{fp(2)}))
|
|
|
|
|
2021-03-08 00:02:49 -06:00
|
|
|
me := &mockEndpoint{
|
|
|
|
Frames: []*data.Frame{dsDF},
|
|
|
|
}
|
2021-11-12 05:16:39 -06:00
|
|
|
|
|
|
|
cfg := setting.NewCfg()
|
|
|
|
|
2021-11-24 07:01:44 -06:00
|
|
|
secretsService := secretsManager.SetupTestService(t, fakes.NewFakeSecretsStore())
|
2021-11-12 05:16:39 -06:00
|
|
|
|
2021-11-10 04:52:16 -06:00
|
|
|
s := Service{
|
2021-11-12 05:16:39 -06:00
|
|
|
cfg: cfg,
|
|
|
|
dataService: me,
|
|
|
|
secretsService: secretsService,
|
2021-11-10 04:52:16 -06:00
|
|
|
}
|
2021-11-12 05:16:39 -06:00
|
|
|
|
2021-12-28 09:08:07 -06:00
|
|
|
bus.AddHandler("test", func(_ context.Context, query *models.GetDataSourceQuery) error {
|
2021-12-16 10:51:46 -06:00
|
|
|
query.Result = &models.DataSource{Uid: "1", OrgId: 1, Type: "test", JsonData: simplejson.New()}
|
2021-03-08 00:02:49 -06:00
|
|
|
return nil
|
|
|
|
})
|
2020-11-19 06:17:00 -06:00
|
|
|
|
2021-04-23 09:52:32 -05:00
|
|
|
queries := []Query{
|
2020-11-19 06:17:00 -06:00
|
|
|
{
|
|
|
|
RefID: "A",
|
2021-12-16 10:51:46 -06:00
|
|
|
DataSource: &models.DataSource{
|
|
|
|
OrgId: 1,
|
|
|
|
Uid: "test",
|
|
|
|
Type: "test",
|
|
|
|
},
|
|
|
|
JSON: json.RawMessage(`{ "datasource": { "uid": "1" }, "intervalMs": 1000, "maxDataPoints": 1000 }`),
|
2020-11-19 06:17:00 -06:00
|
|
|
},
|
|
|
|
{
|
2021-12-16 10:51:46 -06:00
|
|
|
RefID: "B",
|
|
|
|
DataSource: DataSourceModel(),
|
|
|
|
JSON: json.RawMessage(`{ "datasource": { "uid": "__expr__", "type": "__expr__"}, "type": "math", "expression": "$A * 2" }`),
|
2020-11-19 06:17:00 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-04-23 09:52:32 -05:00
|
|
|
req := &Request{Queries: queries}
|
2020-12-07 09:30:38 -06:00
|
|
|
|
|
|
|
pl, err := s.BuildPipeline(req)
|
2020-11-19 06:17:00 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
res, err := s.ExecutePipeline(context.Background(), pl)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
bDF := data.NewFrame("",
|
2021-06-02 11:29:19 -05:00
|
|
|
data.NewField("Time", nil, []time.Time{time.Unix(1, 0)}),
|
2020-12-11 05:59:12 -06:00
|
|
|
data.NewField("B", nil, []*float64{fp(4)}))
|
2020-11-19 06:17:00 -06:00
|
|
|
bDF.RefID = "B"
|
|
|
|
|
|
|
|
expect := &backend.QueryDataResponse{
|
|
|
|
Responses: backend.Responses{
|
|
|
|
"A": {
|
|
|
|
Frames: []*data.Frame{dsDF},
|
|
|
|
},
|
|
|
|
"B": {
|
|
|
|
Frames: []*data.Frame{bDF},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Service currently doesn't care about order of datas in the return.
|
|
|
|
trans := cmp.Transformer("Sort", func(in []*data.Frame) []*data.Frame {
|
|
|
|
out := append([]*data.Frame(nil), in...) // Copy input to avoid mutating it
|
|
|
|
sort.SliceStable(out, func(i, j int) bool {
|
|
|
|
return out[i].RefID > out[j].RefID
|
|
|
|
})
|
|
|
|
return out
|
|
|
|
})
|
|
|
|
options := append([]cmp.Option{trans}, data.FrameTestCompareOptions()...)
|
|
|
|
if diff := cmp.Diff(expect, res, options...); diff != "" {
|
|
|
|
t.Errorf("Result mismatch (-want +got):\n%s", diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func fp(f float64) *float64 {
|
|
|
|
return &f
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockEndpoint struct {
|
|
|
|
Frames data.Frames
|
|
|
|
}
|
|
|
|
|
2021-11-10 04:52:16 -06:00
|
|
|
func (me *mockEndpoint) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
|
|
|
resp := backend.NewQueryDataResponse()
|
|
|
|
resp.Responses["A"] = backend.DataResponse{
|
|
|
|
Frames: me.Frames,
|
|
|
|
}
|
|
|
|
return resp, nil
|
2021-03-08 00:02:49 -06:00
|
|
|
}
|