2015-01-25 14:22:38 -05:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"math/rand"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
. "gopkg.in/check.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
func APITestSuite(c *C, create func() Storage) {
|
|
|
|
testCreateBucket(c, create)
|
|
|
|
testMultipleObjectCreation(c, create)
|
|
|
|
testPaging(c, create)
|
|
|
|
testObjectOverwriteFails(c, create)
|
|
|
|
testNonExistantBucketOperations(c, create)
|
2015-01-25 16:06:36 -05:00
|
|
|
testBucketRecreateFails(c, create)
|
2015-01-25 14:22:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func testCreateBucket(c *C, create func() Storage) {
|
|
|
|
// test create bucket
|
|
|
|
// test bucket exists
|
|
|
|
// test no objects exist
|
|
|
|
// 2x
|
|
|
|
}
|
|
|
|
|
|
|
|
func testMultipleObjectCreation(c *C, create func() Storage) {
|
|
|
|
objects := make(map[string][]byte)
|
|
|
|
storage := create()
|
|
|
|
storage.StoreBucket("bucket")
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
randomPerm := rand.Perm(10)
|
|
|
|
randomString := ""
|
|
|
|
for _, num := range randomPerm {
|
|
|
|
randomString = randomString + strconv.Itoa(num)
|
|
|
|
}
|
|
|
|
key := "obj" + strconv.Itoa(i)
|
|
|
|
objects[key] = []byte(randomString)
|
|
|
|
err := storage.StoreObject("bucket", key, bytes.NewBufferString(randomString))
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure no duplicates
|
|
|
|
etags := make(map[string]string)
|
|
|
|
for key, value := range objects {
|
|
|
|
var byteBuffer bytes.Buffer
|
|
|
|
storage.CopyObjectToWriter(&byteBuffer, "bucket", key)
|
|
|
|
c.Assert(bytes.Equal(value, byteBuffer.Bytes()), Equals, true)
|
|
|
|
|
|
|
|
metadata, err := storage.GetObjectMetadata("bucket", key)
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
c.Assert(metadata.Size, Equals, len(value))
|
|
|
|
|
|
|
|
_, ok := etags[metadata.ETag]
|
|
|
|
c.Assert(ok, Equals, false)
|
|
|
|
etags[metadata.ETag] = metadata.ETag
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testPaging(c *C, create func() Storage) {
|
2015-01-25 15:57:46 -05:00
|
|
|
storage := create()
|
|
|
|
storage.StoreBucket("bucket")
|
|
|
|
storage.ListObjects("bucket", "", 1000)
|
2015-01-25 18:35:08 -05:00
|
|
|
objects, isTruncated, err := storage.ListObjects("bucket", "", 1000)
|
2015-01-25 16:32:39 -05:00
|
|
|
c.Assert(len(objects), Equals, 0)
|
|
|
|
c.Assert(isTruncated, Equals, false)
|
2015-01-25 18:35:08 -05:00
|
|
|
c.Assert(err, IsNil)
|
2015-01-25 15:57:46 -05:00
|
|
|
for i := 1; i <= 1000; i++ {
|
|
|
|
key := "obj" + strconv.Itoa(i)
|
|
|
|
storage.StoreObject("bucket", key, bytes.NewBufferString(key))
|
2015-01-25 18:35:08 -05:00
|
|
|
objects, isTruncated, err = storage.ListObjects("bucket", "", 1000)
|
2015-01-25 16:32:39 -05:00
|
|
|
c.Assert(len(objects), Equals, i)
|
|
|
|
c.Assert(isTruncated, Equals, false)
|
2015-01-25 18:35:08 -05:00
|
|
|
c.Assert(err, IsNil)
|
2015-01-25 15:57:46 -05:00
|
|
|
}
|
|
|
|
for i := 1001; i <= 2000; i++ {
|
|
|
|
key := "obj" + strconv.Itoa(i)
|
|
|
|
storage.StoreObject("bucket", key, bytes.NewBufferString(key))
|
2015-01-25 18:35:08 -05:00
|
|
|
objects, isTruncated, err = storage.ListObjects("bucket", "", 1000)
|
2015-01-25 16:32:39 -05:00
|
|
|
c.Assert(len(objects), Equals, 1000)
|
|
|
|
c.Assert(isTruncated, Equals, true)
|
2015-01-25 18:35:08 -05:00
|
|
|
c.Assert(err, IsNil)
|
2015-01-25 15:57:46 -05:00
|
|
|
}
|
2015-01-25 14:22:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func testObjectOverwriteFails(c *C, create func() Storage) {
|
2015-01-25 16:06:36 -05:00
|
|
|
storage := create()
|
|
|
|
storage.StoreBucket("bucket")
|
|
|
|
err := storage.StoreObject("bucket", "object", bytes.NewBufferString("one"))
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
err = storage.StoreObject("bucket", "object", bytes.NewBufferString("one"))
|
|
|
|
c.Assert(err, Not(IsNil))
|
2015-01-25 14:22:38 -05:00
|
|
|
}
|
2015-01-25 16:06:36 -05:00
|
|
|
|
2015-01-25 14:22:38 -05:00
|
|
|
func testNonExistantBucketOperations(c *C, create func() Storage) {
|
2015-01-25 16:06:36 -05:00
|
|
|
storage := create()
|
|
|
|
err := storage.StoreObject("bucket", "object", bytes.NewBufferString("one"))
|
|
|
|
c.Assert(err, Not(IsNil))
|
|
|
|
}
|
|
|
|
|
|
|
|
func testBucketRecreateFails(c *C, create func() Storage) {
|
|
|
|
storage := create()
|
|
|
|
err := storage.StoreBucket("string")
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
err = storage.StoreBucket("string")
|
|
|
|
c.Assert(err, Not(IsNil))
|
2015-01-25 14:22:38 -05:00
|
|
|
}
|