2015-02-08 05:54:21 -05:00
|
|
|
/*
|
|
|
|
* Mini Object Storage, (C) 2015 Minio, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-01-25 14:22:38 -05:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"math/rand"
|
|
|
|
"strconv"
|
|
|
|
|
2015-03-06 04:50:51 -05:00
|
|
|
"gopkg.in/check.v1"
|
2015-01-25 14:22:38 -05:00
|
|
|
)
|
|
|
|
|
2015-03-05 23:32:04 -05:00
|
|
|
// APITestSuite - collection of API tests
|
2015-03-06 04:50:51 -05:00
|
|
|
func APITestSuite(c *check.C, create func() Storage) {
|
2015-01-25 14:22:38 -05:00
|
|
|
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-27 20:47:11 -05:00
|
|
|
testPutObjectInSubdir(c, create)
|
2015-01-29 14:23:03 -05:00
|
|
|
testListBuckets(c, create)
|
2015-02-01 03:16:25 -05:00
|
|
|
testListBucketsOrder(c, create)
|
2015-02-01 13:48:31 -05:00
|
|
|
testListObjectsTestsForNonExistantBucket(c, create)
|
2015-02-03 19:44:32 -05:00
|
|
|
testNonExistantObjectInBucket(c, create)
|
|
|
|
testGetDirectoryReturnsObjectNotFound(c, create)
|
2015-02-04 20:32:40 -05:00
|
|
|
testDefaultContentType(c, create)
|
2015-03-17 16:32:10 -04:00
|
|
|
//testContentMd5Set(c, create) TODO
|
2015-01-25 14:22:38 -05:00
|
|
|
}
|
|
|
|
|
2015-03-06 04:50:51 -05:00
|
|
|
func testCreateBucket(c *check.C, create func() Storage) {
|
2015-03-17 16:32:10 -04:00
|
|
|
storage := create()
|
|
|
|
err := storage.CreateBucket("bucket")
|
|
|
|
c.Assert(err, check.IsNil)
|
2015-01-25 14:22:38 -05:00
|
|
|
}
|
|
|
|
|
2015-03-06 04:50:51 -05:00
|
|
|
func testMultipleObjectCreation(c *check.C, create func() Storage) {
|
2015-01-25 14:22:38 -05:00
|
|
|
objects := make(map[string][]byte)
|
|
|
|
storage := create()
|
2015-03-11 23:14:40 -04:00
|
|
|
err := storage.CreateBucket("bucket")
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(err, check.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)
|
2015-03-17 16:32:10 -04:00
|
|
|
err := storage.CreateObject("bucket", key, "", "", bytes.NewBufferString(randomString))
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-01-25 14:22:38 -05:00
|
|
|
}
|
|
|
|
|
2015-01-27 16:11:15 -05:00
|
|
|
// ensure no duplicate etags
|
2015-01-25 14:22:38 -05:00
|
|
|
etags := make(map[string]string)
|
|
|
|
for key, value := range objects {
|
|
|
|
var byteBuffer bytes.Buffer
|
2015-03-14 14:07:54 -04:00
|
|
|
_, err := storage.GetObject(&byteBuffer, "bucket", key)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(byteBuffer.Bytes(), check.DeepEquals, value)
|
2015-01-25 14:22:38 -05:00
|
|
|
|
2015-03-07 22:10:55 -05:00
|
|
|
metadata, err := storage.GetObjectMetadata("bucket", key, "")
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(metadata.Size, check.Equals, int64(len(value)))
|
2015-01-25 14:22:38 -05:00
|
|
|
|
2015-03-16 21:03:03 -04:00
|
|
|
_, ok := etags[metadata.Md5]
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(ok, check.Equals, false)
|
2015-03-16 21:03:03 -04:00
|
|
|
etags[metadata.Md5] = metadata.Md5
|
2015-01-25 14:22:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-06 04:50:51 -05:00
|
|
|
func testPaging(c *check.C, create func() Storage) {
|
2015-01-25 15:57:46 -05:00
|
|
|
storage := create()
|
2015-03-11 23:14:40 -04:00
|
|
|
storage.CreateBucket("bucket")
|
2015-02-26 20:23:42 -05:00
|
|
|
resources := BucketResourcesMetadata{}
|
|
|
|
objects, resources, err := storage.ListObjects("bucket", resources)
|
2015-03-16 23:41:00 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(len(objects), check.Equals, 0)
|
|
|
|
c.Assert(resources.IsTruncated, check.Equals, false)
|
2015-03-08 17:47:41 -04:00
|
|
|
// check before paging occurs
|
2015-01-27 16:11:15 -05:00
|
|
|
for i := 0; i < 5; i++ {
|
2015-01-25 15:57:46 -05:00
|
|
|
key := "obj" + strconv.Itoa(i)
|
2015-03-17 16:32:10 -04:00
|
|
|
storage.CreateObject("bucket", key, "", "", bytes.NewBufferString(key))
|
2015-02-26 20:23:42 -05:00
|
|
|
resources.Maxkeys = 5
|
|
|
|
objects, resources, err = storage.ListObjects("bucket", resources)
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(len(objects), check.Equals, i+1)
|
|
|
|
c.Assert(resources.IsTruncated, check.Equals, false)
|
|
|
|
c.Assert(err, check.IsNil)
|
2015-01-25 15:57:46 -05:00
|
|
|
}
|
2015-03-08 17:47:41 -04:00
|
|
|
// check after paging occurs pages work
|
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)
|
2015-03-17 16:32:10 -04:00
|
|
|
storage.CreateObject("bucket", key, "", "", bytes.NewBufferString(key))
|
2015-02-26 20:23:42 -05:00
|
|
|
resources.Maxkeys = 5
|
|
|
|
objects, resources, err = storage.ListObjects("bucket", resources)
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(len(objects), check.Equals, 5)
|
|
|
|
c.Assert(resources.IsTruncated, check.Equals, true)
|
|
|
|
c.Assert(err, check.IsNil)
|
2015-01-27 16:11:15 -05:00
|
|
|
}
|
2015-03-08 17:47:41 -04:00
|
|
|
// check paging with prefix at end returns less objects
|
2015-01-27 16:11:15 -05:00
|
|
|
{
|
2015-03-17 16:32:10 -04:00
|
|
|
storage.CreateObject("bucket", "newPrefix", "", "", bytes.NewBufferString("prefix1"))
|
|
|
|
storage.CreateObject("bucket", "newPrefix2", "", "", bytes.NewBufferString("prefix2"))
|
2015-02-26 20:23:42 -05:00
|
|
|
resources.Prefix = "new"
|
|
|
|
resources.Maxkeys = 5
|
|
|
|
objects, resources, err = storage.ListObjects("bucket", resources)
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(len(objects), check.Equals, 2)
|
2015-01-27 16:11:15 -05:00
|
|
|
}
|
|
|
|
|
2015-03-08 17:47:41 -04:00
|
|
|
// check ordering of pages
|
2015-01-27 16:11:15 -05:00
|
|
|
{
|
2015-02-26 20:23:42 -05:00
|
|
|
resources.Prefix = ""
|
|
|
|
resources.Maxkeys = 1000
|
|
|
|
objects, resources, err = storage.ListObjects("bucket", resources)
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(objects[0].Key, check.Equals, "newPrefix")
|
|
|
|
c.Assert(objects[1].Key, check.Equals, "newPrefix2")
|
|
|
|
c.Assert(objects[2].Key, check.Equals, "obj0")
|
|
|
|
c.Assert(objects[3].Key, check.Equals, "obj1")
|
|
|
|
c.Assert(objects[4].Key, check.Equals, "obj10")
|
2015-01-27 16:11:15 -05:00
|
|
|
}
|
2015-03-08 17:47:41 -04:00
|
|
|
|
|
|
|
// check delimited results with delimiter and prefix
|
|
|
|
{
|
2015-03-17 16:32:10 -04:00
|
|
|
storage.CreateObject("bucket", "this/is/delimited", "", "", bytes.NewBufferString("prefix1"))
|
|
|
|
storage.CreateObject("bucket", "this/is/also/delimited", "", "", bytes.NewBufferString("prefix2"))
|
2015-03-08 17:47:41 -04:00
|
|
|
var prefixes []string
|
|
|
|
resources.CommonPrefixes = prefixes // allocate new everytime
|
|
|
|
resources.Delimiter = "/"
|
|
|
|
resources.Prefix = "this/is/"
|
|
|
|
resources.Maxkeys = 10
|
|
|
|
objects, resources, err = storage.ListObjects("bucket", resources)
|
2015-03-16 23:41:00 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-03-08 17:47:41 -04:00
|
|
|
c.Assert(len(objects), check.Equals, 1)
|
|
|
|
c.Assert(resources.CommonPrefixes[0], check.Equals, "also/")
|
|
|
|
}
|
|
|
|
|
|
|
|
// check delimited results with delimiter without prefix
|
|
|
|
{
|
|
|
|
var prefixes []string
|
|
|
|
resources.CommonPrefixes = prefixes // allocate new everytime
|
|
|
|
resources.Delimiter = "/"
|
|
|
|
resources.Prefix = ""
|
|
|
|
resources.Maxkeys = 1000
|
|
|
|
objects, resources, err = storage.ListObjects("bucket", resources)
|
|
|
|
c.Assert(objects[0].Key, check.Equals, "newPrefix")
|
|
|
|
c.Assert(objects[1].Key, check.Equals, "newPrefix2")
|
|
|
|
c.Assert(objects[2].Key, check.Equals, "obj0")
|
|
|
|
c.Assert(objects[3].Key, check.Equals, "obj1")
|
|
|
|
c.Assert(objects[4].Key, check.Equals, "obj10")
|
|
|
|
c.Assert(resources.CommonPrefixes[0], check.Equals, "this/")
|
|
|
|
}
|
|
|
|
|
|
|
|
// check ordering of results with prefix
|
2015-01-27 16:11:15 -05:00
|
|
|
{
|
2015-02-26 20:23:42 -05:00
|
|
|
resources.Prefix = "obj"
|
2015-03-08 17:47:41 -04:00
|
|
|
resources.Delimiter = ""
|
2015-02-26 20:23:42 -05:00
|
|
|
resources.Maxkeys = 1000
|
|
|
|
objects, resources, err = storage.ListObjects("bucket", resources)
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(objects[0].Key, check.Equals, "obj0")
|
|
|
|
c.Assert(objects[1].Key, check.Equals, "obj1")
|
|
|
|
c.Assert(objects[2].Key, check.Equals, "obj10")
|
|
|
|
c.Assert(objects[3].Key, check.Equals, "obj2")
|
|
|
|
c.Assert(objects[4].Key, check.Equals, "obj3")
|
2015-01-27 16:11:15 -05:00
|
|
|
}
|
2015-03-08 17:47:41 -04:00
|
|
|
// check ordering of results with prefix and no paging
|
2015-01-27 16:11:15 -05:00
|
|
|
{
|
2015-02-26 20:23:42 -05:00
|
|
|
resources.Prefix = "new"
|
|
|
|
resources.Maxkeys = 5
|
|
|
|
objects, resources, err = storage.ListObjects("bucket", resources)
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(objects[0].Key, check.Equals, "newPrefix")
|
|
|
|
c.Assert(objects[1].Key, check.Equals, "newPrefix2")
|
2015-01-25 15:57:46 -05:00
|
|
|
}
|
2015-01-25 14:22:38 -05:00
|
|
|
}
|
|
|
|
|
2015-03-06 04:50:51 -05:00
|
|
|
func testObjectOverwriteFails(c *check.C, create func() Storage) {
|
2015-01-25 16:06:36 -05:00
|
|
|
storage := create()
|
2015-03-11 23:14:40 -04:00
|
|
|
storage.CreateBucket("bucket")
|
2015-03-17 16:32:10 -04:00
|
|
|
err := storage.CreateObject("bucket", "object", "", "", bytes.NewBufferString("one"))
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-03-17 16:32:10 -04:00
|
|
|
err = storage.CreateObject("bucket", "object", "", "", bytes.NewBufferString("three"))
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(err, check.Not(check.IsNil))
|
2015-01-27 14:09:20 -05:00
|
|
|
var bytesBuffer bytes.Buffer
|
2015-03-12 03:36:33 -04:00
|
|
|
length, err := storage.GetObject(&bytesBuffer, "bucket", "object")
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(length, check.Equals, int64(len("one")))
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(string(bytesBuffer.Bytes()), check.Equals, "one")
|
2015-01-25 14:22:38 -05:00
|
|
|
}
|
2015-01-25 16:06:36 -05:00
|
|
|
|
2015-03-06 04:50:51 -05:00
|
|
|
func testNonExistantBucketOperations(c *check.C, create func() Storage) {
|
2015-01-25 16:06:36 -05:00
|
|
|
storage := create()
|
2015-03-17 16:32:10 -04:00
|
|
|
err := storage.CreateObject("bucket", "object", "", "", bytes.NewBufferString("one"))
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(err, check.Not(check.IsNil))
|
2015-01-25 16:06:36 -05:00
|
|
|
}
|
|
|
|
|
2015-03-06 04:50:51 -05:00
|
|
|
func testBucketRecreateFails(c *check.C, create func() Storage) {
|
2015-01-25 16:06:36 -05:00
|
|
|
storage := create()
|
2015-03-11 23:14:40 -04:00
|
|
|
err := storage.CreateBucket("string")
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-03-11 23:14:40 -04:00
|
|
|
err = storage.CreateBucket("string")
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(err, check.Not(check.IsNil))
|
2015-01-25 14:22:38 -05:00
|
|
|
}
|
2015-01-27 20:47:11 -05:00
|
|
|
|
2015-03-06 04:50:51 -05:00
|
|
|
func testPutObjectInSubdir(c *check.C, create func() Storage) {
|
2015-01-27 20:47:11 -05:00
|
|
|
storage := create()
|
2015-03-11 23:14:40 -04:00
|
|
|
err := storage.CreateBucket("bucket")
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-03-17 16:32:10 -04:00
|
|
|
err = storage.CreateObject("bucket", "dir1/dir2/object", "", "", bytes.NewBufferString("hello world"))
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-01-27 20:47:11 -05:00
|
|
|
var bytesBuffer bytes.Buffer
|
2015-03-12 03:36:33 -04:00
|
|
|
length, err := storage.GetObject(&bytesBuffer, "bucket", "dir1/dir2/object")
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(len(bytesBuffer.Bytes()), check.Equals, len("hello world"))
|
|
|
|
c.Assert(int64(len(bytesBuffer.Bytes())), check.Equals, length)
|
|
|
|
c.Assert(err, check.IsNil)
|
2015-01-27 20:47:11 -05:00
|
|
|
}
|
2015-01-29 14:23:03 -05:00
|
|
|
|
2015-03-06 04:50:51 -05:00
|
|
|
func testListBuckets(c *check.C, create func() Storage) {
|
2015-01-29 14:23:03 -05:00
|
|
|
storage := create()
|
|
|
|
|
|
|
|
// test empty list
|
2015-02-15 20:03:27 -05:00
|
|
|
buckets, err := storage.ListBuckets()
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(len(buckets), check.Equals, 0)
|
|
|
|
c.Assert(err, check.IsNil)
|
2015-01-29 14:23:03 -05:00
|
|
|
|
|
|
|
// add one and test exists
|
2015-03-11 23:14:40 -04:00
|
|
|
err = storage.CreateBucket("bucket1")
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-01-29 14:23:03 -05:00
|
|
|
|
2015-02-15 20:03:27 -05:00
|
|
|
buckets, err = storage.ListBuckets()
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(len(buckets), check.Equals, 1)
|
|
|
|
c.Assert(err, check.IsNil)
|
2015-01-29 14:23:03 -05:00
|
|
|
|
|
|
|
// add two and test exists
|
2015-03-11 23:14:40 -04:00
|
|
|
err = storage.CreateBucket("bucket2")
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-01-29 14:23:03 -05:00
|
|
|
|
2015-02-15 20:03:27 -05:00
|
|
|
buckets, err = storage.ListBuckets()
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(len(buckets), check.Equals, 2)
|
|
|
|
c.Assert(err, check.IsNil)
|
2015-01-29 14:23:03 -05:00
|
|
|
|
|
|
|
// add three and test exists + prefix
|
2015-03-11 23:14:40 -04:00
|
|
|
err = storage.CreateBucket("bucket22")
|
2015-01-29 14:23:03 -05:00
|
|
|
|
2015-02-15 20:03:27 -05:00
|
|
|
buckets, err = storage.ListBuckets()
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(len(buckets), check.Equals, 3)
|
|
|
|
c.Assert(err, check.IsNil)
|
2015-01-29 14:23:03 -05:00
|
|
|
}
|
2015-02-01 03:16:25 -05:00
|
|
|
|
2015-03-06 04:50:51 -05:00
|
|
|
func testListBucketsOrder(c *check.C, create func() Storage) {
|
2015-02-01 13:48:31 -05:00
|
|
|
// if implementation contains a map, order of map keys will vary.
|
|
|
|
// this ensures they return in the same order each time
|
2015-02-01 03:16:25 -05:00
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
storage := create()
|
|
|
|
// add one and test exists
|
2015-03-11 23:14:40 -04:00
|
|
|
storage.CreateBucket("bucket1")
|
|
|
|
storage.CreateBucket("bucket2")
|
2015-02-01 03:16:25 -05:00
|
|
|
|
2015-02-15 20:03:27 -05:00
|
|
|
buckets, err := storage.ListBuckets()
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(len(buckets), check.Equals, 2)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(buckets[0].Name, check.Equals, "bucket1")
|
|
|
|
c.Assert(buckets[1].Name, check.Equals, "bucket2")
|
2015-02-01 03:16:25 -05:00
|
|
|
}
|
|
|
|
}
|
2015-02-01 13:48:31 -05:00
|
|
|
|
2015-03-06 04:50:51 -05:00
|
|
|
func testListObjectsTestsForNonExistantBucket(c *check.C, create func() Storage) {
|
2015-02-01 13:48:31 -05:00
|
|
|
storage := create()
|
2015-02-26 20:23:42 -05:00
|
|
|
resources := BucketResourcesMetadata{Prefix: "", Maxkeys: 1000}
|
|
|
|
objects, resources, err := storage.ListObjects("bucket", resources)
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(err, check.Not(check.IsNil))
|
|
|
|
c.Assert(resources.IsTruncated, check.Equals, false)
|
|
|
|
c.Assert(len(objects), check.Equals, 0)
|
2015-02-01 13:48:31 -05:00
|
|
|
}
|
2015-02-03 19:44:32 -05:00
|
|
|
|
2015-03-06 04:50:51 -05:00
|
|
|
func testNonExistantObjectInBucket(c *check.C, create func() Storage) {
|
2015-02-03 19:44:32 -05:00
|
|
|
storage := create()
|
2015-03-11 23:14:40 -04:00
|
|
|
err := storage.CreateBucket("bucket")
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-02-03 19:44:32 -05:00
|
|
|
|
|
|
|
var byteBuffer bytes.Buffer
|
2015-03-12 03:36:33 -04:00
|
|
|
length, err := storage.GetObject(&byteBuffer, "bucket", "dir1")
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(length, check.Equals, int64(0))
|
|
|
|
c.Assert(err, check.Not(check.IsNil))
|
|
|
|
c.Assert(len(byteBuffer.Bytes()), check.Equals, 0)
|
2015-02-03 19:44:32 -05:00
|
|
|
switch err := err.(type) {
|
|
|
|
case ObjectNotFound:
|
|
|
|
{
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(err, check.ErrorMatches, "Object not Found: bucket#dir1")
|
2015-02-03 19:44:32 -05:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(err, check.Equals, "fails")
|
2015-02-03 19:44:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-06 04:50:51 -05:00
|
|
|
func testGetDirectoryReturnsObjectNotFound(c *check.C, create func() Storage) {
|
2015-02-03 19:44:32 -05:00
|
|
|
storage := create()
|
2015-03-11 23:14:40 -04:00
|
|
|
err := storage.CreateBucket("bucket")
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-02-03 19:44:32 -05:00
|
|
|
|
2015-03-17 16:32:10 -04:00
|
|
|
err = storage.CreateObject("bucket", "dir1/dir2/object", "", "", bytes.NewBufferString("hello world"))
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-02-03 19:44:32 -05:00
|
|
|
|
|
|
|
var byteBuffer bytes.Buffer
|
2015-03-12 03:36:33 -04:00
|
|
|
length, err := storage.GetObject(&byteBuffer, "bucket", "dir1")
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(length, check.Equals, int64(0))
|
2015-02-03 19:44:32 -05:00
|
|
|
switch err := err.(type) {
|
|
|
|
case ObjectNotFound:
|
|
|
|
{
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(err.Bucket, check.Equals, "bucket")
|
|
|
|
c.Assert(err.Object, check.Equals, "dir1")
|
2015-02-03 19:44:32 -05:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
// force a failure with a line number
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(err, check.Equals, "ObjectNotFound")
|
2015-02-03 19:44:32 -05:00
|
|
|
}
|
|
|
|
}
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(len(byteBuffer.Bytes()), check.Equals, 0)
|
2015-02-03 19:44:32 -05:00
|
|
|
|
|
|
|
var byteBuffer2 bytes.Buffer
|
2015-03-12 03:36:33 -04:00
|
|
|
length, err = storage.GetObject(&byteBuffer, "bucket", "dir1/")
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(length, check.Equals, int64(0))
|
2015-02-03 19:44:32 -05:00
|
|
|
switch err := err.(type) {
|
|
|
|
case ObjectNotFound:
|
|
|
|
{
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(err.Bucket, check.Equals, "bucket")
|
|
|
|
c.Assert(err.Object, check.Equals, "dir1/")
|
2015-02-03 19:44:32 -05:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
// force a failure with a line number
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(err, check.Equals, "ObjectNotFound")
|
2015-02-03 19:44:32 -05:00
|
|
|
}
|
|
|
|
}
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(len(byteBuffer2.Bytes()), check.Equals, 0)
|
2015-02-03 19:44:32 -05:00
|
|
|
}
|
2015-02-04 20:32:40 -05:00
|
|
|
|
2015-03-06 04:50:51 -05:00
|
|
|
func testDefaultContentType(c *check.C, create func() Storage) {
|
2015-02-04 20:32:40 -05:00
|
|
|
storage := create()
|
2015-03-11 23:14:40 -04:00
|
|
|
err := storage.CreateBucket("bucket")
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-02-04 20:32:40 -05:00
|
|
|
|
|
|
|
// test empty
|
2015-03-17 16:32:10 -04:00
|
|
|
err = storage.CreateObject("bucket", "one", "", "", bytes.NewBufferString("one"))
|
2015-03-07 22:10:55 -05:00
|
|
|
metadata, err := storage.GetObjectMetadata("bucket", "one", "")
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(metadata.ContentType, check.Equals, "application/octet-stream")
|
2015-02-04 20:32:40 -05:00
|
|
|
|
|
|
|
// test custom
|
2015-03-17 16:32:10 -04:00
|
|
|
storage.CreateObject("bucket", "two", "application/text", "", bytes.NewBufferString("two"))
|
2015-03-07 22:10:55 -05:00
|
|
|
metadata, err = storage.GetObjectMetadata("bucket", "two", "")
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(metadata.ContentType, check.Equals, "application/text")
|
2015-02-04 20:32:40 -05:00
|
|
|
|
|
|
|
// test trim space
|
2015-03-17 16:32:10 -04:00
|
|
|
storage.CreateObject("bucket", "three", "\tapplication/json ", "", bytes.NewBufferString("three"))
|
2015-03-07 22:10:55 -05:00
|
|
|
metadata, err = storage.GetObjectMetadata("bucket", "three", "")
|
2015-03-06 04:50:51 -05:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(metadata.ContentType, check.Equals, "application/json")
|
2015-02-04 20:32:40 -05:00
|
|
|
}
|
2015-03-17 16:32:10 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
func testContentMd5Set(c *check.C, create func() Storage) {
|
|
|
|
storage := create()
|
|
|
|
err := storage.CreateBucket("bucket")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
// test md5 invalid
|
|
|
|
err = storage.CreateObject("bucket", "one", "", "NWJiZjVhNTIzMjhlNzQzOWFlNmU3MTlkZmU3MTIyMDA", bytes.NewBufferString("one"))
|
|
|
|
c.Assert(err, check.Not(check.IsNil))
|
|
|
|
err = storage.CreateObject("bucket", "two", "", "NWJiZjVhNTIzMjhlNzQzOWFlNmU3MTlkZmU3MTIyMDA=", bytes.NewBufferString("one"))
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
}
|
|
|
|
*/
|