1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-31 03:45:17 +00:00

chore: add unit tests for pkg/utils/wildcard (#4510)

* chore: add unit tests for pkg/utils/wildcard

Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>

* testify

Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>

Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>
This commit is contained in:
Charles-Edouard Brétéché 2022-09-06 10:02:41 +02:00 committed by GitHub
parent 870462cc6d
commit 429fe175bf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 99 additions and 12 deletions

View file

@ -6,7 +6,6 @@ import (
"github.com/go-logr/logr"
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
"github.com/kyverno/kyverno/pkg/engine/variables"
stringutils "github.com/kyverno/kyverno/pkg/utils/string"
"github.com/kyverno/kyverno/pkg/utils/wildcard"
"go.uber.org/multierr"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@ -70,7 +69,7 @@ func getTargets(target kyvernov1.ResourceSpec, ctx *PolicyContext, logger logr.L
name := target.Name
if namespace != "" && name != "" &&
!stringutils.ContainsWildcard(namespace) && !stringutils.ContainsWildcard(name) {
!wildcard.ContainsWildcard(namespace) && !wildcard.ContainsWildcard(name) {
obj, err := ctx.Client.GetResource(target.APIVersion, target.Kind, namespace, name)
if err != nil {
return nil, fmt.Errorf("failed to get target %s/%s %s/%s : %v", target.APIVersion, target.Kind, namespace, name, err)

View file

@ -4,7 +4,6 @@ import (
"strings"
commonAnchor "github.com/kyverno/kyverno/pkg/engine/anchor"
stringutils "github.com/kyverno/kyverno/pkg/utils/string"
wildcard "github.com/kyverno/kyverno/pkg/utils/wildcard"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
@ -21,7 +20,7 @@ func ReplaceInSelector(labelSelector *metav1.LabelSelector, resourceLabels map[s
func replaceWildcardsInMapKeyValues(patternMap map[string]string, resourceMap map[string]string) map[string]string {
result := map[string]string{}
for k, v := range patternMap {
if stringutils.ContainsWildcard(k) || stringutils.ContainsWildcard(v) {
if wildcard.ContainsWildcard(k) || wildcard.ContainsWildcard(v) {
matchK, matchV := expandWildcards(k, v, resourceMap, true, true)
result[matchK] = matchV
} else {
@ -140,7 +139,7 @@ func getValueAsStringMap(key string, data interface{}) (string, map[string]strin
func replaceWildcardsInMapKeys(patternData, resourceData map[string]string) map[string]interface{} {
results := map[string]interface{}{}
for k, v := range patternData {
if stringutils.ContainsWildcard(k) {
if wildcard.ContainsWildcard(k) {
anchorFreeKey, anchorPrefix := commonAnchor.RemoveAnchor(k)
matchK, _ := expandWildcards(anchorFreeKey, v, resourceData, false, false)
if anchorPrefix != "" {

View file

@ -1,13 +1,13 @@
package kube
import (
stringutils "github.com/kyverno/kyverno/pkg/utils/string"
"github.com/kyverno/kyverno/pkg/utils/wildcard"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func LabelSelectorContainsWildcard(v *metav1.LabelSelector) bool {
for k, v := range v.MatchLabels {
if stringutils.ContainsWildcard(k) || stringutils.ContainsWildcard(v) {
if wildcard.ContainsWildcard(k) || wildcard.ContainsWildcard(v) {
return true
}
}

View file

@ -2,6 +2,8 @@ package wildcard
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestMatch(t *testing.T) {
@ -347,10 +349,8 @@ func TestMatch(t *testing.T) {
},
}
// Iterating over the test cases, call the function under test and asert the output.
for i, testCase := range testCases {
for _, testCase := range testCases {
actualResult := Match(testCase.pattern, testCase.text)
if testCase.matched != actualResult {
t.Errorf("Test %d: Expected the result to be `%v`, but instead found it to be `%v`", i+1, testCase.matched, actualResult)
}
assert.Equal(t, testCase.matched, actualResult)
}
}

View file

@ -1,4 +1,4 @@
package string
package wildcard
import "strings"

View file

@ -0,0 +1,89 @@
package wildcard
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestContainsWildcard(t *testing.T) {
type args struct {
v string
}
tests := []struct {
name string
args args
want bool
}{{
name: "no wildcard",
args: args{
v: "name",
},
want: false,
}, {
name: "empty string",
args: args{
v: "",
},
want: false,
}, {
name: "contains * at the end",
args: args{
v: "name*",
},
want: true,
}, {
name: "contains * at the beginning",
args: args{
v: "*name",
},
want: true,
}, {
name: "contains * in the middle",
args: args{
v: "start*end",
},
want: true,
}, {
name: "only *",
args: args{
v: "*",
},
want: true,
}, {
name: "contains ? at the end",
args: args{
v: "name?",
},
want: true,
}, {
name: "contains ? at the beginning",
args: args{
v: "?name",
},
want: true,
}, {
name: "contains ? in the middle",
args: args{
v: "start?end",
},
want: true,
}, {
name: "only ?",
args: args{
v: "?",
},
want: true,
}, {
name: "both * and ?",
args: args{
v: "*name?",
},
want: true,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, ContainsWildcard(tt.args.v))
})
}
}