test: add tests for glob match (#26027)

Signed-off-by: nitishfy <justnitish06@gmail.com>
This commit is contained in:
Nitish Kumar
2026-01-16 21:30:30 +05:30
committed by GitHub
parent 3453367509
commit fded82ad57
2 changed files with 77 additions and 1 deletions

View File

@@ -443,6 +443,7 @@ func (proj *AppProject) RemoveFinalizer() {
setFinalizer(&proj.ObjectMeta, ResourcesFinalizerName, false)
}
// globMatch matches a value with a glob pattern
func globMatch(pattern string, val string, allowNegation bool, separators ...rune) bool {
if allowNegation && isDenyPattern(pattern) {
return !glob.Match(pattern[1:], val, separators...)
@@ -526,11 +527,12 @@ func (proj AppProject) isDestinationMatched(dst ApplicationDestination) bool {
return anyDestinationMatched
}
// isDenyPattern checks if a pattern contains negation
func isDenyPattern(pattern string) bool {
return strings.HasPrefix(pattern, "!")
}
// TODO: document this method
// NormalizeJWTTokens normalizes JWT Tokens
func (proj *AppProject) NormalizeJWTTokens() bool {
needNormalize := false
for i, role := range proj.Spec.Roles {

View File

@@ -565,6 +565,80 @@ func TestAppProject_IsGroupKindPermitted(t *testing.T) {
assert.True(t, proj7.IsGroupKindNamePermitted(schema.GroupKind{Group: "", Kind: "Namespace"}, "team1-namespace", false))
}
func TestGlobMatch(t *testing.T) {
tests := []struct {
name string
pattern string
val string
allowNegation bool
expected bool
}{
{
name: "exact match",
pattern: "foo",
val: "foo",
allowNegation: false,
expected: true,
},
{
name: "glob wildcard match",
pattern: "foo*",
val: "foobar",
allowNegation: false,
expected: true,
},
{
name: "glob wildcard no match",
pattern: "foo*",
val: "bar",
allowNegation: false,
expected: false,
},
{
name: "star matches everything",
pattern: "*",
val: "anything",
allowNegation: false,
expected: true,
},
{
name: "deny pattern with negation allowed - match",
pattern: "!foo",
val: "foo",
allowNegation: true,
expected: false,
},
{
name: "deny pattern with negation allowed - no match",
pattern: "!foo",
val: "bar",
allowNegation: true,
expected: true,
},
{
name: "deny pattern ignored when negation not allowed",
pattern: "!foo",
val: "foo",
allowNegation: false,
expected: false, // treated as literal pattern "!foo"
},
{
name: "deny pattern ignored when negation not allowed - no match",
pattern: "!foo",
val: "!foo",
allowNegation: false,
expected: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := globMatch(tt.pattern, tt.val, tt.allowNegation)
require.Equal(t, tt.expected, result)
})
}
}
func TestAppProject_GetRoleByName(t *testing.T) {
t.Run("NotExists", func(t *testing.T) {
p := &AppProject{}