@@ -2,15 +2,10 @@ package agentsocket_test
22
33import (
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.
4718func 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 ),
0 commit comments