From 8dea7ea27ebfe3b7ca9d9fb6b57e52c307e5505f Mon Sep 17 00:00:00 2001 From: Lukasz Szaszkiewicz Date: Mon, 2 May 2022 17:33:50 +0200 Subject: [PATCH] hardens integration serviceaccount tests the serviceAccountController controller used by the tests must wait for the caches to sync since the tests don't check /readyz there is no way the tests can tell it is safe to call the server and requests won't be rejected --- .../serviceaccount/service_account_test.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/test/integration/serviceaccount/service_account_test.go b/test/integration/serviceaccount/service_account_test.go index d4f3abb2da4..b33dbb99ec5 100644 --- a/test/integration/serviceaccount/service_account_test.go +++ b/test/integration/serviceaccount/service_account_test.go @@ -66,7 +66,7 @@ const ( ) func TestServiceAccountAutoCreate(t *testing.T) { - c, _, stopFunc, err := startServiceAccountTestServer(t) + c, _, stopFunc, err := startServiceAccountTestServerAndWaitForCaches(t) defer stopFunc() if err != nil { t.Fatalf("failed to setup ServiceAccounts server: %v", err) @@ -104,7 +104,7 @@ func TestServiceAccountAutoCreate(t *testing.T) { func TestServiceAccountTokenAutoCreate(t *testing.T) { defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.LegacyServiceAccountTokenNoAutoGeneration, false)() - c, _, stopFunc, err := startServiceAccountTestServer(t) + c, _, stopFunc, err := startServiceAccountTestServerAndWaitForCaches(t) defer stopFunc() if err != nil { t.Fatalf("failed to setup ServiceAccounts server: %v", err) @@ -202,7 +202,7 @@ func TestServiceAccountTokenAutoCreate(t *testing.T) { } func TestServiceAccountTokenAutoMount(t *testing.T) { - c, _, stopFunc, err := startServiceAccountTestServer(t) + c, _, stopFunc, err := startServiceAccountTestServerAndWaitForCaches(t) defer stopFunc() if err != nil { t.Fatalf("failed to setup ServiceAccounts server: %v", err) @@ -244,7 +244,7 @@ func TestServiceAccountTokenAutoMount(t *testing.T) { } func TestServiceAccountTokenAuthentication(t *testing.T) { - c, config, stopFunc, err := startServiceAccountTestServer(t) + c, config, stopFunc, err := startServiceAccountTestServerAndWaitForCaches(t) defer stopFunc() if err != nil { t.Fatalf("failed to setup ServiceAccounts server: %v", err) @@ -320,9 +320,9 @@ func TestServiceAccountTokenAuthentication(t *testing.T) { doServiceAccountAPIRequests(t, rwClient, otherns, true, false, false) } -// startServiceAccountTestServer returns a started server +// startServiceAccountTestServerAndWaitForCaches returns a started server // It is the responsibility of the caller to ensure the returned stopFunc is called -func startServiceAccountTestServer(t *testing.T) (*clientset.Clientset, restclient.Config, func(), error) { +func startServiceAccountTestServerAndWaitForCaches(t *testing.T) (*clientset.Clientset, restclient.Config, func(), error) { // Listener h := &framework.APIServerHolder{Initialized: make(chan struct{})} apiServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { @@ -448,6 +448,13 @@ func startServiceAccountTestServer(t *testing.T) (*clientset.Clientset, restclie externalInformers.Start(ctx.Done()) go serviceAccountController.Run(ctx, 5) + // since this method starts the controllers in a separate goroutine + // and the tests don't check /readyz there is no way + // the tests can tell it is safe to call the server and requests won't be rejected + // thus we wait until caches have synced + informers.WaitForCacheSync(ctx.Done()) + externalInformers.WaitForCacheSync(ctx.Done()) + return rootClientset, clientConfig, stop, nil }