From 044dc9af80a0061d53edc0c20af54dfda2dded15 Mon Sep 17 00:00:00 2001 From: "Frederick F. Kautz IV" Date: Tue, 27 Jan 2015 13:11:15 -0800 Subject: [PATCH] Adding more tests, including ordering of objects in prefix --- pkg/storage/inmemory/inmemory.go | 11 ++++- pkg/storage/storage_api_suite.go | 84 ++++++++++++++++++++++---------- 2 files changed, 69 insertions(+), 26 deletions(-) diff --git a/pkg/storage/inmemory/inmemory.go b/pkg/storage/inmemory/inmemory.go index 3139baf43..853258b63 100644 --- a/pkg/storage/inmemory/inmemory.go +++ b/pkg/storage/inmemory/inmemory.go @@ -5,6 +5,7 @@ import ( "crypto/sha256" "fmt" "io" + "sort" "strings" "time" @@ -89,10 +90,18 @@ func (storage *storage) StoreBucket(bucketName string) error { func (storage *storage) ListObjects(bucket, prefix string, count int) ([]mstorage.ObjectMetadata, bool, error) { // TODO prefix and count handling var results []mstorage.ObjectMetadata - for key, object := range storage.objectdata { + var keys []string + for key := range storage.objectdata { + if strings.HasPrefix(key, bucket+":"+prefix) { + keys = append(keys, key) + } + } + sort.Strings(keys) + for _, key := range keys { if len(results) == count { return results, true, nil } + object := storage.objectdata[key] if bucket == object.metadata.Bucket { if strings.HasPrefix(key, bucket+":") { results = append(results, object.metadata) diff --git a/pkg/storage/storage_api_suite.go b/pkg/storage/storage_api_suite.go index edc354c24..b593b80f8 100644 --- a/pkg/storage/storage_api_suite.go +++ b/pkg/storage/storage_api_suite.go @@ -25,7 +25,7 @@ func testMultipleObjectCreation(c *C, create func() Storage) { objects := make(map[string][]byte) storage := create() err := storage.StoreBucket("bucket") - c.Check(err, IsNil) + c.Assert(err, IsNil) for i := 0; i < 10; i++ { randomPerm := rand.Perm(10) randomString := "" @@ -35,22 +35,22 @@ func testMultipleObjectCreation(c *C, create func() Storage) { key := "obj" + strconv.Itoa(i) objects[key] = []byte(randomString) err := storage.StoreObject("bucket", key, bytes.NewBufferString(randomString)) - c.Check(err, IsNil) + c.Assert(err, IsNil) } - // ensure no duplicates + // ensure no duplicate etags etags := make(map[string]string) for key, value := range objects { var byteBuffer bytes.Buffer storage.CopyObjectToWriter(&byteBuffer, "bucket", key) - c.Check(bytes.Equal(value, byteBuffer.Bytes()), Equals, true) + c.Assert(bytes.Equal(value, byteBuffer.Bytes()), Equals, true) metadata, err := storage.GetObjectMetadata("bucket", key) - c.Check(err, IsNil) - c.Check(metadata.Size, Equals, int64(len(value))) + c.Assert(err, IsNil) + c.Assert(metadata.Size, Equals, int64(len(value))) _, ok := etags[metadata.ETag] - c.Check(ok, Equals, false) + c.Assert(ok, Equals, false) etags[metadata.ETag] = metadata.ETag } } @@ -60,24 +60,58 @@ func testPaging(c *C, create func() Storage) { storage.StoreBucket("bucket") storage.ListObjects("bucket", "", 5) objects, isTruncated, err := storage.ListObjects("bucket", "", 5) - c.Check(len(objects), Equals, 0) - c.Check(isTruncated, Equals, false) - c.Check(err, IsNil) - for i := 1; i <= 5; i++ { + c.Assert(len(objects), Equals, 0) + c.Assert(isTruncated, Equals, false) + c.Assert(err, IsNil) + // check before paging occurs + for i := 0; i < 5; i++ { key := "obj" + strconv.Itoa(i) storage.StoreObject("bucket", key, bytes.NewBufferString(key)) objects, isTruncated, err = storage.ListObjects("bucket", "", 5) - c.Check(len(objects), Equals, i) - c.Check(isTruncated, Equals, false) - c.Check(err, IsNil) + c.Assert(len(objects), Equals, i+1) + c.Assert(isTruncated, Equals, false) + c.Assert(err, IsNil) } + // check after paging occurs pages work for i := 6; i <= 10; i++ { key := "obj" + strconv.Itoa(i) storage.StoreObject("bucket", key, bytes.NewBufferString(key)) objects, isTruncated, err = storage.ListObjects("bucket", "", 5) - c.Check(len(objects), Equals, 5) - c.Check(isTruncated, Equals, true) - c.Check(err, IsNil) + c.Assert(len(objects), Equals, 5) + c.Assert(isTruncated, Equals, true) + c.Assert(err, IsNil) + } + // check paging with prefix at end returns less objects + { + storage.StoreObject("bucket", "newPrefix", bytes.NewBufferString("prefix1")) + storage.StoreObject("bucket", "newPrefix2", bytes.NewBufferString("prefix2")) + objects, isTruncated, err = storage.ListObjects("bucket", "new", 5) + c.Assert(len(objects), Equals, 2) + } + + // check ordering of pages + { + objects, isTruncated, err = storage.ListObjects("bucket", "", 5) + c.Assert(objects[0].Key, Equals, "newPrefix") + c.Assert(objects[1].Key, Equals, "newPrefix2") + c.Assert(objects[2].Key, Equals, "obj0") + c.Assert(objects[3].Key, Equals, "obj1") + c.Assert(objects[4].Key, Equals, "obj10") + } + // check ordering of results with prefix + { + objects, isTruncated, err = storage.ListObjects("bucket", "obj", 5) + c.Assert(objects[0].Key, Equals, "obj0") + c.Assert(objects[1].Key, Equals, "obj1") + c.Assert(objects[2].Key, Equals, "obj10") + c.Assert(objects[3].Key, Equals, "obj2") + c.Assert(objects[4].Key, Equals, "obj3") + } + // check ordering of results with prefix and no paging + { + objects, isTruncated, err = storage.ListObjects("bucket", "new", 5) + c.Assert(objects[0].Key, Equals, "newPrefix") + c.Assert(objects[1].Key, Equals, "newPrefix2") } } @@ -85,26 +119,26 @@ func testObjectOverwriteFails(c *C, create func() Storage) { storage := create() storage.StoreBucket("bucket") err := storage.StoreObject("bucket", "object", bytes.NewBufferString("one")) - c.Check(err, IsNil) + c.Assert(err, IsNil) err = storage.StoreObject("bucket", "object", bytes.NewBufferString("three")) - c.Check(err, Not(IsNil)) + c.Assert(err, Not(IsNil)) var bytesBuffer bytes.Buffer length, err := storage.CopyObjectToWriter(&bytesBuffer, "bucket", "object") - c.Check(length, Equals, int64(len("one"))) - c.Check(err, IsNil) - c.Check(string(bytesBuffer.Bytes()), Equals, "one") + c.Assert(length, Equals, int64(len("one"))) + c.Assert(err, IsNil) + c.Assert(string(bytesBuffer.Bytes()), Equals, "one") } func testNonExistantBucketOperations(c *C, create func() Storage) { storage := create() err := storage.StoreObject("bucket", "object", bytes.NewBufferString("one")) - c.Check(err, Not(IsNil)) + c.Assert(err, Not(IsNil)) } func testBucketRecreateFails(c *C, create func() Storage) { storage := create() err := storage.StoreBucket("string") - c.Check(err, IsNil) + c.Assert(err, IsNil) err = storage.StoreBucket("string") - c.Check(err, Not(IsNil)) + c.Assert(err, Not(IsNil)) }