fix(tests): fix remaining test failures

- Fix managed resources test: use folder target for first repo to allow second folder repo
- Fix empty resources validation: check len(opts.Resources) == 0 directly (nil check not needed, len() for nil slices is zero)
- Fix folder structure export: clear folder metadata before writing so WriteResourceFileFromObject uses exportPath directly
This commit is contained in:
Roberto Jimenez Sanchez
2025-12-02 22:21:24 +01:00
parent f55beac48a
commit a8886d2acd
3 changed files with 23 additions and 19 deletions
+7 -5
View File
@@ -101,11 +101,13 @@ func validateExportJobOptions(opts *provisioning.ExportJobOptions) field.ErrorLi
}
// Validate resources if specified
// If Resources is provided, it must not be empty
if opts.Resources != nil {
if len(opts.Resources) == 0 {
list = append(list, field.Required(field.NewPath("spec", "push", "resources"), "resources list cannot be empty when specified"))
}
// If Resources is provided (either as non-nil slice or empty slice), it must not be empty
// Note: JSON unmarshaling will set Resources to [] rather than nil when explicitly provided
// Note: len() for nil slices is defined as zero, so we can check len directly
if len(opts.Resources) == 0 {
list = append(list, field.Required(field.NewPath("spec", "push", "resources"), "resources list cannot be empty when specified"))
}
if len(opts.Resources) > 0 {
for i, r := range opts.Resources {
resourcePath := field.NewPath("spec", "push", "resources").Index(i)
@@ -379,21 +379,22 @@ func writeResourceToRepository(
result *jobs.JobResourceResult,
progress jobs.JobProgressRecorder,
) error {
// Temporarily clear folder metadata so WriteResourceFileFromObject doesn't try to resolve
// folder paths from repository tree (we've already computed the path from unmanaged tree)
originalFolder := meta.GetFolder()
if originalFolder != "" {
meta.SetFolder("")
}
defer func() {
if originalFolder != "" {
meta.SetFolder(originalFolder)
}
}()
// Export the resource
progress.SetMessage(ctx, fmt.Sprintf("Exporting resource %s/%s/%s", resourceRef.Group, resourceRef.Kind, resourceRef.Name))
var err error
// exportPath already includes the folder structure from the unmanaged tree.
// We need to clear the folder metadata so WriteResourceFileFromObject doesn't try to resolve
// folder paths from repository tree (which doesn't have unmanaged folders).
// When folder is empty, WriteResourceFileFromObject will use rootFolder logic:
// - For instance targets: rootFolder is empty, so fid.Path will be empty, and it will use exportPath directly
// - For folder targets: rootFolder is repo name, but fid.Path will still be empty, so it will use exportPath directly
originalFolder := meta.GetFolder()
if originalFolder != "" {
meta.SetFolder("")
defer func() {
meta.SetFolder(originalFolder)
}()
}
result.Path, err = repositoryResources.WriteResourceFileFromObject(ctx, item, resources.WriteOptions{
Path: exportPath, // Path already includes folder structure from unmanaged tree
Ref: branch,
@@ -222,9 +222,10 @@ func TestIntegrationProvisioning_ExportSpecificResourcesRejectsManagedResources(
helper := runGrafana(t)
ctx := context.Background()
// Create a managed dashboard via repository sync
// Create a managed dashboard via repository sync (use folder target to allow second repo)
testRepo := TestRepo{
Name: "managed-dashboard-repo",
Name: "managed-dashboard-repo",
Target: "folder",
Copies: map[string]string{
"exportunifiedtorepository/dashboard-test-v1.yaml": "dashboard.json",
},