Make TestImpersonationProxy less flaky.

This test did not tolerate this connection failing, which can happen for any number of flaky networking-related reasons. This change moves the connection setup into an "eventually" retry loop so it's allowed to fail temporarily as long as it eventually connects.

Signed-off-by: Matt Moyer <moyerm@vmware.com>
This commit is contained in:
Matt Moyer 2021-07-08 20:47:37 -05:00
parent 2e18c88e33
commit 04e9897d51
No known key found for this signature in database
GPG Key ID: EAE88AD172C5AE2D
1 changed files with 17 additions and 12 deletions

View File

@ -1088,17 +1088,22 @@ func TestImpersonationProxy(t *testing.T) { //nolint:gocyclo // yeah, it's compl
return proxyURL, nil return proxyURL, nil
} }
} }
c, r, err := dialer.Dial(dest.String(), http.Header{"Origin": {dest.String()}}) var (
if r != nil { resp *http.Response
defer func() { conn *websocket.Conn
require.NoError(t, r.Body.Close()) )
}() testlib.RequireEventually(t, func(requireEventually *require.Assertions) {
var err error
conn, resp, err = dialer.Dial(dest.String(), http.Header{"Origin": {dest.String()}})
if resp != nil {
defer func() { requireEventually.NoError(resp.Body.Close()) }()
} }
if err != nil && r != nil { if err != nil && resp != nil {
body, _ := ioutil.ReadAll(r.Body) body, _ := ioutil.ReadAll(resp.Body)
t.Logf("websocket dial failed: %d:%s", r.StatusCode, body) t.Logf("websocket dial failed: %d:%s", resp.StatusCode, body)
} }
require.NoError(t, err) requireEventually.NoError(err)
}, time.Minute, time.Second)
// perform a create through the admin client // perform a create through the admin client
wantConfigMap := &corev1.ConfigMap{ wantConfigMap := &corev1.ConfigMap{
@ -1115,7 +1120,7 @@ func TestImpersonationProxy(t *testing.T) { //nolint:gocyclo // yeah, it's compl
}) })
// see if the websocket client received an event for the create // see if the websocket client received an event for the create
_, message, err := c.ReadMessage() _, message, err := conn.ReadMessage()
if err != nil { if err != nil {
t.Fatalf("Unexpected error: %v", err) t.Fatalf("Unexpected error: %v", err)
} }