mirror of
https://github.com/pgsty/minio.git
synced 2026-03-16 17:53:43 +01:00
test: use T.TempDir to create temporary test directory (#15400)
This commit replaces `ioutil.TempDir` with `t.TempDir` in tests. The
directory created by `t.TempDir` is automatically removed when the test
and all its subtests complete.
Prior to this commit, temporary directory created using `ioutil.TempDir`
needs to be removed manually by calling `os.RemoveAll`, which is omitted
in some tests. The error handling boilerplate e.g.
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Fatal(err)
}
}
is also tedious, but `t.TempDir` handles this for us nicely.
Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
@@ -20,9 +20,7 @@ package cmd
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"testing"
|
||||
@@ -439,25 +437,21 @@ func testStorageAPIRenameFile(t *testing.T, storage StorageAPI) {
|
||||
}
|
||||
}
|
||||
|
||||
func newStorageRESTHTTPServerClient(t *testing.T) (*httptest.Server, *storageRESTClient, string) {
|
||||
func newStorageRESTHTTPServerClient(t *testing.T) *storageRESTClient {
|
||||
prevHost, prevPort := globalMinioHost, globalMinioPort
|
||||
defer func() {
|
||||
globalMinioHost, globalMinioPort = prevHost, prevPort
|
||||
}()
|
||||
|
||||
endpointPath, err := ioutil.TempDir("", ".TestStorageREST.")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error %v", err)
|
||||
}
|
||||
|
||||
router := mux.NewRouter()
|
||||
httpServer := httptest.NewServer(router)
|
||||
t.Cleanup(httpServer.Close)
|
||||
|
||||
url, err := xnet.ParseHTTPURL(httpServer.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error %v", err)
|
||||
}
|
||||
url.Path = endpointPath
|
||||
url.Path = t.TempDir()
|
||||
|
||||
globalMinioHost, globalMinioPort = mustSplitHostPort(url.Host)
|
||||
|
||||
@@ -476,101 +470,77 @@ func newStorageRESTHTTPServerClient(t *testing.T) (*httptest.Server, *storageRES
|
||||
|
||||
restClient := newStorageRESTClient(endpoint, false)
|
||||
|
||||
return httpServer, restClient, endpointPath
|
||||
return restClient
|
||||
}
|
||||
|
||||
func TestStorageRESTClientDiskInfo(t *testing.T) {
|
||||
httpServer, restClient, endpointPath := newStorageRESTHTTPServerClient(t)
|
||||
defer httpServer.Close()
|
||||
defer os.RemoveAll(endpointPath)
|
||||
restClient := newStorageRESTHTTPServerClient(t)
|
||||
|
||||
testStorageAPIDiskInfo(t, restClient)
|
||||
}
|
||||
|
||||
func TestStorageRESTClientMakeVol(t *testing.T) {
|
||||
httpServer, restClient, endpointPath := newStorageRESTHTTPServerClient(t)
|
||||
defer httpServer.Close()
|
||||
defer os.RemoveAll(endpointPath)
|
||||
restClient := newStorageRESTHTTPServerClient(t)
|
||||
|
||||
testStorageAPIMakeVol(t, restClient)
|
||||
}
|
||||
|
||||
func TestStorageRESTClientListVols(t *testing.T) {
|
||||
httpServer, restClient, endpointPath := newStorageRESTHTTPServerClient(t)
|
||||
defer httpServer.Close()
|
||||
defer os.RemoveAll(endpointPath)
|
||||
restClient := newStorageRESTHTTPServerClient(t)
|
||||
|
||||
testStorageAPIListVols(t, restClient)
|
||||
}
|
||||
|
||||
func TestStorageRESTClientStatVol(t *testing.T) {
|
||||
httpServer, restClient, endpointPath := newStorageRESTHTTPServerClient(t)
|
||||
defer httpServer.Close()
|
||||
defer os.RemoveAll(endpointPath)
|
||||
restClient := newStorageRESTHTTPServerClient(t)
|
||||
|
||||
testStorageAPIStatVol(t, restClient)
|
||||
}
|
||||
|
||||
func TestStorageRESTClientDeleteVol(t *testing.T) {
|
||||
httpServer, restClient, endpointPath := newStorageRESTHTTPServerClient(t)
|
||||
defer httpServer.Close()
|
||||
defer os.RemoveAll(endpointPath)
|
||||
restClient := newStorageRESTHTTPServerClient(t)
|
||||
|
||||
testStorageAPIDeleteVol(t, restClient)
|
||||
}
|
||||
|
||||
func TestStorageRESTClientStatInfoFile(t *testing.T) {
|
||||
httpServer, restClient, endpointPath := newStorageRESTHTTPServerClient(t)
|
||||
defer httpServer.Close()
|
||||
defer os.RemoveAll(endpointPath)
|
||||
restClient := newStorageRESTHTTPServerClient(t)
|
||||
|
||||
testStorageAPIStatInfoFile(t, restClient)
|
||||
}
|
||||
|
||||
func TestStorageRESTClientListDir(t *testing.T) {
|
||||
httpServer, restClient, endpointPath := newStorageRESTHTTPServerClient(t)
|
||||
defer httpServer.Close()
|
||||
defer os.RemoveAll(endpointPath)
|
||||
restClient := newStorageRESTHTTPServerClient(t)
|
||||
|
||||
testStorageAPIListDir(t, restClient)
|
||||
}
|
||||
|
||||
func TestStorageRESTClientReadAll(t *testing.T) {
|
||||
httpServer, restClient, endpointPath := newStorageRESTHTTPServerClient(t)
|
||||
defer httpServer.Close()
|
||||
defer os.RemoveAll(endpointPath)
|
||||
restClient := newStorageRESTHTTPServerClient(t)
|
||||
|
||||
testStorageAPIReadAll(t, restClient)
|
||||
}
|
||||
|
||||
func TestStorageRESTClientReadFile(t *testing.T) {
|
||||
httpServer, restClient, endpointPath := newStorageRESTHTTPServerClient(t)
|
||||
defer httpServer.Close()
|
||||
defer os.RemoveAll(endpointPath)
|
||||
restClient := newStorageRESTHTTPServerClient(t)
|
||||
|
||||
testStorageAPIReadFile(t, restClient)
|
||||
}
|
||||
|
||||
func TestStorageRESTClientAppendFile(t *testing.T) {
|
||||
httpServer, restClient, endpointPath := newStorageRESTHTTPServerClient(t)
|
||||
defer httpServer.Close()
|
||||
defer os.RemoveAll(endpointPath)
|
||||
restClient := newStorageRESTHTTPServerClient(t)
|
||||
|
||||
testStorageAPIAppendFile(t, restClient)
|
||||
}
|
||||
|
||||
func TestStorageRESTClientDeleteFile(t *testing.T) {
|
||||
httpServer, restClient, endpointPath := newStorageRESTHTTPServerClient(t)
|
||||
defer httpServer.Close()
|
||||
defer os.RemoveAll(endpointPath)
|
||||
restClient := newStorageRESTHTTPServerClient(t)
|
||||
|
||||
testStorageAPIDeleteFile(t, restClient)
|
||||
}
|
||||
|
||||
func TestStorageRESTClientRenameFile(t *testing.T) {
|
||||
httpServer, restClient, endpointPath := newStorageRESTHTTPServerClient(t)
|
||||
defer httpServer.Close()
|
||||
defer os.RemoveAll(endpointPath)
|
||||
restClient := newStorageRESTHTTPServerClient(t)
|
||||
|
||||
testStorageAPIRenameFile(t, restClient)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user