@@ -55,36 +55,38 @@ func newPublishedResources(relatedResources []syncagentv1alpha1.RelatedResourceS
55
55
},
56
56
}
57
57
}
58
-
59
58
func TestSyncerProcessingRelatedResources (t * testing.T ) {
60
59
const stateNamespace = "kcp-system"
61
60
62
61
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
+ expectedRemoteObject * unstructured.Unstructured
73
+ expectedLocalObject * unstructured.Unstructured
74
+ expectedRemoteRelatedSecret * unstructured.Unstructured
75
+ expectedLocalRelatedSecret * unstructured.Unstructured
76
+ expectedState string
75
77
}
76
78
77
79
clusterName := logicalcluster .Name ("testcluster" )
78
80
79
81
testcases := []testcase {
80
82
{
81
- name : "optional related resource does not exist " ,
83
+ name : "optional related resource of kcp origin does not existś in source " ,
82
84
remoteAPIGroup : "remote.example.corp" ,
83
85
localCRD : loadCRD ("things" ),
84
86
pubRes : newPublishedResources ([]syncagentv1alpha1.RelatedResourceSpec {
85
87
{
86
88
Identifier : "optional-secret" ,
87
- Origin : "service " ,
89
+ Origin : "kcp " ,
88
90
Kind : "Secret" ,
89
91
Reference : syncagentv1alpha1.RelatedResourceReference {
90
92
Name : syncagentv1alpha1.ResourceLocator {
@@ -97,6 +99,30 @@ func TestSyncerProcessingRelatedResources(t *testing.T) {
97
99
},
98
100
}),
99
101
performRequeues : true ,
102
+ remoteRelatedSecret : newUnstructured (& corev1.Secret {
103
+ ObjectMeta : metav1.ObjectMeta {
104
+ Name : "optional-credentials" ,
105
+ Namespace : stateNamespace ,
106
+ Labels : map [string ]string {
107
+ "hello" : "world" ,
108
+ },
109
+ },
110
+ Data : map [string ][]byte {
111
+ "password" : []byte ("hunter2" ),
112
+ },
113
+ }),
114
+ localRelatedSecret : newUnstructured (& corev1.Secret {
115
+ ObjectMeta : metav1.ObjectMeta {
116
+ Name : "optional-credentials" ,
117
+ Namespace : stateNamespace ,
118
+ Labels : map [string ]string {
119
+ "hello" : "world" ,
120
+ },
121
+ },
122
+ Data : map [string ][]byte {
123
+ "password" : []byte ("hunter2" ),
124
+ },
125
+ }),
100
126
remoteObject : newUnstructured (& dummyv1alpha1.NamespacedThing {
101
127
ObjectMeta : metav1.ObjectMeta {
102
128
Name : "my-test-thing" ,
@@ -125,15 +151,38 @@ func TestSyncerProcessingRelatedResources(t *testing.T) {
125
151
},
126
152
}),
127
153
existingState : "" ,
128
-
129
- expectedRemoteObject : newUnstructured (& dummyv1alpha1.NamespacedThing {
154
+ expectedRemoteRelatedSecret : newUnstructured (& corev1.Secret {
130
155
ObjectMeta : metav1.ObjectMeta {
131
- Name : "my-test-thing " ,
156
+ Name : "optional-credentials " ,
132
157
Namespace : stateNamespace ,
158
+ Labels : map [string ]string {
159
+ "hello" : "world" ,
160
+ },
133
161
Finalizers : []string {
134
162
deletionFinalizer ,
135
163
},
136
164
},
165
+ Data : map [string ][]byte {
166
+ "password" : []byte ("hunter2" ),
167
+ },
168
+ }),
169
+ expectedLocalRelatedSecret : newUnstructured (& corev1.Secret {
170
+ ObjectMeta : metav1.ObjectMeta {
171
+ Name : "optional-credentials" ,
172
+ Namespace : stateNamespace ,
173
+ Labels : map [string ]string {
174
+ "hello" : "world" ,
175
+ },
176
+ },
177
+ Data : map [string ][]byte {
178
+ "password" : []byte ("hunter2" ),
179
+ },
180
+ }),
181
+ expectedRemoteObject : newUnstructured (& dummyv1alpha1.NamespacedThing {
182
+ ObjectMeta : metav1.ObjectMeta {
183
+ Name : "my-test-thing" ,
184
+ Namespace : stateNamespace ,
185
+ },
137
186
Spec : dummyv1alpha1.ThingSpec {
138
187
Username : "Colonel Mustard" ,
139
188
},
@@ -156,10 +205,10 @@ func TestSyncerProcessingRelatedResources(t *testing.T) {
156
205
Username : "Colonel Mustard" ,
157
206
},
158
207
}),
159
- expectedState : `{"apiVersion":"remote.example.corp/v1alpha1","kind":"RemoteThing","metadata":{"name":"my-test-thing","namespace":"kcp-system"},"spec":{"username":"Colonel Mustard"}}` ,
208
+ expectedState : "" ,
160
209
},
161
210
{
162
- name : "mandatory related resource does not exist " ,
211
+ name : "mandatory related resource of kcp origin exists in the source side " ,
163
212
remoteAPIGroup : "remote.example.corp" ,
164
213
localCRD : loadCRD ("things" ),
165
214
pubRes : newPublishedResources ([]syncagentv1alpha1.RelatedResourceSpec {
@@ -178,6 +227,30 @@ func TestSyncerProcessingRelatedResources(t *testing.T) {
178
227
},
179
228
}),
180
229
performRequeues : true ,
230
+ remoteRelatedSecret : newUnstructured (& corev1.Secret {
231
+ ObjectMeta : metav1.ObjectMeta {
232
+ Name : "mandatory-credentials" ,
233
+ Namespace : stateNamespace ,
234
+ Labels : map [string ]string {
235
+ "hello" : "world" ,
236
+ },
237
+ },
238
+ Data : map [string ][]byte {
239
+ "password" : []byte ("hunter2" ),
240
+ },
241
+ }),
242
+ localRelatedSecret : newUnstructured (& corev1.Secret {
243
+ ObjectMeta : metav1.ObjectMeta {
244
+ Name : "mandatory-credentials" ,
245
+ Namespace : stateNamespace ,
246
+ Labels : map [string ]string {
247
+ "hello" : "world" ,
248
+ },
249
+ },
250
+ Data : map [string ][]byte {
251
+ "password" : []byte ("hunter2" ),
252
+ },
253
+ }),
181
254
remoteObject : newUnstructured (& dummyv1alpha1.NamespacedThing {
182
255
ObjectMeta : metav1.ObjectMeta {
183
256
Name : "my-test-thing" ,
@@ -206,57 +279,41 @@ func TestSyncerProcessingRelatedResources(t *testing.T) {
206
279
},
207
280
}),
208
281
existingState : "" ,
209
-
210
- expectedRemoteObject : newUnstructured (& dummyv1alpha1.NamespacedThing {
282
+ expectedRemoteRelatedSecret : newUnstructured (& corev1.Secret {
211
283
ObjectMeta : metav1.ObjectMeta {
212
- Name : "my-test-thing " ,
284
+ Name : "mandatory-credentials " ,
213
285
Namespace : stateNamespace ,
286
+ Labels : map [string ]string {
287
+ "hello" : "world" ,
288
+ },
214
289
Finalizers : []string {
215
290
deletionFinalizer ,
216
291
},
217
292
},
218
- Spec : dummyv1alpha1. ThingSpec {
219
- Username : "Colonel Mustard" ,
293
+ Data : map [ string ][] byte {
294
+ "password" : [] byte ( "hunter2" ) ,
220
295
},
221
- }, withGroupKind ( "remote.example.corp" , "RemoteThing" ) ),
222
- expectedLocalObject : newUnstructured (& dummyv1alpha1. NamespacedThing {
296
+ }),
297
+ expectedLocalRelatedSecret : newUnstructured (& corev1. Secret {
223
298
ObjectMeta : metav1.ObjectMeta {
224
- Name : "testcluster-my-test-thing " ,
299
+ Name : "mandatory-credentials " ,
225
300
Namespace : stateNamespace ,
226
301
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 ,
302
+ "hello" : "world" ,
234
303
},
235
304
},
236
- Spec : dummyv1alpha1. ThingSpec {
237
- Username : "Colonel Mustard" ,
305
+ Data : map [ string ][] byte {
306
+ "password" : [] byte ( "hunter2" ) ,
238
307
},
239
308
}),
240
- expectedState : `{"apiVersion":"remote.example.corp/v1alpha1 ","kind":"RemoteThing ","metadata":{"name":"my-test-thing","namespace ":"kcp-system"},"spec":{"username":"Colonel Mustard "}}` ,
309
+ expectedState : `{"apiVersion":"v1 ","data":{"password":"aHVudGVyMg=="}," kind":"Secret ","metadata":{"labels":{"hello":"world"},"name ":"mandatory-credentials","namespace":"kcp-system "}}` ,
241
310
},
242
311
}
243
312
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
- })
256
313
for _ , testcase := range testcases {
257
314
t .Run (testcase .name , func (t * testing.T ) {
258
- localClient := buildFakeClient (testcase .localObject , credentials )
259
- remoteClient := buildFakeClient (testcase .remoteObject , credentials )
315
+ localClient := buildFakeClient (testcase .localObject , testcase . localRelatedSecret )
316
+ remoteClient := buildFakeClient (testcase .remoteObject , testcase . remoteRelatedSecret )
260
317
261
318
syncer , err := NewResourceSyncer (
262
319
// zap.Must(zap.NewDevelopment()).Sugar(),
@@ -326,42 +383,39 @@ func TestSyncerProcessingRelatedResources(t *testing.T) {
326
383
}
327
384
}
328
385
} else {
329
- requeue , err = syncer .Process (ctx , testcase .remoteObject )
386
+ _ , err = syncer .Process (ctx , testcase .remoteObject )
330
387
}
331
388
332
- finalRemoteObject , getErr := getFinalObjectVersion (remoteCtx , remoteClient , testcase .remoteObject , testcase .expectedRemoteObject )
389
+ finalRemoteObject , getErr := getFinalObjectVersion (remoteCtx , remoteClient , testcase .remoteRelatedSecret , testcase .expectedRemoteRelatedSecret )
333
390
if getErr != nil {
334
391
t .Fatalf ("Failed to get final remote object: %v" , getErr )
335
392
}
336
393
337
- finalLocalObject , getErr := getFinalObjectVersion (localCtx , localClient , testcase .localObject , testcase .expectedLocalObject )
394
+ finalLocalObject , getErr := getFinalObjectVersion (localCtx , localClient , testcase .localRelatedSecret , testcase .expectedLocalRelatedSecret )
338
395
if getErr != nil {
339
396
t .Fatalf ("Failed to get final local object: %v" , getErr )
340
397
}
341
398
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
- }
399
+ if err != nil {
400
+ t .Fatalf ("Processing failed: %v" , err )
401
+ }
348
402
349
- assertObjectsEqual (t , "local" , testcase .expectedLocalObject , finalLocalObject )
350
- assertObjectsEqual (t , "remote" , testcase .expectedRemoteObject , finalRemoteObject )
403
+ assertObjectsEqual (t , "local" , testcase .expectedLocalRelatedSecret , finalLocalObject )
404
+ assertObjectsEqual (t , "remote" , testcase .expectedRemoteRelatedSecret , finalRemoteObject )
351
405
352
- if testcase .expectedState != "" {
353
- if backend == nil {
354
- t .Fatal ("Cannot check object state, state store was never instantiated." )
355
- }
406
+ if testcase .expectedState != "" {
407
+ if backend == nil {
408
+ t .Fatal ("Cannot check object state, state store was never instantiated." )
409
+ }
356
410
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
- }
411
+ finalState , err := backend .Get (testcase .expectedRemoteRelatedSecret , clusterName )
412
+ if err != nil {
413
+ t .Fatalf ("Failed to get final state: %v" , err )
414
+ } else if ! bytes .Equal (finalState , []byte (testcase .expectedState )) {
415
+ t .Fatalf ("States do not match:\n %s" , diff .StringDiff (testcase .expectedState , string (finalState )))
363
416
}
364
417
}
418
+
365
419
})
366
420
}
367
421
}
0 commit comments