Tests: Cleanup/Enhancement: Add few more cases to posix.ReadFile tests and use a cleaner posixTestSetup for posix tests (#2330)

master
karthic rao 8 years ago committed by Harshavardhana
parent 4b05b6a6c1
commit 5b86dd7659
  1. 11
      erasure_test.go
  2. 183
      posix_test.go

@ -18,8 +18,6 @@ package main
import ( import (
"bytes" "bytes"
"io/ioutil"
"os"
"testing" "testing"
) )
@ -171,14 +169,9 @@ func (e erasureTestSetup) Remove() {
func newErasureTestSetup(dataBlocks int, parityBlocks int, blockSize int64) (*erasureTestSetup, error) { func newErasureTestSetup(dataBlocks int, parityBlocks int, blockSize int64) (*erasureTestSetup, error) {
diskPaths := make([]string, dataBlocks+parityBlocks) diskPaths := make([]string, dataBlocks+parityBlocks)
disks := make([]StorageAPI, len(diskPaths)) disks := make([]StorageAPI, len(diskPaths))
for i := range diskPaths {
var err error var err error
diskPaths[i], err = ioutil.TempDir(os.TempDir(), "minio-") for i := range diskPaths {
if err != nil { disks[i], diskPaths[i], err = newPosixTestSetup()
return nil, err
}
disks[i], err = newPosix(diskPaths[i])
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -28,6 +28,20 @@ import (
"testing" "testing"
) )
// creates a temp dir and sets up posix layer.
// returns posix layer, temp dir path to be used for the purpose of tests.
func newPosixTestSetup() (StorageAPI, string, error) {
diskPath, err := ioutil.TempDir(os.TempDir(), "minio-")
if err != nil {
return nil, "", err
}
posixStorage, err := newPosix(diskPath)
if err != nil {
return nil, "", err
}
return posixStorage, diskPath, nil
}
// Tests posix.getDiskInfo() // Tests posix.getDiskInfo()
func TestGetDiskInfo(t *testing.T) { func TestGetDiskInfo(t *testing.T) {
path, err := ioutil.TempDir(os.TempDir(), "minio-") path, err := ioutil.TempDir(os.TempDir(), "minio-")
@ -54,17 +68,13 @@ func TestGetDiskInfo(t *testing.T) {
// Tests the functionality implemented by ReadAll storage API. // Tests the functionality implemented by ReadAll storage API.
func TestReadAll(t *testing.T) { func TestReadAll(t *testing.T) {
path, err := ioutil.TempDir(os.TempDir(), "minio-") // create posix test setup
posix, path, err := newPosixTestSetup()
if err != nil { if err != nil {
t.Fatalf("Unable to create a temporary directory, %s", err) t.Fatalf("Unable to create posix test setup, %s", err)
} }
defer removeAll(path)
// Initialize posix storage layer. defer removeAll(path)
posix, err := newPosix(path)
if err != nil {
t.Fatalf("Unable to initialize posix, %s", err)
}
// Create files for the test cases. // Create files for the test cases.
if err = posix.MakeVol("exists"); err != nil { if err = posix.MakeVol("exists"); err != nil {
@ -173,19 +183,13 @@ func TestNewPosix(t *testing.T) {
// Test posix.MakeVol() // Test posix.MakeVol()
func TestMakeVol(t *testing.T) { func TestMakeVol(t *testing.T) {
// Create temporary directory. // create posix test setup
path, err := ioutil.TempDir("", "minio-") posix, path, err := newPosixTestSetup()
if err != nil { if err != nil {
t.Fatalf("Unable to create a temporary directory, %s", err) t.Fatalf("Unable to create posix test setup, %s", err)
} }
defer removeAll(path) defer removeAll(path)
// Initialize posix storage layer.
posix, err := newPosix(path)
if err != nil {
t.Fatalf("Unable to initialize posix, %s", err)
}
// Setup test environment. // Setup test environment.
// Create a file. // Create a file.
if err := ioutil.WriteFile(slashpath.Join(path, "vol-as-file"), []byte{}, os.ModePerm); err != nil { if err := ioutil.WriteFile(slashpath.Join(path, "vol-as-file"), []byte{}, os.ModePerm); err != nil {
@ -227,19 +231,13 @@ func TestMakeVol(t *testing.T) {
// Test posix.DeleteVol() // Test posix.DeleteVol()
func TestDeleteVol(t *testing.T) { func TestDeleteVol(t *testing.T) {
// Create temporary directory. // create posix test setup
path, err := ioutil.TempDir("", "minio-") posix, path, err := newPosixTestSetup()
if err != nil { if err != nil {
t.Fatalf("Unable to create a temporary directory, %s", err) t.Fatalf("Unable to create posix test setup, %s", err)
} }
defer removeAll(path) defer removeAll(path)
// Initialize posix storage layer.
posix, err := newPosix(path)
if err != nil {
t.Fatalf("Unable to initialize posix, %s", err)
}
// Setup test environment. // Setup test environment.
if err := posix.MakeVol("success-vol"); err != nil { if err := posix.MakeVol("success-vol"); err != nil {
t.Fatalf("Unable to create volume, %s", err) t.Fatalf("Unable to create volume, %s", err)
@ -285,19 +283,13 @@ func TestDeleteVol(t *testing.T) {
// Test posix.StatVol() // Test posix.StatVol()
func TestStatVol(t *testing.T) { func TestStatVol(t *testing.T) {
// Create temporary directory. // create posix test setup
path, err := ioutil.TempDir("", "minio-") posix, path, err := newPosixTestSetup()
if err != nil { if err != nil {
t.Fatalf("Unable to create a temporary directory, %s", err) t.Fatalf("Unable to create posix test setup, %s", err)
} }
defer removeAll(path) defer removeAll(path)
// Initialize posix storage layer.
posix, err := newPosix(path)
if err != nil {
t.Fatalf("Unable to initialize posix, %s", err)
}
// Setup test environment. // Setup test environment.
if err := posix.MakeVol("success-vol"); err != nil { if err := posix.MakeVol("success-vol"); err != nil {
t.Fatalf("Unable to create volume, %s", err) t.Fatalf("Unable to create volume, %s", err)
@ -320,19 +312,13 @@ func TestStatVol(t *testing.T) {
// Test posix.ListVols() // Test posix.ListVols()
func TestListVols(t *testing.T) { func TestListVols(t *testing.T) {
// Create temporary directory. // create posix test setup
path, err := ioutil.TempDir("", "minio-") posix, path, err := newPosixTestSetup()
if err != nil { if err != nil {
t.Fatalf("Unable to create a temporary directory, %s", err) t.Fatalf("Unable to create posix test setup, %s", err)
} }
defer removeAll(path) defer removeAll(path)
// Initialize posix storage layer.
posix, err := newPosix(path)
if err != nil {
t.Fatalf("Unable to initialize posix, %s", err)
}
// Test empty list vols. // Test empty list vols.
if volInfo, err := posix.ListVols(); err != nil { if volInfo, err := posix.ListVols(); err != nil {
t.Fatalf("expected: <nil>, got: %s", err) t.Fatalf("expected: <nil>, got: %s", err)
@ -355,19 +341,13 @@ func TestListVols(t *testing.T) {
// Test posix.DeleteFile() // Test posix.DeleteFile()
func TestDeleteFile(t *testing.T) { func TestDeleteFile(t *testing.T) {
// Create temporary directory. // create posix test setup
path, err := ioutil.TempDir("", "minio-") posix, path, err := newPosixTestSetup()
if err != nil { if err != nil {
t.Fatalf("Unable to create a temporary directory, %s", err) t.Fatalf("Unable to create posix test setup, %s", err)
} }
defer removeAll(path) defer removeAll(path)
// Initialize posix storage layer.
posix, err := newPosix(path)
if err != nil {
t.Fatalf("Unable to initialize posix, %s", err)
}
// Setup test environment. // Setup test environment.
if err := posix.MakeVol("success-vol"); err != nil { if err := posix.MakeVol("success-vol"); err != nil {
t.Fatalf("Unable to create volume, %s", err) t.Fatalf("Unable to create volume, %s", err)
@ -406,21 +386,15 @@ func TestDeleteFile(t *testing.T) {
// Test posix.ReadFile() // Test posix.ReadFile()
func TestReadFile(t *testing.T) { func TestReadFile(t *testing.T) {
// Create temporary directory. // create posix test setup
path, err := ioutil.TempDir("", "minio-") posix, path, err := newPosixTestSetup()
if err != nil { if err != nil {
t.Fatalf("Unable to create a temporary directory, %s", err) t.Fatalf("Unable to create posix test setup, %s", err)
} }
defer removeAll(path) defer removeAll(path)
volume := "success-vol"
// Initialize posix storage layer.
posix, err := newPosix(path)
if err != nil {
t.Fatalf("Unable to initialize posix, %s", err)
}
// Setup test environment. // Setup test environment.
if err = posix.MakeVol("success-vol"); err != nil { if err = posix.MakeVol(volume); err != nil {
t.Fatalf("Unable to create volume, %s", err) t.Fatalf("Unable to create volume, %s", err)
} }
@ -430,6 +404,7 @@ func TestReadFile(t *testing.T) {
} }
testCases := []struct { testCases := []struct {
volume string
fileName string fileName string
offset int64 offset int64
bufSize int bufSize int
@ -438,21 +413,21 @@ func TestReadFile(t *testing.T) {
}{ }{
// Successful read at offset 0 and proper buffer size. - 1 // Successful read at offset 0 and proper buffer size. - 1
{ {
"myobject", 0, 5, volume, "myobject", 0, 5,
[]byte("hello"), nil, []byte("hello"), nil,
}, },
// Success read at hierarchy. - 2 // Success read at hierarchy. - 2
{ {
"path/to/my/object", 0, 5, volume, "path/to/my/object", 0, 5,
[]byte("hello"), nil, []byte("hello"), nil,
}, },
// One path segment length is 255 chars long. - 3 // One path segment length is 255 chars long. - 3
{ {
"path/to/my/object000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", volume, "path/to/my/object000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
0, 5, []byte("hello"), nil}, 0, 5, []byte("hello"), nil},
// Whole path is 1024 characters long, success case. - 4 // Whole path is 1024 characters long, success case. - 4
{ {
"level0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001/level0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002/level0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003/object000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", volume, "level0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001/level0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002/level0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003/object000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
0, 5, []byte("hello"), 0, 5, []byte("hello"),
func() error { func() error {
// On darwin HFS does not support > 1024 characters. // On darwin HFS does not support > 1024 characters.
@ -465,37 +440,37 @@ func TestReadFile(t *testing.T) {
}, },
// Object is a directory. - 5 // Object is a directory. - 5
{ {
"object-as-dir", volume, "object-as-dir",
0, 5, nil, errIsNotRegular}, 0, 5, nil, errIsNotRegular},
// One path segment length is > 255 chars long. - 6 // One path segment length is > 255 chars long. - 6
{ {
"path/to/my/object0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", volume, "path/to/my/object0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
0, 5, nil, errFileNameTooLong}, 0, 5, nil, errFileNameTooLong},
// Path length is > 1024 chars long. - 7 // Path length is > 1024 chars long. - 7
{ {
"level0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001/level0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002/level0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003/object000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", volume, "level0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001/level0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002/level0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003/object000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
0, 5, nil, errFileNameTooLong}, 0, 5, nil, errFileNameTooLong},
// Buffer size greater than object size. - 8 // Buffer size greater than object size. - 8
{ {
"myobject", 0, 16, volume, "myobject", 0, 16,
[]byte("hello, world"), []byte("hello, world"),
io.ErrUnexpectedEOF, io.ErrUnexpectedEOF,
}, },
// Reading from an offset success. - 9 // Reading from an offset success. - 9
{ {
"myobject", 7, 5, volume, "myobject", 7, 5,
[]byte("world"), nil, []byte("world"), nil,
}, },
// Reading from an object but buffer size greater. - 10 // Reading from an object but buffer size greater. - 10
{ {
"myobject", volume, "myobject",
7, 8, 7, 8,
[]byte("world"), []byte("world"),
io.ErrUnexpectedEOF, io.ErrUnexpectedEOF,
}, },
// Seeking into a wrong offset, return PathError. - 11 // Seeking into a wrong offset, return PathError. - 11
{ {
"myobject", volume, "myobject",
-1, 5, -1, 5,
nil, nil,
func() error { func() error {
@ -515,7 +490,23 @@ func TestReadFile(t *testing.T) {
}, },
// Seeking ahead returns io.EOF. - 12 // Seeking ahead returns io.EOF. - 12
{ {
"myobject", 14, 1, nil, io.EOF, volume, "myobject", 14, 1, nil, io.EOF,
},
// Empty volume name. - 13
{
"", "myobject", 14, 1, nil, errInvalidArgument,
},
// Empty filename name. - 14
{
volume, "", 14, 1, nil, errIsNotRegular,
},
// Non existent volume name - 15.
{
"abcd", "", 14, 1, nil, errVolumeNotFound,
},
// Non existent filename - 16.
{
volume, "abcd", 14, 1, nil, errFileNotFound,
}, },
} }
@ -524,7 +515,7 @@ func TestReadFile(t *testing.T) {
// Create test files for further reading. // Create test files for further reading.
for i, appendFile := range appendFiles { for i, appendFile := range appendFiles {
err = posix.AppendFile("success-vol", appendFile.fileName, []byte("hello, world")) err = posix.AppendFile(volume, appendFile.fileName, []byte("hello, world"))
if err != appendFile.expectedErr { if err != appendFile.expectedErr {
t.Fatalf("Creating file failed: %d %#v, expected: %s, got: %s", i+1, appendFile, appendFile.expectedErr, err) t.Fatalf("Creating file failed: %d %#v, expected: %s, got: %s", i+1, appendFile, appendFile.expectedErr, err)
} }
@ -534,7 +525,7 @@ func TestReadFile(t *testing.T) {
for i, testCase := range testCases { for i, testCase := range testCases {
// Common read buffer. // Common read buffer.
var buf = make([]byte, testCase.bufSize) var buf = make([]byte, testCase.bufSize)
n, err := posix.ReadFile("success-vol", testCase.fileName, testCase.offset, buf) n, err := posix.ReadFile(testCase.volume, testCase.fileName, testCase.offset, buf)
if err != nil && testCase.expectedErr != nil { if err != nil && testCase.expectedErr != nil {
// Validate if the type string of the errors are an exact match. // Validate if the type string of the errors are an exact match.
if err.Error() != testCase.expectedErr.Error() { if err.Error() != testCase.expectedErr.Error() {
@ -587,19 +578,13 @@ func TestReadFile(t *testing.T) {
// Test posix.AppendFile() // Test posix.AppendFile()
func TestAppendFile(t *testing.T) { func TestAppendFile(t *testing.T) {
// Create temporary directory. // create posix test setup
path, err := ioutil.TempDir("", "minio-") posix, path, err := newPosixTestSetup()
if err != nil { if err != nil {
t.Fatalf("Unable to create a temporary directory, %s", err) t.Fatalf("Unable to create posix test setup, %s", err)
} }
defer removeAll(path) defer removeAll(path)
// Initialize posix storage layer.
posix, err := newPosix(path)
if err != nil {
t.Fatalf("Unable to initialize posix, %s", err)
}
// Setup test environment. // Setup test environment.
if err = posix.MakeVol("success-vol"); err != nil { if err = posix.MakeVol("success-vol"); err != nil {
t.Fatalf("Unable to create volume, %s", err) t.Fatalf("Unable to create volume, %s", err)
@ -664,19 +649,13 @@ func TestAppendFile(t *testing.T) {
// Test posix.RenameFile() // Test posix.RenameFile()
func TestRenameFile(t *testing.T) { func TestRenameFile(t *testing.T) {
// Create temporary directory. // create posix test setup
path, err := ioutil.TempDir("", "minio-") posix, path, err := newPosixTestSetup()
if err != nil { if err != nil {
t.Fatalf("Unable to create a temporary directory, %s", err) t.Fatalf("Unable to create posix test setup, %s", err)
} }
defer removeAll(path) defer removeAll(path)
// Initialize posix storage layer.
posix, err := newPosix(path)
if err != nil {
t.Fatalf("Unable to initialize posix, %s", err)
}
// Setup test environment. // Setup test environment.
if err := posix.MakeVol("src-vol"); err != nil { if err := posix.MakeVol("src-vol"); err != nil {
t.Fatalf("Unable to create volume, %s", err) t.Fatalf("Unable to create volume, %s", err)
@ -722,19 +701,13 @@ func TestRenameFile(t *testing.T) {
// Test posix.StatFile() // Test posix.StatFile()
func TestStatFile(t *testing.T) { func TestStatFile(t *testing.T) {
// Create temporary directory. // create posix test setup
path, err := ioutil.TempDir("", "minio-") posix, path, err := newPosixTestSetup()
if err != nil { if err != nil {
t.Fatalf("Unable to create a temporary directory, %s", err) t.Fatalf("Unable to create posix test setup, %s", err)
} }
defer removeAll(path) defer removeAll(path)
// Initialize posix storage layer.
posix, err := newPosix(path)
if err != nil {
t.Fatalf("Unable to initialize posix, %s", err)
}
// Setup test environment. // Setup test environment.
if err := posix.MakeVol("success-vol"); err != nil { if err := posix.MakeVol("success-vol"); err != nil {
t.Fatalf("Unable to create volume, %s", err) t.Fatalf("Unable to create volume, %s", err)

Loading…
Cancel
Save