Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a0f70de

Browse files
committed
chore: fix tests on darwin for SSH unix forwarding
1 parent 216c028 commit a0f70de

File tree

5 files changed

+67
-11
lines changed

5 files changed

+67
-11
lines changed

agent/agent_test.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ func TestAgent_UnixLocalForwarding(t *testing.T) {
410410
t.Skip("unix domain sockets are not fully supported on Windows")
411411
}
412412

413-
tmpdir := t.TempDir()
413+
tmpdir := tempDirUnixSocket(t)
414414
remoteSocketPath := filepath.Join(tmpdir, "remote-socket")
415415
localSocketPath := filepath.Join(tmpdir, "local-socket")
416416

@@ -467,7 +467,7 @@ func TestAgent_UnixRemoteForwarding(t *testing.T) {
467467
t.Skip("unix domain sockets are not fully supported on Windows")
468468
}
469469

470-
tmpdir := t.TempDir()
470+
tmpdir := tempDirUnixSocket(t)
471471
remoteSocketPath := filepath.Join(tmpdir, "remote-socket")
472472
localSocketPath := filepath.Join(tmpdir, "local-socket")
473473

@@ -1135,3 +1135,25 @@ func (*client) PostWorkspaceAgentAppHealth(_ context.Context, _ codersdk.PostWor
11351135
func (*client) PostWorkspaceAgentVersion(_ context.Context, _ string) error {
11361136
return nil
11371137
}
1138+
1139+
// tempDirUnixSocket returns a temporary directory that can safely hold unix
1140+
// sockets (probably).
1141+
//
1142+
// During tests on darwin we hit the max path length limit for unix sockets
1143+
// pretty easily in the default location, so this function uses /tmp instead to
1144+
// get shorter paths.
1145+
func tempDirUnixSocket(t *testing.T) string {
1146+
if runtime.GOOS == "darwin" {
1147+
testName := strings.ReplaceAll(t.Name(), "/", "_")
1148+
dir, err := os.MkdirTemp("/tmp", fmt.Sprintf("coder-test-%s-", testName))
1149+
require.NoError(t, err, "create temp dir for gpg test")
1150+
1151+
t.Cleanup(func() {
1152+
err := os.RemoveAll(dir)
1153+
assert.NoError(t, err, "remove temp dir", dir)
1154+
})
1155+
return dir
1156+
}
1157+
1158+
return t.TempDir()
1159+
}

cli/ssh.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ func ssh() *cobra.Command {
239239
cliflag.BoolVarP(cmd.Flags(), &shuffle, "shuffle", "", "CODER_SSH_SHUFFLE", false, "Specifies whether to choose a random workspace")
240240
_ = cmd.Flags().MarkHidden("shuffle")
241241
cliflag.BoolVarP(cmd.Flags(), &forwardAgent, "forward-agent", "A", "CODER_SSH_FORWARD_AGENT", false, "Specifies whether to forward the SSH agent specified in $SSH_AUTH_SOCK")
242-
cliflag.BoolVarP(cmd.Flags(), &forwardGPG, "forward-gpg", "G", "CODER_SSH_FORWARD_GPG", false, "Specifies whether to forward the GPG agent. Unsupported on Windows workspaces, but supports all clients. Requires gnupg (gpg, gpgconf) on both the client and workspace. The GPG agent must already be running and will not be started for you.")
242+
cliflag.BoolVarP(cmd.Flags(), &forwardGPG, "forward-gpg", "G", "CODER_SSH_FORWARD_GPG", false, "Specifies whether to forward the GPG agent. Unsupported on Windows workspaces, but supports all clients. Requires gnupg (gpg, gpgconf) on both the client and workspace. The GPG agent must already be running locally and will not be started for you. If a GPG agent is already running in the workspace, it will be attempted to be killed. It is recommended that you set GPG_TTY, TTY or SSH_TTY to $(tty) beforehand.")
243243
cliflag.StringVarP(cmd.Flags(), &identityAgent, "identity-agent", "", "CODER_SSH_IDENTITY_AGENT", "", "Specifies which identity agent to use (overrides $SSH_AUTH_SOCK), forward agent must also be enabled")
244244
cliflag.DurationVarP(cmd.Flags(), &wsPollInterval, "workspace-poll-interval", "", "CODER_WORKSPACE_POLL_INTERVAL", workspacePollInterval, "Specifies how often to poll for workspace automated shutdown.")
245245
return cmd
@@ -448,6 +448,13 @@ func uploadGPGKeys(ctx context.Context, sshClient *gossh.Client) error {
448448
set -eux
449449
agent_socket=$(gpgconf --list-dir agent-socket)
450450
echo "$agent_socket"
451+
if [ -S "$agent_socket" ]; then
452+
echo "agent socket exists, attempting to kill it" >&2
453+
gpgconf --kill gpg-agent
454+
rm -f "$agent_socket"
455+
sleep 2
456+
fi
457+
451458
test ! -S "$agent_socket"
452459
`)
453460
agentSocket := strings.TrimSpace(string(agentSocketBytes))

cli/ssh_test.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import (
77
"crypto/elliptic"
88
"crypto/rand"
99
"errors"
10+
"fmt"
1011
"io"
1112
"net"
13+
"os"
1214
"os/exec"
1315
"path/filepath"
1416
"runtime"
@@ -230,7 +232,7 @@ func TestSSH(t *testing.T) {
230232
})
231233

232234
// Start up ssh agent listening on unix socket.
233-
tmpdir := t.TempDir()
235+
tmpdir := tempDirUnixSocket(t)
234236
agentSock := filepath.Join(tmpdir, "agent.sock")
235237
l, err := net.Listen("unix", agentSock)
236238
require.NoError(t, err)
@@ -370,7 +372,7 @@ p7KeSZdlk47pMBGOfnvEmoQ=
370372
}
371373

372374
// Setup GPG home directory on the "client".
373-
gnupgHomeClient := t.TempDir()
375+
gnupgHomeClient := tempDirUnixSocket(t)
374376
t.Setenv("GNUPGHOME", gnupgHomeClient)
375377

376378
// Get the agent extra socket path.
@@ -426,7 +428,7 @@ Expire-Date: 0
426428
}()
427429

428430
// Get the agent socket path in the "workspace".
429-
gnupgHomeWorkspace := t.TempDir()
431+
gnupgHomeWorkspace := tempDirUnixSocket(t)
430432

431433
stdout = bytes.NewBuffer(nil)
432434
stderr = bytes.NewBuffer(nil)
@@ -473,9 +475,8 @@ Expire-Date: 0
473475

474476
// Check the GNUPGHOME was correctly inherited via shell.
475477
pty.WriteLine("env && echo env-''-command-done")
476-
pty.ExpectMatch("GNUPGHOME=")
477-
require.Equal(t, pty.ReadLine(), gnupgHomeWorkspace)
478-
pty.ExpectMatch("env--command-done")
478+
match := pty.ExpectMatch("env--command-done")
479+
require.Contains(t, match, "GNUPGHOME="+gnupgHomeWorkspace, match)
479480

480481
// Get the agent extra socket path in the "workspace" via shell.
481482
pty.WriteLine("gpgconf --list-dir agent-socket && echo gpgconf-''-agentsocket-command-done")
@@ -574,3 +575,25 @@ func (*stdioConn) SetReadDeadline(_ time.Time) error {
574575
func (*stdioConn) SetWriteDeadline(_ time.Time) error {
575576
return nil
576577
}
578+
579+
// tempDirUnixSocket returns a temporary directory that can safely hold unix
580+
// sockets (probably).
581+
//
582+
// During tests on darwin we hit the max path length limit for unix sockets
583+
// pretty easily in the default location, so this function uses /tmp instead to
584+
// get shorter paths.
585+
func tempDirUnixSocket(t *testing.T) string {
586+
if runtime.GOOS == "darwin" {
587+
testName := strings.ReplaceAll(t.Name(), "/", "_")
588+
dir, err := os.MkdirTemp("/tmp", fmt.Sprintf("coder-test-%s-", testName))
589+
require.NoError(t, err, "create temp dir for gpg test")
590+
591+
t.Cleanup(func() {
592+
err := os.RemoveAll(dir)
593+
assert.NoError(t, err, "remove temp dir", dir)
594+
})
595+
return dir
596+
}
597+
598+
return t.TempDir()
599+
}

cli/testdata/coder_ssh_--help.golden

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ Flags:
1111
Unsupported on Windows workspaces, but supports all
1212
clients. Requires gnupg (gpg, gpgconf) on both the
1313
client and workspace. The GPG agent must already be
14-
running and will not be started for you.
14+
running locally and will not be started for you. If
15+
a GPG agent is already running in the workspace, it
16+
will be attempted to be killed. It is recommended
17+
that you set GPG_TTY, TTY or SSH_TTY to $(tty)
18+
beforehand.
1519
Consumes $CODER_SSH_FORWARD_GPG
1620
-h, --help help for ssh
1721
--identity-agent string Specifies which identity agent to use (overrides

scripts/develop.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ fatal() {
121121
trap 'fatal "Script encountered an error"' ERR
122122

123123
cdroot
124-
start_cmd API "" "${CODER_DEV_SHIM}" server --http-address 0.0.0.0:3000 --swagger-enable
124+
start_cmd API "" "${CODER_DEV_SHIM}" server --http-address 0.0.0.0:3000 --swagger-enable --access-url "http://127.0.0.1:3000"
125125

126126
echo '== Waiting for Coder to become ready'
127127
# Start the timeout in the background so interrupting this script

0 commit comments

Comments
 (0)