Skip to content

Commit eeb74e1

Browse files
committed
fix: Fix goleak in cli TestSSH tests
Commands are now also run with contexts that time out. Work towards #3221.
1 parent 6377f17 commit eeb74e1

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

cli/ssh_test.go

+25-5
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,20 @@ func TestSSH(t *testing.T) {
6868
t.Parallel()
6969
t.Run("ImmediateExit", func(t *testing.T) {
7070
t.Parallel()
71+
7172
client, workspace, agentToken := setupWorkspaceForSSH(t)
7273
cmd, root := clitest.New(t, "ssh", workspace.Name)
7374
clitest.SetupConfig(t, client, root)
7475
pty := ptytest.New(t)
7576
cmd.SetIn(pty.Input())
7677
cmd.SetErr(pty.Output())
7778
cmd.SetOut(pty.Output())
79+
80+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
81+
defer cancel()
82+
7883
cmdDone := tGo(t, func() {
79-
err := cmd.Execute()
84+
err := cmd.ExecuteContext(ctx)
8085
assert.NoError(t, err)
8186
})
8287
pty.ExpectMatch("Waiting")
@@ -85,9 +90,9 @@ func TestSSH(t *testing.T) {
8590
agentCloser := agent.New(agentClient.ListenWorkspaceAgent, &agent.Options{
8691
Logger: slogtest.Make(t, nil).Leveled(slog.LevelDebug),
8792
})
88-
t.Cleanup(func() {
93+
defer func() {
8994
_ = agentCloser.Close()
90-
})
95+
}()
9196

9297
// Shells on Mac, Windows, and Linux all exit shells with the "exit" command.
9398
pty.WriteLine("exit")
@@ -113,14 +118,22 @@ func TestSSH(t *testing.T) {
113118

114119
clientOutput, clientInput := io.Pipe()
115120
serverOutput, serverInput := io.Pipe()
121+
defer func() {
122+
for _, c := range []io.Closer{clientOutput, clientInput, serverOutput, serverInput} {
123+
_ = c.Close()
124+
}
125+
}()
126+
127+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
128+
defer cancel()
116129

117130
cmd, root := clitest.New(t, "ssh", "--stdio", workspace.Name)
118131
clitest.SetupConfig(t, client, root)
119132
cmd.SetIn(clientOutput)
120133
cmd.SetOut(serverInput)
121134
cmd.SetErr(io.Discard)
122135
cmdDone := tGo(t, func() {
123-
err := cmd.Execute()
136+
err := cmd.ExecuteContext(ctx)
124137
assert.NoError(t, err)
125138
})
126139

@@ -132,9 +145,13 @@ func TestSSH(t *testing.T) {
132145
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
133146
})
134147
require.NoError(t, err)
148+
defer conn.Close()
149+
135150
sshClient := ssh.NewClient(conn, channels, requests)
136151
session, err := sshClient.NewSession()
137152
require.NoError(t, err)
153+
defer session.Close()
154+
138155
command := "sh -c exit"
139156
if runtime.GOOS == "windows" {
140157
command = "cmd.exe /c exit"
@@ -198,6 +215,9 @@ func TestSSH(t *testing.T) {
198215
}
199216
})
200217

218+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
219+
defer cancel()
220+
201221
cmd, root := clitest.New(t,
202222
"ssh",
203223
workspace.Name,
@@ -210,7 +230,7 @@ func TestSSH(t *testing.T) {
210230
cmd.SetOut(pty.Output())
211231
cmd.SetErr(io.Discard)
212232
cmdDone := tGo(t, func() {
213-
err := cmd.Execute()
233+
err := cmd.ExecuteContext(ctx)
214234
assert.NoError(t, err)
215235
})
216236

0 commit comments

Comments
 (0)