@@ -139,6 +139,81 @@ func managerFactoryMocks(
139139	return  rmf , rd 
140140}
141141
142+ func  TestReconcilerCreate_CheckReferencesResolveTwice (t  * testing.T ) {
143+ 	require  :=  require .New (t )
144+ 
145+ 	ctx  :=  context .TODO ()
146+ 	arn  :=  ackv1alpha1 .AWSResourceName ("mybook-arn" )
147+ 
148+ 	desired , _ , _  :=  resourceMocks ()
149+ 	desired .On ("ReplaceConditions" , []* ackv1alpha1.Condition {}).Return ()
150+ 
151+ 	ids  :=  & ackmocks.AWSResourceIdentifiers {}
152+ 	ids .On ("ARN" ).Return (& arn )
153+ 
154+ 	latest , latestRTObj , _  :=  resourceMocks ()
155+ 	latest .On ("Identifiers" ).Return (ids )
156+ 
157+ 	latest .On ("Conditions" ).Return ([]* ackv1alpha1.Condition {})
158+ 	latest .On (
159+ 		"ReplaceConditions" ,
160+ 		mock .AnythingOfType ("[]*v1alpha1.Condition" ),
161+ 	).Return ().Run (func (args  mock.Arguments ) {
162+ 		conditions  :=  args .Get (0 ).([]* ackv1alpha1.Condition )
163+ 		assert .Equal (t , 1 , len (conditions ))
164+ 		cond  :=  conditions [0 ]
165+ 		assert .Equal (t , ackv1alpha1 .ConditionTypeResourceSynced , cond .Type )
166+ 		assert .Equal (t , corev1 .ConditionTrue , cond .Status )
167+ 	})
168+ 
169+ 	rm  :=  & ackmocks.AWSResourceManager {}
170+ 	rm .On ("ResolveReferences" , ctx , nil , desired ).Return (
171+ 		desired , nil ,
172+ 	).Times (2 )
173+ 	rm .On ("ReadOne" , ctx , desired ).Return (
174+ 		latest , ackerr .NotFound ,
175+ 	).Once ()
176+ 	rm .On ("ReadOne" , ctx , latest ).Return (
177+ 		latest , nil ,
178+ 	)
179+ 	rm .On ("Create" , ctx , desired ).Return (
180+ 		latest , nil ,
181+ 	)
182+ 	rm .On ("IsSynced" , ctx , latest ).Return (true , nil )
183+ 	rmf , rd  :=  managedResourceManagerFactoryMocks (desired , latest )
184+ 
185+ 	rm .On ("LateInitialize" , ctx , latest ).Return (latest , nil )
186+ 	rd .On ("IsManaged" , desired ).Return (true )
187+ 	rd .On ("Delta" , desired , latest ).Return (ackcompare .NewDelta ())
188+ 	rd .On ("Delta" , latest , latest ).Return (ackcompare .NewDelta ())
189+ 
190+ 	r , kc  :=  reconcilerMocks (rmf )
191+ 
192+ 	// pointers returned from "client.MergeFrom" fails the equality check during 
193+ 	// assertion even when parameters inside two objects are same. 
194+ 	// hence we use mock.AnythingOfType parameter to assert patch call 
195+ 	kc .On ("Patch" , ctx , latestRTObj , mock .AnythingOfType ("*client.mergeFromPatch" )).Return (nil )
196+ 
197+ 	// With the above mocks and below assertions, we check that if we got a 
198+ 	// NotFound error return from `AWSResourceManager.ReadOne()` that we end 
199+ 	// up calling the AWSResourceManager.Create() call in the Reconciler.Sync() 
200+ 	// method, 
201+ 	_ , err  :=  r .Sync (ctx , rm , desired )
202+ 	require .Nil (err )
203+ 	// Make sure references are resolved twice for the resource creation. 
204+ 	// Once before ReadOne call and one after marking the resource managed. 
205+ 	rm .AssertNumberOfCalls (t , "ResolveReferences" , 2 )
206+ 	rm .AssertCalled (t , "ResolveReferences" , ctx , nil , desired )
207+ 	rm .AssertCalled (t , "ReadOne" , ctx , desired )
208+ 	rm .AssertCalled (t , "Create" , ctx , desired )
209+ 	// No changes to metadata or spec so Patch on the object shouldn't be done 
210+ 	kc .AssertNotCalled (t , "Patch" , ctx , latestRTObj , mock .AnythingOfType ("*client.mergeFromPatch" ))
211+ 	// Only the HandleReconcilerError wrapper function ever calls patchResourceStatus 
212+ 	kc .AssertNotCalled (t , "Status" )
213+ 	rm .AssertCalled (t , "LateInitialize" , ctx , latest )
214+ 	rm .AssertCalled (t , "IsSynced" , ctx , latest )
215+ }
216+ 
142217func  TestReconcilerUpdate (t  * testing.T ) {
143218	require  :=  require .New (t )
144219
@@ -172,7 +247,7 @@ func TestReconcilerUpdate(t *testing.T) {
172247	rm  :=  & ackmocks.AWSResourceManager {}
173248	rm .On ("ResolveReferences" , ctx , nil , desired ).Return (
174249		desired , nil ,
175- 	)
250+ 	). Once () 
176251	rm .On ("ReadOne" , ctx , desired ).Return (
177252		latest , nil ,
178253	)
@@ -204,6 +279,8 @@ func TestReconcilerUpdate(t *testing.T) {
204279	_ , err  :=  r .Sync (ctx , rm , desired )
205280	require .Nil (err )
206281	rm .AssertCalled (t , "ResolveReferences" , ctx , nil , desired )
282+ 	// Assert that References are resolved only once during resource update 
283+ 	rm .AssertNumberOfCalls (t , "ResolveReferences" , 1 )
207284	rm .AssertCalled (t , "ReadOne" , ctx , desired )
208285	rd .AssertCalled (t , "Delta" , desired , latest )
209286	rm .AssertCalled (t , "Update" , ctx , desired , latest , delta )
0 commit comments