Skip to content

Commit 9dfa9b4

Browse files
committed
e2e/apibinding: test webhook in source ws too
On-behalf-of: @SAP robert.vasek@sap.com Signed-off-by: Robert Vasek <robert.vasek@clyso.com>
1 parent b0be1ae commit 9dfa9b4

File tree

1 file changed

+58
-8
lines changed

1 file changed

+58
-8
lines changed

test/e2e/apibinding/apibinding_webhook_test.go

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func TestAPIBindingMutatingWebhook(t *testing.T) {
6565
t.Cleanup(cancel)
6666

6767
orgPath, _ := framework.NewOrganizationFixture(t, server) //nolint:staticcheck // TODO: switch to NewWorkspaceFixture.
68-
sourcePath, _ := kcptesting.NewWorkspaceFixture(t, server, orgPath)
68+
sourcePath, sourceWS := kcptesting.NewWorkspaceFixture(t, server, orgPath)
6969
targetPath, targetWS := kcptesting.NewWorkspaceFixture(t, server, orgPath)
7070

7171
cfg := server.BaseConfig(t)
@@ -209,11 +209,36 @@ func TestAPIBindingMutatingWebhook(t *testing.T) {
209209
return testWebhooks[sourcePath].Calls() >= 1
210210
}, wait.ForeverTestTimeout, 100*time.Millisecond)
211211

212-
t.Logf("Check that the in-workspace webhook was NOT called")
213-
require.Zero(t, testWebhooks[targetPath].Calls(), "in-workspace webhook should not have been called")
214-
215212
t.Logf("Check that the logicalcluster annotation on the object that triggered webhook is matching the target cluster")
216213
require.Equal(t, targetWS.Spec.Cluster, clusterInReviewObject.Load(), "expected that the object passed to the webhook has correct kcp.io/cluster annotation set")
214+
215+
t.Logf("Create an APIBinding in workspace %q that points to the today-cowboys export", sourcePath)
216+
kcptestinghelpers.Eventually(t, func() (bool, string) {
217+
_, err := kcpClusterClient.Cluster(sourcePath).ApisV1alpha2().APIBindings().Create(ctx, apiBinding, metav1.CreateOptions{})
218+
return err == nil, fmt.Sprintf("Error creating APIBinding: %v", err)
219+
}, wait.ForeverTestTimeout, time.Millisecond*100)
220+
221+
t.Logf("Ensure cowboys are served in %q", sourcePath)
222+
require.Eventually(t, func() bool {
223+
_, err := cowbyClusterClient.Cluster(sourcePath).WildwestV1alpha1().Cowboys("default").List(ctx, metav1.ListOptions{})
224+
return err == nil
225+
}, wait.ForeverTestTimeout, 100*time.Millisecond)
226+
t.Logf("Cowboys are served")
227+
228+
sourceWHCalls := testWebhooks[sourcePath].Calls()
229+
230+
t.Logf("Creating cowboy resource in source logical cluster, eventually going through admission webhook")
231+
require.Eventually(t, func() bool {
232+
_, err = cowbyClusterClient.Cluster(sourcePath).WildwestV1alpha1().Cowboys("default").Create(ctx, &cowboy, metav1.CreateOptions{})
233+
require.NoError(t, err)
234+
return testWebhooks[sourcePath].Calls() > sourceWHCalls
235+
}, wait.ForeverTestTimeout, 100*time.Millisecond)
236+
237+
t.Logf("Check that the logicalcluster annotation on the object that triggered webhook is matching the source cluster")
238+
require.Equal(t, sourceWS.Spec.Cluster, clusterInReviewObject.Load(), "expected that the object passed to the webhook has correct kcp.io/cluster annotation set")
239+
240+
t.Logf("Check that the in-workspace webhook was NOT called")
241+
require.Zero(t, testWebhooks[targetPath].Calls(), "in-workspace webhook should not have been called")
217242
}
218243

219244
func TestAPIBindingValidatingWebhook(t *testing.T) {
@@ -226,7 +251,7 @@ func TestAPIBindingValidatingWebhook(t *testing.T) {
226251
t.Cleanup(cancel)
227252

228253
orgPath, _ := framework.NewOrganizationFixture(t, server) //nolint:staticcheck // TODO: switch to NewWorkspaceFixture.
229-
sourcePath, _ := kcptesting.NewWorkspaceFixture(t, server, orgPath)
254+
sourcePath, sourceWS := kcptesting.NewWorkspaceFixture(t, server, orgPath)
230255
targetPath, targetWS := kcptesting.NewWorkspaceFixture(t, server, orgPath)
231256

232257
cfg := server.BaseConfig(t)
@@ -382,9 +407,34 @@ func TestAPIBindingValidatingWebhook(t *testing.T) {
382407
return testWebhooks[sourcePath].Calls() >= 1
383408
}, wait.ForeverTestTimeout, 100*time.Millisecond)
384409

385-
t.Logf("Check that the in-workspace webhook was NOT called")
386-
require.Zero(t, testWebhooks[targetPath].Calls(), "in-workspace webhook should not have been called")
387-
388410
t.Logf("Check that the logicalcluster annotation on the object that triggered webhook is matching the target cluster")
389411
require.Equal(t, targetWS.Spec.Cluster, clusterInReviewObject.Load(), "expected that the object passed to the webhook has correct kcp.io/cluster annotation set")
412+
413+
t.Logf("Create an APIBinding in workspace %q that points to the today-cowboys export", sourcePath)
414+
kcptestinghelpers.Eventually(t, func() (bool, string) {
415+
_, err := kcpClients.Cluster(sourcePath).ApisV1alpha2().APIBindings().Create(ctx, apiBinding, metav1.CreateOptions{})
416+
return err == nil, fmt.Sprintf("Error creating APIBinding: %v", err)
417+
}, wait.ForeverTestTimeout, time.Millisecond*100)
418+
419+
t.Logf("Ensure cowboys are served in %q", sourcePath)
420+
require.Eventually(t, func() bool {
421+
_, err := cowbyClusterClient.Cluster(sourcePath).WildwestV1alpha1().Cowboys("default").List(ctx, metav1.ListOptions{})
422+
return err == nil
423+
}, wait.ForeverTestTimeout, 100*time.Millisecond)
424+
t.Logf("Cowboys are served")
425+
426+
sourceWHCalls := testWebhooks[sourcePath].Calls()
427+
428+
t.Logf("Creating cowboy resource in source logical cluster, eventually going through admission webhook")
429+
require.Eventually(t, func() bool {
430+
_, err = cowbyClusterClient.Cluster(sourcePath).WildwestV1alpha1().Cowboys("default").Create(ctx, &cowboy, metav1.CreateOptions{})
431+
require.NoError(t, err)
432+
return testWebhooks[sourcePath].Calls() > sourceWHCalls
433+
}, wait.ForeverTestTimeout, 100*time.Millisecond)
434+
435+
t.Logf("Check that the logicalcluster annotation on the object that triggered webhook is matching the source cluster")
436+
require.Equal(t, sourceWS.Spec.Cluster, clusterInReviewObject.Load(), "expected that the object passed to the webhook has correct kcp.io/cluster annotation set")
437+
438+
t.Logf("Check that the in-workspace webhook was NOT called")
439+
require.Zero(t, testWebhooks[targetPath].Calls(), "in-workspace webhook should not have been called")
390440
}

0 commit comments

Comments
 (0)