Skip to content

Commit

Permalink
test(agent): Fix tests without cmd.Wait() (coder#7029)
Browse files Browse the repository at this point in the history
  • Loading branch information
mafredri authored Apr 6, 2023
1 parent 2da0702 commit 121c2bc
Showing 1 changed file with 56 additions and 0 deletions.
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

0 comments on commit 121c2bc

Please sign in to comment.