@@ -55,36 +55,36 @@ func newPublishedResources(relatedResources []syncagentv1alpha1.RelatedResourceS
5555		},
5656	}
5757}
58- 
5958func  TestSyncerProcessingRelatedResources (t  * testing.T ) {
6059	const  stateNamespace  =  "kcp-system" 
6160
6261	type  testcase  struct  {
63- 		name                  string 
64- 		remoteAPIGroup        string 
65- 		localCRD              * apiextensionsv1.CustomResourceDefinition 
66- 		pubRes                * syncagentv1alpha1.PublishedResource 
67- 		remoteObject          * unstructured.Unstructured 
68- 		localObject           * unstructured.Unstructured 
69- 		existingState         string 
70- 		performRequeues       bool 
71- 		expectedRemoteObject  * unstructured.Unstructured 
72- 		expectedLocalObject   * unstructured.Unstructured 
73- 		expectedState         string 
74- 		customVerification    func (t  * testing.T , requeue  bool , processErr  error , finalRemoteObject  * unstructured.Unstructured , finalLocalObject  * unstructured.Unstructured , testcase  testcase )
62+ 		name                         string 
63+ 		remoteAPIGroup               string 
64+ 		localCRD                     * apiextensionsv1.CustomResourceDefinition 
65+ 		pubRes                       * syncagentv1alpha1.PublishedResource 
66+ 		remoteRelatedSecret          * unstructured.Unstructured 
67+ 		localRelatedSecret           * unstructured.Unstructured 
68+ 		remoteObject                 * unstructured.Unstructured 
69+ 		localObject                  * unstructured.Unstructured 
70+ 		existingState                string 
71+ 		performRequeues              bool 
72+ 		expectedRemoteRelatedSecret  * unstructured.Unstructured 
73+ 		expectedLocalRelatedSecret   * unstructured.Unstructured 
74+ 		expectedState                string 
7575	}
7676
7777	clusterName  :=  logicalcluster .Name ("testcluster" )
7878
7979	testcases  :=  []testcase {
8080		{
81- 			name :           "optional related resource does not exist" ,
81+ 			name :           "optional related resource of kcp origin  does not exist in the source " ,
8282			remoteAPIGroup : "remote.example.corp" ,
8383			localCRD :       loadCRD ("things" ),
8484			pubRes : newPublishedResources ([]syncagentv1alpha1.RelatedResourceSpec {
8585				{
8686					Identifier : "optional-secret" ,
87- 					Origin :     "service " ,
87+ 					Origin :     "kcp " ,
8888					Kind :       "Secret" ,
8989					Reference : syncagentv1alpha1.RelatedResourceReference {
9090						Name : syncagentv1alpha1.ResourceLocator {
@@ -96,7 +96,9 @@ func TestSyncerProcessingRelatedResources(t *testing.T) {
9696					},
9797				},
9898			}),
99- 			performRequeues : true ,
99+ 			performRequeues :     true ,
100+ 			remoteRelatedSecret : nil ,
101+ 			localRelatedSecret :  nil ,
100102			remoteObject : newUnstructured (& dummyv1alpha1.NamespacedThing {
101103				ObjectMeta : metav1.ObjectMeta {
102104					Name :      "my-test-thing" ,
@@ -124,42 +126,13 @@ func TestSyncerProcessingRelatedResources(t *testing.T) {
124126					Username : "Colonel Mustard" ,
125127				},
126128			}),
127- 			existingState : "" ,
128- 
129- 			expectedRemoteObject : newUnstructured (& dummyv1alpha1.NamespacedThing {
130- 				ObjectMeta : metav1.ObjectMeta {
131- 					Name :      "my-test-thing" ,
132- 					Namespace : stateNamespace ,
133- 					Finalizers : []string {
134- 						deletionFinalizer ,
135- 					},
136- 				},
137- 				Spec : dummyv1alpha1.ThingSpec {
138- 					Username : "Colonel Mustard" ,
139- 				},
140- 			}, withGroupKind ("remote.example.corp" , "RemoteThing" )),
141- 			expectedLocalObject : newUnstructured (& dummyv1alpha1.NamespacedThing {
142- 				ObjectMeta : metav1.ObjectMeta {
143- 					Name :      "testcluster-my-test-thing" ,
144- 					Namespace : stateNamespace ,
145- 					Labels : map [string ]string {
146- 						agentNameLabel :            "textor-the-doctor" ,
147- 						remoteObjectClusterLabel :  "testcluster" ,
148- 						remoteObjectNameHashLabel : "c346c8ceb5d104cc783d09b95e8ea7032c190948" ,
149- 					},
150- 					Annotations : map [string ]string {
151- 						remoteObjectNameAnnotation :      "my-test-thing" ,
152- 						remoteObjectNamespaceAnnotation : stateNamespace ,
153- 					},
154- 				},
155- 				Spec : dummyv1alpha1.ThingSpec {
156- 					Username : "Colonel Mustard" ,
157- 				},
158- 			}),
159- 			expectedState : `{"apiVersion":"remote.example.corp/v1alpha1","kind":"RemoteThing","metadata":{"name":"my-test-thing","namespace":"kcp-system"},"spec":{"username":"Colonel Mustard"}}` ,
129+ 			existingState :               "" ,
130+ 			expectedRemoteRelatedSecret : nil ,
131+ 			expectedLocalRelatedSecret :  nil ,
132+ 			expectedState :               "" ,
160133		},
161134		{
162- 			name :           "mandatory related resource does not exist " ,
135+ 			name :           "mandatory related resource of kcp origin exists in the source side " ,
163136			remoteAPIGroup : "remote.example.corp" ,
164137			localCRD :       loadCRD ("things" ),
165138			pubRes : newPublishedResources ([]syncagentv1alpha1.RelatedResourceSpec {
@@ -178,6 +151,30 @@ func TestSyncerProcessingRelatedResources(t *testing.T) {
178151				},
179152			}),
180153			performRequeues : true ,
154+ 			remoteRelatedSecret : newUnstructured (& corev1.Secret {
155+ 				ObjectMeta : metav1.ObjectMeta {
156+ 					Name :      "mandatory-credentials" ,
157+ 					Namespace : stateNamespace ,
158+ 					Labels : map [string ]string {
159+ 						"hello" : "world" ,
160+ 					},
161+ 				},
162+ 				Data : map [string ][]byte {
163+ 					"password" : []byte ("hunter2" ),
164+ 				},
165+ 			}),
166+ 			localRelatedSecret : newUnstructured (& corev1.Secret {
167+ 				ObjectMeta : metav1.ObjectMeta {
168+ 					Name :      "mandatory-credentials" ,
169+ 					Namespace : stateNamespace ,
170+ 					Labels : map [string ]string {
171+ 						"hello" : "world" ,
172+ 					},
173+ 				},
174+ 				Data : map [string ][]byte {
175+ 					"password" : []byte ("hunter2" ),
176+ 				},
177+ 			}),
181178			remoteObject : newUnstructured (& dummyv1alpha1.NamespacedThing {
182179				ObjectMeta : metav1.ObjectMeta {
183180					Name :      "my-test-thing" ,
@@ -206,57 +203,41 @@ func TestSyncerProcessingRelatedResources(t *testing.T) {
206203				},
207204			}),
208205			existingState : "" ,
209- 
210- 			expectedRemoteObject : newUnstructured (& dummyv1alpha1.NamespacedThing {
206+ 			expectedRemoteRelatedSecret : newUnstructured (& corev1.Secret {
211207				ObjectMeta : metav1.ObjectMeta {
212- 					Name :      "my-test-thing " ,
208+ 					Name :      "mandatory-credentials " ,
213209					Namespace : stateNamespace ,
210+ 					Labels : map [string ]string {
211+ 						"hello" : "world" ,
212+ 					},
214213					Finalizers : []string {
215214						deletionFinalizer ,
216215					},
217216				},
218- 				Spec : dummyv1alpha1. ThingSpec {
219- 					Username :  "Colonel Mustard" ,
217+ 				Data :  map [ string ][] byte {
218+ 					"password" : [] byte ( "hunter2" ) ,
220219				},
221- 			},  withGroupKind ( "remote.example.corp" ,  "RemoteThing" ) ),
222- 			expectedLocalObject : newUnstructured (& dummyv1alpha1. NamespacedThing {
220+ 			}),
221+ 			expectedLocalRelatedSecret : newUnstructured (& corev1. Secret {
223222				ObjectMeta : metav1.ObjectMeta {
224- 					Name :      "testcluster-my-test-thing " ,
223+ 					Name :      "mandatory-credentials " ,
225224					Namespace : stateNamespace ,
226225					Labels : map [string ]string {
227- 						agentNameLabel :            "textor-the-doctor" ,
228- 						remoteObjectClusterLabel :  "testcluster" ,
229- 						remoteObjectNameHashLabel : "c346c8ceb5d104cc783d09b95e8ea7032c190948" ,
230- 					},
231- 					Annotations : map [string ]string {
232- 						remoteObjectNameAnnotation :      "my-test-thing" ,
233- 						remoteObjectNamespaceAnnotation : stateNamespace ,
226+ 						"hello" : "world" ,
234227					},
235228				},
236- 				Spec : dummyv1alpha1. ThingSpec {
237- 					Username :  "Colonel Mustard" ,
229+ 				Data :  map [ string ][] byte {
230+ 					"password" : [] byte ( "hunter2" ) ,
238231				},
239232			}),
240- 			expectedState : `{"apiVersion":"remote.example.corp/v1alpha1 ","kind":"RemoteThing ","metadata":{"name":"my-test-thing","namespace ":"kcp-system"},"spec":{"username":"Colonel Mustard "}}` ,
233+ 			expectedState : `{"apiVersion":"v1 ","data":{"password":"aHVudGVyMg=="}," kind":"Secret ","metadata":{"labels":{"hello":"world"},"name ":"mandatory-credentials","namespace":"kcp-system "}}` ,
241234		},
242235	}
243236
244- 	credentials  :=  newUnstructured (& corev1.Secret {
245- 		ObjectMeta : metav1.ObjectMeta {
246- 			Name :      "mandatory-credentials" ,
247- 			Namespace : stateNamespace ,
248- 			Labels : map [string ]string {
249- 				"hello" : "world" ,
250- 			},
251- 		},
252- 		Data : map [string ][]byte {
253- 			"password" : []byte ("hunter2" ),
254- 		},
255- 	})
256237	for  _ , testcase  :=  range  testcases  {
257238		t .Run (testcase .name , func (t  * testing.T ) {
258- 			localClient  :=  buildFakeClient (testcase .localObject , credentials )
259- 			remoteClient  :=  buildFakeClient (testcase .remoteObject , credentials )
239+ 			localClient  :=  buildFakeClient (testcase .localObject , testcase . localRelatedSecret )
240+ 			remoteClient  :=  buildFakeClient (testcase .remoteObject , testcase . remoteRelatedSecret )
260241
261242			syncer , err  :=  NewResourceSyncer (
262243				// zap.Must(zap.NewDevelopment()).Sugar(), 
@@ -326,42 +307,39 @@ func TestSyncerProcessingRelatedResources(t *testing.T) {
326307					}
327308				}
328309			} else  {
329- 				requeue , err  =  syncer .Process (ctx , testcase .remoteObject )
310+ 				_ , err  =  syncer .Process (ctx , testcase .remoteObject )
330311			}
331312
332- 			finalRemoteObject , getErr  :=  getFinalObjectVersion (remoteCtx , remoteClient , testcase .remoteObject , testcase .expectedRemoteObject )
313+ 			finalRemoteObject , getErr  :=  getFinalObjectVersion (remoteCtx , remoteClient , testcase .remoteRelatedSecret , testcase .expectedRemoteRelatedSecret )
333314			if  getErr  !=  nil  {
334315				t .Fatalf ("Failed to get final remote object: %v" , getErr )
335316			}
336317
337- 			finalLocalObject , getErr  :=  getFinalObjectVersion (localCtx , localClient , testcase .localObject , testcase .expectedLocalObject )
318+ 			finalLocalObject , getErr  :=  getFinalObjectVersion (localCtx , localClient , testcase .localRelatedSecret , testcase .expectedLocalRelatedSecret )
338319			if  getErr  !=  nil  {
339320				t .Fatalf ("Failed to get final local object: %v" , getErr )
340321			}
341322
342- 			if  testcase .customVerification  !=  nil  {
343- 				testcase .customVerification (t , requeue , err , finalRemoteObject , finalLocalObject , testcase )
344- 			} else  {
345- 				if  err  !=  nil  {
346- 					t .Fatalf ("Processing failed: %v" , err )
347- 				}
323+ 			if  err  !=  nil  {
324+ 				t .Fatalf ("Processing failed: %v" , err )
325+ 			}
348326
349- 				 assertObjectsEqual (t , "local" , testcase .expectedLocalObject , finalLocalObject )
350- 				 assertObjectsEqual (t , "remote" , testcase .expectedRemoteObject , finalRemoteObject )
327+ 			assertObjectsEqual (t , "local" , testcase .expectedLocalRelatedSecret , finalLocalObject )
328+ 			assertObjectsEqual (t , "remote" , testcase .expectedRemoteRelatedSecret , finalRemoteObject )
351329
352- 				 if  testcase .expectedState  !=  ""  {
353- 					 if  backend  ==  nil  {
354- 						 t .Fatal ("Cannot check object state, state store was never instantiated." )
355- 					 }
330+ 			if  testcase .expectedState  !=  ""  {
331+ 				if  backend  ==  nil  {
332+ 					t .Fatal ("Cannot check object state, state store was never instantiated." )
333+ 				}
356334
357- 					finalState , err  :=  backend .Get (testcase .expectedRemoteObject , clusterName )
358- 					if  err  !=  nil  {
359- 						t .Fatalf ("Failed to get final state: %v" , err )
360- 					} else  if  ! bytes .Equal (finalState , []byte (testcase .expectedState )) {
361- 						t .Fatalf ("States do not match:\n %s" , diff .StringDiff (testcase .expectedState , string (finalState )))
362- 					}
335+ 				finalState , err  :=  backend .Get (testcase .expectedRemoteRelatedSecret , clusterName )
336+ 				if  err  !=  nil  {
337+ 					t .Fatalf ("Failed to get final state: %v" , err )
338+ 				} else  if  ! bytes .Equal (finalState , []byte (testcase .expectedState )) {
339+ 					t .Fatalf ("States do not match:\n %s" , diff .StringDiff (testcase .expectedState , string (finalState )))
363340				}
364341			}
342+ 
365343		})
366344	}
367345}
0 commit comments