mirror of
https://github.com/minio/minio.git
synced 2024-12-24 06:05:55 -05:00
Setting order when retrieving buckets
This commit is contained in:
parent
c3287b7ad0
commit
54026dbc78
@ -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
|
||||
}
|
||||
|
||||
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user