@@ -20,8 +20,10 @@ import (
2020	"time" 
2121
2222	"github.com/stretchr/testify/assert" 
23+ 	"github.com/stretchr/testify/mock" 
2324	"github.com/stretchr/testify/require" 
2425	"go.uber.org/zap/zapcore" 
26+ 	corev1 "k8s.io/api/core/v1" 
2527	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 
2628	k8sobj "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 
2729	k8srtschema "k8s.io/apimachinery/pkg/runtime/schema" 
@@ -30,7 +32,9 @@ import (
3032
3133	ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1" 
3234	ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare" 
35+ 	"github.com/aws-controllers-k8s/runtime/pkg/condition" 
3336	ackcfg "github.com/aws-controllers-k8s/runtime/pkg/config" 
37+ 	ackerr "github.com/aws-controllers-k8s/runtime/pkg/errors" 
3438	ackmetrics "github.com/aws-controllers-k8s/runtime/pkg/metrics" 
3539	"github.com/aws-controllers-k8s/runtime/pkg/requeue" 
3640	ackrt "github.com/aws-controllers-k8s/runtime/pkg/runtime" 
@@ -96,10 +100,20 @@ func reconcilerMocks(
96100	), kc 
97101}
98102
103+ func  managedResourceManagerFactoryMocks (
104+ 	desired  acktypes.AWSResource ,
105+ 	latest  acktypes.AWSResource ,
106+ ) (
107+ 	* ackmocks.AWSResourceManagerFactory ,
108+ 	* ackmocks.AWSResourceDescriptor ,
109+ ) {
110+ 	return  managerFactoryMocks (desired , latest , true )
111+ }
112+ 
99113func  managerFactoryMocks (
100114	desired  acktypes.AWSResource ,
101115	latest  acktypes.AWSResource ,
102- 	delta   * ackcompare. Delta ,
116+ 	isManaged   bool ,
103117) (
104118	* ackmocks.AWSResourceManagerFactory ,
105119	* ackmocks.AWSResourceDescriptor ,
@@ -114,7 +128,7 @@ func managerFactoryMocks(
114128	rd .On ("EmptyRuntimeObject" ).Return (
115129		& fakeBook {},
116130	)
117- 	rd .On ("IsManaged" , desired ).Return (true )
131+ 	rd .On ("IsManaged" , latest ).Return (isManaged )
118132
119133	rmf  :=  & ackmocks.AWSResourceManagerFactory {}
120134	rmf .On ("ResourceDescriptor" ).Return (rd )
@@ -150,7 +164,7 @@ func TestReconcilerUpdate(t *testing.T) {
150164		latest , nil ,
151165	)
152166
153- 	rmf , rd  :=  managerFactoryMocks (desired , latest ,  delta )
167+ 	rmf , rd  :=  managedResourceManagerFactoryMocks (desired , latest )
154168	rd .On ("Delta" , desired , latest ).Return (
155169		delta ,
156170	).Once ()
@@ -201,7 +215,7 @@ func TestReconcilerUpdate_PatchMetadataAndSpec_DiffInMetadata(t *testing.T) {
201215	// Note the change in annotations 
202216	latestMetaObj .SetAnnotations (map [string ]string {"a" : "b" })
203217
204- 	rmf , rd  :=  managerFactoryMocks (desired , latest ,  delta )
218+ 	rmf , rd  :=  managedResourceManagerFactoryMocks (desired , latest )
205219	rd .On ("Delta" , desired , latest ).Return (
206220		delta ,
207221	).Once ()
@@ -251,7 +265,7 @@ func TestReconcilerUpdate_PatchMetadataAndSpec_DiffInSpec(t *testing.T) {
251265	latest .On ("Conditions" ).Return ([]* ackv1alpha1.Condition {})
252266	// Note no change to metadata... 
253267
254- 	rmf , rd  :=  managerFactoryMocks (desired , latest ,  delta )
268+ 	rmf , rd  :=  managedResourceManagerFactoryMocks (desired , latest )
255269	rd .On ("Delta" , desired , latest ).Return (
256270		delta ,
257271	)
@@ -301,7 +315,7 @@ func TestReconcilerHandleReconcilerError_PatchStatus_Latest(t *testing.T) {
301315
302316	latestMetaObj .SetAnnotations (map [string ]string {"a" : "b" })
303317
304- 	rmf , _  :=  managerFactoryMocks (desired , latest ,  delta )
318+ 	rmf , _  :=  managedResourceManagerFactoryMocks (desired , latest )
305319	r , kc  :=  reconcilerMocks (rmf )
306320
307321	statusWriter  :=  & ctrlrtclientmock.StatusWriter {}
@@ -325,7 +339,7 @@ func TestReconcilerHandleReconcilerError_NoPatchStatus_NoLatest(t *testing.T) {
325339
326340	desired , _ , _  :=  resourceMocks ()
327341
328- 	rmf , _  :=  managerFactoryMocks (desired ,  nil , nil )
342+ 	rmf , _  :=  managedResourceManagerFactoryMocks (desired , nil )
329343	r , kc  :=  reconcilerMocks (rmf )
330344
331345	statusWriter  :=  & ctrlrtclientmock.StatusWriter {}
@@ -368,7 +382,7 @@ func TestReconcilerUpdate_ErrorInLateInitialization(t *testing.T) {
368382		latest , nil ,
369383	)
370384
371- 	rmf , rd  :=  managerFactoryMocks (desired , latest ,  delta )
385+ 	rmf , rd  :=  managedResourceManagerFactoryMocks (desired , latest )
372386	rd .On ("Delta" , desired , latest ).Return (
373387		delta ,
374388	).Once ()
@@ -393,3 +407,62 @@ func TestReconcilerUpdate_ErrorInLateInitialization(t *testing.T) {
393407	kc .AssertNotCalled (t , "Patch" , ctx , latestRTObj , client .MergeFrom (desiredRTObj ))
394408	rm .AssertCalled (t , "LateInitialize" , ctx , latest )
395409}
410+ 
411+ func  TestReconcilerUpdate_ResourceNotManaged (t  * testing.T ) {
412+ 	require  :=  require .New (t )
413+ 	assert  :=  assert .New (t )
414+ 
415+ 	ctx  :=  context .TODO ()
416+ 	arn  :=  ackv1alpha1 .AWSResourceName ("mybook-arn" )
417+ 
418+ 	delta  :=  ackcompare .NewDelta ()
419+ 
420+ 	desired , _ , _  :=  resourceMocks ()
421+ 
422+ 	ids  :=  & ackmocks.AWSResourceIdentifiers {}
423+ 	ids .On ("ARN" ).Return (& arn )
424+ 
425+ 	latest , _ , _  :=  resourceMocks ()
426+ 	latest .On ("Identifiers" ).Return (ids )
427+ 	latest .On ("Conditions" ).Return ([]* ackv1alpha1.Condition {})
428+ 
429+ 	terminalCondition  :=  ackv1alpha1.Condition {
430+ 		Type :    ackv1alpha1 .ConditionTypeTerminal ,
431+ 		Status :  corev1 .ConditionTrue ,
432+ 		Reason :  & condition .NotManagedReason ,
433+ 		Message : & condition .NotManagedMessage ,
434+ 	}
435+ 	latest .On ("ReplaceConditions" , mock .AnythingOfType ("[]*v1alpha1.Condition" )).Return ([]* ackv1alpha1.Condition {& terminalCondition }).Run (func (args  mock.Arguments ) {
436+ 		conditions  :=  args .Get (0 ).([]* ackv1alpha1.Condition )
437+ 		hasTerminal  :=  false 
438+ 		for  _ , condition  :=  range  conditions  {
439+ 			if  condition .Type  !=  ackv1alpha1 .ConditionTypeTerminal  {
440+ 				continue 
441+ 			}
442+ 
443+ 			hasTerminal  =  true 
444+ 			assert .Equal (condition .Message , terminalCondition .Message )
445+ 			assert .Equal (condition .Reason , terminalCondition .Reason )
446+ 		}
447+ 
448+ 		assert .True (hasTerminal )
449+ 	})
450+ 
451+ 	rm  :=  & ackmocks.AWSResourceManager {}
452+ 	rm .On ("ReadOne" , ctx , desired ).Return (
453+ 		latest , nil ,
454+ 	)
455+ 
456+ 	rmf , rd  :=  managerFactoryMocks (desired , latest , false )
457+ 
458+ 	r , _  :=  reconcilerMocks (rmf )
459+ 
460+ 	_ , err  :=  r .Sync (ctx , rm , desired )
461+ 	// Assert the error from late initialization 
462+ 	require .NotNil (err )
463+ 	assert .Equal (ackerr .Terminal , err )
464+ 	rm .AssertCalled (t , "ReadOne" , ctx , desired )
465+ 	rd .AssertNotCalled (t , "Delta" , desired , latest )
466+ 	rm .AssertNotCalled (t , "Update" , ctx , desired , latest , delta )
467+ 	rm .AssertNotCalled (t , "LateInitialize" , ctx , latest )
468+ }
0 commit comments