mirror of
https://github.com/grafana/grafana.git
synced 2025-08-01 09:03:11 +08:00
130 lines
3.2 KiB
Go
130 lines
3.2 KiB
Go
package dashboards
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
|
data "github.com/grafana/grafana-plugin-sdk-go/experimental/apis/data/v0alpha1"
|
|
"github.com/stretchr/testify/require"
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
|
|
"github.com/grafana/grafana/pkg/services/datasources"
|
|
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
|
"github.com/grafana/grafana/pkg/tests/apis"
|
|
"github.com/grafana/grafana/pkg/tests/testinfra"
|
|
"github.com/grafana/grafana/pkg/tests/testsuite"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
testsuite.Run(m)
|
|
}
|
|
|
|
func TestIntegrationSimpleQuery(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("skipping integration test")
|
|
}
|
|
helper := apis.NewK8sTestHelper(t, testinfra.GrafanaOpts{
|
|
AppModeProduction: false, // dev mode required for datasource connections
|
|
EnableFeatureToggles: []string{
|
|
featuremgmt.FlagGrafanaAPIServerWithExperimentalAPIs, // Required to start the example service
|
|
},
|
|
})
|
|
|
|
// Create a single datasource
|
|
ds := helper.CreateDS(&datasources.AddDataSourceCommand{
|
|
Name: "test",
|
|
Type: datasources.DS_TESTDATA,
|
|
UID: "test",
|
|
OrgID: int64(1),
|
|
})
|
|
require.Equal(t, "test", ds.UID)
|
|
|
|
t.Run("Call query with expression", func(t *testing.T) {
|
|
client := helper.Org1.Admin.RESTClient(t, &schema.GroupVersion{
|
|
Group: "query.grafana.app",
|
|
Version: "v0alpha1",
|
|
})
|
|
|
|
q1 := data.DataQuery{
|
|
CommonQueryProperties: data.CommonQueryProperties{
|
|
RefID: "X",
|
|
Datasource: &data.DataSourceRef{
|
|
Type: "grafana-testdata-datasource",
|
|
UID: ds.UID,
|
|
},
|
|
},
|
|
}
|
|
q1.Set("scenarioId", "csv_content")
|
|
q1.Set("csvContent", "a\n1")
|
|
|
|
q2 := data.DataQuery{
|
|
CommonQueryProperties: data.CommonQueryProperties{
|
|
RefID: "Y",
|
|
Datasource: &data.DataSourceRef{
|
|
UID: "__expr__",
|
|
},
|
|
},
|
|
}
|
|
q2.Set("type", "math")
|
|
q2.Set("expression", "$X + 2")
|
|
|
|
body, err := json.Marshal(&data.QueryDataRequest{
|
|
Queries: []data.DataQuery{
|
|
q1, q2,
|
|
// https://github.com/grafana/grafana-plugin-sdk-go/pull/921
|
|
// data.NewDataQuery(map[string]any{
|
|
// "refId": "X",
|
|
// "datasource": data.DataSourceRef{
|
|
// Type: "grafana-testdata-datasource",
|
|
// UID: ds.UID,
|
|
// },
|
|
// "scenarioId": "csv_content",
|
|
// "csvContent": "a\n1",
|
|
// }),
|
|
// data.NewDataQuery(map[string]any{
|
|
// "refId": "Y",
|
|
// "datasource": data.DataSourceRef{
|
|
// UID: "__expr__",
|
|
// },
|
|
// "type": "math",
|
|
// "expression": "$X + 2",
|
|
// }),
|
|
},
|
|
})
|
|
|
|
//fmt.Printf("%s", string(body))
|
|
|
|
require.NoError(t, err)
|
|
|
|
result := client.Post().
|
|
Namespace("default").
|
|
Suffix("query").
|
|
SetHeader("Content-type", "application/json").
|
|
Body(body).
|
|
Do(context.Background())
|
|
|
|
require.NoError(t, result.Error())
|
|
|
|
body, err = result.Raw()
|
|
require.NoError(t, err)
|
|
fmt.Printf("OUT: %s", string(body))
|
|
|
|
rsp := &backend.QueryDataResponse{}
|
|
err = json.Unmarshal(body, rsp)
|
|
require.NoError(t, err)
|
|
require.Equal(t, 2, len(rsp.Responses))
|
|
|
|
frameX := rsp.Responses["X"].Frames[0]
|
|
frameY := rsp.Responses["Y"].Frames[0]
|
|
|
|
vX, _ := frameX.Fields[0].ConcreteAt(0)
|
|
vY, _ := frameY.Fields[0].ConcreteAt(0)
|
|
|
|
require.Equal(t, int64(1), vX)
|
|
require.Equal(t, float64(3), vY) // 1 + 2, but always float64
|
|
})
|
|
}
|