2014-10-09 20:56:30 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package generic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2014-10-10 22:46:30 +00:00
|
|
|
"reflect"
|
2014-10-09 20:56:30 +00:00
|
|
|
"testing"
|
|
|
|
|
2015-03-06 23:29:03 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
|
2014-10-09 20:56:30 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
|
|
|
)
|
|
|
|
|
2014-10-10 22:46:30 +00:00
|
|
|
type Ignored struct {
|
|
|
|
ID string
|
|
|
|
}
|
|
|
|
|
|
|
|
type IgnoredList struct {
|
|
|
|
Items []Ignored
|
|
|
|
}
|
2014-10-09 20:56:30 +00:00
|
|
|
|
2014-10-10 22:46:30 +00:00
|
|
|
func (*Ignored) IsAnAPIObject() {}
|
|
|
|
func (*IgnoredList) IsAnAPIObject() {}
|
2014-10-09 20:56:30 +00:00
|
|
|
|
|
|
|
func TestSelectionPredicate(t *testing.T) {
|
|
|
|
table := map[string]struct {
|
|
|
|
labelSelector, fieldSelector string
|
2015-03-06 23:29:03 +00:00
|
|
|
labels labels.Set
|
|
|
|
fields fields.Set
|
2014-10-09 20:56:30 +00:00
|
|
|
err error
|
|
|
|
shouldMatch bool
|
|
|
|
}{
|
|
|
|
"A": {
|
|
|
|
labelSelector: "name=foo",
|
|
|
|
fieldSelector: "uid=12345",
|
|
|
|
labels: labels.Set{"name": "foo"},
|
2015-03-06 23:29:03 +00:00
|
|
|
fields: fields.Set{"uid": "12345"},
|
2014-10-09 20:56:30 +00:00
|
|
|
shouldMatch: true,
|
|
|
|
},
|
|
|
|
"B": {
|
|
|
|
labelSelector: "name=foo",
|
|
|
|
fieldSelector: "uid=12345",
|
|
|
|
labels: labels.Set{"name": "foo"},
|
2015-03-06 23:29:03 +00:00
|
|
|
fields: fields.Set{},
|
2014-10-09 20:56:30 +00:00
|
|
|
shouldMatch: false,
|
|
|
|
},
|
|
|
|
"C": {
|
|
|
|
labelSelector: "name=foo",
|
|
|
|
fieldSelector: "uid=12345",
|
|
|
|
labels: labels.Set{},
|
2015-03-06 23:29:03 +00:00
|
|
|
fields: fields.Set{"uid": "12345"},
|
2014-10-09 20:56:30 +00:00
|
|
|
shouldMatch: false,
|
|
|
|
},
|
|
|
|
"error": {
|
|
|
|
labelSelector: "name=foo",
|
|
|
|
fieldSelector: "uid=12345",
|
|
|
|
err: errors.New("maybe this is a 'wrong object type' error"),
|
|
|
|
shouldMatch: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, item := range table {
|
2015-02-25 16:19:10 +00:00
|
|
|
parsedLabel, err := labels.Parse(item.labelSelector)
|
2014-10-09 20:56:30 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2015-03-06 23:29:03 +00:00
|
|
|
parsedField, err := fields.ParseSelector(item.fieldSelector)
|
2014-10-09 20:56:30 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
sp := &SelectionPredicate{
|
|
|
|
Label: parsedLabel,
|
|
|
|
Field: parsedField,
|
2015-03-06 23:29:03 +00:00
|
|
|
GetAttrs: func(runtime.Object) (label labels.Set, field fields.Set, err error) {
|
2014-10-09 20:56:30 +00:00
|
|
|
return item.labels, item.fields, item.err
|
|
|
|
},
|
|
|
|
}
|
|
|
|
got, err := sp.Matches(&Ignored{})
|
|
|
|
if e, a := item.err, err; e != a {
|
|
|
|
t.Errorf("%v: expected %v, got %v", name, e, a)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if e, a := item.shouldMatch, got; e != a {
|
|
|
|
t.Errorf("%v: expected %v, got %v", name, e, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-10 22:46:30 +00:00
|
|
|
|
|
|
|
func TestFilterList(t *testing.T) {
|
|
|
|
try := &IgnoredList{
|
|
|
|
Items: []Ignored{
|
|
|
|
{"foo"},
|
|
|
|
{"bar"},
|
|
|
|
{"baz"},
|
|
|
|
{"qux"},
|
|
|
|
{"zot"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
expect := &IgnoredList{
|
|
|
|
Items: []Ignored{
|
|
|
|
{"bar"},
|
|
|
|
{"baz"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}),
|
2015-02-11 23:35:05 +00:00
|
|
|
nil,
|
2014-10-10 22:46:30 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if e, a := expect, got; !reflect.DeepEqual(e, a) {
|
|
|
|
t.Errorf("Expected %#v, got %#v", e, a)
|
|
|
|
}
|
|
|
|
}
|