Adding more tests, including ordering of objects in prefix

master
Frederick F. Kautz IV 10 years ago
parent af52f88187
commit 044dc9af80
  1. 11
      pkg/storage/inmemory/inmemory.go
  2. 84
      pkg/storage/storage_api_suite.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)

@ -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))
}

Loading…
Cancel
Save