Fix for tests leaving out temp directories (#2025)

This commit is contained in:
karthic rao 2016-06-28 16:51:52 +05:30 committed by Harshavardhana
parent a854e8cc5c
commit 6dcfa7b046
2 changed files with 81 additions and 100 deletions

View File

@ -17,58 +17,53 @@
package main
import (
"io/ioutil"
"os"
. "gopkg.in/check.v1"
)
type MySuite struct{}
type ObjectLayerAPISuite struct{}
var _ = Suite(&MySuite{})
func (s *MySuite) TestFSAPISuite(c *C) {
var storageList []string
var _ = Suite(&ObjectLayerAPISuite{})
// TestFSAPISuite - Calls object layer suite tests with FS backend.
func (s *ObjectLayerAPISuite) TestFSAPISuite(c *C) {
// Initialize name space lock.
initNSLock()
create := func() ObjectLayer {
path, err := ioutil.TempDir(os.TempDir(), "minio-")
// function which creates a temp FS backend and executes the object layer suite test.
execObjectLayerSuiteTestFS := func(objSuiteTest objSuiteTestType) {
// create temp object layer backend.
// returns the disk and FS object layer.
objLayer, fsDisk, err := makeTestBackend("FS")
c.Check(err, IsNil)
objAPI, err := newFSObjects(path)
c.Check(err, IsNil)
storageList = append(storageList, path)
return objAPI
// remove the disks.
defer removeRoots(fsDisk)
// execute the object layer suite tests.
objSuiteTest(c, objLayer)
}
APITestSuite(c, create)
defer removeRootsC(c, storageList)
// APITestSuite contains set of all object layer suite test.
// These set of test functions are called here.
APITestSuite(c, execObjectLayerSuiteTestFS)
}
func (s *MySuite) TestXLAPISuite(c *C) {
var storageList []string
// type for object layer suites tests.
type objSuiteTestType func(c *C, obj ObjectLayer)
// TestXLAPISuite - Calls object layer suite tests with XL backend.
func (s *ObjectLayerAPISuite) TestXLAPISuite(c *C) {
// Initialize name space lock.
initNSLock()
create := func() ObjectLayer {
var nDisks = 16 // Maximum disks.
var erasureDisks []string
for i := 0; i < nDisks; i++ {
path, err := ioutil.TempDir(os.TempDir(), "minio-")
c.Check(err, IsNil)
erasureDisks = append(erasureDisks, path)
}
objAPI, err := newXLObjects(erasureDisks)
// function which creates a temp XL backend and executes the object layer suite test.
execObjectLayerSuiteTestXL := func(objSuiteTest objSuiteTestType) {
// create temp object layer backend.
// returns the disk and XL object layer.
objLayer, erasureDisks, err := makeTestBackend("XL")
c.Check(err, IsNil)
return objAPI
// remove the disks.
defer removeRoots(erasureDisks)
// execute the object layer suite tests.
objSuiteTest(c, objLayer)
}
APITestSuite(c, create)
defer removeRootsC(c, storageList)
}
// APITestSuite contains set of all object layer suite test.
// These set of test functions are called here.
APITestSuite(c, execObjectLayerSuiteTestXL)
func removeRootsC(c *C, roots []string) {
for _, root := range roots {
removeAll(root)
}
}

View File

@ -69,36 +69,39 @@ func (r *testOneByteReadNoEOF) Read(p []byte) (n int, err error) {
}
// APITestSuite - collection of API tests.
func APITestSuite(c *check.C, create func() ObjectLayer) {
testMakeBucket(c, create)
testMultipleObjectCreation(c, create)
testPaging(c, create)
testObjectOverwriteWorks(c, create)
testNonExistantBucketOperations(c, create)
testBucketRecreateFails(c, create)
testPutObject(c, create)
testPutObjectInSubdir(c, create)
testListBuckets(c, create)
testListBucketsOrder(c, create)
testListObjectsTestsForNonExistantBucket(c, create)
testNonExistantObjectInBucket(c, create)
testGetDirectoryReturnsObjectNotFound(c, create)
testContentType(c, create)
testMultipartObjectCreation(c, create)
testMultipartObjectAbort(c, create)
func APITestSuite(c *check.C, execObjLayerSuiteTest func(objSuiteTest objSuiteTestType)) {
// List containing collection of all tests.
testFuncList := []func(c *check.C, obj ObjectLayer){
testMakeBucket,
testMultipartObjectCreation,
testPaging,
testObjectOverwriteWorks,
testNonExistantBucketOperations,
testBucketRecreateFails,
testPutObject,
testPutObjectInSubdir,
testListBuckets,
testListBucketsOrder,
testListObjectsTestsForNonExistantBucket,
testNonExistantObjectInBucket,
testGetDirectoryReturnsObjectNotFound,
testContentType,
testMultipartObjectAbort,
}
// iterate over list of tests and execute them.
for _, testFunc := range testFuncList {
execObjLayerSuiteTest(testFunc)
}
}
// Tests validate bucket creation.
func testMakeBucket(c *check.C, create func() ObjectLayer) {
obj := create()
func testMakeBucket(c *check.C, obj ObjectLayer) {
err := obj.MakeBucket("bucket-unknown")
c.Assert(err, check.IsNil)
}
// Tests validate creation of part files during Multipart operation.
func testMultipartObjectCreation(c *check.C, create func() ObjectLayer) {
obj := create()
func testMultipartObjectCreation(c *check.C, obj ObjectLayer) {
err := obj.MakeBucket("bucket")
c.Assert(err, check.IsNil)
uploadID, err := obj.NewMultipartUpload("bucket", "key", nil)
@ -123,8 +126,7 @@ func testMultipartObjectCreation(c *check.C, create func() ObjectLayer) {
}
// Tests validate abortion of Multipart operation.
func testMultipartObjectAbort(c *check.C, create func() ObjectLayer) {
obj := create()
func testMultipartObjectAbort(c *check.C, obj ObjectLayer) {
err := obj.MakeBucket("bucket")
c.Assert(err, check.IsNil)
uploadID, err := obj.NewMultipartUpload("bucket", "key", nil)
@ -155,9 +157,8 @@ func testMultipartObjectAbort(c *check.C, create func() ObjectLayer) {
}
// Tests validate object creation.
func testMultipleObjectCreation(c *check.C, create func() ObjectLayer) {
func testMultipleObjectCreation(c *check.C, obj ObjectLayer) {
objects := make(map[string][]byte)
obj := create()
err := obj.MakeBucket("bucket")
c.Assert(err, check.IsNil)
for i := 0; i < 10; i++ {
@ -194,8 +195,7 @@ func testMultipleObjectCreation(c *check.C, create func() ObjectLayer) {
}
// Tests validate creation of objects and the order of listing using various filters for ListObjects operation.
func testPaging(c *check.C, create func() ObjectLayer) {
obj := create()
func testPaging(c *check.C, obj ObjectLayer) {
obj.MakeBucket("bucket")
result, err := obj.ListObjects("bucket", "", "", "", 0)
c.Assert(err, check.IsNil)
@ -298,8 +298,7 @@ func testPaging(c *check.C, create func() ObjectLayer) {
}
// Tests validate overwriting of an existing object.
func testObjectOverwriteWorks(c *check.C, create func() ObjectLayer) {
obj := create()
func testObjectOverwriteWorks(c *check.C, obj ObjectLayer) {
err := obj.MakeBucket("bucket")
c.Assert(err, check.IsNil)
@ -317,16 +316,14 @@ func testObjectOverwriteWorks(c *check.C, create func() ObjectLayer) {
}
// Tests validate that bucket operation on non-existent bucket fails.
func testNonExistantBucketOperations(c *check.C, create func() ObjectLayer) {
obj := create()
func testNonExistantBucketOperations(c *check.C, obj ObjectLayer) {
_, err := obj.PutObject("bucket1", "object", int64(len("one")), bytes.NewBufferString("one"), nil)
c.Assert(err, check.Not(check.IsNil))
c.Assert(err.Error(), check.Equals, "Bucket not found: bucket1")
}
// Tests validate that recreation of the bucket fails.
func testBucketRecreateFails(c *check.C, create func() ObjectLayer) {
obj := create()
func testBucketRecreateFails(c *check.C, obj ObjectLayer) {
err := obj.MakeBucket("string")
c.Assert(err, check.IsNil)
err = obj.MakeBucket("string")
@ -335,8 +332,7 @@ func testBucketRecreateFails(c *check.C, create func() ObjectLayer) {
}
// Tests validate PutObject without prefix.
func testPutObject(c *check.C, create func() ObjectLayer) {
obj := create()
func testPutObject(c *check.C, obj ObjectLayer) {
content := []byte("testcontent")
length := int64(len(content))
readerEOF := newTestReaderEOF(content)
@ -360,8 +356,7 @@ func testPutObject(c *check.C, create func() ObjectLayer) {
}
// Tests validate PutObject with subdirectory prefix.
func testPutObjectInSubdir(c *check.C, create func() ObjectLayer) {
obj := create()
func testPutObjectInSubdir(c *check.C, obj ObjectLayer) {
err := obj.MakeBucket("bucket")
c.Assert(err, check.IsNil)
@ -376,9 +371,7 @@ func testPutObjectInSubdir(c *check.C, create func() ObjectLayer) {
}
// Tests validate ListBuckets.
func testListBuckets(c *check.C, create func() ObjectLayer) {
obj := create()
func testListBuckets(c *check.C, obj ObjectLayer) {
// test empty list.
buckets, err := obj.ListBuckets()
c.Assert(err, check.IsNil)
@ -409,27 +402,23 @@ func testListBuckets(c *check.C, create func() ObjectLayer) {
}
// Tests validate the order of result of ListBuckets.
func testListBucketsOrder(c *check.C, create func() ObjectLayer) {
func testListBucketsOrder(c *check.C, obj ObjectLayer) {
// if implementation contains a map, order of map keys will vary.
// this ensures they return in the same order each time.
for i := 0; i < 10; i++ {
obj := create()
// add one and test exists.
err := obj.MakeBucket("bucket1")
c.Assert(err, check.IsNil)
err = obj.MakeBucket("bucket2")
c.Assert(err, check.IsNil)
buckets, err := obj.ListBuckets()
c.Assert(err, check.IsNil)
c.Assert(len(buckets), check.Equals, 2)
c.Assert(buckets[0].Name, check.Equals, "bucket1")
c.Assert(buckets[1].Name, check.Equals, "bucket2")
}
// add one and test exists.
err := obj.MakeBucket("bucket1")
c.Assert(err, check.IsNil)
err = obj.MakeBucket("bucket2")
c.Assert(err, check.IsNil)
buckets, err := obj.ListBuckets()
c.Assert(err, check.IsNil)
c.Assert(len(buckets), check.Equals, 2)
c.Assert(buckets[0].Name, check.Equals, "bucket1")
c.Assert(buckets[1].Name, check.Equals, "bucket2")
}
// Tests validate that ListObjects operation on a non-existent bucket fails as expected.
func testListObjectsTestsForNonExistantBucket(c *check.C, create func() ObjectLayer) {
obj := create()
func testListObjectsTestsForNonExistantBucket(c *check.C, obj ObjectLayer) {
result, err := obj.ListObjects("bucket", "", "", "", 1000)
c.Assert(err, check.Not(check.IsNil))
c.Assert(result.IsTruncated, check.Equals, false)
@ -438,8 +427,7 @@ func testListObjectsTestsForNonExistantBucket(c *check.C, create func() ObjectLa
}
// Tests validate that GetObject fails on a non-existent bucket as expected.
func testNonExistantObjectInBucket(c *check.C, create func() ObjectLayer) {
obj := create()
func testNonExistantObjectInBucket(c *check.C, obj ObjectLayer) {
err := obj.MakeBucket("bucket")
c.Assert(err, check.IsNil)
@ -454,8 +442,7 @@ func testNonExistantObjectInBucket(c *check.C, create func() ObjectLayer) {
}
// Tests validate that GetObject on an existing directory fails as expected.
func testGetDirectoryReturnsObjectNotFound(c *check.C, create func() ObjectLayer) {
obj := create()
func testGetDirectoryReturnsObjectNotFound(c *check.C, obj ObjectLayer) {
err := obj.MakeBucket("bucket")
c.Assert(err, check.IsNil)
@ -484,8 +471,7 @@ func testGetDirectoryReturnsObjectNotFound(c *check.C, create func() ObjectLayer
}
// Test content-type
func testContentType(c *check.C, create func() ObjectLayer) {
obj := create()
func testContentType(c *check.C, obj ObjectLayer) {
err := obj.MakeBucket("bucket")
c.Assert(err, check.IsNil)