Provisioning: Fix Dashboard Creation For First-Level Repository Folders (#109962)
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
dashboardV1 "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v1beta1"
|
||||
provisioning "github.com/grafana/grafana/apps/provisioning/pkg/apis/provisioning/v0alpha1"
|
||||
"github.com/grafana/grafana/pkg/registry/apis/provisioning/repository"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
)
|
||||
|
||||
func TestParser(t *testing.T) {
|
||||
@@ -27,6 +28,16 @@ func TestParser(t *testing.T) {
|
||||
Name: "repo",
|
||||
},
|
||||
clients: clients,
|
||||
config: &provisioning.Repository{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Namespace: "xxx",
|
||||
Name: "repo",
|
||||
},
|
||||
Spec: provisioning.RepositorySpec{
|
||||
Type: provisioning.LocalRepositoryType,
|
||||
Sync: provisioning.SyncOptions{Target: provisioning.SyncTargetTypeFolder},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
t.Run("invalid input", func(t *testing.T) {
|
||||
@@ -93,4 +104,43 @@ spec:
|
||||
require.Equal(t, "dashboard.grafana.app", dash.GVR.Group)
|
||||
require.Equal(t, "v0alpha1", dash.GVR.Version)
|
||||
})
|
||||
|
||||
t.Run("validate proper folder metadata is set", func(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
filePath string
|
||||
expectedFolder string
|
||||
}{
|
||||
{
|
||||
name: "file in subdirectory should use parsed folder ID",
|
||||
filePath: "team-a/testing-valid-dashboard.json",
|
||||
expectedFolder: ParseFolder("team-a/", "repo").ID,
|
||||
},
|
||||
{
|
||||
name: "file in first-level directory should use parent folder id",
|
||||
filePath: "testing-valid-dashboard.json",
|
||||
expectedFolder: parser.repo.Name,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
dash, err := parser.Parse(context.Background(), &repository.FileInfo{
|
||||
Path: tc.filePath,
|
||||
Data: []byte(`apiVersion: dashboard.grafana.app/v0alpha1
|
||||
kind: Dashboard
|
||||
metadata:
|
||||
name: test-dashboard
|
||||
spec:
|
||||
title: Test dashboard
|
||||
`),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tc.expectedFolder, dash.Meta.GetFolder(), "folder should match expected")
|
||||
annotations := dash.Obj.GetAnnotations()
|
||||
require.NotNil(t, annotations, "annotations should not be nil")
|
||||
require.Equal(t, tc.expectedFolder, annotations["grafana.app/folder"], "folder annotation should match expected")
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user