refactor: consolidate darwin unix socket test helpers (#21283)

This commit is contained in:
Zach
2025-12-16 09:11:54 -07:00
committed by GitHub
parent dac822b7f4
commit 174a6192fa
5 changed files with 56 additions and 97 deletions
+8 -31
View File
@@ -851,7 +851,7 @@ func TestSSH(t *testing.T) {
sshClient := ssh.NewClient(conn, channels, requests)
tmpdir := tempDirUnixSocket(t)
tmpdir := testutil.TempDirUnixSocket(t)
remoteSock := path.Join(tmpdir, "remote.sock")
_, err = sshClient.ListenUnix(remoteSock)
@@ -937,7 +937,7 @@ func TestSSH(t *testing.T) {
<-ctx.Done()
})
tmpdir := tempDirUnixSocket(t)
tmpdir := testutil.TempDirUnixSocket(t)
localSock := filepath.Join(tmpdir, "local.sock")
remoteSock := path.Join(tmpdir, "remote.sock")
for i := 0; i < 2; i++ {
@@ -1143,7 +1143,7 @@ func TestSSH(t *testing.T) {
})
// Start up ssh agent listening on unix socket.
tmpdir := tempDirUnixSocket(t)
tmpdir := testutil.TempDirUnixSocket(t)
agentSock := filepath.Join(tmpdir, "agent.sock")
l, err := net.Listen("unix", agentSock)
require.NoError(t, err)
@@ -1318,7 +1318,7 @@ func TestSSH(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()
tmpdir := tempDirUnixSocket(t)
tmpdir := testutil.TempDirUnixSocket(t)
localSock := filepath.Join(tmpdir, "local.sock")
remoteSock := filepath.Join(tmpdir, "remote.sock")
@@ -1408,7 +1408,7 @@ func TestSSH(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitSuperLong*2)
defer cancel()
tmpdir := tempDirUnixSocket(t)
tmpdir := testutil.TempDirUnixSocket(t)
localSock := filepath.Join(tmpdir, "local.sock")
l, err := net.Listen("unix", localSock)
@@ -1521,7 +1521,7 @@ func TestSSH(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitSuperLong)
defer cancel()
tmpdir := tempDirUnixSocket(t)
tmpdir := testutil.TempDirUnixSocket(t)
type testSocket struct {
local string
@@ -1904,7 +1904,7 @@ p7KeSZdlk47pMBGOfnvEmoQ=
}
// Setup GPG home directory on the "client".
gnupgHomeClient := tempDirUnixSocket(t)
gnupgHomeClient := testutil.TempDirUnixSocket(t)
t.Setenv("GNUPGHOME", gnupgHomeClient)
// Get the agent extra socket path.
@@ -1960,7 +1960,7 @@ Expire-Date: 0
}()
// Get the agent socket path in the "workspace".
gnupgHomeWorkspace := tempDirUnixSocket(t)
gnupgHomeWorkspace := testutil.TempDirUnixSocket(t)
stdout = bytes.NewBuffer(nil)
stderr = bytes.NewBuffer(nil)
@@ -2425,29 +2425,6 @@ func tGo(t *testing.T, fn func()) (done <-chan struct{}) {
return doneC
}
// tempDirUnixSocket returns a temporary directory that can safely hold unix
// sockets (probably).
//
// During tests on darwin we hit the max path length limit for unix sockets
// pretty easily in the default location, so this function uses /tmp instead to
// get shorter paths.
func tempDirUnixSocket(t *testing.T) string {
t.Helper()
if runtime.GOOS == "darwin" {
testName := strings.ReplaceAll(t.Name(), "/", "_")
dir, err := os.MkdirTemp("/tmp", fmt.Sprintf("coder-test-%s-", testName))
require.NoError(t, err, "create temp dir for gpg test")
t.Cleanup(func() {
err := os.RemoveAll(dir)
assert.NoError(t, err, "remove temp dir", dir)
})
return dir
}
return t.TempDir()
}
func TestSSH_Completion(t *testing.T) {
t.Parallel()
+1 -1
View File
@@ -25,7 +25,7 @@ func setupSocketServer(t *testing.T) (path string, cleanup func()) {
t.Helper()
// Use a temporary socket path for each test
socketPath := filepath.Join(tempDirUnixSocket(t), "test.sock")
socketPath := filepath.Join(testutil.TempDirUnixSocket(t), "test.sock")
// Create parent directory if needed
parentDir := filepath.Dir(socketPath)