Skip to content

Commit

Permalink
Merge pull request #84694 from knz/backport22.1-84532
Browse files Browse the repository at this point in the history
  • Loading branch information
knz authored Jul 22, 2022
2 parents 4bd7dc6 + 36486a1 commit 42fb83a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 16 deletions.
16 changes: 10 additions & 6 deletions pkg/sql/pgwire/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
"github.com/cockroachdb/errors/stdstrings"
"github.com/cockroachdb/redact"
"github.com/lib/pq"
"github.com/stretchr/testify/require"
)

// TestAuthenticationAndHBARules exercises the authentication code
Expand Down Expand Up @@ -138,24 +139,27 @@ func makeSocketFile(t *testing.T) (socketDir, socketFile string, cleanupFn func(
// Unix sockets not supported on windows.
return "", "", func() {}
}
socketName := ".s.PGSQL." + socketConnVirtualPort
// We need a temp directory in which we'll create the unix socket.
//
// On BSD, binding to a socket is limited to a path length of 104 characters
// (including the NUL terminator). In glibc, this limit is 108 characters.
//
// macOS has a tendency to produce very long temporary directory names, so
// we are careful to keep all the constants involved short.
baseTmpDir := ""
if runtime.GOOS == "darwin" || strings.Contains(runtime.GOOS, "bsd") {
baseTmpDir := os.TempDir()
if len(baseTmpDir) >= 104-1-len(socketName)-1-len("TestAuth")-10 {
t.Logf("temp dir name too long: %s", baseTmpDir)
t.Logf("using /tmp instead.")
// Note: /tmp might fail in some systems, that's why we still prefer
// os.TempDir() if available.
baseTmpDir = "/tmp"
}
tempDir, err := ioutil.TempDir(baseTmpDir, "TestAuth")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
// ".s.PGSQL.NNNN" is the standard unix socket name supported by pg clients.
return tempDir,
filepath.Join(tempDir, ".s.PGSQL."+socketConnVirtualPort),
filepath.Join(tempDir, socketName),
func() { _ = os.RemoveAll(tempDir) }
}

Expand Down
13 changes: 13 additions & 0 deletions pkg/util/sdnotify/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -25,42 +25,55 @@ go_test(
deps = select({
"@io_bazel_rules_go//go/platform:aix": [
"//pkg/util/log",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:android": [
"//pkg/util/log",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:darwin": [
"//pkg/util/log",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:dragonfly": [
"//pkg/util/log",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:freebsd": [
"//pkg/util/log",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:illumos": [
"//pkg/util/log",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:ios": [
"//pkg/util/log",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:js": [
"//pkg/util/log",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:linux": [
"//pkg/util/log",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:netbsd": [
"//pkg/util/log",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:openbsd": [
"//pkg/util/log",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:plan9": [
"//pkg/util/log",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:solaris": [
"//pkg/util/log",
"@com_github_stretchr_testify//require",
],
"//conditions:default": [],
}),
Expand Down
14 changes: 7 additions & 7 deletions pkg/util/sdnotify/sdnotify_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ func notify(path, msg string) error {
}

func bgExec(cmd *exec.Cmd) error {
l, err := listen()
dir, err := ioutil.TempDir("", "sdnotify")
if err != nil {
return err
}

l, err := listen(dir)
if err != nil {
return err
}
Expand Down Expand Up @@ -97,12 +102,7 @@ type listener struct {
conn *net.UnixConn
}

func listen() (listener, error) {
dir, err := ioutil.TempDir("", "sdnotify")
if err != nil {
return listener{}, err
}

func listen(dir string) (listener, error) {
path := filepath.Join(dir, "notify.sock")
conn, err := net.ListenUnixgram(netType, &net.UnixAddr{
Net: netType,
Expand Down
22 changes: 19 additions & 3 deletions pkg/util/sdnotify/sdnotify_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,32 @@
package sdnotify

import (
"io/ioutil"
"os"
"testing"

_ "github.com/cockroachdb/cockroach/pkg/util/log" // for flags
"github.com/stretchr/testify/require"
)

func TestSDNotify(t *testing.T) {
l, err := listen()
if err != nil {
t.Fatal(err)
tmpDir := os.TempDir()
// On BSD, binding to a socket is limited to a path length of 104 characters
// (including the NUL terminator). In glibc, this limit is 108 characters.
// macOS also has a tendency to produce very long temporary directory names.
if len(tmpDir) >= 104-1-len("sdnotify/notify.sock")-10 {
// Perhaps running inside a sandbox?
t.Logf("default temp dir name is too long: %s", tmpDir)
t.Logf("forcing use of /tmp instead")
// Note: Using /tmp may fail on some systems; this is why we
// prefer os.TempDir() by default.
tmpDir = "/tmp"
}
dir, err := ioutil.TempDir(tmpDir, "sdnotify")
require.NoError(t, err)

l, err := listen(dir)
require.NoError(t, err)
defer func() { _ = l.close() }()

ch := make(chan error)
Expand Down

0 comments on commit 42fb83a

Please sign in to comment.