Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use t.TempDir to create temporary test directory #1856

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 14 additions & 25 deletions internal/proxy/fuse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,6 @@ import (
"github.com/hanwen/go-fuse/v2/fs"
)

func randTmpDir(t interface {
Fatalf(format string, args ...interface{})
}) string {
name, err := os.MkdirTemp("", "*")
if err != nil {
t.Fatalf("failed to create tmp dir: %v", err)
}
return name
}

// newTestClient is a convenience function for testing that creates a
// proxy.Client and starts it. The returned cleanup function is also a
// convenience. Callers may choose to ignore it and manually close the client.
Expand Down Expand Up @@ -68,9 +58,9 @@ func TestFUSEREADME(t *testing.T) {
if testing.Short() {
t.Skip("skipping fuse tests in short mode.")
}
dir := randTmpDir(t)
dir := t.TempDir()
d := &fakeDialer{}
_, cleanup := newTestClient(t, d, dir, randTmpDir(t))
_, cleanup := newTestClient(t, d, dir, t.TempDir())

fi, err := os.Stat(dir)
if err != nil {
Expand Down Expand Up @@ -131,8 +121,8 @@ func TestFUSEDialInstance(t *testing.T) {
if testing.Short() {
t.Skip("skipping fuse tests in short mode.")
}
fuseDir := randTmpDir(t)
fuseTempDir := randTmpDir(t)
fuseDir := t.TempDir()
fuseTempDir := t.TempDir()
tcs := []struct {
desc string
wantInstance string
Expand Down Expand Up @@ -181,7 +171,6 @@ func TestFUSEDialInstance(t *testing.T) {
if want, inst := tc.wantInstance, got[0]; want != inst {
t.Fatalf("instance: want = %v, got = %v", want, inst)
}

})
}
}
Expand All @@ -190,8 +179,8 @@ func TestFUSEReadDir(t *testing.T) {
if testing.Short() {
t.Skip("skipping fuse tests in short mode.")
}
fuseDir := randTmpDir(t)
_, cleanup := newTestClient(t, &fakeDialer{}, fuseDir, randTmpDir(t))
fuseDir := t.TempDir()
_, cleanup := newTestClient(t, &fakeDialer{}, fuseDir, t.TempDir())
defer cleanup()

// Initiate a connection so the FUSE server will list it in the dir entries.
Expand Down Expand Up @@ -221,7 +210,7 @@ func TestFUSEErrors(t *testing.T) {
}
ctx := context.Background()
d := &fakeDialer{}
c, _ := newTestClient(t, d, randTmpDir(t), randTmpDir(t))
c, _ := newTestClient(t, d, t.TempDir(), t.TempDir())

// Simulate FUSE file access by invoking Lookup directly to control
// how the socket cache is populated.
Expand Down Expand Up @@ -259,9 +248,9 @@ func TestFUSEWithBadInstanceName(t *testing.T) {
if testing.Short() {
t.Skip("skipping fuse tests in short mode.")
}
fuseDir := randTmpDir(t)
fuseDir := t.TempDir()
d := &fakeDialer{}
_, cleanup := newTestClient(t, d, fuseDir, randTmpDir(t))
_, cleanup := newTestClient(t, d, fuseDir, t.TempDir())
defer cleanup()

_, dialErr := net.Dial("unix", filepath.Join(fuseDir, "notvalid"))
Expand All @@ -278,9 +267,9 @@ func TestFUSECheckConnections(t *testing.T) {
if testing.Short() {
t.Skip("skipping fuse tests in short mode.")
}
fuseDir := randTmpDir(t)
fuseDir := t.TempDir()
d := &fakeDialer{}
c, cleanup := newTestClient(t, d, fuseDir, randTmpDir(t))
c, cleanup := newTestClient(t, d, fuseDir, t.TempDir())
defer cleanup()

// first establish a connection to "register" it with the proxy
Expand Down Expand Up @@ -313,9 +302,9 @@ func TestFUSEClose(t *testing.T) {
if testing.Short() {
t.Skip("skipping fuse tests in short mode.")
}
fuseDir := randTmpDir(t)
fuseDir := t.TempDir()
d := &fakeDialer{}
c, _ := newTestClient(t, d, fuseDir, randTmpDir(t))
c, _ := newTestClient(t, d, fuseDir, t.TempDir())

// first establish a connection to "register" it with the proxy
conn := tryDialUnix(t, filepath.Join(fuseDir, "proj:reg:mysql"))
Expand All @@ -336,7 +325,7 @@ func TestFUSEWithBadDir(t *testing.T) {
if testing.Short() {
t.Skip("skipping fuse tests in short mode.")
}
conf := &proxy.Config{FUSEDir: "/not/a/dir", FUSETempDir: randTmpDir(t)}
conf := &proxy.Config{FUSEDir: "/not/a/dir", FUSETempDir: t.TempDir()}
_, err := proxy.NewClient(context.Background(), &fakeDialer{}, testLogger, conf)
if err == nil {
t.Fatal("proxy client should fail with bad dir")
Expand Down
20 changes: 2 additions & 18 deletions internal/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,26 +100,12 @@ func (*errorDialer) Close() error {
return errors.New("errorDialer returns error on Close")
}

func createTempDir(t *testing.T) (string, func()) {
testDir, err := os.MkdirTemp("", "*")
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
return testDir, func() {
if err := os.RemoveAll(testDir); err != nil {
t.Logf("failed to cleanup temp dir: %v", err)
}
}
}

func TestClientInitialization(t *testing.T) {
ctx := context.Background()
testDir, cleanup := createTempDir(t)
testDir := t.TempDir()
testUnixSocketPath := path.Join(testDir, "db")
testUnixSocketPathPg := path.Join(testDir, "db", ".s.PGSQL.5432")

defer cleanup()

tcs := []struct {
desc string
in *proxy.Config
Expand Down Expand Up @@ -518,8 +504,7 @@ func TestClientInitializationWorksRepeatedly(t *testing.T) {
// it on shutdown. This test ensures the existing socket does not cause
// problems for a second invocation.
ctx := context.Background()
testDir, cleanup := createTempDir(t)
defer cleanup()
testDir := t.TempDir()

in := &proxy.Config{
UnixSocket: testDir,
Expand Down Expand Up @@ -709,5 +694,4 @@ func TestRunConnectionCheck(t *testing.T) {
if err := verifyDialAttempts(); err != nil {
t.Fatal(err)
}

}
6 changes: 2 additions & 4 deletions tests/fuse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ func TestPostgresFUSEConnect(t *testing.T) {
if testing.Short() {
t.Skip("skipping Postgres integration tests")
}
tmpDir, cleanup := createTempDir(t)
defer cleanup()
tmpDir := t.TempDir()

host := proxy.UnixAddress(tmpDir, *postgresConnName)
dsn := fmt.Sprintf(
Expand All @@ -41,8 +40,7 @@ func TestPostgresFUSEConnect(t *testing.T) {
}

func testFUSE(t *testing.T, tmpDir, host string, dsn string) {
tmpDir2, cleanup2 := createTempDir(t)
defer cleanup2()
tmpDir2 := t.TempDir()

waitForFUSE := func() error {
var err error
Expand Down
3 changes: 1 addition & 2 deletions tests/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ func TestMySQLUnix(t *testing.T) {
t.Skip("skipping MySQL integration tests")
}
requireMySQLVars(t)
tmpDir, cleanup := createTempDir(t)
defer cleanup()
tmpDir := t.TempDir()

cfg := mysql.Config{
User: *mysqlUser,
Expand Down
15 changes: 1 addition & 14 deletions tests/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ func TestPostgresUnix(t *testing.T) {
t.Skip("skipping Postgres integration tests")
}
requirePostgresVars(t)
tmpDir, cleanup := createTempDir(t)
defer cleanup()
tmpDir := t.TempDir()

dsn := fmt.Sprintf("host=%s user=%s password=%s database=%s sslmode=disable",
// re-use utility function to determine the Unix address in a
Expand All @@ -79,18 +78,6 @@ func TestPostgresUnix(t *testing.T) {
[]string{"--unix-socket", tmpDir, *postgresConnName}, "pgx", dsn)
}

func createTempDir(t *testing.T) (string, func()) {
testDir, err := os.MkdirTemp("", "*")
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
return testDir, func() {
if err := os.RemoveAll(testDir); err != nil {
t.Logf("failed to cleanup temp dir: %v", err)
}
}
}

func TestPostgresImpersonation(t *testing.T) {
if testing.Short() {
t.Skip("skipping Postgres integration tests")
Expand Down
Loading