add basic tests
This commit is contained in:
@@ -62,6 +62,7 @@ func (r *converter) asGenericDataSource(ds *datasources.DataSource) (*v0alpha1.G
|
||||
ReadOnly: ds.ReadOnly,
|
||||
},
|
||||
}
|
||||
cfg.UID = gapiutil.CalculateClusterWideUID(cfg)
|
||||
|
||||
if ds.JsonData != nil {
|
||||
val, ok := ds.JsonData.Interface().(map[string]any)
|
||||
@@ -83,23 +84,56 @@ func (r *converter) asGenericDataSource(ds *datasources.DataSource) (*v0alpha1.G
|
||||
|
||||
func (r *converter) toAddCommand(ds *v0alpha1.GenericDataSource) (*datasources.AddDataSourceCommand, error) {
|
||||
cmd := &datasources.AddDataSourceCommand{
|
||||
Name: ds.Name,
|
||||
Type: "", // TODO... group > datasource type????
|
||||
Access: datasources.DsAccess(ds.Spec.Access),
|
||||
URL: ds.Spec.URL,
|
||||
Database: ds.Spec.Database,
|
||||
// TODO... secrets
|
||||
Name: ds.Spec.Title,
|
||||
UID: ds.Name,
|
||||
|
||||
Type: "???", // TODO... group > datasource type????
|
||||
Access: datasources.DsAccess(ds.Spec.Access),
|
||||
URL: ds.Spec.URL,
|
||||
Database: ds.Spec.Database,
|
||||
User: ds.Spec.User,
|
||||
BasicAuth: ds.Spec.BasicAuth,
|
||||
BasicAuthUser: ds.Spec.BasicAuthUser,
|
||||
WithCredentials: ds.Spec.WithCredentials,
|
||||
IsDefault: ds.Spec.IsDefault,
|
||||
ReadOnly: ds.Spec.ReadOnly,
|
||||
}
|
||||
|
||||
if len(ds.Spec.JsonData.Object) > 0 {
|
||||
cmd.JsonData = simplejson.NewFromAny(ds.Spec.JsonData.Object)
|
||||
}
|
||||
|
||||
if len(ds.Secure) > 0 {
|
||||
cmd.SecureJsonData = map[string]string{
|
||||
"TODO": "values",
|
||||
}
|
||||
}
|
||||
|
||||
return cmd, nil
|
||||
}
|
||||
|
||||
func (r *converter) toUpdateCommand(ds *v0alpha1.GenericDataSource) (*datasources.UpdateDataSourceCommand, error) {
|
||||
cmd := &datasources.UpdateDataSourceCommand{}
|
||||
// TODO!!!
|
||||
cmd := &datasources.UpdateDataSourceCommand{
|
||||
Name: ds.Spec.Title,
|
||||
UID: ds.Name,
|
||||
|
||||
Type: "???", // TODO... group > datasource type????
|
||||
Access: datasources.DsAccess(ds.Spec.Access),
|
||||
URL: ds.Spec.URL,
|
||||
Database: ds.Spec.Database,
|
||||
User: ds.Spec.User,
|
||||
BasicAuth: ds.Spec.BasicAuth,
|
||||
BasicAuthUser: ds.Spec.BasicAuthUser,
|
||||
WithCredentials: ds.Spec.WithCredentials,
|
||||
IsDefault: ds.Spec.IsDefault,
|
||||
ReadOnly: ds.Spec.ReadOnly,
|
||||
}
|
||||
|
||||
if len(ds.Spec.JsonData.Object) > 0 {
|
||||
cmd.JsonData = simplejson.NewFromAny(ds.Spec.JsonData.Object)
|
||||
}
|
||||
|
||||
// Update specific things
|
||||
cmd.Version = int(ds.Generation)
|
||||
return cmd, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package datasource
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/grafana/authlib/types"
|
||||
"github.com/grafana/grafana/pkg/apis/datasource/v0alpha1"
|
||||
)
|
||||
|
||||
func TestConverter(t *testing.T) {
|
||||
t.Run("resource to command", func(t *testing.T) {
|
||||
obj := &v0alpha1.GenericDataSource{}
|
||||
converter := converter{mapper: types.OrgNamespaceFormatter}
|
||||
check := []string{
|
||||
"convert-testdata-A",
|
||||
}
|
||||
for _, name := range check {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
fpath := filepath.Join("testdata", name+"-input.json")
|
||||
raw, err := os.ReadFile(fpath)
|
||||
require.NoError(t, err)
|
||||
err = json.Unmarshal(raw, obj)
|
||||
require.NoError(t, err)
|
||||
|
||||
// The add command
|
||||
fpath = filepath.Join("testdata", name+"-output-add.json")
|
||||
add, err := converter.toAddCommand(obj)
|
||||
require.NoError(t, err)
|
||||
out, err := json.MarshalIndent(add, "", " ")
|
||||
require.NoError(t, err)
|
||||
raw, _ = os.ReadFile(fpath)
|
||||
if !assert.JSONEq(t, string(raw), string(out)) {
|
||||
os.WriteFile(fpath, out, 0600)
|
||||
}
|
||||
|
||||
// The update command
|
||||
fpath = filepath.Join("testdata", name+"-output-update.json")
|
||||
update, err := converter.toUpdateCommand(obj)
|
||||
require.NoError(t, err)
|
||||
out, err = json.MarshalIndent(update, "", " ")
|
||||
require.NoError(t, err)
|
||||
raw, _ = os.ReadFile(fpath)
|
||||
if !assert.JSONEq(t, string(raw), string(out)) {
|
||||
os.WriteFile(fpath, out, 0600)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"metadata": {
|
||||
"name": "cejobd88i85j4d",
|
||||
"namespace": "default",
|
||||
"uid": "IGIUtEQS21DtLpBG2rSGfuDoUX8cwsGrtb5aXauYeA4X",
|
||||
"resourceVersion": "1745320815000",
|
||||
"generation": 2,
|
||||
"creationTimestamp": "2025-04-22T11:20:11Z"
|
||||
},
|
||||
"spec": {
|
||||
"title": "grafana-testdata-datasource",
|
||||
"access": "proxy",
|
||||
"isDefault": true,
|
||||
"url": "http://something/",
|
||||
"database": "db",
|
||||
"jsonData": {
|
||||
"aaa": "bbb",
|
||||
"bbb": true,
|
||||
"ccc": 1.234
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "grafana-testdata-datasource",
|
||||
"type": "???",
|
||||
"access": "proxy",
|
||||
"url": "http://something/",
|
||||
"database": "db",
|
||||
"user": "",
|
||||
"basicAuth": false,
|
||||
"basicAuthUser": "",
|
||||
"withCredentials": false,
|
||||
"isDefault": true,
|
||||
"jsonData": {
|
||||
"aaa": "bbb",
|
||||
"bbb": true,
|
||||
"ccc": 1.234
|
||||
},
|
||||
"secureJsonData": null,
|
||||
"uid": "cejobd88i85j4d",
|
||||
"apiVersion": "",
|
||||
"IsPrunable": false
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"name": "grafana-testdata-datasource",
|
||||
"type": "???",
|
||||
"access": "proxy",
|
||||
"url": "http://something/",
|
||||
"user": "",
|
||||
"database": "db",
|
||||
"basicAuth": false,
|
||||
"basicAuthUser": "",
|
||||
"withCredentials": false,
|
||||
"isDefault": true,
|
||||
"jsonData": {
|
||||
"aaa": "bbb",
|
||||
"bbb": true,
|
||||
"ccc": 1.234
|
||||
},
|
||||
"secureJsonData": null,
|
||||
"version": 2,
|
||||
"uid": "cejobd88i85j4d",
|
||||
"apiVersion": "",
|
||||
"IsPrunable": false
|
||||
}
|
||||
@@ -2,13 +2,16 @@ package dashboards
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/services/datasources"
|
||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||
"github.com/grafana/grafana/pkg/tests/apis"
|
||||
@@ -38,6 +41,20 @@ func TestIntegrationTestDatasource(t *testing.T) {
|
||||
Type: datasources.DS_TESTDATA,
|
||||
UID: "test",
|
||||
OrgID: int64(1),
|
||||
|
||||
// These settings are not actually used, but testing that they get saved
|
||||
Database: "testdb",
|
||||
URL: "http://fake.url",
|
||||
Access: datasources.DS_ACCESS_PROXY,
|
||||
User: "example",
|
||||
ReadOnly: true,
|
||||
JsonData: simplejson.NewFromAny(map[string]any{
|
||||
"hello": "world",
|
||||
}),
|
||||
SecureJsonData: map[string]string{
|
||||
"aaa": "AAA",
|
||||
"bbb": "BBB",
|
||||
},
|
||||
})
|
||||
require.Equal(t, "test", ds.UID)
|
||||
|
||||
@@ -47,81 +64,130 @@ func TestIntegrationTestDatasource(t *testing.T) {
|
||||
|
||||
require.JSONEq(t, `[
|
||||
{
|
||||
"freshness": "Current",
|
||||
"resources": [
|
||||
{
|
||||
"resource": "connections",
|
||||
"responseKind": {
|
||||
"group": "",
|
||||
"kind": "DataSourceConnection",
|
||||
"version": ""
|
||||
},
|
||||
"scope": "Namespaced",
|
||||
"shortNames": [
|
||||
"grafana-testdata-datasource-connection"
|
||||
],
|
||||
"singularResource": "connection",
|
||||
"subresources": [
|
||||
"freshness": "Current",
|
||||
"resources": [
|
||||
{
|
||||
"responseKind": {
|
||||
"group": "",
|
||||
"kind": "HealthCheckResult",
|
||||
"version": ""
|
||||
},
|
||||
"subresource": "health",
|
||||
"verbs": [
|
||||
"get"
|
||||
]
|
||||
"resource": "connections",
|
||||
"responseKind": {
|
||||
"group": "",
|
||||
"kind": "DataSourceConnection",
|
||||
"version": ""
|
||||
},
|
||||
"scope": "Namespaced",
|
||||
"shortNames": [
|
||||
"grafana-testdata-datasource-connection"
|
||||
],
|
||||
"singularResource": "connection",
|
||||
"subresources": [
|
||||
{
|
||||
"responseKind": {
|
||||
"group": "",
|
||||
"kind": "HealthCheckResult",
|
||||
"version": ""
|
||||
},
|
||||
"subresource": "health",
|
||||
"verbs": [
|
||||
"get"
|
||||
]
|
||||
},
|
||||
{
|
||||
"responseKind": {
|
||||
"group": "",
|
||||
"kind": "QueryDataResponse",
|
||||
"version": ""
|
||||
},
|
||||
"subresource": "query",
|
||||
"verbs": [
|
||||
"create"
|
||||
]
|
||||
},
|
||||
{
|
||||
"responseKind": {
|
||||
"group": "",
|
||||
"kind": "Status",
|
||||
"version": ""
|
||||
},
|
||||
"subresource": "resource",
|
||||
"verbs": [
|
||||
"create",
|
||||
"delete",
|
||||
"get",
|
||||
"patch",
|
||||
"update"
|
||||
]
|
||||
}
|
||||
],
|
||||
"verbs": [
|
||||
"get",
|
||||
"list"
|
||||
]
|
||||
},
|
||||
{
|
||||
"responseKind": {
|
||||
"group": "",
|
||||
"kind": "QueryDataResponse",
|
||||
"version": ""
|
||||
},
|
||||
"subresource": "query",
|
||||
"verbs": [
|
||||
"create"
|
||||
]
|
||||
"resource": "datasource",
|
||||
"responseKind": {
|
||||
"group": "",
|
||||
"kind": "GenericDataSource",
|
||||
"version": ""
|
||||
},
|
||||
"scope": "Namespaced",
|
||||
"singularResource": "datasource",
|
||||
"verbs": [
|
||||
"create",
|
||||
"delete",
|
||||
"deletecollection",
|
||||
"get",
|
||||
"list",
|
||||
"patch",
|
||||
"update"
|
||||
]
|
||||
},
|
||||
{
|
||||
"responseKind": {
|
||||
"group": "",
|
||||
"kind": "Status",
|
||||
"version": ""
|
||||
},
|
||||
"subresource": "resource",
|
||||
"verbs": [
|
||||
"create",
|
||||
"delete",
|
||||
"get",
|
||||
"patch",
|
||||
"update"
|
||||
]
|
||||
"resource": "queryconvert",
|
||||
"responseKind": {
|
||||
"group": "",
|
||||
"kind": "QueryDataRequest",
|
||||
"version": ""
|
||||
},
|
||||
"scope": "Namespaced",
|
||||
"singularResource": "queryconvert",
|
||||
"verbs": [
|
||||
"create"
|
||||
]
|
||||
}
|
||||
],
|
||||
"verbs": [
|
||||
"get",
|
||||
"list"
|
||||
]
|
||||
},
|
||||
{
|
||||
"resource": "queryconvert",
|
||||
"responseKind": {
|
||||
"group": "",
|
||||
"kind": "QueryDataRequest",
|
||||
"version": ""
|
||||
},
|
||||
"scope": "Namespaced",
|
||||
"singularResource": "queryconvert",
|
||||
"verbs": [
|
||||
"create"
|
||||
]
|
||||
}
|
||||
],
|
||||
"version": "v0alpha1"
|
||||
],
|
||||
"version": "v0alpha1"
|
||||
}
|
||||
]`, disco)
|
||||
]`, disco)
|
||||
})
|
||||
|
||||
t.Run("Admin configs", func(t *testing.T) {
|
||||
client := helper.Org1.Admin.ResourceClient(t, schema.GroupVersionResource{
|
||||
Group: "testdata.datasource.grafana.app",
|
||||
Version: "v0alpha1",
|
||||
Resource: "datasource",
|
||||
}).Namespace("default")
|
||||
ctx := context.Background()
|
||||
|
||||
list, err := client.List(ctx, metav1.ListOptions{})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, list.Items, 1, "expected a single connection")
|
||||
require.Equal(t, "test", list.Items[0].GetName(), "with the test uid")
|
||||
|
||||
spec, _, _ := unstructured.NestedMap(list.Items[0].Object, "spec")
|
||||
jj, _ := json.MarshalIndent(spec, "", " ")
|
||||
fmt.Printf("%s\n", string(jj))
|
||||
require.JSONEq(t, `{
|
||||
"access": "proxy",
|
||||
"database": "testdb",
|
||||
"isDefault": true,
|
||||
"jsonData": {
|
||||
"hello": "world"
|
||||
},
|
||||
"readOnly": true,
|
||||
"title": "test",
|
||||
"url": "http://fake.url",
|
||||
"use": "example"
|
||||
}`, string(jj))
|
||||
})
|
||||
|
||||
t.Run("Call subresources", func(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user