Merge pull request #109755 from p0lyn0mial/harden-test-service-account

hardens integration serviceaccount tests
This commit is contained in:
Kubernetes Prow Robot 2022-05-05 01:43:14 -07:00 committed by GitHub
commit e514c5d79c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -66,7 +66,7 @@ const (
) )
func TestServiceAccountAutoCreate(t *testing.T) { func TestServiceAccountAutoCreate(t *testing.T) {
c, _, stopFunc, err := startServiceAccountTestServer(t) c, _, stopFunc, err := startServiceAccountTestServerAndWaitForCaches(t)
defer stopFunc() defer stopFunc()
if err != nil { if err != nil {
t.Fatalf("failed to setup ServiceAccounts server: %v", err) t.Fatalf("failed to setup ServiceAccounts server: %v", err)
@ -104,7 +104,7 @@ func TestServiceAccountAutoCreate(t *testing.T) {
func TestServiceAccountTokenAutoCreate(t *testing.T) { func TestServiceAccountTokenAutoCreate(t *testing.T) {
defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.LegacyServiceAccountTokenNoAutoGeneration, false)() defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.LegacyServiceAccountTokenNoAutoGeneration, false)()
c, _, stopFunc, err := startServiceAccountTestServer(t) c, _, stopFunc, err := startServiceAccountTestServerAndWaitForCaches(t)
defer stopFunc() defer stopFunc()
if err != nil { if err != nil {
t.Fatalf("failed to setup ServiceAccounts server: %v", err) t.Fatalf("failed to setup ServiceAccounts server: %v", err)
@ -202,7 +202,7 @@ func TestServiceAccountTokenAutoCreate(t *testing.T) {
} }
func TestServiceAccountTokenAutoMount(t *testing.T) { func TestServiceAccountTokenAutoMount(t *testing.T) {
c, _, stopFunc, err := startServiceAccountTestServer(t) c, _, stopFunc, err := startServiceAccountTestServerAndWaitForCaches(t)
defer stopFunc() defer stopFunc()
if err != nil { if err != nil {
t.Fatalf("failed to setup ServiceAccounts server: %v", err) t.Fatalf("failed to setup ServiceAccounts server: %v", err)
@ -244,7 +244,7 @@ func TestServiceAccountTokenAutoMount(t *testing.T) {
} }
func TestServiceAccountTokenAuthentication(t *testing.T) { func TestServiceAccountTokenAuthentication(t *testing.T) {
c, config, stopFunc, err := startServiceAccountTestServer(t) c, config, stopFunc, err := startServiceAccountTestServerAndWaitForCaches(t)
defer stopFunc() defer stopFunc()
if err != nil { if err != nil {
t.Fatalf("failed to setup ServiceAccounts server: %v", err) 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) 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 // 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 // Listener
h := &framework.APIServerHolder{Initialized: make(chan struct{})} h := &framework.APIServerHolder{Initialized: make(chan struct{})}
apiServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { 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()) externalInformers.Start(ctx.Done())
go serviceAccountController.Run(ctx, 5) 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 return rootClientset, clientConfig, stop, nil
} }