Skip to content

Commit 23945af

Browse files
authored
Add unit tests for adoption_reconciler.go (#53)
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.
1 parent 787f953 commit 23945af

File tree

6 files changed

+685
-11
lines changed

6 files changed

+685
-11
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ mocks: install-mockery ## Build mocks
3333
@bin/mockery --quiet --name=ObjectKind --case=underscore --output=mocks/apimachinery/pkg/runtime/schema --dir="$(K8S_APIMACHINERY_DIR)/pkg/runtime/schema"
3434
@echo "ok."
3535
@echo -n "building mocks for sigs.k8s.io/controller-runtime/pkg/client ... "
36-
@bin/mockery --quiet --name="(Client|Status)" --case=underscore --output=mocks/controller-runtime/pkg/client --dir="$(CONTROLLER_RUNTIME_DIR)/pkg/client"
36+
@bin/mockery --quiet --name="(Client|Status|Reader)" --case=underscore --output=mocks/controller-runtime/pkg/client --dir="$(CONTROLLER_RUNTIME_DIR)/pkg/client"
3737
@echo "ok."
3838

3939
help: ## Show this help.

mocks/controller-runtime/pkg/client/reader.go

Lines changed: 55 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mocks/pkg/types/adopted_resource_reconciler.go

Lines changed: 91 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/runtime/adoption_reconciler.go

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,10 @@ func (r *adoptionReconciler) reconcile(req ctrlrt.Request) error {
140140
return nil
141141
}
142142

143-
return r.sync(ctx, targetDescriptor, rm, res)
143+
return r.Sync(ctx, targetDescriptor, rm, res)
144144
}
145145

146-
func (r *adoptionReconciler) sync(
146+
func (r *adoptionReconciler) Sync(
147147
ctx context.Context,
148148
targetDescriptor acktypes.AWSResourceDescriptor,
149149
rm acktypes.AWSResourceManager,
@@ -171,7 +171,10 @@ func (r *adoptionReconciler) sync(
171171
GenerateName: rmo.GetGenerateName(),
172172
}
173173

174-
desiredMetadata := desired.Spec.Kubernetes.Metadata
174+
var desiredMetadata *ackv1alpha1.PartialObjectMeta
175+
if desired.Spec.Kubernetes != nil {
176+
desiredMetadata = desired.Spec.Kubernetes.Metadata
177+
}
175178

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

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

512516
// patchStatus patches the Status for AdoptedResource into k8s. The adopted
513517
// resource 'res' also gets updated with the content returned from apiserver.
514-
// TODO(vijat@): Refactor this and use single 'patchStatus' method
518+
// TODO(vijtrip2): Refactor this and use single 'patchStatus' method
515519
// for reconciler and adoptionReconciler
516520
func (r *adoptionReconciler) patchStatus(
517521
ctx context.Context,
@@ -533,13 +537,31 @@ func NewAdoptionReconciler(
533537
metrics *ackmetrics.Metrics,
534538
cache ackrtcache.Caches,
535539
) acktypes.Reconciler {
540+
return NewAdoptionReconcilerWithClient(sc, log, cfg, metrics, cache, nil, nil)
541+
}
542+
543+
// NewAdoptionReconcilerWithClient returns a new adoptionReconciler object with
544+
// specified k8s client and Reader. Currently this function is used for testing
545+
// purpose only because "adoptionReconciler" struct is not available outside
546+
// 'runtime' package for dependency injection.
547+
func NewAdoptionReconcilerWithClient(
548+
sc acktypes.ServiceController,
549+
log logr.Logger,
550+
cfg ackcfg.Config,
551+
metrics *ackmetrics.Metrics,
552+
cache ackrtcache.Caches,
553+
kc client.Client,
554+
apiReader client.Reader,
555+
) acktypes.AdoptedResourceReconciler {
536556
return &adoptionReconciler{
537557
reconciler: reconciler{
538-
sc: sc,
539-
log: log.WithName("adopted-reconciler"),
540-
cfg: cfg,
541-
metrics: metrics,
542-
cache: cache,
558+
sc: sc,
559+
log: log.WithName("adopted-reconciler"),
560+
cfg: cfg,
561+
metrics: metrics,
562+
cache: cache,
563+
kc: kc,
564+
apiReader: apiReader,
543565
},
544566
}
545567
}

0 commit comments

Comments
 (0)