mirror of https://github.com/grafana/grafana
Provisioning: Integration test that Repository resources work (#100197)
* Provisioning: Set up server in practice * Provisioning: Integration test that Repository resources workpull/100253/head^2
parent
a51e785bc1
commit
e815a2850e
@ -0,0 +1,126 @@ |
||||
package provisioning |
||||
|
||||
import ( |
||||
"context" |
||||
"encoding/json" |
||||
"testing" |
||||
|
||||
dashboard "github.com/grafana/grafana/pkg/apis/dashboard/v1alpha1" |
||||
folder "github.com/grafana/grafana/pkg/apis/folder/v0alpha1" |
||||
provisioning "github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1" |
||||
"github.com/grafana/grafana/pkg/apiserver/rest" |
||||
"github.com/grafana/grafana/pkg/services/featuremgmt" |
||||
"github.com/grafana/grafana/pkg/setting" |
||||
"github.com/grafana/grafana/pkg/tests/apis" |
||||
"github.com/grafana/grafana/pkg/tests/testinfra" |
||||
"github.com/grafana/grafana/pkg/tests/testsuite" |
||||
"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" |
||||
) |
||||
|
||||
func TestMain(m *testing.M) { |
||||
testsuite.Run(m) |
||||
} |
||||
|
||||
func TestIntegrationProvisioning(t *testing.T) { |
||||
if testing.Short() { |
||||
t.Skip("skipping integration test") |
||||
} |
||||
|
||||
ctx := context.Background() |
||||
helper := apis.NewK8sTestHelper(t, testinfra.GrafanaOpts{ |
||||
AppModeProduction: false, // required for experimental APIs
|
||||
EnableFeatureToggles: []string{ |
||||
featuremgmt.FlagProvisioning, |
||||
featuremgmt.FlagGrafanaAPIServerWithExperimentalAPIs, |
||||
featuremgmt.FlagKubernetesCliDashboards, |
||||
featuremgmt.FlagKubernetesFoldersServiceV2, |
||||
}, |
||||
UnifiedStorageConfig: map[string]setting.UnifiedStorageConfig{ |
||||
"dashboards.dashboard.grafana.app": { |
||||
DualWriterMode: rest.Mode5, |
||||
}, |
||||
"folders.folder.grafana.app": { |
||||
DualWriterMode: rest.Mode5, |
||||
}, |
||||
}, |
||||
}) |
||||
|
||||
client := helper.GetResourceClient(apis.ResourceClientArgs{ |
||||
User: helper.Org1.Admin, |
||||
Namespace: "default", // actually org1
|
||||
GVR: provisioning.RepositoryResourceInfo.GroupVersionResource(), |
||||
}) |
||||
|
||||
folderClient := helper.GetResourceClient(apis.ResourceClientArgs{ |
||||
User: helper.Org1.Admin, |
||||
Namespace: "default", // actually org1
|
||||
GVR: folder.FolderResourceInfo.GroupVersionResource(), |
||||
}) |
||||
|
||||
dashboardClient := helper.GetResourceClient(apis.ResourceClientArgs{ |
||||
User: helper.Org1.Admin, |
||||
Namespace: "default", // actually org1
|
||||
GVR: dashboard.DashboardResourceInfo.GroupVersionResource(), |
||||
}) |
||||
|
||||
// Repo client, but less guard rails. Useful for subresources. We'll need this later...
|
||||
restClient := helper.Org1.Admin.RESTClient(t, &schema.GroupVersion{ |
||||
Group: "provisioning.grafana.app", Version: "v0alpha1", |
||||
}) |
||||
_ = restClient |
||||
|
||||
cleanSlate := func(t *testing.T) { |
||||
deleteAll := func(client *apis.K8sResourceClient) error { |
||||
list, err := client.Resource.List(ctx, metav1.ListOptions{}) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
for _, resource := range list.Items { |
||||
if err := client.Resource.Delete(ctx, resource.GetName(), metav1.DeleteOptions{}); err != nil { |
||||
return err |
||||
} |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
require.NoError(t, deleteAll(dashboardClient), "deleting all dashboards") |
||||
require.NoError(t, deleteAll(folderClient), "deleting all folders") |
||||
require.NoError(t, deleteAll(client), "deleting all repositories") |
||||
} |
||||
cleanSlate(t) |
||||
|
||||
t.Run("Creating and getting repositories", func(t *testing.T) { |
||||
cleanSlate(t) |
||||
|
||||
createOptions := metav1.CreateOptions{FieldValidation: "Strict"} |
||||
|
||||
for _, inputFilePath := range []string{ |
||||
"testdata/local-devenv.json", |
||||
"testdata/github-example.json", |
||||
} { |
||||
t.Run(inputFilePath, func(t *testing.T) { |
||||
input := helper.LoadYAMLOrJSONFile(inputFilePath) |
||||
expectedOutput, err := json.MarshalIndent(input.Object["spec"], "", " ") |
||||
require.NoError(t, err, "failed to marshal JSON from input spec") |
||||
|
||||
_, err = client.Resource.Create(ctx, input, createOptions) |
||||
require.NoError(t, err, "failed to create resource") |
||||
|
||||
output, err := client.Resource.Get(ctx, mustNestedString(input.Object, "metadata", "name"), metav1.GetOptions{}) |
||||
require.NoError(t, err, "failed to read back resource") |
||||
outputJSON, err := json.MarshalIndent(output.Object["spec"], "", " ") |
||||
require.NoError(t, err, "failed to marshal JSON from read back resource") |
||||
|
||||
require.JSONEq(t, string(expectedOutput), string(outputJSON)) |
||||
}) |
||||
} |
||||
}) |
||||
} |
||||
|
||||
func mustNestedString(obj map[string]interface{}, fields ...string) string { |
||||
v, _, _ := unstructured.NestedString(obj, fields...) |
||||
return v |
||||
} |
@ -0,0 +1,25 @@ |
||||
{ |
||||
"apiVersion": "provisioning.grafana.app/v0alpha1", |
||||
"kind": "Repository", |
||||
"metadata": { |
||||
"name": "github-example" |
||||
}, |
||||
"spec": { |
||||
"title": "Github Example", |
||||
"description": "load resources from github", |
||||
"type": "github", |
||||
"editing": { |
||||
"create": true, |
||||
"update": true, |
||||
"delete": true |
||||
}, |
||||
"github": { |
||||
"owner": "grafana", |
||||
"repository": "git-ui-sync-demo", |
||||
"branch": "dummy-branch", |
||||
"branchWorkflow": true, |
||||
"generateDashboardPreviews": true, |
||||
"token": "github_pat_dummy" |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,20 @@ |
||||
{ |
||||
"apiVersion": "provisioning.grafana.app/v0alpha1", |
||||
"kind": "Repository", |
||||
"metadata": { |
||||
"name": "local-devenv" |
||||
}, |
||||
"spec": { |
||||
"title": "Load devenv dashboards", |
||||
"description": "Load /devenv/dev-dashboards (from root of repository)", |
||||
"editing": { |
||||
"create": true, |
||||
"update": true, |
||||
"delete": true |
||||
}, |
||||
"type": "local", |
||||
"local": { |
||||
"path": "devenv/dev-dashboards" |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue