mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-11-12 05:08:50 +00:00
Single-key matching behavior in generic.Matcher
This commit is contained in:
@@ -44,6 +44,7 @@ func TestSelectionPredicate(t *testing.T) {
|
||||
fields fields.Set
|
||||
err error
|
||||
shouldMatch bool
|
||||
matchSingleKey string
|
||||
}{
|
||||
"A": {
|
||||
labelSelector: "name=foo",
|
||||
@@ -66,6 +67,13 @@ func TestSelectionPredicate(t *testing.T) {
|
||||
fields: fields.Set{"uid": "12345"},
|
||||
shouldMatch: false,
|
||||
},
|
||||
"D": {
|
||||
fieldSelector: "name=12345",
|
||||
labels: labels.Set{},
|
||||
fields: fields.Set{"name": "12345"},
|
||||
shouldMatch: true,
|
||||
matchSingleKey: "12345",
|
||||
},
|
||||
"error": {
|
||||
labelSelector: "name=foo",
|
||||
fieldSelector: "uid=12345",
|
||||
@@ -98,6 +106,15 @@ func TestSelectionPredicate(t *testing.T) {
|
||||
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 {
|
||||
t.Errorf("%v: expected single match", name)
|
||||
}
|
||||
if e, a := key, got; e != a {
|
||||
t.Errorf("%v: expected %v, got %v", name, e, a)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,16 +135,18 @@ func TestFilterList(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
got, err := FilterList(try,
|
||||
MatcherFunc(func(obj runtime.Object) (bool, error) {
|
||||
i, ok := obj.(*Ignored)
|
||||
if !ok {
|
||||
return false, errors.New("wrong type")
|
||||
}
|
||||
return i.ID[0] == 'b', nil
|
||||
}),
|
||||
nil,
|
||||
)
|
||||
m := MatcherFunc(func(obj runtime.Object) (bool, error) {
|
||||
i, ok := obj.(*Ignored)
|
||||
if !ok {
|
||||
return false, errors.New("wrong type")
|
||||
}
|
||||
return i.ID[0] == 'b', nil
|
||||
})
|
||||
if _, matchesSingleObject := m.MatchesSingle(); matchesSingleObject {
|
||||
t.Errorf("matcher unexpectedly matches only a single object.")
|
||||
}
|
||||
|
||||
got, err := FilterList(try, m, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error %v", err)
|
||||
}
|
||||
@@ -136,3 +155,14 @@ func TestFilterList(t *testing.T) {
|
||||
t.Errorf("Expected %#v, got %#v", e, a)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSingleMatch(t *testing.T) {
|
||||
m := MatchOnKey("pod-name-here", func(obj runtime.Object) (bool, error) { return true, nil })
|
||||
got, ok := m.MatchesSingle()
|
||||
if !ok {
|
||||
t.Errorf("Expected MatchesSingle to return true")
|
||||
}
|
||||
if e, a := "pod-name-here", got; e != a {
|
||||
t.Errorf("Expected %#v, got %#v", e, a)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user