From ba4aade23a76969d7598fb7ac170891c183e7327 Mon Sep 17 00:00:00 2001 From: Jordan Liggitt Date: Sat, 6 Mar 2021 19:46:00 -0500 Subject: [PATCH] Deflake TestPrepareRun --- .../pkg/server/genericapiserver_test.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/staging/src/k8s.io/apiserver/pkg/server/genericapiserver_test.go b/staging/src/k8s.io/apiserver/pkg/server/genericapiserver_test.go index a304b6db651..ab0952e088d 100644 --- a/staging/src/k8s.io/apiserver/pkg/server/genericapiserver_test.go +++ b/staging/src/k8s.io/apiserver/pkg/server/genericapiserver_test.go @@ -40,6 +40,7 @@ import ( "k8s.io/apimachinery/pkg/runtime/serializer" utilruntime "k8s.io/apimachinery/pkg/util/runtime" "k8s.io/apimachinery/pkg/util/sets" + "k8s.io/apimachinery/pkg/util/wait" "k8s.io/apimachinery/pkg/version" "k8s.io/apiserver/pkg/apis/example" examplev1 "k8s.io/apiserver/pkg/apis/example/v1" @@ -320,6 +321,7 @@ func TestPrepareRun(t *testing.T) { server := httptest.NewServer(s.Handler.Director) defer server.Close() done := make(chan struct{}) + defer close(done) s.PrepareRun() s.RunPostStartHooks(done) @@ -329,10 +331,19 @@ func TestPrepareRun(t *testing.T) { assert.NoError(err) assert.Equal(http.StatusOK, resp.StatusCode) - // healthz checks are installed in PrepareRun - resp, err = http.Get(server.URL + "/healthz") - assert.NoError(err) - assert.Equal(http.StatusOK, resp.StatusCode) + // wait for health (max-in-flight-filter is initialized asynchronously, can take a few milliseconds to initialize) + assert.NoError(wait.PollImmediate(100*time.Millisecond, wait.ForeverTestTimeout, func() (bool, error) { + // healthz checks are installed in PrepareRun + resp, err = http.Get(server.URL + "/healthz") + assert.NoError(err) + data, _ := ioutil.ReadAll(resp.Body) + if http.StatusOK != resp.StatusCode { + t.Logf("got %d", resp.StatusCode) + t.Log(string(data)) + return false, nil + } + return true, nil + })) resp, err = http.Get(server.URL + "/healthz/ping") assert.NoError(err) assert.Equal(http.StatusOK, resp.StatusCode)