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)
|
2015-01-27 14:09:20 -05:00
|
|
|
testPaging(c, create)
|
|
|
|
testObjectOverwriteFails(c, create)
|
|
|
|
testNonExistantBucketOperations(c, create)
|
|
|
|
testBucketRecreateFails(c, create)
|
2015-01-25 14:22:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func testCreateBucket(c *C, create func() Storage) {
|
2015-01-25 20:41:59 -05:00
|
|
|
// TODO
|
2015-01-25 14:22:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func testMultipleObjectCreation(c *C, create func() Storage) {
|
|
|
|
objects := make(map[string][]byte)
|
|
|
|
storage := create()
|
2015-01-25 20:41:59 -05:00
|
|
|
err := storage.StoreBucket("bucket")
|
|
|
|
c.Check(err, IsNil)
|
2015-01-25 14:22:38 -05:00
|
|
|
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))
|
2015-01-25 20:41:59 -05:00
|
|
|
c.Check(err, IsNil)
|
2015-01-25 14:22:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ensure no duplicates
|
|
|
|
etags := make(map[string]string)
|
|
|
|
for key, value := range objects {
|
|
|
|
var byteBuffer bytes.Buffer
|
|
|
|
storage.CopyObjectToWriter(&byteBuffer, "bucket", key)
|
2015-01-25 20:41:59 -05:00
|
|
|
c.Check(bytes.Equal(value, byteBuffer.Bytes()), Equals, true)
|
2015-01-25 14:22:38 -05:00
|
|
|
|
|
|
|
metadata, err := storage.GetObjectMetadata("bucket", key)
|
2015-01-25 20:41:59 -05:00
|
|
|
c.Check(err, IsNil)
|
|
|
|
c.Check(metadata.Size, Equals, int64(len(value)))
|
2015-01-25 14:22:38 -05:00
|
|
|
|
|
|
|
_, ok := etags[metadata.ETag]
|
2015-01-25 20:41:59 -05:00
|
|
|
c.Check(ok, Equals, false)
|
2015-01-25 14:22:38 -05:00
|
|
|
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")
|
2015-01-27 14:09:20 -05:00
|
|
|
storage.ListObjects("bucket", "", 5)
|
|
|
|
objects, isTruncated, err := storage.ListObjects("bucket", "", 5)
|
2015-01-25 20:41:59 -05:00
|
|
|
c.Check(len(objects), Equals, 0)
|
|
|
|
c.Check(isTruncated, Equals, false)
|
|
|
|
c.Check(err, IsNil)
|
2015-01-27 14:09:20 -05:00
|
|
|
for i := 1; i <= 5; i++ {
|
2015-01-25 15:57:46 -05:00
|
|
|
key := "obj" + strconv.Itoa(i)
|
|
|
|
storage.StoreObject("bucket", key, bytes.NewBufferString(key))
|
2015-01-27 14:09:20 -05:00
|
|
|
objects, isTruncated, err = storage.ListObjects("bucket", "", 5)
|
2015-01-25 20:41:59 -05:00
|
|
|
c.Check(len(objects), Equals, i)
|
|
|
|
c.Check(isTruncated, Equals, false)
|
|
|
|
c.Check(err, IsNil)
|
2015-01-25 15:57:46 -05:00
|
|
|
}
|
2015-01-27 14:09:20 -05:00
|
|
|
for i := 6; i <= 10; i++ {
|
2015-01-25 15:57:46 -05:00
|
|
|
key := "obj" + strconv.Itoa(i)
|
|
|
|
storage.StoreObject("bucket", key, bytes.NewBufferString(key))
|
2015-01-27 14:09:20 -05:00
|
|
|
objects, isTruncated, err = storage.ListObjects("bucket", "", 5)
|
|
|
|
c.Check(len(objects), Equals, 5)
|
2015-01-25 20:41:59 -05:00
|
|
|
c.Check(isTruncated, Equals, true)
|
|
|
|
c.Check(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"))
|
2015-01-25 20:41:59 -05:00
|
|
|
c.Check(err, IsNil)
|
2015-01-27 14:09:20 -05:00
|
|
|
err = storage.StoreObject("bucket", "object", bytes.NewBufferString("three"))
|
2015-01-25 20:41:59 -05:00
|
|
|
c.Check(err, Not(IsNil))
|
2015-01-27 14:09:20 -05:00
|
|
|
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")
|
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"))
|
2015-01-25 20:41:59 -05:00
|
|
|
c.Check(err, Not(IsNil))
|
2015-01-25 16:06:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func testBucketRecreateFails(c *C, create func() Storage) {
|
|
|
|
storage := create()
|
|
|
|
err := storage.StoreBucket("string")
|
2015-01-25 20:41:59 -05:00
|
|
|
c.Check(err, IsNil)
|
2015-01-25 16:06:36 -05:00
|
|
|
err = storage.StoreBucket("string")
|
2015-01-25 20:41:59 -05:00
|
|
|
c.Check(err, Not(IsNil))
|
2015-01-25 14:22:38 -05:00
|
|
|
}
|