Skip to content

Commit 74cdaf1

Browse files
authored
Merge pull request #3489 from xrstf/improve-auth-webhook-tests
Improve authorizer webhook tests
2 parents 9b0ddc8 + b842391 commit 74cdaf1

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

test/e2e/authorizer/authorizationorder_test.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package authorizer
1919
import (
2020
"context"
2121
"testing"
22+
"time"
2223

2324
"github.com/stretchr/testify/require"
2425

@@ -42,8 +43,8 @@ func TestAuthorizationOrder(t *testing.T) {
4243
webhookPort := "8080"
4344
ctx, cancelFunc := context.WithCancel(context.Background())
4445
t.Cleanup(cancelFunc)
45-
webhookStop := RunWebhook(ctx, t, webhookPort, "kubernetes:authz:allow")
46-
t.Cleanup(webhookStop)
46+
webhook1Stop := RunWebhook(ctx, t, webhookPort, "kubernetes:authz:allow")
47+
t.Cleanup(webhook1Stop)
4748

4849
server, kcpClusterClient, kubeClusterClient := setupTest(t, "AlwaysAllowGroups,AlwaysAllowPaths,Webhook,RBAC", "testdata/webhook1.kubeconfig")
4950

@@ -52,8 +53,9 @@ func TestAuthorizationOrder(t *testing.T) {
5253
require.NoError(t, err)
5354

5455
// stop the webhook and switch to a deny policy
55-
webhookStop()
56-
RunWebhook(ctx, t, webhookPort, "kubernetes:authz:deny")
56+
webhook1Stop()
57+
webhook2Stop := RunWebhook(ctx, t, webhookPort, "kubernetes:authz:deny")
58+
t.Cleanup(webhook2Stop)
5759

5860
t.Log("Admin should not be allowed to list ConfigMaps.")
5961
_, err = kubeClusterClient.Cluster(logicalcluster.NewPath("root")).CoreV1().ConfigMaps("default").List(ctx, metav1.ListOptions{})
@@ -68,8 +70,8 @@ func TestAuthorizationOrder(t *testing.T) {
6870
webhookPort := "8081"
6971
ctx, cancelFunc := context.WithCancel(context.Background())
7072
t.Cleanup(cancelFunc)
71-
webhookStop := RunWebhook(ctx, t, webhookPort, "kubernetes:authz:allow")
72-
t.Cleanup(webhookStop)
73+
webhook1Stop := RunWebhook(ctx, t, webhookPort, "kubernetes:authz:allow")
74+
t.Cleanup(webhook1Stop)
7375

7476
server, kcpClusterClient, kubeClusterClient := setupTest(t, "Webhook,AlwaysAllowGroups,AlwaysAllowPaths,RBAC", "testdata/webhook2.kubeconfig")
7577

@@ -81,8 +83,9 @@ func TestAuthorizationOrder(t *testing.T) {
8183
require.NoError(t, err)
8284

8385
// stop the webhook and switch to a deny policy
84-
webhookStop()
85-
RunWebhook(ctx, t, webhookPort, "kubernetes:authz:deny")
86+
webhook1Stop()
87+
webhook2Stop := RunWebhook(ctx, t, webhookPort, "kubernetes:authz:deny")
88+
t.Cleanup(webhook2Stop)
8689

8790
t.Log("Admin should not be allowed now to list Logical clusters.")
8891
_, err = kcpClusterClient.Cluster(logicalcluster.NewPath("root")).CoreV1alpha1().LogicalClusters().List(ctx, metav1.ListOptions{})
@@ -124,6 +127,14 @@ func setupTest(t *testing.T, authOrder, webhookConfigFile string) (kcptestingser
124127

125128
server := kcptesting.PrivateKcpServer(t, kcptestingserver.WithCustomArguments(args...))
126129

130+
// The testing framework has a rare race condition where if you stop kcp too early after it became "ready",
131+
// it will run into loads of shutdown issues and the shutdown will take 3-4 minutes.
132+
// This can be easily avoided by simply waiting a few seconds here. Since the tests that use setupTest()
133+
// are very, very short anyway, this will not harm the test runtime overall, but make them much more
134+
// stable on some certain PCs/laptops.
135+
// See https://github.com/kcp-dev/kcp/issues/3488 for more information.
136+
time.Sleep(3 * time.Second)
137+
127138
kcpConfig := server.BaseConfig(t)
128139
kubeClusterClient, err := kcpkubernetesclientset.NewForConfig(kcpConfig)
129140
require.NoError(t, err)

test/e2e/authorizer/utils.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"net"
2323
"os"
2424
"os/exec"
25+
"sync"
2526
"testing"
2627
"time"
2728

@@ -66,11 +67,11 @@ func RunWebhook(ctx context.Context, t *testing.T, port string, response string)
6667
return true, ""
6768
}, wait.ForeverTestTimeout, time.Millisecond*200)
6869

69-
return func() {
70+
return sync.OnceFunc(func() {
7071
t.Log("Stopping webhook...")
7172
cancel()
72-
if err := cmd.Wait(); err != nil {
73-
t.Logf("error waiting for webhook to finish: %v", err)
73+
if err := cmd.Wait(); err != nil && err.Error() != "signal: killed" {
74+
t.Logf("Error waiting for webhook to finish: %v", err)
7475
}
75-
}
76+
})
7677
}

0 commit comments

Comments
 (0)