Skip to content

Commit 5b723c6

Browse files
committed
e2e/apibinding: test webhook in source ws too
On-behalf-of: @SAP [email protected] Signed-off-by: Robert Vasek <[email protected]>
1 parent 2b7b2fe commit 5b723c6

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)
@@ -208,11 +208,36 @@ func TestAPIBindingMutatingWebhook(t *testing.T) {
208208
return testWebhooks[sourcePath].Calls() >= 1, ""
209209
}, wait.ForeverTestTimeout, 100*time.Millisecond, "failed to create cowboy resource")
210210

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

218243
func TestAPIBindingValidatingWebhook(t *testing.T) {
@@ -225,7 +250,7 @@ func TestAPIBindingValidatingWebhook(t *testing.T) {
225250
t.Cleanup(cancel)
226251

227252
orgPath, _ := framework.NewOrganizationFixture(t, server) //nolint:staticcheck // TODO: switch to NewWorkspaceFixture.
228-
sourcePath, _ := kcptesting.NewWorkspaceFixture(t, server, orgPath)
253+
sourcePath, sourceWS := kcptesting.NewWorkspaceFixture(t, server, orgPath)
229254
targetPath, targetWS := kcptesting.NewWorkspaceFixture(t, server, orgPath)
230255

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

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

0 commit comments

Comments
 (0)