Add more test code for SelectionPredicate

Kubernetes-commit: 5468db05f0ca33e78ebf96420281097d28971140
This commit is contained in:
njuptlzf 2021-04-09 22:28:51 +08:00 committed by Kubernetes Publisher
parent b9ad7382f7
commit b86a0eee08
1 changed files with 85 additions and 0 deletions

View File

@ -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)
}
}
}