Merge pull request #100964 from njuptlzf/SelectionPredicateUT
Add more unit tests for SelectionPredicate Kubernetes-commit: 90e599f56a931f9ba32244c7f6250db27cb61af5
This commit is contained in:
commit
f48391aefe
|
|
@ -716,7 +716,7 @@
|
|||
},
|
||||
{
|
||||
"ImportPath": "k8s.io/client-go",
|
||||
"Rev": "bfec2914ea57"
|
||||
"Rev": "4e3d56081793"
|
||||
},
|
||||
{
|
||||
"ImportPath": "k8s.io/component-base",
|
||||
|
|
|
|||
4
go.mod
4
go.mod
|
|
@ -43,7 +43,7 @@ require (
|
|||
gopkg.in/square/go-jose.v2 v2.2.2
|
||||
k8s.io/api v0.0.0-20210518101553-e993e0004b6d
|
||||
k8s.io/apimachinery v0.0.0-20210518100448-37b6f01a7c3f
|
||||
k8s.io/client-go v0.0.0-20210518102847-bfec2914ea57
|
||||
k8s.io/client-go v0.0.0-20210518102850-4e3d56081793
|
||||
k8s.io/component-base v0.0.0-20210518111221-460e8c72807d
|
||||
k8s.io/klog/v2 v2.8.0
|
||||
k8s.io/kube-openapi v0.0.0-20210323165736-1a6458611d18
|
||||
|
|
@ -56,6 +56,6 @@ require (
|
|||
replace (
|
||||
k8s.io/api => k8s.io/api v0.0.0-20210518101553-e993e0004b6d
|
||||
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20210518100448-37b6f01a7c3f
|
||||
k8s.io/client-go => k8s.io/client-go v0.0.0-20210518102847-bfec2914ea57
|
||||
k8s.io/client-go => k8s.io/client-go v0.0.0-20210518102850-4e3d56081793
|
||||
k8s.io/component-base => k8s.io/component-base v0.0.0-20210518111221-460e8c72807d
|
||||
)
|
||||
|
|
|
|||
4
go.sum
4
go.sum
|
|
@ -609,8 +609,8 @@ k8s.io/api v0.0.0-20210518101553-e993e0004b6d h1:e3ARFj2dj320BWQAzmiUNdac9VcuBI3
|
|||
k8s.io/api v0.0.0-20210518101553-e993e0004b6d/go.mod h1:sTK/5ng6+OlzD6jxX0VKLMj1mt5pOWF1kjIREvzieZc=
|
||||
k8s.io/apimachinery v0.0.0-20210518100448-37b6f01a7c3f h1:XUj108KM/gDEMEQouuiwJfAxuFqVkZG21tGwnMvwgRk=
|
||||
k8s.io/apimachinery v0.0.0-20210518100448-37b6f01a7c3f/go.mod h1:exP6IuvfFJEujWInqgHSgUSbuoVRIoONGdyLzbFWRXE=
|
||||
k8s.io/client-go v0.0.0-20210518102847-bfec2914ea57 h1:W14X2Oun19RVH1iCCgrFOxPwvAQP+jiWVUKSuHLo1bs=
|
||||
k8s.io/client-go v0.0.0-20210518102847-bfec2914ea57/go.mod h1:JDgVKdpc4hwcNiCI83RRlQS4QiTIem8RpXsoMuv7MLY=
|
||||
k8s.io/client-go v0.0.0-20210518102850-4e3d56081793 h1:ALEZoTxdNH8cAVcjEMXDIr2lrSiULYy1OFW2+zieqnI=
|
||||
k8s.io/client-go v0.0.0-20210518102850-4e3d56081793/go.mod h1:JDgVKdpc4hwcNiCI83RRlQS4QiTIem8RpXsoMuv7MLY=
|
||||
k8s.io/component-base v0.0.0-20210518111221-460e8c72807d h1:Y43xlmyUx2g5Ry43BRXoKfbMfIZJcrV0Zwe0CGLLkzo=
|
||||
k8s.io/component-base v0.0.0-20210518111221-460e8c72807d/go.mod h1:a9irTREgkp0RoIdWNQYfznZKYrgz6UpEb0idO0wA7Ss=
|
||||
k8s.io/gengo v0.0.0-20200413195148-3a45101e95ac/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ package storage
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"k8s.io/apimachinery/pkg/fields"
|
||||
|
|
@ -112,6 +113,10 @@ func TestSelectionPredicate(t *testing.T) {
|
|||
if e, a := item.shouldMatch, got; e != a {
|
||||
t.Errorf("%v: expected %v, got %v", name, e, a)
|
||||
}
|
||||
got = sp.MatchesObjectAttributes(item.labels, item.fields)
|
||||
if e, a := item.shouldMatch, got; e != a {
|
||||
t.Errorf("%v: expected %v, got %v", name, e, a)
|
||||
}
|
||||
if key := item.matchSingleKey; key != "" {
|
||||
got, ok := sp.MatchesSingle()
|
||||
if !ok {
|
||||
|
|
@ -123,3 +128,83 @@ func TestSelectionPredicate(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSelectionPredicateMatcherIndex(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
labelSelector, fieldSelector string
|
||||
indexLabels []string
|
||||
indexFields []string
|
||||
expected []MatchValue
|
||||
}{
|
||||
"Match nil": {
|
||||
labelSelector: "name=foo",
|
||||
fieldSelector: "uid=12345",
|
||||
indexLabels: []string{"bar"},
|
||||
indexFields: []string{},
|
||||
expected: nil,
|
||||
},
|
||||
"Match field": {
|
||||
labelSelector: "name=foo",
|
||||
fieldSelector: "uid=12345",
|
||||
indexLabels: []string{},
|
||||
indexFields: []string{"uid"},
|
||||
expected: []MatchValue{{IndexName: FieldIndex("uid"), Value: "12345"}},
|
||||
},
|
||||
"Match label": {
|
||||
labelSelector: "name=foo",
|
||||
fieldSelector: "uid=12345",
|
||||
indexLabels: []string{"name"},
|
||||
indexFields: []string{},
|
||||
expected: []MatchValue{{IndexName: LabelIndex("name"), Value: "foo"}},
|
||||
},
|
||||
"Match field and label": {
|
||||
labelSelector: "name=foo",
|
||||
fieldSelector: "uid=12345",
|
||||
indexLabels: []string{"name"},
|
||||
indexFields: []string{"uid"},
|
||||
expected: []MatchValue{{IndexName: FieldIndex("uid"), Value: "12345"}, {IndexName: LabelIndex("name"), Value: "foo"}},
|
||||
},
|
||||
"Negative match field and label": {
|
||||
labelSelector: "name!=foo",
|
||||
fieldSelector: "uid!=12345",
|
||||
indexLabels: []string{"name"},
|
||||
indexFields: []string{"uid"},
|
||||
expected: nil,
|
||||
},
|
||||
"Negative match field and match label": {
|
||||
labelSelector: "name=foo",
|
||||
fieldSelector: "uid!=12345",
|
||||
indexLabels: []string{"name"},
|
||||
indexFields: []string{"uid"},
|
||||
expected: []MatchValue{{IndexName: LabelIndex("name"), Value: "foo"}},
|
||||
},
|
||||
"Negative match label and match field": {
|
||||
labelSelector: "name!=foo",
|
||||
fieldSelector: "uid=12345",
|
||||
indexLabels: []string{"name"},
|
||||
indexFields: []string{"uid"},
|
||||
expected: []MatchValue{{IndexName: FieldIndex("uid"), Value: "12345"}},
|
||||
},
|
||||
}
|
||||
for name, testCase := range testCases {
|
||||
parsedLabel, err := labels.Parse(testCase.labelSelector)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
parsedField, err := fields.ParseSelector(testCase.fieldSelector)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
sp := &SelectionPredicate{
|
||||
Label: parsedLabel,
|
||||
Field: parsedField,
|
||||
IndexLabels: testCase.indexLabels,
|
||||
IndexFields: testCase.indexFields,
|
||||
}
|
||||
actual := sp.MatcherIndex()
|
||||
if !reflect.DeepEqual(testCase.expected, actual) {
|
||||
t.Errorf("%v: expected %v, got %v", name, testCase.expected, actual)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue