2022-01-07 13:45:26 -06:00
|
|
|
package query_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-08-31 10:11:10 -05:00
|
|
|
"errors"
|
2022-01-07 13:45:26 -06:00
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2022-06-27 11:23:15 -05:00
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
2022-08-31 10:11:10 -05:00
|
|
|
"github.com/grafana/grafana/pkg/expr"
|
2022-06-27 11:23:15 -05:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-01-07 13:45:26 -06:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2022-08-25 16:04:44 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2022-01-07 13:45:26 -06:00
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
2022-04-25 11:57:45 -05:00
|
|
|
acmock "github.com/grafana/grafana/pkg/services/accesscontrol/mock"
|
2022-06-27 11:23:15 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/datasources"
|
2022-08-31 10:11:10 -05:00
|
|
|
fakeDatasources "github.com/grafana/grafana/pkg/services/datasources/fakes"
|
2022-06-27 11:23:15 -05:00
|
|
|
dsSvc "github.com/grafana/grafana/pkg/services/datasources/service"
|
2022-04-25 11:57:45 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
2022-01-07 13:45:26 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/query"
|
2022-04-25 11:57:45 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/secrets/fakes"
|
2022-08-25 16:04:44 -05:00
|
|
|
secretskvs "github.com/grafana/grafana/pkg/services/secrets/kvstore"
|
|
|
|
secretsmng "github.com/grafana/grafana/pkg/services/secrets/manager"
|
|
|
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
2022-08-10 04:56:48 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/user"
|
2022-01-07 13:45:26 -06:00
|
|
|
)
|
|
|
|
|
2022-08-31 10:11:10 -05:00
|
|
|
func TestQueryDataMultipleSources(t *testing.T) {
|
|
|
|
t.Run("can query multiple datasources", func(t *testing.T) {
|
|
|
|
tc := setup(t)
|
|
|
|
query1, err := simplejson.NewJson([]byte(`
|
|
|
|
{
|
|
|
|
"datasource": {
|
|
|
|
"type": "mysql",
|
|
|
|
"uid": "ds1"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`))
|
|
|
|
require.NoError(t, err)
|
|
|
|
query2, err := simplejson.NewJson([]byte(`
|
|
|
|
{
|
|
|
|
"datasource": {
|
|
|
|
"type": "mysql",
|
|
|
|
"uid": "ds2"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`))
|
|
|
|
require.NoError(t, err)
|
|
|
|
queries := []*simplejson.Json{query1, query2}
|
|
|
|
reqDTO := dtos.MetricRequest{
|
|
|
|
From: "2022-01-01",
|
|
|
|
To: "2022-01-02",
|
|
|
|
Queries: queries,
|
|
|
|
Debug: false,
|
|
|
|
PublicDashboardAccessToken: "abc123",
|
|
|
|
HTTPRequest: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = tc.queryService.QueryDataMultipleSources(context.Background(), nil, true, reqDTO, false)
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("can query multiple datasources with an expression present", func(t *testing.T) {
|
|
|
|
tc := setup(t)
|
|
|
|
query1, err := simplejson.NewJson([]byte(`
|
|
|
|
{
|
|
|
|
"datasource": {
|
|
|
|
"type": "mysql",
|
|
|
|
"uid": "ds1"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`))
|
|
|
|
require.NoError(t, err)
|
|
|
|
query2, err := simplejson.NewJson([]byte(`
|
|
|
|
{
|
|
|
|
"datasource": {
|
|
|
|
"type": "mysql",
|
|
|
|
"uid": "ds2"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`))
|
|
|
|
require.NoError(t, err)
|
|
|
|
query3, err := simplejson.NewJson([]byte(`
|
|
|
|
{
|
|
|
|
"datasource": {
|
|
|
|
"name": "Expression",
|
|
|
|
"type": "__expr__",
|
|
|
|
"uid": "__expr__"
|
|
|
|
},
|
|
|
|
"expression": "$A + 1",
|
|
|
|
"hide": false,
|
|
|
|
"refId": "EXPRESSION",
|
|
|
|
"type": "math"
|
|
|
|
}
|
|
|
|
`))
|
|
|
|
require.NoError(t, err)
|
|
|
|
queries := []*simplejson.Json{query1, query2, query3}
|
|
|
|
reqDTO := dtos.MetricRequest{
|
|
|
|
From: "2022-01-01",
|
|
|
|
To: "2022-01-02",
|
|
|
|
Queries: queries,
|
|
|
|
Debug: false,
|
|
|
|
PublicDashboardAccessToken: "abc123",
|
|
|
|
HTTPRequest: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = tc.queryService.QueryDataMultipleSources(context.Background(), nil, true, reqDTO, false)
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
2022-10-05 16:45:17 -05:00
|
|
|
|
|
|
|
t.Run("error is returned when one of the queries fails", func(t *testing.T) {
|
|
|
|
tc := setup(t)
|
|
|
|
|
|
|
|
query1, _ := simplejson.NewJson([]byte(`
|
|
|
|
{
|
|
|
|
"datasource": {
|
|
|
|
"type": "mysql",
|
|
|
|
"uid": "ds1"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`))
|
|
|
|
query2, _ := simplejson.NewJson([]byte(`
|
|
|
|
{
|
|
|
|
"datasource": {
|
|
|
|
"type": "prometheus",
|
|
|
|
"uid": "ds2"
|
|
|
|
},
|
|
|
|
"queryType": "FAIL"
|
|
|
|
}
|
|
|
|
`))
|
|
|
|
|
|
|
|
queries := []*simplejson.Json{query1, query2}
|
|
|
|
|
|
|
|
reqDTO := dtos.MetricRequest{
|
|
|
|
From: "2022-01-01",
|
|
|
|
To: "2022-01-02",
|
|
|
|
Queries: queries,
|
|
|
|
Debug: false,
|
|
|
|
PublicDashboardAccessToken: "abc123",
|
|
|
|
HTTPRequest: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := tc.queryService.QueryDataMultipleSources(context.Background(), nil, true, reqDTO, false)
|
|
|
|
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
2022-08-31 10:11:10 -05:00
|
|
|
}
|
|
|
|
|
2022-01-07 13:45:26 -06:00
|
|
|
func TestQueryData(t *testing.T) {
|
|
|
|
t.Run("it auth custom headers to the request", func(t *testing.T) {
|
|
|
|
token := &oauth2.Token{
|
|
|
|
TokenType: "bearer",
|
|
|
|
AccessToken: "access-token",
|
|
|
|
}
|
|
|
|
token = token.WithExtra(map[string]interface{}{"id_token": "id-token"})
|
|
|
|
|
2022-04-25 11:57:45 -05:00
|
|
|
tc := setup(t)
|
2022-01-07 13:45:26 -06:00
|
|
|
tc.oauthTokenService.passThruEnabled = true
|
|
|
|
tc.oauthTokenService.token = token
|
|
|
|
|
|
|
|
_, err := tc.queryService.QueryData(context.Background(), nil, true, metricRequest(), false)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
expected := map[string]string{
|
|
|
|
"Authorization": "Bearer access-token",
|
|
|
|
"X-ID-Token": "id-token",
|
|
|
|
}
|
|
|
|
require.Equal(t, expected, tc.pluginContext.req.Headers)
|
|
|
|
})
|
2022-05-31 10:02:58 -05:00
|
|
|
|
|
|
|
t.Run("it doesn't add cookie header to the request when keepCookies configured and no cookies provided", func(t *testing.T) {
|
|
|
|
tc := setup(t)
|
|
|
|
json, err := simplejson.NewJson([]byte(`{"keepCookies": [ "foo", "bar" ]}`))
|
|
|
|
require.NoError(t, err)
|
|
|
|
tc.dataSourceCache.ds.JsonData = json
|
|
|
|
|
|
|
|
metricReq := metricRequest()
|
|
|
|
httpReq, err := http.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
metricReq.HTTPRequest = httpReq
|
|
|
|
_, err = tc.queryService.QueryData(context.Background(), nil, true, metricReq, false)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Empty(t, tc.pluginContext.req.Headers)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("it adds cookie header to the request when keepCookies configured and cookie provided", func(t *testing.T) {
|
|
|
|
tc := setup(t)
|
|
|
|
json, err := simplejson.NewJson([]byte(`{"keepCookies": [ "foo", "bar" ]}`))
|
|
|
|
require.NoError(t, err)
|
|
|
|
tc.dataSourceCache.ds.JsonData = json
|
|
|
|
|
|
|
|
metricReq := metricRequest()
|
|
|
|
httpReq, err := http.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
httpReq.AddCookie(&http.Cookie{Name: "a"})
|
|
|
|
httpReq.AddCookie(&http.Cookie{Name: "bar", Value: "rab"})
|
|
|
|
httpReq.AddCookie(&http.Cookie{Name: "b"})
|
|
|
|
httpReq.AddCookie(&http.Cookie{Name: "foo", Value: "oof"})
|
|
|
|
httpReq.AddCookie(&http.Cookie{Name: "c"})
|
|
|
|
metricReq.HTTPRequest = httpReq
|
|
|
|
_, err = tc.queryService.QueryData(context.Background(), nil, true, metricReq, false)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, map[string]string{"Cookie": "bar=rab; foo=oof"}, tc.pluginContext.req.Headers)
|
|
|
|
})
|
2022-01-07 13:45:26 -06:00
|
|
|
}
|
|
|
|
|
2022-04-25 11:57:45 -05:00
|
|
|
func setup(t *testing.T) *testContext {
|
2022-01-07 13:45:26 -06:00
|
|
|
pc := &fakePluginClient{}
|
2022-06-27 11:23:15 -05:00
|
|
|
dc := &fakeDataSourceCache{ds: &datasources.DataSource{}}
|
2022-01-07 13:45:26 -06:00
|
|
|
tc := &fakeOAuthTokenService{}
|
|
|
|
rv := &fakePluginRequestValidator{}
|
|
|
|
|
2022-08-25 16:04:44 -05:00
|
|
|
sqlStore := sqlstore.InitTestDB(t)
|
|
|
|
secretsService := secretsmng.SetupTestService(t, fakes.NewFakeSecretsStore())
|
|
|
|
ss := secretskvs.NewSQLSecretsKVStore(sqlStore, secretsService, log.New("test.logger"))
|
|
|
|
ssvc := secretsmng.SetupTestService(t, fakes.NewFakeSecretsStore())
|
2022-06-27 11:23:15 -05:00
|
|
|
ds := dsSvc.ProvideService(nil, ssvc, ss, nil, featuremgmt.WithFeatures(), acmock.New(), acmock.NewMockedPermissionsService())
|
2022-08-31 10:11:10 -05:00
|
|
|
fakeDatasourceService := &fakeDatasources.FakeDataSourceService{
|
|
|
|
DataSources: nil,
|
|
|
|
SimulatePluginFailure: false,
|
|
|
|
}
|
|
|
|
exprService := expr.ProvideService(nil, pc, fakeDatasourceService)
|
2022-04-25 11:57:45 -05:00
|
|
|
|
2022-01-07 13:45:26 -06:00
|
|
|
return &testContext{
|
|
|
|
pluginContext: pc,
|
2022-04-25 11:57:45 -05:00
|
|
|
secretStore: ss,
|
2022-01-07 13:45:26 -06:00
|
|
|
dataSourceCache: dc,
|
|
|
|
oauthTokenService: tc,
|
|
|
|
pluginRequestValidator: rv,
|
2022-08-31 10:11:10 -05:00
|
|
|
queryService: query.ProvideService(nil, dc, exprService, rv, ds, pc, tc),
|
2022-01-07 13:45:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type testContext struct {
|
|
|
|
pluginContext *fakePluginClient
|
2022-08-25 16:04:44 -05:00
|
|
|
secretStore secretskvs.SecretsKVStore
|
2022-01-07 13:45:26 -06:00
|
|
|
dataSourceCache *fakeDataSourceCache
|
|
|
|
oauthTokenService *fakeOAuthTokenService
|
|
|
|
pluginRequestValidator *fakePluginRequestValidator
|
|
|
|
queryService *query.Service
|
|
|
|
}
|
|
|
|
|
|
|
|
func metricRequest() dtos.MetricRequest {
|
|
|
|
q, _ := simplejson.NewJson([]byte(`{"datasourceId":1}`))
|
|
|
|
return dtos.MetricRequest{
|
|
|
|
From: "",
|
|
|
|
To: "",
|
|
|
|
Queries: []*simplejson.Json{q},
|
|
|
|
Debug: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakePluginRequestValidator struct {
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rv *fakePluginRequestValidator) Validate(dsURL string, req *http.Request) error {
|
|
|
|
return rv.err
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeOAuthTokenService struct {
|
|
|
|
passThruEnabled bool
|
|
|
|
token *oauth2.Token
|
|
|
|
}
|
|
|
|
|
2022-08-10 04:56:48 -05:00
|
|
|
func (ts *fakeOAuthTokenService) GetCurrentOAuthToken(context.Context, *user.SignedInUser) *oauth2.Token {
|
2022-01-07 13:45:26 -06:00
|
|
|
return ts.token
|
|
|
|
}
|
|
|
|
|
2022-06-27 11:23:15 -05:00
|
|
|
func (ts *fakeOAuthTokenService) IsOAuthPassThruEnabled(*datasources.DataSource) bool {
|
2022-01-07 13:45:26 -06:00
|
|
|
return ts.passThruEnabled
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeDataSourceCache struct {
|
2022-06-27 11:23:15 -05:00
|
|
|
ds *datasources.DataSource
|
2022-01-07 13:45:26 -06:00
|
|
|
}
|
|
|
|
|
2022-08-10 04:56:48 -05:00
|
|
|
func (c *fakeDataSourceCache) GetDatasource(ctx context.Context, datasourceID int64, user *user.SignedInUser, skipCache bool) (*datasources.DataSource, error) {
|
2022-01-07 13:45:26 -06:00
|
|
|
return c.ds, nil
|
|
|
|
}
|
|
|
|
|
2022-08-10 04:56:48 -05:00
|
|
|
func (c *fakeDataSourceCache) GetDatasourceByUID(ctx context.Context, datasourceUID string, user *user.SignedInUser, skipCache bool) (*datasources.DataSource, error) {
|
2022-01-07 13:45:26 -06:00
|
|
|
return c.ds, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakePluginClient struct {
|
|
|
|
plugins.Client
|
|
|
|
req *backend.QueryDataRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *fakePluginClient) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
|
|
|
c.req = req
|
2022-08-31 10:11:10 -05:00
|
|
|
|
|
|
|
// If an expression query ends up getting directly queried, we want it to return an error in our test.
|
|
|
|
if req.PluginContext.PluginID == "__expr__" {
|
|
|
|
return nil, errors.New("cant query an expression datasource")
|
|
|
|
}
|
|
|
|
|
2022-10-05 16:45:17 -05:00
|
|
|
if req.Queries[0].QueryType == "FAIL" {
|
|
|
|
return nil, errors.New("plugin client failed")
|
|
|
|
}
|
|
|
|
|
2022-08-31 10:11:10 -05:00
|
|
|
return &backend.QueryDataResponse{Responses: make(backend.Responses)}, nil
|
2022-01-07 13:45:26 -06:00
|
|
|
}
|