Skip to content

Commit 56c8aee

Browse files
committed
Ignore default namespace in policy tests
1 parent fa0e46f commit 56c8aee

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

internal/testrunner/runners/policy/policy.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"os"
1313
"path/filepath"
1414
"regexp"
15+
"slices"
1516
"strings"
1617

1718
"github.com/pmezard/go-difflib/difflib"
@@ -98,6 +99,7 @@ type policyEntryFilter struct {
9899
elementsEntries []policyEntryFilter
99100
memberReplace *policyEntryReplace
100101
onlyIfEmpty bool
102+
ignoreValues []any
101103
}
102104

103105
type policyEntryReplace struct {
@@ -151,7 +153,7 @@ var policyEntryFilters = []policyEntryFilter{
151153
}},
152154

153155
// Namespaces may not be present in older versions of the stack.
154-
{name: "namespaces", onlyIfEmpty: true},
156+
{name: "namespaces", onlyIfEmpty: true, ignoreValues: []any{"default"}},
155157
}
156158

157159
// cleanPolicy prepares a policy YAML as returned by the download API to be compared with other
@@ -213,7 +215,7 @@ func cleanPolicyMap(policyMap common.MapStr, entries []policyEntryFilter) (commo
213215
}
214216
}
215217
default:
216-
if entry.onlyIfEmpty && !isEmpty(v) {
218+
if entry.onlyIfEmpty && !isEmpty(v, entry.ignoreValues) {
217219
continue
218220
}
219221
err := policyMap.Delete(entry.name)
@@ -229,15 +231,21 @@ func cleanPolicyMap(policyMap common.MapStr, entries []policyEntryFilter) (commo
229231
return policyMap, nil
230232
}
231233

232-
func isEmpty(v any) bool {
234+
func isEmpty(v any, ignoreValues []any) bool {
233235
switch v := v.(type) {
234236
case nil:
235237
return true
236238
case []any:
237-
return len(v) == 0
239+
return len(filterIgnored(v, ignoreValues)) == 0
238240
case map[string]any:
239241
return len(v) == 0
240242
}
241243

242244
return false
243245
}
246+
247+
func filterIgnored(v []any, ignoredValues []any) []any {
248+
return slices.DeleteFunc(v, func(e any) bool {
249+
return slices.Contains(ignoredValues, e)
250+
})
251+
}

internal/testrunner/runners/policy/policy_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ id: "2e19c1c4-185b-11ef-a7fc-43855f39047f"
6464
`,
6565
found: `
6666
namespaces: []
67+
`,
68+
equal: true,
69+
},
70+
{
71+
title: "clean namespaces if default",
72+
expected: `
73+
`,
74+
found: `
75+
namespaces: [default]
6776
`,
6877
equal: true,
6978
},

0 commit comments

Comments
 (0)