Skip to content

fix: Fix goleak in cli TestSSH/ForwardAgent test #3369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 3, 2022
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
25 changes: 11 additions & 14 deletions cli/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,13 @@ func TestSSH(t *testing.T) {
t.Parallel()

client, workspace, agentToken := setupWorkspaceForSSH(t)
_, _ = tGoContext(t, func(ctx context.Context) {
// Run this async so the SSH command has to wait for
// the build and agent to connect!
agentClient := codersdk.New(client.URL)
agentClient.SessionToken = agentToken
agentCloser := agent.New(agentClient.ListenWorkspaceAgent, &agent.Options{
Logger: slogtest.Make(t, nil).Leveled(slog.LevelDebug),
})
<-ctx.Done()
_ = agentCloser.Close()

agentClient := codersdk.New(client.URL)
agentClient.SessionToken = agentToken
agentCloser := agent.New(agentClient.ListenWorkspaceAgent, &agent.Options{
Logger: slogtest.Make(t, nil).Leveled(slog.LevelDebug),
})
defer agentCloser.Close()

// Generate private key.
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
Expand All @@ -204,15 +200,16 @@ func TestSSH(t *testing.T) {
fd, err := l.Accept()
if err != nil {
if !errors.Is(err, net.ErrClosed) {
t.Logf("accept error: %v", err)
assert.NoError(t, err, "listener accept failed")
}
return
}

err = gosshagent.ServeAgent(kr, fd)
if !errors.Is(err, io.EOF) {
assert.NoError(t, err)
assert.NoError(t, err, "serve agent failed")
}
_ = fd.Close()
}
})

Expand All @@ -232,7 +229,7 @@ func TestSSH(t *testing.T) {
cmd.SetErr(pty.Output())
cmdDone := tGo(t, func() {
err := cmd.ExecuteContext(ctx)
assert.NoError(t, err)
assert.NoError(t, err, "ssh command failed")
})

// Ensure that SSH_AUTH_SOCK is set.
Expand All @@ -243,7 +240,7 @@ func TestSSH(t *testing.T) {
// Ensure that ssh-add lists our key.
pty.WriteLine("ssh-add -L")
keys, err := kr.List()
require.NoError(t, err)
require.NoError(t, err, "list keys failed")
pty.ExpectMatch(keys[0].String())

// And we're done.
Expand Down