mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
K8s: Add basic query service (#80325)
This commit is contained in:
@@ -107,7 +107,7 @@ func TestTestDatasource(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Call subresources", func(t *testing.T) {
|
||||
client := helper.Org1.Admin.Client.Resource(schema.GroupVersionResource{
|
||||
client := helper.Org1.Admin.ResourceClient(t, schema.GroupVersionResource{
|
||||
Group: "testdata.datasource.grafana.app",
|
||||
Version: "v0alpha1",
|
||||
Resource: "connections",
|
||||
|
||||
@@ -29,7 +29,7 @@ func TestExampleApp(t *testing.T) {
|
||||
|
||||
t.Run("Check runtime info resource", func(t *testing.T) {
|
||||
// Resource is not namespaced!
|
||||
client := helper.Org1.Admin.Client.Resource(schema.GroupVersionResource{
|
||||
client := helper.Org1.Admin.ResourceClient(t, schema.GroupVersionResource{
|
||||
Group: "example.grafana.app",
|
||||
Version: "v0alpha1",
|
||||
Resource: "runtime",
|
||||
@@ -139,7 +139,7 @@ func TestExampleApp(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Check dummy with subresource", func(t *testing.T) {
|
||||
client := helper.Org1.Viewer.Client.Resource(schema.GroupVersionResource{
|
||||
client := helper.Org1.Viewer.ResourceClient(t, schema.GroupVersionResource{
|
||||
Group: "example.grafana.app",
|
||||
Version: "v0alpha1",
|
||||
Resource: "dummy",
|
||||
|
||||
@@ -98,10 +98,13 @@ func (c *K8sTestHelper) GetResourceClient(args ResourceClientArgs) *K8sResourceC
|
||||
args.Namespace = c.namespacer(args.User.Identity.GetOrgID())
|
||||
}
|
||||
|
||||
client, err := dynamic.NewForConfig(args.User.NewRestConfig())
|
||||
require.NoError(c.t, err)
|
||||
|
||||
return &K8sResourceClient{
|
||||
t: c.t,
|
||||
Args: args,
|
||||
Resource: args.User.Client.Resource(args.GVR).Namespace(args.Namespace),
|
||||
Resource: client.Resource(args.GVR).Namespace(args.Namespace),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -163,8 +166,31 @@ type OrgUsers struct {
|
||||
|
||||
type User struct {
|
||||
Identity identity.Requester
|
||||
Client *dynamic.DynamicClient
|
||||
password string
|
||||
baseURL string
|
||||
}
|
||||
|
||||
func (c *User) NewRestConfig() *rest.Config {
|
||||
return &rest.Config{
|
||||
Host: c.baseURL,
|
||||
Username: c.Identity.GetLogin(),
|
||||
Password: c.password,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *User) ResourceClient(t *testing.T, gvr schema.GroupVersionResource) dynamic.NamespaceableResourceInterface {
|
||||
client, err := dynamic.NewForConfig(c.NewRestConfig())
|
||||
require.NoError(t, err)
|
||||
return client.Resource(gvr)
|
||||
}
|
||||
|
||||
func (c *User) RESTClient(t *testing.T, gv *schema.GroupVersion) *rest.RESTClient {
|
||||
cfg := dynamic.ConfigFor(c.NewRestConfig()) // adds negotiated serializers!
|
||||
cfg.GroupVersion = gv
|
||||
cfg.APIPath = "apis" // the plural
|
||||
client, err := rest.RESTClientFor(cfg)
|
||||
require.NoError(t, err)
|
||||
return client
|
||||
}
|
||||
|
||||
type RequestParams struct {
|
||||
@@ -380,19 +406,10 @@ func (c K8sTestHelper) createTestUsers(orgName string) OrgUsers {
|
||||
require.Equal(c.t, orgId, s.OrgID)
|
||||
require.Equal(c.t, role, s.OrgRole) // make sure the role was set properly
|
||||
|
||||
config := &rest.Config{
|
||||
Host: baseUrl,
|
||||
Username: s.Login,
|
||||
Password: key,
|
||||
}
|
||||
|
||||
client, err := dynamic.NewForConfig(config)
|
||||
require.NoError(c.t, err)
|
||||
|
||||
return User{
|
||||
Identity: s,
|
||||
Client: client,
|
||||
password: key,
|
||||
baseURL: baseUrl,
|
||||
}
|
||||
}
|
||||
return OrgUsers{
|
||||
|
||||
98
pkg/tests/apis/query/query_test.go
Normal file
98
pkg/tests/apis/query/query_test.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package dashboards
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
|
||||
query "github.com/grafana/grafana/pkg/apis/query/v0alpha1"
|
||||
"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"
|
||||
)
|
||||
|
||||
func TestSimpleQuery(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", func(t *testing.T) {
|
||||
client := helper.Org1.Admin.RESTClient(t, &schema.GroupVersion{
|
||||
Group: "query.grafana.app",
|
||||
Version: "v0alpha1",
|
||||
})
|
||||
|
||||
q := query.GenericDataQuery{
|
||||
Datasource: &query.DataSourceRef{
|
||||
Type: "grafana-testdata-datasource",
|
||||
UID: ds.UID,
|
||||
},
|
||||
}
|
||||
q.AdditionalProperties()["csvContent"] = "a,b,c\n1,hello,true"
|
||||
q.AdditionalProperties()["scenarioId"] = "csv_content"
|
||||
body, err := json.Marshal(&query.GenericQueryRequest{Queries: []query.GenericDataQuery{q}})
|
||||
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, 1, len(rsp.Responses))
|
||||
|
||||
frame := rsp.Responses["A"].Frames[0]
|
||||
disp, err := frame.StringTable(100, 10)
|
||||
require.NoError(t, err)
|
||||
fmt.Printf("%s\n", disp)
|
||||
|
||||
type expect struct {
|
||||
idx int
|
||||
name string
|
||||
val any
|
||||
}
|
||||
for _, check := range []expect{
|
||||
{0, "a", int64(1)},
|
||||
{1, "b", "hello"},
|
||||
{2, "c", true},
|
||||
} {
|
||||
field := frame.Fields[check.idx]
|
||||
require.Equal(t, check.name, field.Name)
|
||||
|
||||
v, _ := field.ConcreteAt(0)
|
||||
require.Equal(t, check.val, v)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user