Skip to content

Commit a4ccd6a

Browse files
committed
update tests
Signed-off-by: Karol Szwaj <karol.szwaj@gmail.com> On-behalf-of: @SAP karol.szwaj@sap.com
1 parent 05c1537 commit a4ccd6a

File tree

1 file changed

+127
-73
lines changed

1 file changed

+127
-73
lines changed

internal/sync/syncer_related_test.go

Lines changed: 127 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -55,36 +55,38 @@ func newPublishedResources(relatedResources []syncagentv1alpha1.RelatedResourceS
5555
},
5656
}
5757
}
58-
5958
func 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+
expectedRemoteObject *unstructured.Unstructured
73+
expectedLocalObject *unstructured.Unstructured
74+
expectedRemoteRelatedSecret *unstructured.Unstructured
75+
expectedLocalRelatedSecret *unstructured.Unstructured
76+
expectedState string
7577
}
7678

7779
clusterName := logicalcluster.Name("testcluster")
7880

7981
testcases := []testcase{
8082
{
81-
name: "optional related resource does not exist",
83+
name: "optional related resource of kcp origin does not existś in source",
8284
remoteAPIGroup: "remote.example.corp",
8385
localCRD: loadCRD("things"),
8486
pubRes: newPublishedResources([]syncagentv1alpha1.RelatedResourceSpec{
8587
{
8688
Identifier: "optional-secret",
87-
Origin: "service",
89+
Origin: "kcp",
8890
Kind: "Secret",
8991
Reference: syncagentv1alpha1.RelatedResourceReference{
9092
Name: syncagentv1alpha1.ResourceLocator{
@@ -97,6 +99,30 @@ func TestSyncerProcessingRelatedResources(t *testing.T) {
9799
},
98100
}),
99101
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+
}),
100126
remoteObject: newUnstructured(&dummyv1alpha1.NamespacedThing{
101127
ObjectMeta: metav1.ObjectMeta{
102128
Name: "my-test-thing",
@@ -125,15 +151,38 @@ func TestSyncerProcessingRelatedResources(t *testing.T) {
125151
},
126152
}),
127153
existingState: "",
128-
129-
expectedRemoteObject: newUnstructured(&dummyv1alpha1.NamespacedThing{
154+
expectedRemoteRelatedSecret: newUnstructured(&corev1.Secret{
130155
ObjectMeta: metav1.ObjectMeta{
131-
Name: "my-test-thing",
156+
Name: "optional-credentials",
132157
Namespace: stateNamespace,
158+
Labels: map[string]string{
159+
"hello": "world",
160+
},
133161
Finalizers: []string{
134162
deletionFinalizer,
135163
},
136164
},
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+
},
137186
Spec: dummyv1alpha1.ThingSpec{
138187
Username: "Colonel Mustard",
139188
},
@@ -156,10 +205,10 @@ func TestSyncerProcessingRelatedResources(t *testing.T) {
156205
Username: "Colonel Mustard",
157206
},
158207
}),
159-
expectedState: `{"apiVersion":"remote.example.corp/v1alpha1","kind":"RemoteThing","metadata":{"name":"my-test-thing","namespace":"kcp-system"},"spec":{"username":"Colonel Mustard"}}`,
208+
expectedState: "",
160209
},
161210
{
162-
name: "mandatory related resource does not exist",
211+
name: "mandatory related resource of kcp origin exists in the source side",
163212
remoteAPIGroup: "remote.example.corp",
164213
localCRD: loadCRD("things"),
165214
pubRes: newPublishedResources([]syncagentv1alpha1.RelatedResourceSpec{
@@ -178,6 +227,30 @@ func TestSyncerProcessingRelatedResources(t *testing.T) {
178227
},
179228
}),
180229
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+
}),
181254
remoteObject: newUnstructured(&dummyv1alpha1.NamespacedThing{
182255
ObjectMeta: metav1.ObjectMeta{
183256
Name: "my-test-thing",
@@ -206,57 +279,41 @@ func TestSyncerProcessingRelatedResources(t *testing.T) {
206279
},
207280
}),
208281
existingState: "",
209-
210-
expectedRemoteObject: newUnstructured(&dummyv1alpha1.NamespacedThing{
282+
expectedRemoteRelatedSecret: newUnstructured(&corev1.Secret{
211283
ObjectMeta: metav1.ObjectMeta{
212-
Name: "my-test-thing",
284+
Name: "mandatory-credentials",
213285
Namespace: stateNamespace,
286+
Labels: map[string]string{
287+
"hello": "world",
288+
},
214289
Finalizers: []string{
215290
deletionFinalizer,
216291
},
217292
},
218-
Spec: dummyv1alpha1.ThingSpec{
219-
Username: "Colonel Mustard",
293+
Data: map[string][]byte{
294+
"password": []byte("hunter2"),
220295
},
221-
}, withGroupKind("remote.example.corp", "RemoteThing")),
222-
expectedLocalObject: newUnstructured(&dummyv1alpha1.NamespacedThing{
296+
}),
297+
expectedLocalRelatedSecret: newUnstructured(&corev1.Secret{
223298
ObjectMeta: metav1.ObjectMeta{
224-
Name: "testcluster-my-test-thing",
299+
Name: "mandatory-credentials",
225300
Namespace: stateNamespace,
226301
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",
234303
},
235304
},
236-
Spec: dummyv1alpha1.ThingSpec{
237-
Username: "Colonel Mustard",
305+
Data: map[string][]byte{
306+
"password": []byte("hunter2"),
238307
},
239308
}),
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"}}`,
241310
},
242311
}
243312

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-
})
256313
for _, testcase := range testcases {
257314
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)
260317

261318
syncer, err := NewResourceSyncer(
262319
// zap.Must(zap.NewDevelopment()).Sugar(),
@@ -326,42 +383,39 @@ func TestSyncerProcessingRelatedResources(t *testing.T) {
326383
}
327384
}
328385
} else {
329-
requeue, err = syncer.Process(ctx, testcase.remoteObject)
386+
_, err = syncer.Process(ctx, testcase.remoteObject)
330387
}
331388

332-
finalRemoteObject, getErr := getFinalObjectVersion(remoteCtx, remoteClient, testcase.remoteObject, testcase.expectedRemoteObject)
389+
finalRemoteObject, getErr := getFinalObjectVersion(remoteCtx, remoteClient, testcase.remoteRelatedSecret, testcase.expectedRemoteRelatedSecret)
333390
if getErr != nil {
334391
t.Fatalf("Failed to get final remote object: %v", getErr)
335392
}
336393

337-
finalLocalObject, getErr := getFinalObjectVersion(localCtx, localClient, testcase.localObject, testcase.expectedLocalObject)
394+
finalLocalObject, getErr := getFinalObjectVersion(localCtx, localClient, testcase.localRelatedSecret, testcase.expectedLocalRelatedSecret)
338395
if getErr != nil {
339396
t.Fatalf("Failed to get final local object: %v", getErr)
340397
}
341398

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+
}
348402

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)
351405

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+
}
356410

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)))
363416
}
364417
}
418+
365419
})
366420
}
367421
}

0 commit comments

Comments
 (0)