Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Ignore default namespace in policy tests
  • Loading branch information
jsoriano committed Jul 30, 2025
commit 56c8aee453776517d91bd9d97a50dec4154f188a
16 changes: 12 additions & 4 deletions internal/testrunner/runners/policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os"
"path/filepath"
"regexp"
"slices"
"strings"

"github.com/pmezard/go-difflib/difflib"
Expand Down Expand Up @@ -98,6 +99,7 @@ type policyEntryFilter struct {
elementsEntries []policyEntryFilter
memberReplace *policyEntryReplace
onlyIfEmpty bool
ignoreValues []any
}

type policyEntryReplace struct {
Expand Down Expand Up @@ -151,7 +153,7 @@ var policyEntryFilters = []policyEntryFilter{
}},

// Namespaces may not be present in older versions of the stack.
{name: "namespaces", onlyIfEmpty: true},
{name: "namespaces", onlyIfEmpty: true, ignoreValues: []any{"default"}},
}

// cleanPolicy prepares a policy YAML as returned by the download API to be compared with other
Expand Down Expand Up @@ -213,7 +215,7 @@ func cleanPolicyMap(policyMap common.MapStr, entries []policyEntryFilter) (commo
}
}
default:
if entry.onlyIfEmpty && !isEmpty(v) {
if entry.onlyIfEmpty && !isEmpty(v, entry.ignoreValues) {
continue
}
err := policyMap.Delete(entry.name)
Expand All @@ -229,15 +231,21 @@ func cleanPolicyMap(policyMap common.MapStr, entries []policyEntryFilter) (commo
return policyMap, nil
}

func isEmpty(v any) bool {
func isEmpty(v any, ignoreValues []any) bool {
switch v := v.(type) {
case nil:
return true
case []any:
return len(v) == 0
return len(filterIgnored(v, ignoreValues)) == 0
case map[string]any:
return len(v) == 0
}

return false
}

func filterIgnored(v []any, ignoredValues []any) []any {
return slices.DeleteFunc(v, func(e any) bool {
return slices.Contains(ignoredValues, e)
})
}
9 changes: 9 additions & 0 deletions internal/testrunner/runners/policy/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ id: "2e19c1c4-185b-11ef-a7fc-43855f39047f"
`,
found: `
namespaces: []
`,
equal: true,
},
{
title: "clean namespaces if default",
expected: `
`,
found: `
namespaces: [default]
`,
equal: true,
},
Expand Down