From 53a76439a20442f2835904a98d5d19db0a9f7b85 Mon Sep 17 00:00:00 2001 From: Karthic Rao Date: Sat, 12 Mar 2016 12:03:36 +0530 Subject: [PATCH] test for GetBucketInfo --- pkg/fs/fs-bucket_test.go | 101 +++++++++++++++++++++++++++++++++------ 1 file changed, 86 insertions(+), 15 deletions(-) diff --git a/pkg/fs/fs-bucket_test.go b/pkg/fs/fs-bucket_test.go index 1d5cb9d90..5e0ed3d22 100644 --- a/pkg/fs/fs-bucket_test.go +++ b/pkg/fs/fs-bucket_test.go @@ -24,11 +24,82 @@ import ( "testing" ) +// The test not just includes asserting the correctness of the output, +// But also includes test cases for which the function should fail. +// For those cases for which it fails, its also asserted whether the function fails as expected. +func TestGetBucketInfo(t *testing.T) { + // Make a temporary directory to use as the filesystem. + directory, e := ioutil.TempDir("", "minio-metadata-test") + if e != nil { + t.Fatal(e) + } + defer os.RemoveAll(directory) + + // Create the filesystem. + filesystem, err := New(directory, 0) + if err != nil { + t.Fatal(err) + } + + // Creating few buckets. + for i := 0; i < 4; i++ { + err = filesystem.MakeBucket("meta-test-bucket." + strconv.Itoa(i)) + if err != nil { + t.Fatal(err) + } + } + testCases := []struct { + bucketName string + metaData BucketInfo + e error + shouldPass bool + }{ + // Test cases with invalid bucket names. + {".test", BucketInfo{}, BucketNameInvalid{Bucket: ".test"}, false}, + {"Test", BucketInfo{}, BucketNameInvalid{Bucket: "Test"}, false}, + {"---", BucketInfo{}, BucketNameInvalid{Bucket: "---"}, false}, + {"ad", BucketInfo{}, BucketNameInvalid{Bucket: "ad"}, false}, + // Test cases with non-existent buckets. + {"volatile-bucket-1", BucketInfo{}, BucketNotFound{Bucket: "volatile-bucket-1"}, false}, + {"volatile-bucket-2", BucketInfo{}, BucketNotFound{Bucket: "volatile-bucket-2"}, false}, + // Test cases with existing buckets. + {"meta-test-bucket.0", BucketInfo{Name: "meta-test-bucket.0"}, nil, true}, + {"meta-test-bucket.1", BucketInfo{Name: "meta-test-bucket.1"}, nil, true}, + {"meta-test-bucket.2", BucketInfo{Name: "meta-test-bucket.2"}, nil, true}, + {"meta-test-bucket.3", BucketInfo{Name: "meta-test-bucket.3"}, nil, true}, + } + for i, testCase := range testCases { + //the err returned is of type *probe.Error + bucketInfo, err := filesystem.GetBucketInfo(testCase.bucketName) + + if err != nil && testCase.shouldPass { + t.Errorf("Test %d: Expected to pass, but failed with: %s", i+1, err.Cause.Error()) + } + if err == nil && !testCase.shouldPass { + t.Errorf("Test %d: Expected to fail with \"%s\", but passed instead", i+1, testCase.e.Error()) + + } + // Failed as expected, but does it fail for the expected reason. + if err != nil && !testCase.shouldPass { + if testCase.e.Error() != err.Cause.Error() { + t.Errorf("Test %d: Expected to fail with error \"%s\", but instead failed with error \"%s\" instead", i+1, testCase.e.Error(), err.Cause.Error()) + } + } + // Since there are cases for which GetBucketInfo fails, this is necessary. + // Test passes as expected, but the output values are verified for correctness here. + if err == nil && testCase.shouldPass { + if testCase.bucketName != bucketInfo.Name { + t.Errorf("Test %d: Expected the bucket name to be \"%s\", but found \"%s\" instead", i+1, testCase.bucketName, bucketInfo.Name) + } + } + } +} + func TestListBuckets(t *testing.T) { // Make a temporary directory to use as the filesystem. - directory, fserr := ioutil.TempDir("", "minio-benchmark") - if fserr != nil { - t.Fatal(fserr) + directory, e := ioutil.TempDir("", "minio-benchmark") + if e != nil { + t.Fatal(e) } defer os.RemoveAll(directory) @@ -66,9 +137,9 @@ func TestListBuckets(t *testing.T) { func TestDeleteBucket(t *testing.T) { // Make a temporary directory to use as the filesystem. - directory, fserr := ioutil.TempDir("", "minio-benchmark") - if fserr != nil { - t.Fatal(fserr) + directory, e := ioutil.TempDir("", "minio-benchmark") + if e != nil { + t.Fatal(e) } defer os.RemoveAll(directory) @@ -87,9 +158,9 @@ func TestDeleteBucket(t *testing.T) { func BenchmarkListBuckets(b *testing.B) { // Make a temporary directory to use as the filesystem. - directory, fserr := ioutil.TempDir("", "minio-benchmark") - if fserr != nil { - b.Fatal(fserr) + directory, e := ioutil.TempDir("", "minio-benchmark") + if e != nil { + b.Fatal(e) } defer os.RemoveAll(directory) @@ -120,9 +191,9 @@ func BenchmarkListBuckets(b *testing.B) { func BenchmarkDeleteBucket(b *testing.B) { // Make a temporary directory to use as the filesystem. - directory, fserr := ioutil.TempDir("", "minio-benchmark") - if fserr != nil { - b.Fatal(fserr) + directory, e := ioutil.TempDir("", "minio-benchmark") + if e != nil { + b.Fatal(e) } defer os.RemoveAll(directory) @@ -155,9 +226,9 @@ func BenchmarkDeleteBucket(b *testing.B) { func BenchmarkGetBucketInfo(b *testing.B) { // Make a temporary directory to use as the filesystem. - directory, fserr := ioutil.TempDir("", "minio-benchmark") - if fserr != nil { - b.Fatal(fserr) + directory, e := ioutil.TempDir("", "minio-benchmark") + if e != nil { + b.Fatal(e) } defer os.RemoveAll(directory)