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:
Eng Zer Jun 2022-07-26 03:37:26 +08:00 committed by GitHub
parent f23f442d33
commit 0a3b1ad4eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 137 additions and 412 deletions

View File

@ -20,17 +20,11 @@ package cmd
import (
"context"
"io"
"io/ioutil"
"os"
"testing"
)
func testBitrotReaderWriterAlgo(t *testing.T, bitrotAlgo BitrotAlgorithm) {
tmpDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
tmpDir := t.TempDir()
volume := "testvol"
filePath := "testfile"
@ -60,7 +54,9 @@ func testBitrotReaderWriterAlgo(t *testing.T, bitrotAlgo BitrotAlgorithm) {
if err != nil {
t.Fatal(err)
}
writer.(io.Closer).Close()
if bw, ok := writer.(io.Closer); ok {
bw.Close()
}
reader := newBitrotReader(disk, nil, volume, filePath, 35, bitrotAlgo, bitrotWriterSum(writer), 10)
b := make([]byte, 10)
@ -76,6 +72,9 @@ func testBitrotReaderWriterAlgo(t *testing.T, bitrotAlgo BitrotAlgorithm) {
if _, err = reader.ReadAt(b[:5], 30); err != nil {
t.Fatal(err)
}
if br, ok := reader.(io.Closer); ok {
br.Close()
}
}
func TestAllBitrotAlgorithms(t *testing.T) {

View File

@ -37,11 +37,7 @@ func TestServerConfigMigrateV1(t *testing.T) {
if err != nil {
t.Fatalf("Init Test config failed")
}
rootPath, err := ioutil.TempDir(globalTestTmpDir, "minio-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(rootPath)
rootPath := t.TempDir()
globalConfigDir = &ConfigDir{path: rootPath}
globalObjLayerMutex.Lock()
@ -74,13 +70,7 @@ func TestServerConfigMigrateV1(t *testing.T) {
// Test if all migrate code returns nil when config file does not
// exist
func TestServerConfigMigrateInexistentConfig(t *testing.T) {
rootPath, err := ioutil.TempDir(globalTestTmpDir, "minio-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(rootPath)
globalConfigDir = &ConfigDir{path: rootPath}
globalConfigDir = &ConfigDir{path: t.TempDir()}
if err := migrateV2ToV3(); err != nil {
t.Fatal("migrate v2 to v3 should succeed when no config file is found")
@ -164,11 +154,7 @@ func TestServerConfigMigrateInexistentConfig(t *testing.T) {
// Test if a config migration from v2 to v33 is successfully done
func TestServerConfigMigrateV2toV33(t *testing.T) {
rootPath, err := ioutil.TempDir(globalTestTmpDir, "minio-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(rootPath)
rootPath := t.TempDir()
globalConfigDir = &ConfigDir{path: rootPath}
@ -234,11 +220,7 @@ func TestServerConfigMigrateV2toV33(t *testing.T) {
// Test if all migrate code returns error with corrupted config files
func TestServerConfigMigrateFaultyConfig(t *testing.T) {
rootPath, err := ioutil.TempDir(globalTestTmpDir, "minio-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(rootPath)
rootPath := t.TempDir()
globalConfigDir = &ConfigDir{path: rootPath}
configPath := rootPath + SlashSeparator + minioConfigFile
@ -331,35 +313,31 @@ func TestServerConfigMigrateFaultyConfig(t *testing.T) {
// Test if all migrate code returns error with corrupted config files
func TestServerConfigMigrateCorruptedConfig(t *testing.T) {
rootPath, err := ioutil.TempDir(globalTestTmpDir, "minio-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(rootPath)
rootPath := t.TempDir()
globalConfigDir = &ConfigDir{path: rootPath}
configPath := rootPath + SlashSeparator + minioConfigFile
for i := 3; i <= 17; i++ {
// Create a corrupted config file
if err = ioutil.WriteFile(configPath, []byte(fmt.Sprintf("{ \"version\":\"%d\", \"credential\": { \"accessKey\": 1 } }", i)),
if err := ioutil.WriteFile(configPath, []byte(fmt.Sprintf("{ \"version\":\"%d\", \"credential\": { \"accessKey\": 1 } }", i)),
0o644); err != nil {
t.Fatal("Unexpected error: ", err)
}
// Test different migrate versions and be sure they are returning an error
if err = migrateConfig(); err == nil {
if err := migrateConfig(); err == nil {
t.Fatal("migrateConfig() should fail with a corrupted json")
}
}
// Create a corrupted config file for version '2'.
if err = ioutil.WriteFile(configPath, []byte("{ \"version\":\"2\", \"credentials\": { \"accessKeyId\": 1 } }"), 0o644); err != nil {
if err := ioutil.WriteFile(configPath, []byte("{ \"version\":\"2\", \"credentials\": { \"accessKeyId\": 1 } }"), 0o644); err != nil {
t.Fatal("Unexpected error: ", err)
}
// Test different migrate versions and be sure they are returning an error
if err = migrateConfig(); err == nil {
if err := migrateConfig(); err == nil {
t.Fatal("migrateConfig() should fail with a corrupted json")
}
}

View File

@ -21,7 +21,6 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"math/rand"
"os"
"path"
@ -100,15 +99,11 @@ func TestDataUpdateTracker(t *testing.T) {
dut.Current.bf = dut.newBloomFilter()
tmpDir, err := ioutil.TempDir("", "TestDataUpdateTracker")
tmpDir := t.TempDir()
err := os.MkdirAll(filepath.Dir(filepath.Join(tmpDir, dataUpdateTrackerFilename)), os.ModePerm)
if err != nil {
t.Fatal(err)
}
err = os.MkdirAll(filepath.Dir(filepath.Join(tmpDir, dataUpdateTrackerFilename)), os.ModePerm)
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
dut.start(ctx, tmpDir)

View File

@ -35,12 +35,8 @@ type usageTestFile struct {
}
func TestDataUsageUpdate(t *testing.T) {
base, err := ioutil.TempDir("", "TestDataUsageUpdate")
if err != nil {
t.Skip(err)
}
base := t.TempDir()
const bucket = "bucket"
defer os.RemoveAll(base)
files := []usageTestFile{
{name: "rootfile", size: 10000},
{name: "rootfile2", size: 10000},
@ -251,12 +247,8 @@ func TestDataUsageUpdate(t *testing.T) {
}
func TestDataUsageUpdatePrefix(t *testing.T) {
base, err := ioutil.TempDir("", "TestDataUpdateUsagePrefix")
if err != nil {
t.Skip(err)
}
base := t.TempDir()
scannerSleeper.Update(0, 0)
defer os.RemoveAll(base)
files := []usageTestFile{
{name: "bucket/rootfile", size: 10000},
{name: "bucket/rootfile2", size: 10000},
@ -537,12 +529,8 @@ func generateUsageTestFiles(t *testing.T, base, bucket string, nFolders, nFiles,
}
func TestDataUsageCacheSerialize(t *testing.T) {
base, err := ioutil.TempDir("", "TestDataUsageCacheSerialize")
if err != nil {
t.Skip(err)
}
base := t.TempDir()
const bucket = "abucket"
defer os.RemoveAll(base)
files := []usageTestFile{
{name: "rootfile", size: 10000},
{name: "rootfile2", size: 10000},

View File

@ -85,19 +85,17 @@ var erasureDecodeTests = []struct {
func TestErasureDecode(t *testing.T) {
for i, test := range erasureDecodeTests {
setup, err := newErasureTestSetup(test.dataBlocks, test.onDisks-test.dataBlocks, test.blocksize)
setup, err := newErasureTestSetup(t, test.dataBlocks, test.onDisks-test.dataBlocks, test.blocksize)
if err != nil {
t.Fatalf("Test %d: failed to create test setup: %v", i, err)
}
erasure, err := NewErasure(context.Background(), test.dataBlocks, test.onDisks-test.dataBlocks, test.blocksize)
if err != nil {
setup.Remove()
t.Fatalf("Test %d: failed to create ErasureStorage: %v", i, err)
}
disks := setup.disks
data := make([]byte, test.data)
if _, err = io.ReadFull(crand.Reader, data); err != nil {
setup.Remove()
t.Fatalf("Test %d: failed to generate random test data: %v", i, err)
}
@ -113,11 +111,9 @@ func TestErasureDecode(t *testing.T) {
n, err := erasure.Encode(context.Background(), bytes.NewReader(data), writers, buffer, erasure.dataBlocks+1)
closeBitrotWriters(writers)
if err != nil {
setup.Remove()
t.Fatalf("Test %d: failed to create erasure test file: %v", i, err)
}
if n != test.data {
setup.Remove()
t.Fatalf("Test %d: failed to create erasure test file", i)
}
for i, w := range writers {
@ -195,7 +191,6 @@ func TestErasureDecode(t *testing.T) {
}
}
}
setup.Remove()
}
}
@ -210,12 +205,11 @@ func TestErasureDecodeRandomOffsetLength(t *testing.T) {
dataBlocks := 7
parityBlocks := 7
blockSize := int64(1 * humanize.MiByte)
setup, err := newErasureTestSetup(dataBlocks, parityBlocks, blockSize)
setup, err := newErasureTestSetup(t, dataBlocks, parityBlocks, blockSize)
if err != nil {
t.Error(err)
return
}
defer setup.Remove()
disks := setup.disks
erasure, err := NewErasure(context.Background(), dataBlocks, parityBlocks, blockSize)
if err != nil {
@ -288,11 +282,10 @@ func TestErasureDecodeRandomOffsetLength(t *testing.T) {
// Benchmarks
func benchmarkErasureDecode(data, parity, dataDown, parityDown int, size int64, b *testing.B) {
setup, err := newErasureTestSetup(data, parity, blockSizeV2)
setup, err := newErasureTestSetup(b, data, parity, blockSizeV2)
if err != nil {
b.Fatalf("failed to create test setup: %v", err)
}
defer setup.Remove()
disks := setup.disks
erasure, err := NewErasure(context.Background(), data, parity, blockSizeV2)
if err != nil {

View File

@ -87,21 +87,19 @@ var erasureEncodeTests = []struct {
func TestErasureEncode(t *testing.T) {
for i, test := range erasureEncodeTests {
setup, err := newErasureTestSetup(test.dataBlocks, test.onDisks-test.dataBlocks, test.blocksize)
setup, err := newErasureTestSetup(t, test.dataBlocks, test.onDisks-test.dataBlocks, test.blocksize)
if err != nil {
t.Fatalf("Test %d: failed to create test setup: %v", i, err)
}
disks := setup.disks
erasure, err := NewErasure(context.Background(), test.dataBlocks, test.onDisks-test.dataBlocks, test.blocksize)
if err != nil {
setup.Remove()
t.Fatalf("Test %d: failed to create ErasureStorage: %v", i, err)
}
buffer := make([]byte, test.blocksize, 2*test.blocksize)
data := make([]byte, test.data)
if _, err = io.ReadFull(rand.Reader, data); err != nil {
setup.Remove()
t.Fatalf("Test %d: failed to generate random test data: %v", i, err)
}
writers := make([]io.Writer, len(disks))
@ -160,18 +158,16 @@ func TestErasureEncode(t *testing.T) {
}
}
}
setup.Remove()
}
}
// Benchmarks
func benchmarkErasureEncode(data, parity, dataDown, parityDown int, size int64, b *testing.B) {
setup, err := newErasureTestSetup(data, parity, blockSizeV2)
setup, err := newErasureTestSetup(b, data, parity, blockSizeV2)
if err != nil {
b.Fatalf("failed to create test setup: %v", err)
}
defer setup.Remove()
erasure, err := NewErasure(context.Background(), data, parity, blockSizeV2)
if err != nil {
b.Fatalf("failed to create ErasureStorage: %v", err)

View File

@ -70,19 +70,17 @@ func TestErasureHeal(t *testing.T) {
}
// create some test data
setup, err := newErasureTestSetup(test.dataBlocks, test.disks-test.dataBlocks, test.blocksize)
setup, err := newErasureTestSetup(t, test.dataBlocks, test.disks-test.dataBlocks, test.blocksize)
if err != nil {
t.Fatalf("Test %d: failed to setup Erasure environment: %v", i, err)
}
disks := setup.disks
erasure, err := NewErasure(context.Background(), test.dataBlocks, test.disks-test.dataBlocks, test.blocksize)
if err != nil {
setup.Remove()
t.Fatalf("Test %d: failed to create ErasureStorage: %v", i, err)
}
data := make([]byte, test.size)
if _, err = io.ReadFull(rand.Reader, data); err != nil {
setup.Remove()
t.Fatalf("Test %d: failed to create random test data: %v", i, err)
}
buffer := make([]byte, test.blocksize, 2*test.blocksize)
@ -93,7 +91,6 @@ func TestErasureHeal(t *testing.T) {
_, err = erasure.Encode(context.Background(), bytes.NewReader(data), writers, buffer, erasure.dataBlocks+1)
closeBitrotWriters(writers)
if err != nil {
setup.Remove()
t.Fatalf("Test %d: failed to create random test data: %v", i, err)
}
@ -156,6 +153,5 @@ func TestErasureHeal(t *testing.T) {
}
}
}
setup.Remove()
}
}

View File

@ -1073,10 +1073,7 @@ func TestGetObjectInlineNotInline(t *testing.T) {
// Create a backend with 4 disks named disk{1...4}, this name convention
// because we will unzip some object data from a sample archive.
const numDisks = 4
path, err := ioutil.TempDir(globalTestTmpDir, "minio-")
if err != nil {
t.Fatal(err)
}
path := t.TempDir()
var fsDirs []string
for i := 1; i <= numDisks; i++ {

View File

@ -22,7 +22,6 @@ import (
"context"
"crypto/rand"
"io"
"os"
"testing"
)
@ -118,20 +117,13 @@ type erasureTestSetup struct {
disks []StorageAPI
}
// Removes the temporary disk directories.
func (e erasureTestSetup) Remove() {
for _, path := range e.diskPaths {
os.RemoveAll(path)
}
}
// Returns an initialized setup for erasure tests.
func newErasureTestSetup(dataBlocks int, parityBlocks int, blockSize int64) (*erasureTestSetup, error) {
func newErasureTestSetup(tb testing.TB, dataBlocks int, parityBlocks int, blockSize int64) (*erasureTestSetup, error) {
diskPaths := make([]string, dataBlocks+parityBlocks)
disks := make([]StorageAPI, len(diskPaths))
var err error
for i := range diskPaths {
disks[i], diskPaths[i], err = newXLStorageTestSetup()
disks[i], diskPaths[i], err = newXLStorageTestSetup(tb)
if err != nil {
return nil, err
}

View File

@ -104,11 +104,7 @@ func TestFormatErasureEmpty(t *testing.T) {
// Tests xl format migration.
func TestFormatErasureMigrate(t *testing.T) {
// Get test root.
rootPath, err := getTestRoot()
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(rootPath)
rootPath := t.TempDir()
m := &formatErasureV1{}
m.Format = formatBackendErasure

View File

@ -30,11 +30,10 @@ import (
func TestFSRenameFile(t *testing.T) {
// create xlStorage test setup
_, path, err := newXLStorageTestSetup()
_, path, err := newXLStorageTestSetup(t)
if err != nil {
t.Fatalf("Unable to create xlStorage test setup, %s", err)
}
defer os.RemoveAll(path)
if err = fsMkdir(GlobalContext, pathJoin(path, "testvolume1")); err != nil {
t.Fatal(err)
@ -55,11 +54,10 @@ func TestFSRenameFile(t *testing.T) {
func TestFSStats(t *testing.T) {
// create xlStorage test setup
_, path, err := newXLStorageTestSetup()
_, path, err := newXLStorageTestSetup(t)
if err != nil {
t.Fatalf("Unable to create xlStorage test setup, %s", err)
}
defer os.RemoveAll(path)
// Setup test environment.
@ -183,11 +181,10 @@ func TestFSStats(t *testing.T) {
func TestFSCreateAndOpen(t *testing.T) {
// Setup test environment.
_, path, err := newXLStorageTestSetup()
_, path, err := newXLStorageTestSetup(t)
if err != nil {
t.Fatalf("Unable to create xlStorage test setup, %s", err)
}
defer os.RemoveAll(path)
if err = fsMkdir(GlobalContext, pathJoin(path, "success-vol")); err != nil {
t.Fatalf("Unable to create directory, %s", err)
@ -248,11 +245,10 @@ func TestFSCreateAndOpen(t *testing.T) {
func TestFSDeletes(t *testing.T) {
// create xlStorage test setup
_, path, err := newXLStorageTestSetup()
_, path, err := newXLStorageTestSetup(t)
if err != nil {
t.Fatalf("Unable to create xlStorage test setup, %s", err)
}
defer os.RemoveAll(path)
// Setup test environment.
if err = fsMkdir(GlobalContext, pathJoin(path, "success-vol")); err != nil {
@ -350,11 +346,10 @@ func TestFSDeletes(t *testing.T) {
func BenchmarkFSDeleteFile(b *testing.B) {
// create xlStorage test setup
_, path, err := newXLStorageTestSetup()
_, path, err := newXLStorageTestSetup(b)
if err != nil {
b.Fatalf("Unable to create xlStorage test setup, %s", err)
}
defer os.RemoveAll(path)
// Setup test environment.
if err = fsMkdir(GlobalContext, pathJoin(path, "benchmark")); err != nil {
@ -384,11 +379,10 @@ func BenchmarkFSDeleteFile(b *testing.B) {
// Tests fs removes.
func TestFSRemoves(t *testing.T) {
// create xlStorage test setup
_, path, err := newXLStorageTestSetup()
_, path, err := newXLStorageTestSetup(t)
if err != nil {
t.Fatalf("Unable to create xlStorage test setup, %s", err)
}
defer os.RemoveAll(path)
// Setup test environment.
if err = fsMkdir(GlobalContext, pathJoin(path, "success-vol")); err != nil {
@ -501,11 +495,10 @@ func TestFSRemoves(t *testing.T) {
func TestFSRemoveMeta(t *testing.T) {
// create xlStorage test setup
_, fsPath, err := newXLStorageTestSetup()
_, fsPath, err := newXLStorageTestSetup(t)
if err != nil {
t.Fatalf("Unable to create xlStorage test setup, %s", err)
}
defer os.RemoveAll(fsPath)
// Setup test environment.
if err = fsMkdir(GlobalContext, pathJoin(fsPath, "success-vol")); err != nil {
@ -529,10 +522,7 @@ func TestFSRemoveMeta(t *testing.T) {
defer rwPool.Close(filePath)
tmpDir, tmpErr := ioutil.TempDir(globalTestTmpDir, "minio-")
if tmpErr != nil {
t.Fatal(tmpErr)
}
tmpDir := t.TempDir()
if err := fsRemoveMeta(GlobalContext, fsPath, filePath, tmpDir); err != nil {
t.Fatalf("Unable to remove file, %s", err)
@ -548,15 +538,9 @@ func TestFSRemoveMeta(t *testing.T) {
}
func TestFSIsFile(t *testing.T) {
dirPath, err := ioutil.TempDir(globalTestTmpDir, "minio-")
if err != nil {
t.Fatalf("Unable to create tmp directory %s", err)
}
defer os.RemoveAll(dirPath)
filePath := pathJoin(t.TempDir(), "tmpfile")
filePath := pathJoin(dirPath, "tmpfile")
if err = ioutil.WriteFile(filePath, nil, 0o777); err != nil {
if err := ioutil.WriteFile(filePath, nil, 0o777); err != nil {
t.Fatalf("Unable to create file %s", filePath)
}

View File

@ -18,7 +18,6 @@
package cmd
import (
"os"
"runtime"
"testing"
@ -48,11 +47,10 @@ func TestRWPoolLongPath(t *testing.T) {
// Tests all RWPool methods.
func TestRWPool(t *testing.T) {
// create xlStorage test setup
_, path, err := newXLStorageTestSetup()
_, path, err := newXLStorageTestSetup(t)
if err != nil {
t.Fatalf("Unable to create xlStorage test setup, %s", err)
}
defer os.RemoveAll(path)
rwPool := &fsIOPool{
readersMap: make(map[string]*lock.RLockedFile),

View File

@ -23,8 +23,6 @@ import (
"crypto/md5"
"encoding/hex"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"testing"
@ -1886,18 +1884,14 @@ func initFSObjectsB(disk string, t *testing.B) (obj ObjectLayer) {
// BenchmarkListObjects - Run ListObject Repeatedly and benchmark.
func BenchmarkListObjects(b *testing.B) {
// Make a temporary directory to use as the obj.
directory, err := ioutil.TempDir(globalTestTmpDir, "minio-list-benchmark")
if err != nil {
b.Fatal(err)
}
defer os.RemoveAll(directory)
directory := b.TempDir()
// Create the obj.
obj := initFSObjectsB(directory, b)
bucket := "ls-benchmark-bucket"
// Create a bucket.
err = obj.MakeBucketWithLocation(context.Background(), bucket, BucketOptions{})
err := obj.MakeBucketWithLocation(context.Background(), bucket, BucketOptions{})
if err != nil {
b.Fatal(err)
}

View File

@ -67,25 +67,16 @@ type result struct {
entries []string
}
func mustSetupDir(t *testing.T) string {
// Create unique test directory.
dir, err := ioutil.TempDir(globalTestTmpDir, "minio-list-dir")
if err != nil {
t.Fatalf("Unable to setup directory, %s", err)
}
return dir
}
// Test to read empty directory.
func setupTestReadDirEmpty(t *testing.T) (testResults []result) {
// Add empty entry slice for this test directory.
testResults = append(testResults, result{mustSetupDir(t), []string{}})
testResults = append(testResults, result{t.TempDir(), []string{}})
return testResults
}
// Test to read non-empty directory with only files.
func setupTestReadDirFiles(t *testing.T) (testResults []result) {
dir := mustSetupDir(t)
dir := t.TempDir()
entries := []string{}
for i := 0; i < 10; i++ {
name := fmt.Sprintf("file-%d", i)
@ -107,7 +98,7 @@ func setupTestReadDirFiles(t *testing.T) (testResults []result) {
// Test to read non-empty directory with directories and files.
func setupTestReadDirGeneric(t *testing.T) (testResults []result) {
dir := mustSetupDir(t)
dir := t.TempDir()
if err := os.MkdirAll(filepath.Join(dir, "mydir"), 0o777); err != nil {
t.Fatalf("Unable to create prefix directory \"mydir\", %s", err)
}
@ -134,7 +125,7 @@ func setupTestReadDirSymlink(t *testing.T) (testResults []result) {
t.Skip("symlinks not available on windows")
return nil
}
dir := mustSetupDir(t)
dir := t.TempDir()
entries := []string{}
for i := 0; i < 10; i++ {
name1 := fmt.Sprintf("file-%d", i)
@ -241,7 +232,7 @@ func TestReadDirN(t *testing.T) {
}
for i, testCase := range testCases {
dir := mustSetupDir(t)
dir := t.TempDir()
for c := 1; c <= testCase.numFiles; c++ {
err := ioutil.WriteFile(filepath.Join(dir, fmt.Sprintf("%d", c)), []byte{}, os.ModePerm)

View File

@ -18,18 +18,16 @@
package cmd
import (
"os"
"testing"
)
// Tests - mkdirAll()
func TestOSMkdirAll(t *testing.T) {
// create xlStorage test setup
_, path, err := newXLStorageTestSetup()
_, path, err := newXLStorageTestSetup(t)
if err != nil {
t.Fatalf("Unable to create xlStorage test setup, %s", err)
}
defer os.RemoveAll(path)
if err = mkdirAll("", 0o777); err != errInvalidArgument {
t.Fatal("Unexpected error", err)
@ -47,11 +45,10 @@ func TestOSMkdirAll(t *testing.T) {
// Tests - renameAll()
func TestOSRenameAll(t *testing.T) {
// create xlStorage test setup
_, path, err := newXLStorageTestSetup()
_, path, err := newXLStorageTestSetup(t)
if err != nil {
t.Fatalf("Unable to create xlStorage test setup, %s", err)
}
defer os.RemoveAll(path)
if err = mkdirAll(pathJoin(path, "testvolume1"), 0o777); err != nil {
t.Fatal(err)

View File

@ -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)
}

View File

@ -1452,11 +1452,6 @@ func getListenNotificationURL(endPoint, bucketName string, prefixes, suffixes, e
return makeTestTargetURL(endPoint, bucketName, "", queryValue)
}
// returns temp root directory. `
func getTestRoot() (string, error) {
return ioutil.TempDir(globalTestTmpDir, "api-")
}
// getRandomDisks - Creates a slice of N random disks, each of the form - minio-XXX
func getRandomDisks(N int) ([]string, error) {
var erasureDisks []string

View File

@ -20,8 +20,6 @@ package cmd
import (
"context"
"fmt"
"io/ioutil"
"os"
"reflect"
"sort"
"strings"
@ -137,11 +135,7 @@ func testTreeWalkMarker(t *testing.T, listDir ListDirFunc, isLeaf IsLeafFunc, is
// Test tree-walk.
func TestTreeWalk(t *testing.T) {
fsDir, err := ioutil.TempDir(globalTestTmpDir, "minio-")
if err != nil {
t.Fatalf("Unable to create tmp directory: %s", err)
}
defer os.RemoveAll(fsDir)
fsDir := t.TempDir()
endpoints := mustGetNewEndpoints(fsDir)
disk, err := newStorageAPI(endpoints[0])
@ -181,11 +175,7 @@ func TestTreeWalk(t *testing.T) {
// Test if tree walk go-routine exits cleanly if tree walk is aborted because of timeout.
func TestTreeWalkTimeout(t *testing.T) {
fsDir, err := ioutil.TempDir(globalTestTmpDir, "minio-")
if err != nil {
t.Fatalf("Unable to create tmp directory: %s", err)
}
defer os.RemoveAll(fsDir)
fsDir := t.TempDir()
endpoints := mustGetNewEndpoints(fsDir)
disk, err := newStorageAPI(endpoints[0])
if err != nil {
@ -254,11 +244,7 @@ func TestTreeWalkTimeout(t *testing.T) {
// without recursively traversing prefixes.
func TestRecursiveTreeWalk(t *testing.T) {
// Create a backend directories fsDir1.
fsDir1, err := ioutil.TempDir(globalTestTmpDir, "minio-")
if err != nil {
t.Fatalf("Unable to create tmp directory: %s", err)
}
defer os.RemoveAll(fsDir1)
fsDir1 := t.TempDir()
endpoints := mustGetNewEndpoints(fsDir1)
disk1, err := newStorageAPI(endpoints[0])
@ -365,11 +351,7 @@ func TestRecursiveTreeWalk(t *testing.T) {
func TestSortedness(t *testing.T) {
// Create a backend directories fsDir1.
fsDir1, err := ioutil.TempDir(globalTestTmpDir, "minio-")
if err != nil {
t.Errorf("Unable to create tmp directory: %s", err)
}
defer os.RemoveAll(fsDir1)
fsDir1 := t.TempDir()
endpoints := mustGetNewEndpoints(fsDir1)
disk1, err := newStorageAPI(endpoints[0])
@ -440,11 +422,7 @@ func TestSortedness(t *testing.T) {
func TestTreeWalkIsEnd(t *testing.T) {
// Create a backend directories fsDir1.
fsDir1, err := ioutil.TempDir(globalTestTmpDir, "minio-")
if err != nil {
t.Errorf("Unable to create tmp directory: %s", err)
}
defer os.RemoveAll(fsDir1)
fsDir1 := t.TempDir()
endpoints := mustGetNewEndpoints(fsDir1)
disk1, err := newStorageAPI(endpoints[0])

View File

@ -117,11 +117,8 @@ func TestIsValidVolname(t *testing.T) {
// creates a temp dir and sets up xlStorage layer.
// returns xlStorage layer, temp dir path to be used for the purpose of tests.
func newXLStorageTestSetup() (*xlStorageDiskIDCheck, string, error) {
diskPath, err := ioutil.TempDir(globalTestTmpDir, "minio-")
if err != nil {
return nil, "", err
}
func newXLStorageTestSetup(tb testing.TB) (*xlStorageDiskIDCheck, string, error) {
diskPath := tb.TempDir()
// Initialize a new xlStorage layer.
storage, err := newLocalXLStorage(diskPath)
@ -141,64 +138,37 @@ func newXLStorageTestSetup() (*xlStorageDiskIDCheck, string, error) {
// createPermDeniedFile - creates temporary directory and file with path '/mybucket/myobject'
func createPermDeniedFile(t *testing.T) (permDeniedDir string) {
var errMsg string
defer func() {
if errMsg == "" {
return
}
if permDeniedDir != "" {
os.RemoveAll(permDeniedDir)
}
t.Fatalf(errMsg)
}()
var err error
if permDeniedDir, err = ioutil.TempDir(globalTestTmpDir, "minio-"); err != nil {
errMsg = fmt.Sprintf("Unable to create temporary directory. %v", err)
return permDeniedDir
}
permDeniedDir = t.TempDir()
if err = os.Mkdir(slashpath.Join(permDeniedDir, "mybucket"), 0o775); err != nil {
errMsg = fmt.Sprintf("Unable to create temporary directory %v. %v", slashpath.Join(permDeniedDir, "mybucket"), err)
return permDeniedDir
t.Fatalf(fmt.Sprintf("Unable to create temporary directory %v. %v", slashpath.Join(permDeniedDir, "mybucket"), err))
}
if err = ioutil.WriteFile(slashpath.Join(permDeniedDir, "mybucket", "myobject"), []byte(""), 0o400); err != nil {
errMsg = fmt.Sprintf("Unable to create file %v. %v", slashpath.Join(permDeniedDir, "mybucket", "myobject"), err)
return permDeniedDir
t.Fatalf(fmt.Sprintf("Unable to create file %v. %v", slashpath.Join(permDeniedDir, "mybucket", "myobject"), err))
}
if err = os.Chmod(slashpath.Join(permDeniedDir, "mybucket"), 0o400); err != nil {
errMsg = fmt.Sprintf("Unable to change permission to temporary directory %v. %v", slashpath.Join(permDeniedDir, "mybucket"), err)
return permDeniedDir
t.Fatalf(fmt.Sprintf("Unable to change permission to temporary directory %v. %v", slashpath.Join(permDeniedDir, "mybucket"), err))
}
t.Cleanup(func() {
os.Chmod(slashpath.Join(permDeniedDir, "mybucket"), 0o775)
})
if err = os.Chmod(permDeniedDir, 0o400); err != nil {
errMsg = fmt.Sprintf("Unable to change permission to temporary directory %v. %v", permDeniedDir, err)
t.Fatalf(fmt.Sprintf("Unable to change permission to temporary directory %v. %v", permDeniedDir, err))
}
t.Cleanup(func() {
os.Chmod(permDeniedDir, 0o775)
})
return permDeniedDir
}
// removePermDeniedFile - removes temporary directory and file with path '/mybucket/myobject'
func removePermDeniedFile(permDeniedDir string) {
if err := os.Chmod(permDeniedDir, 0o775); err == nil {
if err = os.Chmod(slashpath.Join(permDeniedDir, "mybucket"), 0o775); err == nil {
os.RemoveAll(permDeniedDir)
}
}
}
// TestXLStorages xlStorage.getDiskInfo()
func TestXLStorageGetDiskInfo(t *testing.T) {
path, err := ioutil.TempDir(globalTestTmpDir, "minio-")
if err != nil {
t.Fatalf("Unable to create a temporary directory, %s", err)
}
defer os.RemoveAll(path)
path := t.TempDir()
testCases := []struct {
diskPath string
@ -217,11 +187,7 @@ func TestXLStorageGetDiskInfo(t *testing.T) {
}
func TestXLStorageIsDirEmpty(t *testing.T) {
tmp, err := ioutil.TempDir(globalTestTmpDir, "minio-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmp)
tmp := t.TempDir()
// Should give false on non-existent directory.
dir1 := slashpath.Join(tmp, "non-existent-directory")
@ -231,7 +197,7 @@ func TestXLStorageIsDirEmpty(t *testing.T) {
// Should give false for not-a-directory.
dir2 := slashpath.Join(tmp, "file")
err = ioutil.WriteFile(dir2, []byte("hello"), 0o777)
err := ioutil.WriteFile(dir2, []byte("hello"), 0o777)
if err != nil {
t.Fatal(err)
}
@ -256,13 +222,11 @@ func TestXLStorageReadVersionLegacy(t *testing.T) {
const legacyJSON = `{"version":"1.0.1","format":"xl","stat":{"size":2016,"modTime":"2021-10-11T23:40:34.914361617Z"},"erasure":{"algorithm":"klauspost/reedsolomon/vandermonde","data":2,"parity":2,"blockSize":10485760,"index":2,"distribution":[2,3,4,1],"checksum":[{"name":"part.1","algorithm":"highwayhash256S"}]},"minio":{"release":"RELEASE.2019-12-30T05-45-39Z"},"meta":{"X-Minio-Internal-Server-Side-Encryption-Iv":"kInsJB/0yxyz/40ZI+lmQYJfZacDYqZsGh2wEiv+N50=","X-Minio-Internal-Server-Side-Encryption-S3-Kms-Key-Id":"my-minio-key","X-Minio-Internal-Server-Side-Encryption-S3-Kms-Sealed-Key":"eyJhZWFkIjoiQUVTLTI1Ni1HQ00tSE1BQy1TSEEtMjU2IiwiaWQiOiJjMzEwNDVjODFmMTA2MWU5NTI4ODcxZmNhMmRkYzA3YyIsIml2IjoiOWQ5cUxGMFhSaFBXbEVqT2JDMmo0QT09Iiwibm9uY2UiOiJYaERsemlCU1cwSENuK2RDIiwiYnl0ZXMiOiJUM0lmY1haQ1dtMWpLeWxBWmFUUnczbDVoYldLWW95dm5iNTZVaWJEbE5LOFZVU2tuQmx3NytIMG8yZnRzZ1UrIn0=","X-Minio-Internal-Server-Side-Encryption-S3-Sealed-Key":"IAAfANqt801MT+wwzQRkfFhTrndmhfNiN0alKwDS4AQ1dznNADRQgoq6I4pPVfRsbDp5rQawlripQZvPWUSNJA==","X-Minio-Internal-Server-Side-Encryption-Seal-Algorithm":"DAREv2-HMAC-SHA256","content-type":"application/octet-stream","etag":"20000f00cf5e68d3d6b60e44fcd8b9e8-1"},"parts":[{"number":1,"name":"part.1","etag":"","size":2016,"actualSize":1984}]}`
// create xlStorage test setup
xlStorage, path, err := newXLStorageTestSetup()
xlStorage, _, err := newXLStorageTestSetup(t)
if err != nil {
t.Fatalf("Unable to cfgreate xlStorage test setup, %s", err)
}
defer os.RemoveAll(path)
// Create files for the test cases.
if err = xlStorage.MakeVol(context.Background(), "exists-legacy"); err != nil {
t.Fatalf("Unable to create a volume \"exists-legacy\", %s", err)
@ -285,13 +249,11 @@ func TestXLStorageReadVersionLegacy(t *testing.T) {
// TestXLStorageReadVersion - TestXLStorages the functionality implemented by xlStorage ReadVersion storage API.
func TestXLStorageReadVersion(t *testing.T) {
// create xlStorage test setup
xlStorage, path, err := newXLStorageTestSetup()
xlStorage, _, err := newXLStorageTestSetup(t)
if err != nil {
t.Fatalf("Unable to cfgreate xlStorage test setup, %s", err)
}
defer os.RemoveAll(path)
xlMeta, _ := ioutil.ReadFile("testdata/xl.meta")
// Create files for the test cases.
@ -370,13 +332,11 @@ func TestXLStorageReadVersion(t *testing.T) {
// TestXLStorageReadAll - TestXLStorages the functionality implemented by xlStorage ReadAll storage API.
func TestXLStorageReadAll(t *testing.T) {
// create xlStorage test setup
xlStorage, path, err := newXLStorageTestSetup()
xlStorage, _, err := newXLStorageTestSetup(t)
if err != nil {
t.Fatalf("Unable to create xlStorage test setup, %s", err)
}
defer os.RemoveAll(path)
// Create files for the test cases.
if err = xlStorage.MakeVol(context.Background(), "exists"); err != nil {
t.Fatalf("Unable to create a volume \"exists\", %s", err)
@ -505,11 +465,10 @@ func TestNewXLStorage(t *testing.T) {
// Asserts the failures too against the expected failures.
func TestXLStorageMakeVol(t *testing.T) {
// create xlStorage test setup
xlStorage, path, err := newXLStorageTestSetup()
xlStorage, path, err := newXLStorageTestSetup(t)
if err != nil {
t.Fatalf("Unable to create xlStorage test setup, %s", err)
}
defer os.RemoveAll(path)
// Setup test environment.
// Create a file.
@ -558,11 +517,7 @@ func TestXLStorageMakeVol(t *testing.T) {
// TestXLStorage for permission denied.
if runtime.GOOS != globalWindowsOSName {
permDeniedDir, err := ioutil.TempDir(globalTestTmpDir, "minio-")
if err != nil {
t.Fatalf("Unable to create temporary directory. %v", err)
}
defer os.RemoveAll(permDeniedDir)
permDeniedDir := createPermDeniedFile(t)
if err = os.Chmod(permDeniedDir, 0o400); err != nil {
t.Fatalf("Unable to change permission to temporary directory %v. %v", permDeniedDir, err)
}
@ -596,11 +551,10 @@ func TestXLStorageMakeVol(t *testing.T) {
// TestXLStorageDeleteVol - Validates the expected behavior of xlStorage.DeleteVol for various cases.
func TestXLStorageDeleteVol(t *testing.T) {
// create xlStorage test setup
xlStorage, path, err := newXLStorageTestSetup()
xlStorage, path, err := newXLStorageTestSetup(t)
if err != nil {
t.Fatalf("Unable to create xlStorage test setup, %s", err)
}
defer os.RemoveAll(path)
// Setup test environment.
if err = xlStorage.MakeVol(context.Background(), "success-vol"); err != nil {
@ -654,17 +608,20 @@ func TestXLStorageDeleteVol(t *testing.T) {
// TestXLStorage for permission denied.
if runtime.GOOS != globalWindowsOSName {
var permDeniedDir string
if permDeniedDir, err = ioutil.TempDir(globalTestTmpDir, "minio-"); err != nil {
t.Fatalf("Unable to create temporary directory. %v", err)
}
defer removePermDeniedFile(permDeniedDir)
permDeniedDir := t.TempDir()
if err = os.Mkdir(slashpath.Join(permDeniedDir, "mybucket"), 0o400); err != nil {
t.Fatalf("Unable to create temporary directory %v. %v", slashpath.Join(permDeniedDir, "mybucket"), err)
}
t.Cleanup(func() {
os.Chmod(slashpath.Join(permDeniedDir, "mybucket"), 0o775)
})
if err = os.Chmod(permDeniedDir, 0o400); err != nil {
t.Fatalf("Unable to change permission to temporary directory %v. %v", permDeniedDir, err)
}
t.Cleanup(func() {
os.Chmod(permDeniedDir, 0o775)
})
// Initialize xlStorage storage layer for permission denied error.
_, err = newLocalXLStorage(permDeniedDir)
@ -691,7 +648,7 @@ func TestXLStorageDeleteVol(t *testing.T) {
}
}
xlStorageDeletedStorage, diskPath, err := newXLStorageTestSetup()
xlStorageDeletedStorage, diskPath, err := newXLStorageTestSetup(t)
if err != nil {
t.Fatalf("Unable to create xlStorage test setup, %s", err)
}
@ -709,11 +666,10 @@ func TestXLStorageDeleteVol(t *testing.T) {
// TestXLStorageStatVol - TestXLStorages validate the volume info returned by xlStorage.StatVol() for various inputs.
func TestXLStorageStatVol(t *testing.T) {
// create xlStorage test setup
xlStorage, path, err := newXLStorageTestSetup()
xlStorage, _, err := newXLStorageTestSetup(t)
if err != nil {
t.Fatalf("Unable to create xlStorage test setup, %s", err)
}
defer os.RemoveAll(path)
// Setup test environment.
if err = xlStorage.MakeVol(context.Background(), "success-vol"); err != nil {
@ -756,7 +712,7 @@ func TestXLStorageStatVol(t *testing.T) {
}
}
xlStorageDeletedStorage, diskPath, err := newXLStorageTestSetup()
xlStorageDeletedStorage, diskPath, err := newXLStorageTestSetup(t)
if err != nil {
t.Fatalf("Unable to create xlStorage test setup, %s", err)
}
@ -774,7 +730,7 @@ func TestXLStorageStatVol(t *testing.T) {
// TestXLStorageListVols - Validates the result and the error output for xlStorage volume listing functionality xlStorage.ListVols().
func TestXLStorageListVols(t *testing.T) {
// create xlStorage test setup
xlStorage, path, err := newXLStorageTestSetup()
xlStorage, path, err := newXLStorageTestSetup(t)
if err != nil {
t.Fatalf("Unable to create xlStorage test setup, %s", err)
}
@ -821,14 +777,13 @@ func TestXLStorageListVols(t *testing.T) {
// TestXLStorageListDir - TestXLStorages validate the directory listing functionality provided by xlStorage.ListDir .
func TestXLStorageListDir(t *testing.T) {
// create xlStorage test setup
xlStorage, path, err := newXLStorageTestSetup()
xlStorage, _, err := newXLStorageTestSetup(t)
if err != nil {
t.Fatalf("Unable to create xlStorage test setup, %s", err)
}
defer os.RemoveAll(path)
// create xlStorage test setup.
xlStorageDeletedStorage, diskPath, err := newXLStorageTestSetup()
xlStorageDeletedStorage, diskPath, err := newXLStorageTestSetup(t)
if err != nil {
t.Fatalf("Unable to create xlStorage test setup, %s", err)
}
@ -916,7 +871,6 @@ func TestXLStorageListDir(t *testing.T) {
// TestXLStorage for permission denied.
if runtime.GOOS != globalWindowsOSName {
permDeniedDir := createPermDeniedFile(t)
defer removePermDeniedFile(permDeniedDir)
// Initialize xlStorage storage layer for permission denied error.
_, err = newLocalXLStorage(permDeniedDir)
@ -959,11 +913,10 @@ func TestXLStorageDeleteFile(t *testing.T) {
}
// create xlStorage test setup
xlStorage, path, err := newXLStorageTestSetup()
xlStorage, path, err := newXLStorageTestSetup(t)
if err != nil {
t.Fatalf("Unable to create xlStorage test setup, %s", err)
}
defer os.RemoveAll(path)
// Setup test environment.
if err = xlStorage.MakeVol(context.Background(), "success-vol"); err != nil {
@ -983,6 +936,9 @@ func TestXLStorageDeleteFile(t *testing.T) {
if err = os.Chmod(pathJoin(path, "no-permissions"), 0o555); err != nil {
t.Fatalf("Unable to chmod directory, %s", err.Error())
}
t.Cleanup(func() {
os.Chmod(pathJoin(path, "no-permissions"), 0o775)
})
testCases := []struct {
srcVol string
@ -1046,7 +1002,6 @@ func TestXLStorageDeleteFile(t *testing.T) {
// TestXLStorage for permission denied.
if runtime.GOOS != globalWindowsOSName {
permDeniedDir := createPermDeniedFile(t)
defer removePermDeniedFile(permDeniedDir)
// Initialize xlStorage storage layer for permission denied error.
_, err = newLocalXLStorage(permDeniedDir)
@ -1072,7 +1027,7 @@ func TestXLStorageDeleteFile(t *testing.T) {
}
// create xlStorage test setup
xlStorageDeletedStorage, diskPath, err := newXLStorageTestSetup()
xlStorageDeletedStorage, diskPath, err := newXLStorageTestSetup(t)
if err != nil {
t.Fatalf("Unable to create xlStorage test setup, %s", err)
}
@ -1096,11 +1051,10 @@ func TestXLStorageDeleteFile(t *testing.T) {
// TestXLStorageReadFile - TestXLStorages xlStorage.ReadFile with wide range of cases and asserts the result and error response.
func TestXLStorageReadFile(t *testing.T) {
// create xlStorage test setup
xlStorage, path, err := newXLStorageTestSetup()
xlStorage, path, err := newXLStorageTestSetup(t)
if err != nil {
t.Fatalf("Unable to create xlStorage test setup, %s", err)
}
defer os.RemoveAll(path)
volume := "success-vol"
// Setup test environment.
@ -1269,7 +1223,6 @@ func TestXLStorageReadFile(t *testing.T) {
// TestXLStorage for permission denied.
if runtime.GOOS != globalWindowsOSName {
permDeniedDir := createPermDeniedFile(t)
defer removePermDeniedFile(permDeniedDir)
// Initialize xlStorage storage layer for permission denied error.
_, err = newLocalXLStorage(permDeniedDir)
@ -1325,22 +1278,18 @@ var xlStorageReadFileWithVerifyTests = []struct {
// TestXLStorageReadFile.
func TestXLStorageReadFileWithVerify(t *testing.T) {
volume, object := "test-vol", "myobject"
xlStorage, path, err := newXLStorageTestSetup()
xlStorage, _, err := newXLStorageTestSetup(t)
if err != nil {
os.RemoveAll(path)
t.Fatalf("Unable to create xlStorage test setup, %s", err)
}
if err = xlStorage.MakeVol(context.Background(), volume); err != nil {
os.RemoveAll(path)
t.Fatalf("Unable to create volume %s: %v", volume, err)
}
data := make([]byte, 8*1024)
if _, err = io.ReadFull(rand.Reader, data); err != nil {
os.RemoveAll(path)
t.Fatalf("Unable to create generate random data: %v", err)
}
if err = xlStorage.AppendFile(context.Background(), volume, object, data); err != nil {
os.RemoveAll(path)
t.Fatalf("Unable to create object: %v", err)
}
@ -1369,11 +1318,10 @@ func TestXLStorageReadFileWithVerify(t *testing.T) {
// TestXLStorageFormatFileChange - to test if changing the diskID makes the calls fail.
func TestXLStorageFormatFileChange(t *testing.T) {
xlStorage, path, err := newXLStorageTestSetup()
xlStorage, _, err := newXLStorageTestSetup(t)
if err != nil {
t.Fatalf("Unable to create xlStorage test setup, %s", err)
}
defer os.RemoveAll(path)
if err = xlStorage.MakeVol(context.Background(), volume); err != nil {
t.Fatalf("MakeVol failed with %s", err)
@ -1393,11 +1341,10 @@ func TestXLStorageFormatFileChange(t *testing.T) {
// TestXLStorage xlStorage.AppendFile()
func TestXLStorageAppendFile(t *testing.T) {
// create xlStorage test setup
xlStorage, path, err := newXLStorageTestSetup()
xlStorage, path, err := newXLStorageTestSetup(t)
if err != nil {
t.Fatalf("Unable to create xlStorage test setup, %s", err)
}
defer os.RemoveAll(path)
// Setup test environment.
if err = xlStorage.MakeVol(context.Background(), "success-vol"); err != nil {
@ -1438,7 +1385,6 @@ func TestXLStorageAppendFile(t *testing.T) {
// TestXLStorage for permission denied.
if runtime.GOOS != globalWindowsOSName {
permDeniedDir := createPermDeniedFile(t)
defer removePermDeniedFile(permDeniedDir)
var xlStoragePermStorage StorageAPI
// Initialize xlStorage storage layer for permission denied error.
@ -1472,11 +1418,10 @@ func TestXLStorageAppendFile(t *testing.T) {
// TestXLStorage xlStorage.RenameFile()
func TestXLStorageRenameFile(t *testing.T) {
// create xlStorage test setup
xlStorage, path, err := newXLStorageTestSetup()
xlStorage, _, err := newXLStorageTestSetup(t)
if err != nil {
t.Fatalf("Unable to create xlStorage test setup, %s", err)
}
defer os.RemoveAll(path)
// Setup test environment.
if err := xlStorage.MakeVol(context.Background(), "src-vol"); err != nil {
@ -1690,11 +1635,10 @@ func TestXLStorageRenameFile(t *testing.T) {
// TestXLStorageDeleteVersion will test if version deletes and bulk deletes work as expected.
func TestXLStorageDeleteVersion(t *testing.T) {
// create xlStorage test setup
xl, path, err := newXLStorageTestSetup()
xl, _, err := newXLStorageTestSetup(t)
if err != nil {
t.Fatalf("Unable to create xlStorage test setup, %s", err)
}
defer os.RemoveAll(path)
ctx := context.Background()
volume := "myvol-vol"
@ -1782,11 +1726,10 @@ func TestXLStorageDeleteVersion(t *testing.T) {
// TestXLStorage xlStorage.StatInfoFile()
func TestXLStorageStatInfoFile(t *testing.T) {
// create xlStorage test setup
xlStorage, path, err := newXLStorageTestSetup()
xlStorage, _, err := newXLStorageTestSetup(t)
if err != nil {
t.Fatalf("Unable to create xlStorage test setup, %s", err)
}
defer os.RemoveAll(path)
// Setup test environment.
if err := xlStorage.MakeVol(context.Background(), "success-vol"); err != nil {
@ -1878,11 +1821,10 @@ func TestXLStorageVerifyFile(t *testing.T) {
// 4) Streaming bitrot check on corrupted file
// create xlStorage test setup
storage, path, err := newXLStorageTestSetup()
storage, path, err := newXLStorageTestSetup(t)
if err != nil {
t.Fatalf("Unable to create xlStorage test setup, %s", err)
}
defer os.RemoveAll(path)
volName := "testvol"
fileName := "testfile"
@ -1976,11 +1918,7 @@ func TestXLStorageVerifyFile(t *testing.T) {
// TestXLStorageReadMetadata tests readMetadata
func TestXLStorageReadMetadata(t *testing.T) {
volume, object := "test-vol", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
tmpDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
tmpDir := t.TempDir()
disk, err := newLocalXLStorage(tmpDir)
if err != nil {

View File

@ -22,7 +22,6 @@ package cmd
import (
"context"
"io/ioutil"
"os"
"path"
"syscall"
@ -39,10 +38,7 @@ func getUmask() int {
// Tests if the directory and file creations happen with proper umask.
func TestIsValidUmaskVol(t *testing.T) {
tmpPath, err := ioutil.TempDir(globalTestTmpDir, "minio-")
if err != nil {
t.Fatalf("Initializing temporary directory failed with %s.", err)
}
tmpPath := t.TempDir()
testCases := []struct {
volName string
expectedUmask int
@ -62,7 +58,6 @@ func TestIsValidUmaskVol(t *testing.T) {
if err = disk.MakeVol(context.Background(), testCase.volName); err != nil {
t.Fatalf("Creating a volume failed with %s expected to pass.", err)
}
defer os.RemoveAll(tmpPath)
// Stat to get permissions bits.
st, err := os.Stat(path.Join(tmpPath, testCase.volName))
@ -81,10 +76,7 @@ func TestIsValidUmaskVol(t *testing.T) {
// Tests if the file creations happen with proper umask.
func TestIsValidUmaskFile(t *testing.T) {
tmpPath, err := ioutil.TempDir(globalTestTmpDir, "minio-")
if err != nil {
t.Fatalf("Initializing temporary directory failed with %s.", err)
}
tmpPath := t.TempDir()
testCases := []struct {
volName string
expectedUmask int
@ -105,8 +97,6 @@ func TestIsValidUmaskFile(t *testing.T) {
t.Fatalf("Creating a volume failed with %s expected to pass.", err)
}
defer os.RemoveAll(tmpPath)
// Attempt to create a file to verify the permissions later.
// AppendFile creates file with 0666 perms.
if err = disk.AppendFile(context.Background(), testCase.volName, pathJoin("hello-world.txt", xlStorageFormatFile), []byte("Hello World")); err != nil {

View File

@ -24,8 +24,6 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"testing"
)
@ -42,16 +40,10 @@ func TestUNCPaths(t *testing.T) {
{string(bytes.Repeat([]byte("界"), 280)), false},
{`/p/q/r/s/t`, true},
}
dir, err := ioutil.TempDir("", "testdisk-")
if err != nil {
t.Fatal(err)
}
// Cleanup on exit of test
defer os.RemoveAll(dir)
dir := t.TempDir()
// Instantiate posix object to manage a disk
var fs StorageAPI
fs, err = newLocalXLStorage(dir)
fs, err := newLocalXLStorage(dir)
if err != nil {
t.Fatal(err)
}
@ -81,15 +73,9 @@ func TestUNCPaths(t *testing.T) {
// Test to validate xlStorage behavior on windows when a non-final path component is a file.
func TestUNCPathENOTDIR(t *testing.T) {
// Instantiate posix object to manage a disk
dir, err := ioutil.TempDir("", "testdisk-")
if err != nil {
t.Fatal(err)
}
// Cleanup on exit of test
defer os.RemoveAll(dir)
dir := t.TempDir()
var fs StorageAPI
fs, err = newLocalXLStorage(dir)
fs, err := newLocalXLStorage(dir)
if err != nil {
t.Fatal(err)
}

View File

@ -21,21 +21,13 @@
package disk_test
import (
"io/ioutil"
"os"
"testing"
"github.com/minio/minio/internal/disk"
)
func TestFree(t *testing.T) {
path, err := ioutil.TempDir(os.TempDir(), "minio-")
defer os.RemoveAll(path)
if err != nil {
t.Fatal(err)
}
di, err := disk.GetInfo(path)
di, err := disk.GetInfo(t.TempDir())
if err != nil {
t.Fatal(err)
}

View File

@ -46,18 +46,9 @@ func TestLockFail(t *testing.T) {
// Tests lock directory fail.
func TestLockDirFail(t *testing.T) {
d, err := ioutil.TempDir("", "lockDir")
if err != nil {
t.Fatal(err)
}
defer func() {
err = os.Remove(d)
if err != nil {
t.Fatal(err)
}
}()
d := t.TempDir()
_, err = LockedOpenFile(d, os.O_APPEND, 0o600)
_, err := LockedOpenFile(d, os.O_APPEND, 0o600)
if err == nil {
t.Fatal("Should fail here")
}

View File

@ -37,11 +37,8 @@ func TestCrossDeviceMountPaths(t *testing.T) {
/dev/2 /path/to/1/2 type2 flags,1,2=3 2 2
/dev/3 /path/to/1.1 type3 falgs,1,2=3 3 3
`
dir, err := ioutil.TempDir("", "TestReadProcmountInfos")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
var err error
dir := t.TempDir()
mountsPath := filepath.Join(dir, "mounts")
if err = ioutil.WriteFile(mountsPath, []byte(successCase), 0o666); err != nil {
t.Fatal(err)
@ -89,11 +86,8 @@ func TestCrossDeviceMount(t *testing.T) {
/dev/2 /path/to/1/2 type2 flags,1,2=3 2 2
/dev/3 /path/to/1.1 type3 falgs,1,2=3 3 3
`
dir, err := ioutil.TempDir("", "TestReadProcmountInfos")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
var err error
dir := t.TempDir()
mountsPath := filepath.Join(dir, "mounts")
if err = ioutil.WriteFile(mountsPath, []byte(successCase), 0o666); err != nil {
t.Fatal(err)
@ -140,11 +134,8 @@ func TestReadProcmountInfos(t *testing.T) {
/dev/1 /path/to/1 type1 flags 1 1
/dev/2 /path/to/2 type2 flags,1,2=3 2 2
`
dir, err := ioutil.TempDir("", "TestReadProcmountInfos")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
var err error
dir := t.TempDir()
mountsPath := filepath.Join(dir, "mounts")
if err = ioutil.WriteFile(mountsPath, []byte(successCase), 0o666); err != nil {