New tests for non-deletion scenarios added to Managed Reconciler.
Signed-off-by: Muvaffak Onus <onus.muvaffak@gmail.com>
This commit is contained in:
parent
542990b820
commit
19daef9388
|
@ -79,6 +79,14 @@ type ManagedEstablisher interface {
|
|||
Establish(ctx context.Context, mg Managed) error
|
||||
}
|
||||
|
||||
// ManagedEstablisherFn is the pluggable struct to produce objects with ManagedEstablisher interface.
|
||||
type ManagedEstablisherFn func(ctx context.Context, mg Managed) error
|
||||
|
||||
// Establish calls ManagedEstablisherFn function.
|
||||
func (m ManagedEstablisherFn) Establish(ctx context.Context, mg Managed) error {
|
||||
return m(ctx, mg)
|
||||
}
|
||||
|
||||
// An ExternalConnecter produces a new ExternalClient given the supplied
|
||||
// Managed resource.
|
||||
type ExternalConnecter interface {
|
||||
|
@ -119,8 +127,8 @@ type ExternalClient interface {
|
|||
Delete(ctx context.Context, mg Managed) error
|
||||
}
|
||||
|
||||
// ExternalClientFn is the pluggable struct to produce an ExternalClient from given functions.
|
||||
type ExternalClientFn struct {
|
||||
// ExternalClientFns is the pluggable struct to produce an ExternalClient from given functions.
|
||||
type ExternalClientFns struct {
|
||||
ObserveFn func(ctx context.Context, mg Managed) (ExternalObservation, error)
|
||||
CreateFn func(ctx context.Context, mg Managed) (ExternalCreation, error)
|
||||
UpdateFn func(ctx context.Context, mg Managed) (ExternalUpdate, error)
|
||||
|
@ -128,22 +136,22 @@ type ExternalClientFn struct {
|
|||
}
|
||||
|
||||
// Observe calls plugged ObserveFn function.
|
||||
func (e ExternalClientFn) Observe(ctx context.Context, mg Managed) (ExternalObservation, error) {
|
||||
func (e ExternalClientFns) Observe(ctx context.Context, mg Managed) (ExternalObservation, error) {
|
||||
return e.ObserveFn(ctx, mg)
|
||||
}
|
||||
|
||||
// Create calls plugged CreateFn function.
|
||||
func (e ExternalClientFn) Create(ctx context.Context, mg Managed) (ExternalCreation, error) {
|
||||
func (e ExternalClientFns) Create(ctx context.Context, mg Managed) (ExternalCreation, error) {
|
||||
return e.CreateFn(ctx, mg)
|
||||
}
|
||||
|
||||
// Update calls plugged UpdateFn function.
|
||||
func (e ExternalClientFn) Update(ctx context.Context, mg Managed) (ExternalUpdate, error) {
|
||||
func (e ExternalClientFns) Update(ctx context.Context, mg Managed) (ExternalUpdate, error) {
|
||||
return e.UpdateFn(ctx, mg)
|
||||
}
|
||||
|
||||
// Delete calls plugged DeleteFn function.
|
||||
func (e ExternalClientFn) Delete(ctx context.Context, mg Managed) error { return e.DeleteFn(ctx, mg) }
|
||||
func (e ExternalClientFns) Delete(ctx context.Context, mg Managed) error { return e.DeleteFn(ctx, mg) }
|
||||
|
||||
// A NopConnecter does nothing.
|
||||
type NopConnecter struct{}
|
||||
|
|
|
@ -57,6 +57,11 @@ func TestManagedReconciler(t *testing.T) {
|
|||
errBoom := errors.New("boom")
|
||||
//errUnexpected := errors.New("unexpected object type")
|
||||
now := metav1.Now()
|
||||
testFinalizers := []string{"finalizer.crossplane.io"}
|
||||
testConnectionDetails := ConnectionDetails{
|
||||
"username": []byte("crossplane.io"),
|
||||
"password": []byte("open-cloud"),
|
||||
}
|
||||
|
||||
cases := map[string]struct {
|
||||
args args
|
||||
|
@ -117,7 +122,7 @@ func TestManagedReconciler(t *testing.T) {
|
|||
},
|
||||
s: MockSchemeWith(&MockManaged{}),
|
||||
},
|
||||
e: &ExternalClientFn{
|
||||
e: &ExternalClientFns{
|
||||
ObserveFn: func(ctx context.Context, mg Managed) (observation ExternalObservation, e error) {
|
||||
return ExternalObservation{}, errBoom
|
||||
},
|
||||
|
@ -145,7 +150,7 @@ func TestManagedReconciler(t *testing.T) {
|
|||
},
|
||||
s: MockSchemeWith(&MockManaged{}),
|
||||
},
|
||||
e: &ExternalClientFn{
|
||||
e: &ExternalClientFns{
|
||||
ObserveFn: func(ctx context.Context, mg Managed) (observation ExternalObservation, e error) {
|
||||
mg.SetDeletionTimestamp(&now)
|
||||
mg.SetReclaimPolicy(v1alpha1.ReclaimDelete)
|
||||
|
@ -180,7 +185,7 @@ func TestManagedReconciler(t *testing.T) {
|
|||
},
|
||||
s: MockSchemeWith(&MockManaged{}),
|
||||
},
|
||||
e: &ExternalClientFn{
|
||||
e: &ExternalClientFns{
|
||||
ObserveFn: func(ctx context.Context, mg Managed) (observation ExternalObservation, e error) {
|
||||
mg.SetDeletionTimestamp(&now)
|
||||
mg.SetReclaimPolicy(v1alpha1.ReclaimRetain)
|
||||
|
@ -228,7 +233,7 @@ func TestManagedReconciler(t *testing.T) {
|
|||
},
|
||||
s: MockSchemeWith(&MockManaged{}),
|
||||
},
|
||||
e: &ExternalClientFn{
|
||||
e: &ExternalClientFns{
|
||||
ObserveFn: func(ctx context.Context, mg Managed) (observation ExternalObservation, e error) {
|
||||
mg.SetDeletionTimestamp(&now)
|
||||
return ExternalObservation{
|
||||
|
@ -256,6 +261,104 @@ func TestManagedReconciler(t *testing.T) {
|
|||
},
|
||||
want: want{result: reconcile.Result{RequeueAfter: defaultManagedShortWait}},
|
||||
},
|
||||
"ResourceDoesNotExist": {
|
||||
args: args{
|
||||
m: &MockManager{
|
||||
c: &test.MockClient{
|
||||
MockGet: test.NewMockGetFn(nil),
|
||||
MockStatusUpdate: test.MockStatusUpdateFn(func(ctx context.Context, obj runtime.Object) error {
|
||||
want := &MockManaged{}
|
||||
want.SetConditions(v1alpha1.ReconcileSuccess())
|
||||
want.SetFinalizers(testFinalizers)
|
||||
if diff := cmp.Diff(want, obj, test.EquateConditions()); diff != "" {
|
||||
t.Errorf("-want, +got:\n%s", diff)
|
||||
}
|
||||
return nil
|
||||
}),
|
||||
},
|
||||
s: MockSchemeWith(&MockManaged{}),
|
||||
},
|
||||
mg: ManagedKind(MockGVK(&MockManaged{})),
|
||||
e: &ExternalClientFns{
|
||||
ObserveFn: func(ctx context.Context, mg Managed) (ExternalObservation, error) {
|
||||
return ExternalObservation{
|
||||
ResourceExists: false,
|
||||
}, nil
|
||||
},
|
||||
CreateFn: func(ctx context.Context, mg Managed) (ExternalCreation, error) {
|
||||
return ExternalCreation{
|
||||
ConnectionDetails: testConnectionDetails,
|
||||
}, nil
|
||||
},
|
||||
},
|
||||
o: []ManagedReconcilerOption{
|
||||
func(r *ManagedReconciler) {
|
||||
r.managed.ManagedConnectionPublisher = ManagedConnectionPublisherFns{
|
||||
PublishConnectionFn: func(ctx context.Context, mg Managed, c ConnectionDetails) error {
|
||||
if len(c) != 0 {
|
||||
if diff := cmp.Diff(testConnectionDetails, c); diff != "" {
|
||||
t.Errorf("-want, +got:\n%s", diff)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
},
|
||||
func(r *ManagedReconciler) {
|
||||
r.managed.ManagedEstablisher = ManagedEstablisherFn(func(ctx context.Context, mg Managed) error {
|
||||
mg.SetFinalizers(testFinalizers)
|
||||
return nil
|
||||
})
|
||||
},
|
||||
},
|
||||
},
|
||||
want: want{result: reconcile.Result{RequeueAfter: defaultManagedShortWait}},
|
||||
},
|
||||
"ResourceExists": {
|
||||
args: args{
|
||||
m: &MockManager{
|
||||
c: &test.MockClient{
|
||||
MockGet: test.NewMockGetFn(nil),
|
||||
MockStatusUpdate: test.MockStatusUpdateFn(func(ctx context.Context, obj runtime.Object) error {
|
||||
want := &MockManaged{}
|
||||
want.SetConditions(v1alpha1.ReconcileSuccess())
|
||||
if diff := cmp.Diff(want, obj, test.EquateConditions()); diff != "" {
|
||||
t.Errorf("-want, +got:\n%s", diff)
|
||||
}
|
||||
return nil
|
||||
}),
|
||||
},
|
||||
s: MockSchemeWith(&MockManaged{}),
|
||||
},
|
||||
mg: ManagedKind(MockGVK(&MockManaged{})),
|
||||
e: &ExternalClientFns{
|
||||
ObserveFn: func(ctx context.Context, mg Managed) (observation ExternalObservation, e error) {
|
||||
return ExternalObservation{
|
||||
ResourceExists: true,
|
||||
ConnectionDetails: testConnectionDetails,
|
||||
}, nil
|
||||
},
|
||||
UpdateFn: func(ctx context.Context, mg Managed) (update ExternalUpdate, e error) {
|
||||
return ExternalUpdate{}, nil
|
||||
},
|
||||
},
|
||||
o: []ManagedReconcilerOption{
|
||||
func(r *ManagedReconciler) {
|
||||
r.managed.ManagedConnectionPublisher = ManagedConnectionPublisherFns{
|
||||
PublishConnectionFn: func(ctx context.Context, mg Managed, c ConnectionDetails) error {
|
||||
if len(c) != 0 {
|
||||
if diff := cmp.Diff(testConnectionDetails, c); diff != "" {
|
||||
t.Errorf("-want, +got:\n%s", diff)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
want: want{result: reconcile.Result{RequeueAfter: defaultManagedLongWait}},
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range cases {
|
||||
|
|
Loading…
Reference in New Issue