Provisioning: Fix Dashboard Creation For First-Level Repository Folders (#109962)
This commit is contained in:
@@ -43,6 +43,8 @@ func TestIntegrationProvisioning_DeleteResources(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 3, len(dashboards.Items))
|
||||
|
||||
helper.validateManagedDashboardsFolderMetadata(t, ctx, repo, dashboards.Items)
|
||||
|
||||
folders, err := helper.Folders.Resource.List(ctx, metav1.ListOptions{})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 2, len(folders.Items))
|
||||
@@ -126,6 +128,13 @@ func TestIntegrationProvisioning_MoveResources(t *testing.T) {
|
||||
ExpectedFolders: 0,
|
||||
})
|
||||
|
||||
// Validate the dashboard metadata
|
||||
dashboards, err := helper.DashboardsV1.Resource.List(ctx, metav1.ListOptions{})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 1, len(dashboards.Items))
|
||||
|
||||
helper.validateManagedDashboardsFolderMetadata(t, ctx, repo, dashboards.Items)
|
||||
|
||||
// Verify the original dashboard exists in Grafana (using the UID from all-panels.json)
|
||||
const allPanelsUID = "n1jR8vnnz" // This is the UID from the all-panels.json file
|
||||
obj, err := helper.DashboardsV1.Resource.Get(ctx, allPanelsUID, metav1.GetOptions{})
|
||||
@@ -420,6 +429,17 @@ func TestIntegrationProvisioning_FilesOwnershipProtection(t *testing.T) {
|
||||
ExpectedFolders: 2, // Total across both repos
|
||||
})
|
||||
|
||||
allDashboards, err := helper.DashboardsV1.Resource.List(ctx, metav1.ListOptions{})
|
||||
require.NoError(t, err)
|
||||
for _, dashboard := range allDashboards.Items {
|
||||
annotations := dashboard.GetAnnotations()
|
||||
// Expect to be managed by repo1 or repo2
|
||||
managerID := annotations["grafana.app/managerId"]
|
||||
if managerID != repo1 && managerID != repo2 {
|
||||
t.Fatalf("dashboard %s is not managed by repo1 or repo2", dashboard.GetName())
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("CREATE file with UID already owned by different repository - should fail", func(t *testing.T) {
|
||||
// Try to create a dashboard in repo2 that has the same UID as the one in repo1
|
||||
// The all-panels.json has UID "n1jR8vnnz" which is already owned by repo1
|
||||
|
||||
@@ -460,6 +460,34 @@ func (h *provisioningTestHelper) logRepositoryObject(t *testing.T, obj map[strin
|
||||
}
|
||||
}
|
||||
|
||||
// validateManagedDashboardsFolderMetadata validates the folder metadata
|
||||
// of the managed dashboards.
|
||||
// If folder is nested, folder annotations should not be empty.
|
||||
// Also checks that the managerId property exists.
|
||||
func (h *provisioningTestHelper) validateManagedDashboardsFolderMetadata(t *testing.T,
|
||||
ctx context.Context, repoName string, dashboards []unstructured.Unstructured) {
|
||||
t.Helper()
|
||||
|
||||
// Check if folder is nested or not.
|
||||
// If not, folder annotations should be empty as we have an "instance" sync target
|
||||
for _, d := range dashboards {
|
||||
sourcePath, _, _ := unstructured.NestedString(d.Object, "metadata", "annotations", "grafana.app/sourcePath")
|
||||
isNested := strings.Contains(sourcePath, "/")
|
||||
|
||||
folder, found, _ := unstructured.NestedString(d.Object, "metadata", "annotations", "grafana.app/folder")
|
||||
if isNested {
|
||||
require.True(t, found, "dashboard should have a folder annotation")
|
||||
require.NotEmpty(t, folder, "dashboard should be in a non-empty folder")
|
||||
} else {
|
||||
require.False(t, found, "dashboard should not have a folder annotation")
|
||||
}
|
||||
|
||||
managerID, _, _ := unstructured.NestedString(d.Object, "metadata", "annotations", "grafana.app/managerId")
|
||||
// require.Equal(t, repoName, managerID, "dashboard should be managed by gitsync repo")
|
||||
require.Equal(t, repoName, managerID, "dashboard should be managed by gitsync repo")
|
||||
}
|
||||
}
|
||||
|
||||
type TestRepo struct {
|
||||
Name string
|
||||
Target string
|
||||
|
||||
Reference in New Issue
Block a user