From 54026dbc78f605333fd4fd3b25544b38e9772126 Mon Sep 17 00:00:00 2001 From: "Frederick F. Kautz IV" Date: Sun, 1 Feb 2015 00:16:25 -0800 Subject: [PATCH] Setting order when retrieving buckets --- pkg/storage/inmemory/inmemory.go | 7 +++++++ pkg/storage/storage_api_suite.go | 16 ++++++++++++++++ pkg/webapi/minioapi/minioapi_test.go | 10 +++------- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/pkg/storage/inmemory/inmemory.go b/pkg/storage/inmemory/inmemory.go index b85403dd5..241304458 100644 --- a/pkg/storage/inmemory/inmemory.go +++ b/pkg/storage/inmemory/inmemory.go @@ -111,6 +111,12 @@ func (storage *storage) ListObjects(bucket, prefix string, count int) ([]mstorag return results, false, nil } +type ByBucketName []mstorage.BucketMetadata + +func (b ByBucketName) Len() int { return len(b) } +func (b ByBucketName) Swap(i, j int) { b[i], b[j] = b[j], b[i] } +func (b ByBucketName) Less(i, j int) bool { return b[i].Name < b[j].Name } + func (storage *storage) ListBuckets(prefix string) ([]mstorage.BucketMetadata, error) { var results []mstorage.BucketMetadata for key, bucket := range storage.bucketdata { @@ -118,6 +124,7 @@ func (storage *storage) ListBuckets(prefix string) ([]mstorage.BucketMetadata, e results = append(results, bucket.metadata) } } + sort.Sort(ByBucketName(results)) return results, nil } diff --git a/pkg/storage/storage_api_suite.go b/pkg/storage/storage_api_suite.go index 24f82e1b2..5f5e916ca 100644 --- a/pkg/storage/storage_api_suite.go +++ b/pkg/storage/storage_api_suite.go @@ -17,6 +17,7 @@ func APITestSuite(c *C, create func() Storage) { testBucketRecreateFails(c, create) testPutObjectInSubdir(c, create) testListBuckets(c, create) + testListBucketsOrder(c, create) } func testCreateBucket(c *C, create func() Storage) { @@ -193,3 +194,18 @@ func testListBuckets(c *C, create func() Storage) { c.Assert(len(buckets), Equals, 2) c.Assert(err, IsNil) } + +func testListBucketsOrder(c *C, create func() Storage) { + for i := 0; i < 10; i++ { + storage := create() + // add one and test exists + storage.StoreBucket("bucket1") + storage.StoreBucket("bucket2") + + buckets, err := storage.ListBuckets("bucket") + c.Assert(len(buckets), Equals, 2) + c.Assert(err, IsNil) + c.Assert(buckets[0].Name, Equals, "bucket1") + c.Assert(buckets[1].Name, Equals, "bucket2") + } +} diff --git a/pkg/webapi/minioapi/minioapi_test.go b/pkg/webapi/minioapi/minioapi_test.go index e7fafe070..a0f12ab66 100644 --- a/pkg/webapi/minioapi/minioapi_test.go +++ b/pkg/webapi/minioapi/minioapi_test.go @@ -23,7 +23,6 @@ import ( "io/ioutil" "net/http" "net/http/httptest" - "sort" "strconv" "testing" "time" @@ -323,12 +322,9 @@ func (s *MySuite) TestListBuckets(c *C) { listResponse, err = readListBucket(response.Body) c.Assert(err, IsNil) c.Assert(len(listResponse.Buckets.Bucket), Equals, 2) - var buckets []string - buckets = append(buckets, listResponse.Buckets.Bucket[0].Name) - buckets = append(buckets, listResponse.Buckets.Bucket[1].Name) - sort.Strings(buckets) - c.Assert(buckets[0], Equals, "bar") - c.Assert(buckets[1], Equals, "foo") + + c.Assert(listResponse.Buckets.Bucket[0].Name, Equals, "bar") + c.Assert(listResponse.Buckets.Bucket[1].Name, Equals, "foo") } func readListBucket(reader io.Reader) (BucketListResponse, error) {