forked from coder/coder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunixsocket.go
More file actions
49 lines (43 loc) · 1.33 KB
/
unixsocket.go
File metadata and controls
49 lines (43 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package testutil
import (
"crypto/rand"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// 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.
//
// On Linux, we also hit this limit on GitHub Actions runners where TMPDIR is
// set to a long path like /home/runner/work/_temp/go-tmp/.
func TempDirUnixSocket(t *testing.T) string {
t.Helper()
// Windows doesn't have the same unix socket path length limits,
// and callers of this function are generally gated to !windows.
if runtime.GOOS == "windows" {
return t.TempDir()
}
testName := strings.ReplaceAll(t.Name(), "/", "_")
dir, err := os.MkdirTemp("/tmp", testName)
require.NoError(t, err, "create temp dir for unix socket test")
t.Cleanup(func() {
err := os.RemoveAll(dir)
assert.NoError(t, err, "remove temp dir", dir)
})
return dir
}
func AgentSocketPath(t *testing.T) string {
if runtime.GOOS == "windows" {
return fmt.Sprintf(`\\.\pipe\com.coder.agentsocket_test.%s.%s`, t.Name(), rand.Text())
}
return filepath.Join(TempDirUnixSocket(t), "test.sock")
}