@@ -19,6 +19,7 @@ package authorizer
1919import (
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 )
0 commit comments