diff --git a/internal/proxy/fuse_test.go b/internal/proxy/fuse_test.go index 2068e4618..584aee153 100644 --- a/internal/proxy/fuse_test.go +++ b/internal/proxy/fuse_test.go @@ -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. @@ -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 { @@ -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 @@ -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) } - }) } } @@ -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. @@ -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. @@ -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")) @@ -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 @@ -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")) @@ -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") diff --git a/internal/proxy/proxy_test.go b/internal/proxy/proxy_test.go index 8d509295e..96d7f7040 100644 --- a/internal/proxy/proxy_test.go +++ b/internal/proxy/proxy_test.go @@ -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 @@ -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, @@ -709,5 +694,4 @@ func TestRunConnectionCheck(t *testing.T) { if err := verifyDialAttempts(); err != nil { t.Fatal(err) } - } diff --git a/tests/fuse_test.go b/tests/fuse_test.go index e52a5e8b1..0b6c0df77 100644 --- a/tests/fuse_test.go +++ b/tests/fuse_test.go @@ -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( @@ -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 diff --git a/tests/mysql_test.go b/tests/mysql_test.go index b26247ec3..bde99a76c 100644 --- a/tests/mysql_test.go +++ b/tests/mysql_test.go @@ -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, diff --git a/tests/postgres_test.go b/tests/postgres_test.go index baf02b867..08c8cba5f 100644 --- a/tests/postgres_test.go +++ b/tests/postgres_test.go @@ -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 @@ -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")