@@ -55,36 +55,36 @@ 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
+ expectedRemoteRelatedSecret * unstructured.Unstructured
73
+ expectedLocalRelatedSecret * unstructured.Unstructured
74
+ expectedState string
75
75
}
76
76
77
77
clusterName := logicalcluster .Name ("testcluster" )
78
78
79
79
testcases := []testcase {
80
80
{
81
- name : "optional related resource does not exist" ,
81
+ name : "optional related resource of kcp origin does not exist in the source " ,
82
82
remoteAPIGroup : "remote.example.corp" ,
83
83
localCRD : loadCRD ("things" ),
84
84
pubRes : newPublishedResources ([]syncagentv1alpha1.RelatedResourceSpec {
85
85
{
86
86
Identifier : "optional-secret" ,
87
- Origin : "service " ,
87
+ Origin : "kcp " ,
88
88
Kind : "Secret" ,
89
89
Reference : syncagentv1alpha1.RelatedResourceReference {
90
90
Name : syncagentv1alpha1.ResourceLocator {
@@ -96,7 +96,9 @@ func TestSyncerProcessingRelatedResources(t *testing.T) {
96
96
},
97
97
},
98
98
}),
99
- performRequeues : true ,
99
+ performRequeues : true ,
100
+ remoteRelatedSecret : nil ,
101
+ localRelatedSecret : nil ,
100
102
remoteObject : newUnstructured (& dummyv1alpha1.NamespacedThing {
101
103
ObjectMeta : metav1.ObjectMeta {
102
104
Name : "my-test-thing" ,
@@ -124,42 +126,13 @@ func TestSyncerProcessingRelatedResources(t *testing.T) {
124
126
Username : "Colonel Mustard" ,
125
127
},
126
128
}),
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 : "" ,
160
133
},
161
134
{
162
- name : "mandatory related resource does not exist " ,
135
+ name : "mandatory related resource of kcp origin exists in the source side " ,
163
136
remoteAPIGroup : "remote.example.corp" ,
164
137
localCRD : loadCRD ("things" ),
165
138
pubRes : newPublishedResources ([]syncagentv1alpha1.RelatedResourceSpec {
@@ -178,6 +151,30 @@ func TestSyncerProcessingRelatedResources(t *testing.T) {
178
151
},
179
152
}),
180
153
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
+ }),
181
178
remoteObject : newUnstructured (& dummyv1alpha1.NamespacedThing {
182
179
ObjectMeta : metav1.ObjectMeta {
183
180
Name : "my-test-thing" ,
@@ -206,57 +203,41 @@ func TestSyncerProcessingRelatedResources(t *testing.T) {
206
203
},
207
204
}),
208
205
existingState : "" ,
209
-
210
- expectedRemoteObject : newUnstructured (& dummyv1alpha1.NamespacedThing {
206
+ expectedRemoteRelatedSecret : newUnstructured (& corev1.Secret {
211
207
ObjectMeta : metav1.ObjectMeta {
212
- Name : "my-test-thing " ,
208
+ Name : "mandatory-credentials " ,
213
209
Namespace : stateNamespace ,
210
+ Labels : map [string ]string {
211
+ "hello" : "world" ,
212
+ },
214
213
Finalizers : []string {
215
214
deletionFinalizer ,
216
215
},
217
216
},
218
- Spec : dummyv1alpha1. ThingSpec {
219
- Username : "Colonel Mustard" ,
217
+ Data : map [ string ][] byte {
218
+ "password" : [] byte ( "hunter2" ) ,
220
219
},
221
- }, withGroupKind ( "remote.example.corp" , "RemoteThing" ) ),
222
- expectedLocalObject : newUnstructured (& dummyv1alpha1. NamespacedThing {
220
+ }),
221
+ expectedLocalRelatedSecret : newUnstructured (& corev1. Secret {
223
222
ObjectMeta : metav1.ObjectMeta {
224
- Name : "testcluster-my-test-thing " ,
223
+ Name : "mandatory-credentials " ,
225
224
Namespace : stateNamespace ,
226
225
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" ,
234
227
},
235
228
},
236
- Spec : dummyv1alpha1. ThingSpec {
237
- Username : "Colonel Mustard" ,
229
+ Data : map [ string ][] byte {
230
+ "password" : [] byte ( "hunter2" ) ,
238
231
},
239
232
}),
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 "}}` ,
241
234
},
242
235
}
243
236
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
237
for _ , testcase := range testcases {
257
238
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 )
260
241
261
242
syncer , err := NewResourceSyncer (
262
243
// zap.Must(zap.NewDevelopment()).Sugar(),
@@ -326,42 +307,39 @@ func TestSyncerProcessingRelatedResources(t *testing.T) {
326
307
}
327
308
}
328
309
} else {
329
- requeue , err = syncer .Process (ctx , testcase .remoteObject )
310
+ _ , err = syncer .Process (ctx , testcase .remoteObject )
330
311
}
331
312
332
- finalRemoteObject , getErr := getFinalObjectVersion (remoteCtx , remoteClient , testcase .remoteObject , testcase .expectedRemoteObject )
313
+ finalRemoteObject , getErr := getFinalObjectVersion (remoteCtx , remoteClient , testcase .remoteRelatedSecret , testcase .expectedRemoteRelatedSecret )
333
314
if getErr != nil {
334
315
t .Fatalf ("Failed to get final remote object: %v" , getErr )
335
316
}
336
317
337
- finalLocalObject , getErr := getFinalObjectVersion (localCtx , localClient , testcase .localObject , testcase .expectedLocalObject )
318
+ finalLocalObject , getErr := getFinalObjectVersion (localCtx , localClient , testcase .localRelatedSecret , testcase .expectedLocalRelatedSecret )
338
319
if getErr != nil {
339
320
t .Fatalf ("Failed to get final local object: %v" , getErr )
340
321
}
341
322
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
+ }
348
326
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 )
351
329
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
+ }
356
334
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 )))
363
340
}
364
341
}
342
+
365
343
})
366
344
}
367
345
}
0 commit comments