Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
test(agent): Fix tests without cmd.Wait()
Ran into inexplicable test failures for these tests, so I added some
output logging and a call to `cmd.Wait()` which is mandatory to clean up
resources after `cmd.Start()`.
  • Loading branch information
mafredri committed Apr 6, 2023
commit 3dec2de130b080fdba11c922ef65ea5bd3520c97
56 changes: 56 additions & 0 deletions agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,10 +480,24 @@ func TestAgent_TCPLocalForwarding(t *testing.T) {
}
}()

pty := ptytest.New(t)

cmd := setupSSHCommand(t, []string{"-L", fmt.Sprintf("%d:127.0.0.1:%d", randomPort, remotePort)}, []string{"sleep", "5"})
cmd.Stdin = pty.Input()
cmd.Stdout = pty.Output()
cmd.Stderr = pty.Output()
err = cmd.Start()
require.NoError(t, err)

go func() {
err := cmd.Wait()
select {
case <-done:
default:
assert.NoError(t, err)
}
}()

require.Eventually(t, func() bool {
conn, err := net.Dial("tcp", "127.0.0.1:"+strconv.Itoa(randomPort))
if err != nil {
Expand Down Expand Up @@ -547,10 +561,24 @@ func TestAgent_TCPRemoteForwarding(t *testing.T) {
}
}()

pty := ptytest.New(t)

cmd := setupSSHCommand(t, []string{"-R", fmt.Sprintf("127.0.0.1:%d:127.0.0.1:%d", randomPort, localPort)}, []string{"sleep", "5"})
cmd.Stdin = pty.Input()
cmd.Stdout = pty.Output()
cmd.Stderr = pty.Output()
err = cmd.Start()
require.NoError(t, err)

go func() {
err := cmd.Wait()
select {
case <-done:
default:
assert.NoError(t, err)
}
}()

require.Eventually(t, func() bool {
conn, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", randomPort))
if err != nil {
Expand Down Expand Up @@ -612,10 +640,24 @@ func TestAgent_UnixLocalForwarding(t *testing.T) {
}
}()

pty := ptytest.New(t)

cmd := setupSSHCommand(t, []string{"-L", fmt.Sprintf("%s:%s", localSocketPath, remoteSocketPath)}, []string{"sleep", "5"})
cmd.Stdin = pty.Input()
cmd.Stdout = pty.Output()
cmd.Stderr = pty.Output()
err = cmd.Start()
require.NoError(t, err)

go func() {
err := cmd.Wait()
select {
case <-done:
default:
assert.NoError(t, err)
}
}()

require.Eventually(t, func() bool {
_, err := os.Stat(localSocketPath)
return err == nil
Expand Down Expand Up @@ -670,10 +712,24 @@ func TestAgent_UnixRemoteForwarding(t *testing.T) {
}
}()

pty := ptytest.New(t)

cmd := setupSSHCommand(t, []string{"-R", fmt.Sprintf("%s:%s", remoteSocketPath, localSocketPath)}, []string{"sleep", "5"})
cmd.Stdin = pty.Input()
cmd.Stdout = pty.Output()
cmd.Stderr = pty.Output()
err = cmd.Start()
require.NoError(t, err)

go func() {
err := cmd.Wait()
select {
case <-done:
default:
assert.NoError(t, err)
}
}()

// It's possible that the socket is created but the server is not ready to
// accept connections yet. We need to retry until we can connect.
var conn net.Conn
Expand Down