test/integration: catch early 'kubectl attach' return

Signed-off-by: Andrew Keesler <akeesler@vmware.com>
This commit is contained in:
Andrew Keesler 2021-03-19 09:59:24 -04:00
parent 28d00ce67b
commit ebe01a5aef
No known key found for this signature in database
GPG Key ID: 27CE0444346F9413
1 changed files with 19 additions and 2 deletions

View File

@ -599,6 +599,11 @@ func TestImpersonationProxy(t *testing.T) { //nolint:gocyclo // yeah, it's compl
// start but don't wait for the attach command // start but don't wait for the attach command
err = attachCmd.Start() err = attachCmd.Start()
require.NoError(t, err) require.NoError(t, err)
attachExitCh := make(chan struct{})
go func() {
assert.NoError(t, attachCmd.Wait())
close(attachExitCh)
}()
// write to stdin on the attach process // write to stdin on the attach process
_, err = attachStdin.Write([]byte(echoString + "\n")) _, err = attachStdin.Write([]byte(echoString + "\n"))
@ -611,8 +616,7 @@ func TestImpersonationProxy(t *testing.T) { //nolint:gocyclo // yeah, it's compl
// close stdin and attach process should exit // close stdin and attach process should exit
err = attachStdin.Close() err = attachStdin.Close()
require.NoError(t, err) require.NoError(t, err)
err = attachCmd.Wait() requireClose(t, attachExitCh, time.Second*20)
require.NoError(t, err)
}) })
t.Run("websocket client", func(t *testing.T) { t.Run("websocket client", func(t *testing.T) {
@ -1183,3 +1187,16 @@ func isServiceUnavailableViaSquidError(err error, proxyServiceEndpoint string) (
return true, "" return true, ""
} }
func requireClose(t *testing.T, c chan struct{}, timeout time.Duration) {
t.Helper()
timer := time.NewTimer(timeout)
select {
case <-c:
if !timer.Stop() {
<-timer.C
}
case <-timer.C:
require.FailNow(t, "failed to receive from channel within "+timeout.String())
}
}