Merge pull request #31 from negz/somanypredicates

Add an IsManagedKind watch predicate.
This commit is contained in:
Nic Cope 2019-09-18 17:29:09 -07:00 committed by GitHub
commit d8050430d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 153 additions and 9 deletions

View File

@ -70,8 +70,20 @@ func HasManagedResourceReferenceKind(k ManagedKind) PredicateFn {
}
}
// IsManagedKind accepts objects that are of the supplied managed resource kind.
func IsManagedKind(k ManagedKind, ot runtime.ObjectTyper) PredicateFn {
return func(obj runtime.Object) bool {
gvk, err := GetKind(obj, ot)
if err != nil {
return false
}
return gvk == schema.GroupVersionKind(k)
}
}
// HasDirectClassReferenceKind accepts objects that reference the supplied
// non-portable class kind directly.
// non-portable class kind directly. It is deprecated - use IsManagedKind
// instead.
func HasDirectClassReferenceKind(k NonPortableClassKind) PredicateFn {
return func(obj runtime.Object) bool {
r, ok := obj.(NonPortableClassReferencer)

View File

@ -23,6 +23,7 @@ import (
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/crossplaneio/crossplane-runtime/pkg/test"
@ -104,6 +105,41 @@ func TestHasManagedResourceReferenceKind(t *testing.T) {
})
}
}
func TestIsManagedKind(t *testing.T) {
cases := map[string]struct {
kind ManagedKind
ot runtime.ObjectTyper
obj runtime.Object
want bool
}{
"IsKind": {
kind: ManagedKind(MockGVK(&MockManaged{})),
ot: MockTyper{GVKs: []schema.GroupVersionKind{MockGVK(&MockManaged{})}},
want: true,
},
"IsNotKind": {
kind: ManagedKind(MockGVK(&MockManaged{})),
ot: MockTyper{GVKs: []schema.GroupVersionKind{MockGVK(&MockClaim{})}},
want: false,
},
"ErrorDeterminingKind": {
kind: ManagedKind(MockGVK(&MockManaged{})),
ot: MockTyper{Error: errors.New("boom")},
want: false,
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
got := IsManagedKind(tc.kind, tc.ot)(tc.obj)
if diff := cmp.Diff(tc.want, got); diff != "" {
t.Errorf("IsManagedKind(...): -want, +got:\n%s", diff)
}
})
}
}
func TestHasDirectClassReferenceKind(t *testing.T) {
cases := map[string]struct {
obj runtime.Object

View File

@ -57,21 +57,32 @@ func MustCreateObject(kind schema.GroupVersionKind, oc runtime.ObjectCreater) ru
return obj
}
// GetKind returns the GroupVersionKind of the supplied object. It return an
// error if the object is unknown to the supplied ObjectTyper, the object is
// unversioned, or the object does not have exactly one registered kind.
func GetKind(obj runtime.Object, ot runtime.ObjectTyper) (schema.GroupVersionKind, error) {
kinds, unversioned, err := ot.ObjectKinds(obj)
if err != nil {
return schema.GroupVersionKind{}, errors.Wrap(err, "cannot get kind of supplied object")
}
if unversioned {
return schema.GroupVersionKind{}, errors.New("supplied object is unversioned")
}
if len(kinds) != 1 {
return schema.GroupVersionKind{}, errors.New("supplied object does not have exactly one kind")
}
return kinds[0], nil
}
// MustGetKind returns the GroupVersionKind of the supplied object. It panics if
// the object is unknown to the supplied ObjectTyper, the object is unversioned,
// or the object does not have exactly one registered kind.
func MustGetKind(obj runtime.Object, ot runtime.ObjectTyper) schema.GroupVersionKind {
kinds, unversioned, err := ot.ObjectKinds(obj)
if unversioned {
panic("supplied object is unversioned")
}
gvk, err := GetKind(obj, ot)
if err != nil {
panic(err)
}
if len(kinds) != 1 {
panic("supplied ")
}
return kinds[0]
return gvk
}
// An ErrorIs function returns true if an error satisfies a particular condition.

View File

@ -101,6 +101,91 @@ func TestConnectionSecretFor(t *testing.T) {
}
}
type MockTyper struct {
GVKs []schema.GroupVersionKind
Unversioned bool
Error error
}
func (t MockTyper) ObjectKinds(_ runtime.Object) ([]schema.GroupVersionKind, bool, error) {
return t.GVKs, t.Unversioned, t.Error
}
func (t MockTyper) Recognizes(_ schema.GroupVersionKind) bool { return true }
func TestGetKind(t *testing.T) {
type args struct {
obj runtime.Object
ot runtime.ObjectTyper
}
type want struct {
kind schema.GroupVersionKind
err error
}
errBoom := errors.New("boom")
cases := map[string]struct {
args args
want want
}{
"KindFound": {
args: args{
ot: MockTyper{GVKs: []schema.GroupVersionKind{MockGVK(&MockManaged{})}},
},
want: want{
kind: MockGVK(&MockManaged{}),
},
},
"KindError": {
args: args{
ot: MockTyper{Error: errBoom},
},
want: want{
err: errors.Wrap(errBoom, "cannot get kind of supplied object"),
},
},
"KindIsUnversioned": {
args: args{
ot: MockTyper{Unversioned: true},
},
want: want{
err: errors.New("supplied object is unversioned"),
},
},
"NotEnoughKinds": {
args: args{
ot: MockTyper{},
},
want: want{
err: errors.New("supplied object does not have exactly one kind"),
},
},
"TooManyKinds": {
args: args{
ot: MockTyper{GVKs: []schema.GroupVersionKind{
MockGVK(&MockClaim{}),
MockGVK(&MockManaged{}),
}},
},
want: want{
err: errors.New("supplied object does not have exactly one kind"),
},
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
got, err := GetKind(tc.args.obj, tc.args.ot)
if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" {
t.Errorf("GetKind(...): -want error, +got error:\n%s", diff)
}
if diff := cmp.Diff(tc.want.kind, got); diff != "" {
t.Errorf("GetKind(...): -want, +got:\n%s", diff)
}
})
}
}
func TestMustCreateObject(t *testing.T) {
type args struct {
kind schema.GroupVersionKind