Merge pull request #74 from fkautz/pr_out_adding_more_tests_including_ordering_of_objects_in_prefix

This commit is contained in:
Frederick F. Kautz IV 2015-01-27 13:12:55 -08:00
commit e1e8bf8b26
2 changed files with 69 additions and 26 deletions

View File

@ -5,6 +5,7 @@ import (
"crypto/sha256" "crypto/sha256"
"fmt" "fmt"
"io" "io"
"sort"
"strings" "strings"
"time" "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) { func (storage *storage) ListObjects(bucket, prefix string, count int) ([]mstorage.ObjectMetadata, bool, error) {
// TODO prefix and count handling // TODO prefix and count handling
var results []mstorage.ObjectMetadata 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 { if len(results) == count {
return results, true, nil return results, true, nil
} }
object := storage.objectdata[key]
if bucket == object.metadata.Bucket { if bucket == object.metadata.Bucket {
if strings.HasPrefix(key, bucket+":") { if strings.HasPrefix(key, bucket+":") {
results = append(results, object.metadata) results = append(results, object.metadata)

View File

@ -25,7 +25,7 @@ func testMultipleObjectCreation(c *C, create func() Storage) {
objects := make(map[string][]byte) objects := make(map[string][]byte)
storage := create() storage := create()
err := storage.StoreBucket("bucket") err := storage.StoreBucket("bucket")
c.Check(err, IsNil) c.Assert(err, IsNil)
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
randomPerm := rand.Perm(10) randomPerm := rand.Perm(10)
randomString := "" randomString := ""
@ -35,22 +35,22 @@ func testMultipleObjectCreation(c *C, create func() Storage) {
key := "obj" + strconv.Itoa(i) key := "obj" + strconv.Itoa(i)
objects[key] = []byte(randomString) objects[key] = []byte(randomString)
err := storage.StoreObject("bucket", key, bytes.NewBufferString(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) etags := make(map[string]string)
for key, value := range objects { for key, value := range objects {
var byteBuffer bytes.Buffer var byteBuffer bytes.Buffer
storage.CopyObjectToWriter(&byteBuffer, "bucket", key) 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) metadata, err := storage.GetObjectMetadata("bucket", key)
c.Check(err, IsNil) c.Assert(err, IsNil)
c.Check(metadata.Size, Equals, int64(len(value))) c.Assert(metadata.Size, Equals, int64(len(value)))
_, ok := etags[metadata.ETag] _, ok := etags[metadata.ETag]
c.Check(ok, Equals, false) c.Assert(ok, Equals, false)
etags[metadata.ETag] = metadata.ETag etags[metadata.ETag] = metadata.ETag
} }
} }
@ -60,24 +60,58 @@ func testPaging(c *C, create func() Storage) {
storage.StoreBucket("bucket") storage.StoreBucket("bucket")
storage.ListObjects("bucket", "", 5) storage.ListObjects("bucket", "", 5)
objects, isTruncated, err := storage.ListObjects("bucket", "", 5) objects, isTruncated, err := storage.ListObjects("bucket", "", 5)
c.Check(len(objects), Equals, 0) c.Assert(len(objects), Equals, 0)
c.Check(isTruncated, Equals, false) c.Assert(isTruncated, Equals, false)
c.Check(err, IsNil) c.Assert(err, IsNil)
for i := 1; i <= 5; i++ { // check before paging occurs
for i := 0; i < 5; i++ {
key := "obj" + strconv.Itoa(i) key := "obj" + strconv.Itoa(i)
storage.StoreObject("bucket", key, bytes.NewBufferString(key)) storage.StoreObject("bucket", key, bytes.NewBufferString(key))
objects, isTruncated, err = storage.ListObjects("bucket", "", 5) objects, isTruncated, err = storage.ListObjects("bucket", "", 5)
c.Check(len(objects), Equals, i) c.Assert(len(objects), Equals, i+1)
c.Check(isTruncated, Equals, false) c.Assert(isTruncated, Equals, false)
c.Check(err, IsNil) c.Assert(err, IsNil)
} }
// check after paging occurs pages work
for i := 6; i <= 10; i++ { for i := 6; i <= 10; i++ {
key := "obj" + strconv.Itoa(i) key := "obj" + strconv.Itoa(i)
storage.StoreObject("bucket", key, bytes.NewBufferString(key)) storage.StoreObject("bucket", key, bytes.NewBufferString(key))
objects, isTruncated, err = storage.ListObjects("bucket", "", 5) objects, isTruncated, err = storage.ListObjects("bucket", "", 5)
c.Check(len(objects), Equals, 5) c.Assert(len(objects), Equals, 5)
c.Check(isTruncated, Equals, true) c.Assert(isTruncated, Equals, true)
c.Check(err, IsNil) 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 := create()
storage.StoreBucket("bucket") storage.StoreBucket("bucket")
err := storage.StoreObject("bucket", "object", bytes.NewBufferString("one")) err := storage.StoreObject("bucket", "object", bytes.NewBufferString("one"))
c.Check(err, IsNil) c.Assert(err, IsNil)
err = storage.StoreObject("bucket", "object", bytes.NewBufferString("three")) err = storage.StoreObject("bucket", "object", bytes.NewBufferString("three"))
c.Check(err, Not(IsNil)) c.Assert(err, Not(IsNil))
var bytesBuffer bytes.Buffer var bytesBuffer bytes.Buffer
length, err := storage.CopyObjectToWriter(&bytesBuffer, "bucket", "object") length, err := storage.CopyObjectToWriter(&bytesBuffer, "bucket", "object")
c.Check(length, Equals, int64(len("one"))) c.Assert(length, Equals, int64(len("one")))
c.Check(err, IsNil) c.Assert(err, IsNil)
c.Check(string(bytesBuffer.Bytes()), Equals, "one") c.Assert(string(bytesBuffer.Bytes()), Equals, "one")
} }
func testNonExistantBucketOperations(c *C, create func() Storage) { func testNonExistantBucketOperations(c *C, create func() Storage) {
storage := create() storage := create()
err := storage.StoreObject("bucket", "object", bytes.NewBufferString("one")) 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) { func testBucketRecreateFails(c *C, create func() Storage) {
storage := create() storage := create()
err := storage.StoreBucket("string") err := storage.StoreBucket("string")
c.Check(err, IsNil) c.Assert(err, IsNil)
err = storage.StoreBucket("string") err = storage.StoreBucket("string")
c.Check(err, Not(IsNil)) c.Assert(err, Not(IsNil))
} }