Setting order when retrieving buckets

master
Frederick F. Kautz IV 10 years ago
parent c3287b7ad0
commit 54026dbc78
  1. 7
      pkg/storage/inmemory/inmemory.go
  2. 16
      pkg/storage/storage_api_suite.go
  3. 10
      pkg/webapi/minioapi/minioapi_test.go

@ -111,6 +111,12 @@ func (storage *storage) ListObjects(bucket, prefix string, count int) ([]mstorag
return results, false, nil 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) { func (storage *storage) ListBuckets(prefix string) ([]mstorage.BucketMetadata, error) {
var results []mstorage.BucketMetadata var results []mstorage.BucketMetadata
for key, bucket := range storage.bucketdata { for key, bucket := range storage.bucketdata {
@ -118,6 +124,7 @@ func (storage *storage) ListBuckets(prefix string) ([]mstorage.BucketMetadata, e
results = append(results, bucket.metadata) results = append(results, bucket.metadata)
} }
} }
sort.Sort(ByBucketName(results))
return results, nil return results, nil
} }

@ -17,6 +17,7 @@ func APITestSuite(c *C, create func() Storage) {
testBucketRecreateFails(c, create) testBucketRecreateFails(c, create)
testPutObjectInSubdir(c, create) testPutObjectInSubdir(c, create)
testListBuckets(c, create) testListBuckets(c, create)
testListBucketsOrder(c, create)
} }
func testCreateBucket(c *C, create func() Storage) { 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(len(buckets), Equals, 2)
c.Assert(err, IsNil) 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")
}
}

@ -23,7 +23,6 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"sort"
"strconv" "strconv"
"testing" "testing"
"time" "time"
@ -323,12 +322,9 @@ func (s *MySuite) TestListBuckets(c *C) {
listResponse, err = readListBucket(response.Body) listResponse, err = readListBucket(response.Body)
c.Assert(err, IsNil) c.Assert(err, IsNil)
c.Assert(len(listResponse.Buckets.Bucket), Equals, 2) c.Assert(len(listResponse.Buckets.Bucket), Equals, 2)
var buckets []string
buckets = append(buckets, listResponse.Buckets.Bucket[0].Name) c.Assert(listResponse.Buckets.Bucket[0].Name, Equals, "bar")
buckets = append(buckets, listResponse.Buckets.Bucket[1].Name) c.Assert(listResponse.Buckets.Bucket[1].Name, Equals, "foo")
sort.Strings(buckets)
c.Assert(buckets[0], Equals, "bar")
c.Assert(buckets[1], Equals, "foo")
} }
func readListBucket(reader io.Reader) (BucketListResponse, error) { func readListBucket(reader io.Reader) (BucketListResponse, error) {

Loading…
Cancel
Save