Skip to content

Commit 174a619

Browse files
authored
refactor: consolidate darwin unix socket test helpers (#21283)
1 parent dac822b commit 174a619

File tree

5 files changed

+56
-97
lines changed

5 files changed

+56
-97
lines changed

agent/agent_test.go

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ func TestAgent_UnixLocalForwarding(t *testing.T) {
947947
t.Skip("unix domain sockets are not fully supported on Windows")
948948
}
949949
ctx := testutil.Context(t, testutil.WaitLong)
950-
tmpdir := tempDirUnixSocket(t)
950+
tmpdir := testutil.TempDirUnixSocket(t)
951951
remoteSocketPath := filepath.Join(tmpdir, "remote-socket")
952952

953953
l, err := net.Listen("unix", remoteSocketPath)
@@ -975,7 +975,7 @@ func TestAgent_UnixRemoteForwarding(t *testing.T) {
975975
t.Skip("unix domain sockets are not fully supported on Windows")
976976
}
977977

978-
tmpdir := tempDirUnixSocket(t)
978+
tmpdir := testutil.TempDirUnixSocket(t)
979979
remoteSocketPath := filepath.Join(tmpdir, "remote-socket")
980980

981981
ctx := testutil.Context(t, testutil.WaitLong)
@@ -3466,29 +3466,6 @@ func testSessionOutput(t *testing.T, session *ssh.Session, expected, unexpected
34663466
}
34673467
}
34683468

3469-
// tempDirUnixSocket returns a temporary directory that can safely hold unix
3470-
// sockets (probably).
3471-
//
3472-
// During tests on darwin we hit the max path length limit for unix sockets
3473-
// pretty easily in the default location, so this function uses /tmp instead to
3474-
// get shorter paths.
3475-
func tempDirUnixSocket(t *testing.T) string {
3476-
t.Helper()
3477-
if runtime.GOOS == "darwin" {
3478-
testName := strings.ReplaceAll(t.Name(), "/", "_")
3479-
dir, err := os.MkdirTemp("/tmp", fmt.Sprintf("coder-test-%s-", testName))
3480-
require.NoError(t, err, "create temp dir for gpg test")
3481-
3482-
t.Cleanup(func() {
3483-
err := os.RemoveAll(dir)
3484-
assert.NoError(t, err, "remove temp dir", dir)
3485-
})
3486-
return dir
3487-
}
3488-
3489-
return t.TempDir()
3490-
}
3491-
34923469
func TestAgent_Metrics_SSH(t *testing.T) {
34933470
t.Parallel()
34943471
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)

agent/agentsocket/service_test.go

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@ package agentsocket_test
22

33
import (
44
"context"
5-
"crypto/sha256"
6-
"encoding/hex"
7-
"fmt"
8-
"os"
95
"path/filepath"
106
"runtime"
117
"testing"
128

13-
"github.com/stretchr/testify/assert"
149
"github.com/stretchr/testify/require"
1510

1611
"cdr.dev/slog"
@@ -19,30 +14,6 @@ import (
1914
"github.com/coder/coder/v2/testutil"
2015
)
2116

22-
// tempDirUnixSocket returns a temporary directory that can safely hold unix
23-
// sockets (probably).
24-
//
25-
// During tests on darwin we hit the max path length limit for unix sockets
26-
// pretty easily in the default location, so this function uses /tmp instead to
27-
// get shorter paths. To keep paths short, we use a hash of the test name
28-
// instead of the full test name.
29-
func tempDirUnixSocket(t *testing.T) string {
30-
t.Helper()
31-
if runtime.GOOS == "darwin" {
32-
// Use a short hash of the test name to keep the path under 104 chars
33-
hash := sha256.Sum256([]byte(t.Name()))
34-
hashStr := hex.EncodeToString(hash[:])[:8] // Use first 8 chars of hash
35-
dir, err := os.MkdirTemp("/tmp", fmt.Sprintf("c-%s-", hashStr))
36-
require.NoError(t, err, "create temp dir for unix socket test")
37-
t.Cleanup(func() {
38-
err := os.RemoveAll(dir)
39-
assert.NoError(t, err, "remove temp dir", dir)
40-
})
41-
return dir
42-
}
43-
return t.TempDir()
44-
}
45-
4617
// newSocketClient creates a DRPC client connected to the Unix socket at the given path.
4718
func newSocketClient(ctx context.Context, t *testing.T, socketPath string) *agentsocket.Client {
4819
t.Helper()
@@ -66,7 +37,7 @@ func TestDRPCAgentSocketService(t *testing.T) {
6637
t.Run("Ping", func(t *testing.T) {
6738
t.Parallel()
6839

69-
socketPath := filepath.Join(tempDirUnixSocket(t), "test.sock")
40+
socketPath := filepath.Join(testutil.TempDirUnixSocket(t), "test.sock")
7041
ctx := testutil.Context(t, testutil.WaitShort)
7142
server, err := agentsocket.NewServer(
7243
slog.Make().Leveled(slog.LevelDebug),
@@ -86,7 +57,7 @@ func TestDRPCAgentSocketService(t *testing.T) {
8657

8758
t.Run("NewUnit", func(t *testing.T) {
8859
t.Parallel()
89-
socketPath := filepath.Join(tempDirUnixSocket(t), "test.sock")
60+
socketPath := filepath.Join(testutil.TempDirUnixSocket(t), "test.sock")
9061
ctx := testutil.Context(t, testutil.WaitShort)
9162
server, err := agentsocket.NewServer(
9263
slog.Make().Leveled(slog.LevelDebug),
@@ -108,7 +79,7 @@ func TestDRPCAgentSocketService(t *testing.T) {
10879
t.Run("UnitAlreadyStarted", func(t *testing.T) {
10980
t.Parallel()
11081

111-
socketPath := filepath.Join(tempDirUnixSocket(t), "test.sock")
82+
socketPath := filepath.Join(testutil.TempDirUnixSocket(t), "test.sock")
11283
ctx := testutil.Context(t, testutil.WaitShort)
11384
server, err := agentsocket.NewServer(
11485
slog.Make().Leveled(slog.LevelDebug),
@@ -138,7 +109,7 @@ func TestDRPCAgentSocketService(t *testing.T) {
138109
t.Run("UnitAlreadyCompleted", func(t *testing.T) {
139110
t.Parallel()
140111

141-
socketPath := filepath.Join(tempDirUnixSocket(t), "test.sock")
112+
socketPath := filepath.Join(testutil.TempDirUnixSocket(t), "test.sock")
142113
ctx := testutil.Context(t, testutil.WaitShort)
143114
server, err := agentsocket.NewServer(
144115
slog.Make().Leveled(slog.LevelDebug),
@@ -177,7 +148,7 @@ func TestDRPCAgentSocketService(t *testing.T) {
177148
t.Run("UnitNotReady", func(t *testing.T) {
178149
t.Parallel()
179150

180-
socketPath := filepath.Join(tempDirUnixSocket(t), "test.sock")
151+
socketPath := filepath.Join(testutil.TempDirUnixSocket(t), "test.sock")
181152
ctx := testutil.Context(t, testutil.WaitShort)
182153
server, err := agentsocket.NewServer(
183154
slog.Make().Leveled(slog.LevelDebug),
@@ -207,7 +178,7 @@ func TestDRPCAgentSocketService(t *testing.T) {
207178
t.Run("NewUnits", func(t *testing.T) {
208179
t.Parallel()
209180

210-
socketPath := filepath.Join(tempDirUnixSocket(t), "test.sock")
181+
socketPath := filepath.Join(testutil.TempDirUnixSocket(t), "test.sock")
211182
ctx := testutil.Context(t, testutil.WaitShort)
212183
server, err := agentsocket.NewServer(
213184
slog.Make().Leveled(slog.LevelDebug),
@@ -232,7 +203,7 @@ func TestDRPCAgentSocketService(t *testing.T) {
232203
t.Run("DependencyAlreadyRegistered", func(t *testing.T) {
233204
t.Parallel()
234205

235-
socketPath := filepath.Join(tempDirUnixSocket(t), "test.sock")
206+
socketPath := filepath.Join(testutil.TempDirUnixSocket(t), "test.sock")
236207
ctx := testutil.Context(t, testutil.WaitShort)
237208
server, err := agentsocket.NewServer(
238209
slog.Make().Leveled(slog.LevelDebug),
@@ -267,7 +238,7 @@ func TestDRPCAgentSocketService(t *testing.T) {
267238
t.Run("DependencyAddedAfterDependentStarted", func(t *testing.T) {
268239
t.Parallel()
269240

270-
socketPath := filepath.Join(tempDirUnixSocket(t), "test.sock")
241+
socketPath := filepath.Join(testutil.TempDirUnixSocket(t), "test.sock")
271242
ctx := testutil.Context(t, testutil.WaitShort)
272243
server, err := agentsocket.NewServer(
273244
slog.Make().Leveled(slog.LevelDebug),
@@ -309,7 +280,7 @@ func TestDRPCAgentSocketService(t *testing.T) {
309280
t.Run("UnregisteredUnit", func(t *testing.T) {
310281
t.Parallel()
311282

312-
socketPath := filepath.Join(tempDirUnixSocket(t), "test.sock")
283+
socketPath := filepath.Join(testutil.TempDirUnixSocket(t), "test.sock")
313284
ctx := testutil.Context(t, testutil.WaitShort)
314285
server, err := agentsocket.NewServer(
315286
slog.Make().Leveled(slog.LevelDebug),
@@ -328,7 +299,7 @@ func TestDRPCAgentSocketService(t *testing.T) {
328299
t.Run("UnitNotReady", func(t *testing.T) {
329300
t.Parallel()
330301

331-
socketPath := filepath.Join(tempDirUnixSocket(t), "test.sock")
302+
socketPath := filepath.Join(testutil.TempDirUnixSocket(t), "test.sock")
332303
ctx := testutil.Context(t, testutil.WaitShort)
333304
server, err := agentsocket.NewServer(
334305
slog.Make().Leveled(slog.LevelDebug),
@@ -352,7 +323,7 @@ func TestDRPCAgentSocketService(t *testing.T) {
352323
t.Run("UnitReady", func(t *testing.T) {
353324
t.Parallel()
354325

355-
socketPath := filepath.Join(tempDirUnixSocket(t), "test.sock")
326+
socketPath := filepath.Join(testutil.TempDirUnixSocket(t), "test.sock")
356327
ctx := testutil.Context(t, testutil.WaitShort)
357328
server, err := agentsocket.NewServer(
358329
slog.Make().Leveled(slog.LevelDebug),

cli/ssh_test.go

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ func TestSSH(t *testing.T) {
851851

852852
sshClient := ssh.NewClient(conn, channels, requests)
853853

854-
tmpdir := tempDirUnixSocket(t)
854+
tmpdir := testutil.TempDirUnixSocket(t)
855855

856856
remoteSock := path.Join(tmpdir, "remote.sock")
857857
_, err = sshClient.ListenUnix(remoteSock)
@@ -937,7 +937,7 @@ func TestSSH(t *testing.T) {
937937
<-ctx.Done()
938938
})
939939

940-
tmpdir := tempDirUnixSocket(t)
940+
tmpdir := testutil.TempDirUnixSocket(t)
941941
localSock := filepath.Join(tmpdir, "local.sock")
942942
remoteSock := path.Join(tmpdir, "remote.sock")
943943
for i := 0; i < 2; i++ {
@@ -1143,7 +1143,7 @@ func TestSSH(t *testing.T) {
11431143
})
11441144

11451145
// Start up ssh agent listening on unix socket.
1146-
tmpdir := tempDirUnixSocket(t)
1146+
tmpdir := testutil.TempDirUnixSocket(t)
11471147
agentSock := filepath.Join(tmpdir, "agent.sock")
11481148
l, err := net.Listen("unix", agentSock)
11491149
require.NoError(t, err)
@@ -1318,7 +1318,7 @@ func TestSSH(t *testing.T) {
13181318
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
13191319
defer cancel()
13201320

1321-
tmpdir := tempDirUnixSocket(t)
1321+
tmpdir := testutil.TempDirUnixSocket(t)
13221322
localSock := filepath.Join(tmpdir, "local.sock")
13231323
remoteSock := filepath.Join(tmpdir, "remote.sock")
13241324

@@ -1408,7 +1408,7 @@ func TestSSH(t *testing.T) {
14081408
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitSuperLong*2)
14091409
defer cancel()
14101410

1411-
tmpdir := tempDirUnixSocket(t)
1411+
tmpdir := testutil.TempDirUnixSocket(t)
14121412

14131413
localSock := filepath.Join(tmpdir, "local.sock")
14141414
l, err := net.Listen("unix", localSock)
@@ -1521,7 +1521,7 @@ func TestSSH(t *testing.T) {
15211521
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitSuperLong)
15221522
defer cancel()
15231523

1524-
tmpdir := tempDirUnixSocket(t)
1524+
tmpdir := testutil.TempDirUnixSocket(t)
15251525

15261526
type testSocket struct {
15271527
local string
@@ -1904,7 +1904,7 @@ p7KeSZdlk47pMBGOfnvEmoQ=
19041904
}
19051905

19061906
// Setup GPG home directory on the "client".
1907-
gnupgHomeClient := tempDirUnixSocket(t)
1907+
gnupgHomeClient := testutil.TempDirUnixSocket(t)
19081908
t.Setenv("GNUPGHOME", gnupgHomeClient)
19091909

19101910
// Get the agent extra socket path.
@@ -1960,7 +1960,7 @@ Expire-Date: 0
19601960
}()
19611961

19621962
// Get the agent socket path in the "workspace".
1963-
gnupgHomeWorkspace := tempDirUnixSocket(t)
1963+
gnupgHomeWorkspace := testutil.TempDirUnixSocket(t)
19641964

19651965
stdout = bytes.NewBuffer(nil)
19661966
stderr = bytes.NewBuffer(nil)
@@ -2425,29 +2425,6 @@ func tGo(t *testing.T, fn func()) (done <-chan struct{}) {
24252425
return doneC
24262426
}
24272427

2428-
// tempDirUnixSocket returns a temporary directory that can safely hold unix
2429-
// sockets (probably).
2430-
//
2431-
// During tests on darwin we hit the max path length limit for unix sockets
2432-
// pretty easily in the default location, so this function uses /tmp instead to
2433-
// get shorter paths.
2434-
func tempDirUnixSocket(t *testing.T) string {
2435-
t.Helper()
2436-
if runtime.GOOS == "darwin" {
2437-
testName := strings.ReplaceAll(t.Name(), "/", "_")
2438-
dir, err := os.MkdirTemp("/tmp", fmt.Sprintf("coder-test-%s-", testName))
2439-
require.NoError(t, err, "create temp dir for gpg test")
2440-
2441-
t.Cleanup(func() {
2442-
err := os.RemoveAll(dir)
2443-
assert.NoError(t, err, "remove temp dir", dir)
2444-
})
2445-
return dir
2446-
}
2447-
2448-
return t.TempDir()
2449-
}
2450-
24512428
func TestSSH_Completion(t *testing.T) {
24522429
t.Parallel()
24532430

cli/sync_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func setupSocketServer(t *testing.T) (path string, cleanup func()) {
2525
t.Helper()
2626

2727
// Use a temporary socket path for each test
28-
socketPath := filepath.Join(tempDirUnixSocket(t), "test.sock")
28+
socketPath := filepath.Join(testutil.TempDirUnixSocket(t), "test.sock")
2929

3030
// Create parent directory if needed
3131
parentDir := filepath.Dir(socketPath)

testutil/unixsocket.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package testutil
2+
3+
import (
4+
"os"
5+
"runtime"
6+
"strings"
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
// TempDirUnixSocket returns a temporary directory that can safely hold unix
14+
// sockets (probably).
15+
//
16+
// During tests on darwin we hit the max path length limit for unix sockets
17+
// pretty easily in the default location, so this function uses /tmp instead to
18+
// get shorter paths.
19+
func TempDirUnixSocket(t *testing.T) string {
20+
t.Helper()
21+
if runtime.GOOS == "darwin" {
22+
testName := strings.ReplaceAll(t.Name(), "/", "_")
23+
dir, err := os.MkdirTemp("/tmp", testName)
24+
require.NoError(t, err, "create temp dir for gpg test")
25+
26+
t.Cleanup(func() {
27+
err := os.RemoveAll(dir)
28+
assert.NoError(t, err, "remove temp dir", dir)
29+
})
30+
return dir
31+
}
32+
33+
return t.TempDir()
34+
}

0 commit comments

Comments
 (0)