Provisioning: Integration test that Repository resources work (#100197)

* Provisioning: Set up server in practice

* Provisioning: Integration test that Repository resources work
pull/100253/head^2
Mariell Hoversholm 3 months ago committed by GitHub
parent a51e785bc1
commit e815a2850e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 2
      pkg/registry/apis/apis.go
  2. 126
      pkg/tests/apis/provisioning/provisioning_test.go
  3. 25
      pkg/tests/apis/provisioning/testdata/github-example.json
  4. 20
      pkg/tests/apis/provisioning/testdata/local-devenv.json

@ -12,6 +12,7 @@ import (
"github.com/grafana/grafana/pkg/registry/apis/folders"
"github.com/grafana/grafana/pkg/registry/apis/iam"
"github.com/grafana/grafana/pkg/registry/apis/peakq"
"github.com/grafana/grafana/pkg/registry/apis/provisioning"
"github.com/grafana/grafana/pkg/registry/apis/query"
"github.com/grafana/grafana/pkg/registry/apis/scope"
"github.com/grafana/grafana/pkg/registry/apis/userstorage"
@ -36,6 +37,7 @@ func ProvideRegistryServiceSink(
_ *query.QueryAPIBuilder,
_ *notifications.NotificationsAPIBuilder,
_ *userstorage.UserStorageAPIBuilder,
_ *provisioning.APIBuilder,
) *Service {
return &Service{}
}

@ -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…
Cancel
Save