Skip to content

Commit

Permalink
Add unit tests for adoption_reconciler.go (#53)
Browse files Browse the repository at this point in the history
Issue #, if available: aws-controllers-k8s/community#939

Description of changes:
* Add an interface to test Sync method of adoption_reconciler similar to resource_reconciler
* Write unit tests for Sync method of AdoptionReconciler
* Add a new factory method to allow dependency injection of k8sClient and Reader.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
  • Loading branch information
vijtrip2 authored Sep 16, 2021
1 parent 787f953 commit 23945af
Show file tree
Hide file tree
Showing 6 changed files with 685 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ mocks: install-mockery ## Build mocks
@bin/mockery --quiet --name=ObjectKind --case=underscore --output=mocks/apimachinery/pkg/runtime/schema --dir="$(K8S_APIMACHINERY_DIR)/pkg/runtime/schema"
@echo "ok."
@echo -n "building mocks for sigs.k8s.io/controller-runtime/pkg/client ... "
@bin/mockery --quiet --name="(Client|Status)" --case=underscore --output=mocks/controller-runtime/pkg/client --dir="$(CONTROLLER_RUNTIME_DIR)/pkg/client"
@bin/mockery --quiet --name="(Client|Status|Reader)" --case=underscore --output=mocks/controller-runtime/pkg/client --dir="$(CONTROLLER_RUNTIME_DIR)/pkg/client"
@echo "ok."

help: ## Show this help.
Expand Down
55 changes: 55 additions & 0 deletions mocks/controller-runtime/pkg/client/reader.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 91 additions & 0 deletions mocks/pkg/types/adopted_resource_reconciler.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 32 additions & 10 deletions pkg/runtime/adoption_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ func (r *adoptionReconciler) reconcile(req ctrlrt.Request) error {
return nil
}

return r.sync(ctx, targetDescriptor, rm, res)
return r.Sync(ctx, targetDescriptor, rm, res)
}

func (r *adoptionReconciler) sync(
func (r *adoptionReconciler) Sync(
ctx context.Context,
targetDescriptor acktypes.AWSResourceDescriptor,
rm acktypes.AWSResourceManager,
Expand Down Expand Up @@ -171,7 +171,10 @@ func (r *adoptionReconciler) sync(
GenerateName: rmo.GetGenerateName(),
}

desiredMetadata := desired.Spec.Kubernetes.Metadata
var desiredMetadata *ackv1alpha1.PartialObjectMeta
if desired.Spec.Kubernetes != nil {
desiredMetadata = desired.Spec.Kubernetes.Metadata
}

// Attempt to use metadata values from the adopted resource target metadata
if desiredMetadata != nil {
Expand Down Expand Up @@ -235,6 +238,7 @@ func (r *adoptionReconciler) sync(
}
}

// TODO(vijtrip2@): Should adopted resource be marked as managed earlier ?
if err := r.markManaged(ctx, desired); err != nil {
return r.onError(ctx, desired, err)
}
Expand Down Expand Up @@ -489,7 +493,7 @@ func (r *adoptionReconciler) getRegion(
// patchMetadataAndSpec patches the Metadata and Spec for AdoptedResource into
// k8s. The adopted resource 'res' also gets updated with content returned from
// apiserver.
// TODO(vijat@): Refactor this and use single 'patchMetadataAndSpec' method
// TODO(vijtrip2@): Refactor this and use single 'patchMetadataAndSpec' method
// for reconciler and adoptionReconciler
func (r *adoptionReconciler) patchMetadataAndSpec(
ctx context.Context,
Expand All @@ -511,7 +515,7 @@ func (r *adoptionReconciler) patchMetadataAndSpec(

// patchStatus patches the Status for AdoptedResource into k8s. The adopted
// resource 'res' also gets updated with the content returned from apiserver.
// TODO(vijat@): Refactor this and use single 'patchStatus' method
// TODO(vijtrip2): Refactor this and use single 'patchStatus' method
// for reconciler and adoptionReconciler
func (r *adoptionReconciler) patchStatus(
ctx context.Context,
Expand All @@ -533,13 +537,31 @@ func NewAdoptionReconciler(
metrics *ackmetrics.Metrics,
cache ackrtcache.Caches,
) acktypes.Reconciler {
return NewAdoptionReconcilerWithClient(sc, log, cfg, metrics, cache, nil, nil)
}

// NewAdoptionReconcilerWithClient returns a new adoptionReconciler object with
// specified k8s client and Reader. Currently this function is used for testing
// purpose only because "adoptionReconciler" struct is not available outside
// 'runtime' package for dependency injection.
func NewAdoptionReconcilerWithClient(
sc acktypes.ServiceController,
log logr.Logger,
cfg ackcfg.Config,
metrics *ackmetrics.Metrics,
cache ackrtcache.Caches,
kc client.Client,
apiReader client.Reader,
) acktypes.AdoptedResourceReconciler {
return &adoptionReconciler{
reconciler: reconciler{
sc: sc,
log: log.WithName("adopted-reconciler"),
cfg: cfg,
metrics: metrics,
cache: cache,
sc: sc,
log: log.WithName("adopted-reconciler"),
cfg: cfg,
metrics: metrics,
cache: cache,
kc: kc,
apiReader: apiReader,
},
}
}
Loading

0 comments on commit 23945af

Please sign in to comment.