2015-04-08 19:28:14 -04:00
|
|
|
/*
|
2015-07-24 20:51:40 -04:00
|
|
|
* Minio Cloud Storage, (C) 2015 Minio, Inc.
|
2015-04-08 19:28:14 -04:00
|
|
|
*
|
|
|
|
* 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,
|
2015-07-02 01:36:33 -04:00
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or impliedd.
|
2015-04-08 19:28:14 -04:00
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-04-07 00:42:54 -04:00
|
|
|
package donut
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-04-07 18:55:44 -04:00
|
|
|
"crypto/md5"
|
2015-07-01 21:09:44 -04:00
|
|
|
"encoding/base64"
|
2015-04-07 18:55:44 -04:00
|
|
|
"encoding/hex"
|
2015-04-07 00:42:54 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2015-06-18 19:02:34 -04:00
|
|
|
"path/filepath"
|
2015-04-07 00:42:54 -04:00
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
2015-07-18 14:20:09 -04:00
|
|
|
. "gopkg.in/check.v1"
|
2015-04-07 00:42:54 -04:00
|
|
|
)
|
|
|
|
|
2015-07-02 01:36:33 -04:00
|
|
|
func TestDonut(t *testing.T) { TestingT(t) }
|
2015-04-07 00:42:54 -04:00
|
|
|
|
2015-07-02 01:36:33 -04:00
|
|
|
type MyDonutSuite struct {
|
2015-07-01 21:09:44 -04:00
|
|
|
root string
|
|
|
|
}
|
2015-04-07 00:42:54 -04:00
|
|
|
|
2015-07-02 01:36:33 -04:00
|
|
|
var _ = Suite(&MyDonutSuite{})
|
2015-04-07 00:42:54 -04:00
|
|
|
|
|
|
|
// create a dummy TestNodeDiskMap
|
|
|
|
func createTestNodeDiskMap(p string) map[string][]string {
|
|
|
|
nodes := make(map[string][]string)
|
|
|
|
nodes["localhost"] = make([]string, 16)
|
|
|
|
for i := 0; i < len(nodes["localhost"]); i++ {
|
2015-06-18 19:02:34 -04:00
|
|
|
diskPath := filepath.Join(p, strconv.Itoa(i))
|
2015-04-07 00:42:54 -04:00
|
|
|
if _, err := os.Stat(diskPath); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
os.MkdirAll(diskPath, 0700)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nodes["localhost"][i] = diskPath
|
|
|
|
}
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
2015-07-02 23:31:22 -04:00
|
|
|
var dd Interface
|
2015-07-01 21:09:44 -04:00
|
|
|
|
2015-07-02 01:36:33 -04:00
|
|
|
func (s *MyDonutSuite) SetUpSuite(c *C) {
|
2015-04-07 00:42:54 -04:00
|
|
|
root, err := ioutil.TempDir(os.TempDir(), "donut-")
|
|
|
|
c.Assert(err, IsNil)
|
2015-07-01 21:09:44 -04:00
|
|
|
s.root = root
|
2015-07-02 23:31:22 -04:00
|
|
|
|
|
|
|
conf := new(Config)
|
2015-07-06 01:17:56 -04:00
|
|
|
conf.Version = "0.0.1"
|
2015-07-02 23:31:22 -04:00
|
|
|
conf.DonutName = "test"
|
|
|
|
conf.NodeDiskMap = createTestNodeDiskMap(root)
|
|
|
|
conf.MaxSize = 100000
|
2015-07-14 14:55:28 -04:00
|
|
|
SetDonutConfigPath(filepath.Join(root, "donut.json"))
|
2015-07-06 01:17:56 -04:00
|
|
|
err = SaveConfig(conf)
|
|
|
|
c.Assert(err, IsNil)
|
2015-07-02 23:31:22 -04:00
|
|
|
|
2015-07-06 01:17:56 -04:00
|
|
|
dd, err = New()
|
2015-07-02 23:31:22 -04:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
2015-07-02 01:36:33 -04:00
|
|
|
// testing empty donut
|
2015-07-09 22:34:07 -04:00
|
|
|
buckets, err := dd.ListBuckets(nil)
|
2015-04-07 00:42:54 -04:00
|
|
|
c.Assert(err, IsNil)
|
2015-07-01 21:09:44 -04:00
|
|
|
c.Assert(len(buckets), Equals, 0)
|
|
|
|
}
|
2015-04-07 00:42:54 -04:00
|
|
|
|
2015-07-02 01:36:33 -04:00
|
|
|
func (s *MyDonutSuite) TearDownSuite(c *C) {
|
2015-07-01 21:09:44 -04:00
|
|
|
os.RemoveAll(s.root)
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// test make bucket without name
|
2015-07-02 01:36:33 -04:00
|
|
|
func (s *MyDonutSuite) TestBucketWithoutNameFails(c *C) {
|
2015-04-07 00:42:54 -04:00
|
|
|
// fail to create new bucket without a name
|
2015-07-14 12:17:30 -04:00
|
|
|
err := dd.MakeBucket("", "private", nil, nil)
|
2015-04-07 00:42:54 -04:00
|
|
|
c.Assert(err, Not(IsNil))
|
|
|
|
|
2015-07-14 12:17:30 -04:00
|
|
|
err = dd.MakeBucket(" ", "private", nil, nil)
|
2015-04-07 00:42:54 -04:00
|
|
|
c.Assert(err, Not(IsNil))
|
|
|
|
}
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// test empty bucket
|
2015-07-02 01:36:33 -04:00
|
|
|
func (s *MyDonutSuite) TestEmptyBucket(c *C) {
|
2015-07-14 12:17:30 -04:00
|
|
|
c.Assert(dd.MakeBucket("foo1", "private", nil, nil), IsNil)
|
2015-04-08 19:28:14 -04:00
|
|
|
// check if bucket is empty
|
2015-07-01 21:09:44 -04:00
|
|
|
var resources BucketResourcesMetadata
|
|
|
|
resources.Maxkeys = 1
|
2015-07-09 22:34:07 -04:00
|
|
|
objectsMetadata, resources, err := dd.ListObjects("foo1", resources, nil)
|
2015-04-08 19:28:14 -04:00
|
|
|
c.Assert(err, IsNil)
|
2015-07-01 21:09:44 -04:00
|
|
|
c.Assert(len(objectsMetadata), Equals, 0)
|
|
|
|
c.Assert(resources.CommonPrefixes, DeepEquals, []string{})
|
|
|
|
c.Assert(resources.IsTruncated, Equals, false)
|
2015-04-08 19:28:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// test bucket list
|
2015-07-02 01:36:33 -04:00
|
|
|
func (s *MyDonutSuite) TestMakeBucketAndList(c *C) {
|
2015-04-07 00:42:54 -04:00
|
|
|
// create bucket
|
2015-07-14 12:17:30 -04:00
|
|
|
err := dd.MakeBucket("foo2", "private", nil, nil)
|
2015-04-07 00:42:54 -04:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
// check bucket exists
|
2015-07-09 22:34:07 -04:00
|
|
|
buckets, err := dd.ListBuckets(nil)
|
2015-04-07 00:42:54 -04:00
|
|
|
c.Assert(err, IsNil)
|
2015-07-01 21:09:44 -04:00
|
|
|
c.Assert(len(buckets), Equals, 5)
|
|
|
|
c.Assert(buckets[0].ACL, Equals, BucketACL("private"))
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// test re-create bucket
|
2015-07-02 01:36:33 -04:00
|
|
|
func (s *MyDonutSuite) TestMakeBucketWithSameNameFails(c *C) {
|
2015-07-14 12:17:30 -04:00
|
|
|
err := dd.MakeBucket("foo3", "private", nil, nil)
|
2015-04-07 00:42:54 -04:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
2015-07-14 12:17:30 -04:00
|
|
|
err = dd.MakeBucket("foo3", "private", nil, nil)
|
2015-04-07 00:42:54 -04:00
|
|
|
c.Assert(err, Not(IsNil))
|
|
|
|
}
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// test make multiple buckets
|
2015-07-02 01:36:33 -04:00
|
|
|
func (s *MyDonutSuite) TestCreateMultipleBucketsAndList(c *C) {
|
2015-04-07 00:42:54 -04:00
|
|
|
// add a second bucket
|
2015-07-14 12:17:30 -04:00
|
|
|
err := dd.MakeBucket("foo4", "private", nil, nil)
|
2015-04-07 00:42:54 -04:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
2015-07-14 12:17:30 -04:00
|
|
|
err = dd.MakeBucket("bar1", "private", nil, nil)
|
2015-04-07 00:42:54 -04:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
2015-07-09 22:34:07 -04:00
|
|
|
buckets, err := dd.ListBuckets(nil)
|
2015-04-07 00:42:54 -04:00
|
|
|
c.Assert(err, IsNil)
|
2015-05-21 13:57:38 -04:00
|
|
|
|
2015-07-01 21:09:44 -04:00
|
|
|
c.Assert(len(buckets), Equals, 2)
|
|
|
|
c.Assert(buckets[0].Name, Equals, "bar1")
|
|
|
|
c.Assert(buckets[1].Name, Equals, "foo4")
|
2015-04-07 00:42:54 -04:00
|
|
|
|
2015-07-14 12:17:30 -04:00
|
|
|
err = dd.MakeBucket("foobar1", "private", nil, nil)
|
2015-04-07 00:42:54 -04:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
2015-07-09 22:34:07 -04:00
|
|
|
buckets, err = dd.ListBuckets(nil)
|
2015-04-07 00:42:54 -04:00
|
|
|
c.Assert(err, IsNil)
|
2015-07-01 21:09:44 -04:00
|
|
|
|
|
|
|
c.Assert(len(buckets), Equals, 3)
|
|
|
|
c.Assert(buckets[2].Name, Equals, "foobar1")
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// test object create without bucket
|
2015-07-02 01:36:33 -04:00
|
|
|
func (s *MyDonutSuite) TestNewObjectFailsWithoutBucket(c *C) {
|
2015-07-09 17:42:04 -04:00
|
|
|
_, err := dd.CreateObject("unknown", "obj", "", 0, nil, nil, nil)
|
2015-04-07 00:42:54 -04:00
|
|
|
c.Assert(err, Not(IsNil))
|
|
|
|
}
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// test create object metadata
|
2015-07-02 01:36:33 -04:00
|
|
|
func (s *MyDonutSuite) TestNewObjectMetadata(c *C) {
|
2015-04-07 18:55:44 -04:00
|
|
|
data := "Hello World"
|
|
|
|
hasher := md5.New()
|
|
|
|
hasher.Write([]byte(data))
|
2015-07-01 21:09:44 -04:00
|
|
|
expectedMd5Sum := base64.StdEncoding.EncodeToString(hasher.Sum(nil))
|
2015-04-07 18:55:44 -04:00
|
|
|
reader := ioutil.NopCloser(bytes.NewReader([]byte(data)))
|
|
|
|
|
2015-07-14 12:17:30 -04:00
|
|
|
err := dd.MakeBucket("foo6", "private", nil, nil)
|
2015-04-07 18:55:44 -04:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
2015-07-09 17:42:04 -04:00
|
|
|
objectMetadata, err := dd.CreateObject("foo6", "obj", expectedMd5Sum, int64(len(data)), reader, map[string]string{"contentType": "application/json"}, nil)
|
2015-04-07 18:55:44 -04:00
|
|
|
c.Assert(err, IsNil)
|
2015-07-01 21:09:44 -04:00
|
|
|
c.Assert(objectMetadata.MD5Sum, Equals, hex.EncodeToString(hasher.Sum(nil)))
|
|
|
|
c.Assert(objectMetadata.Metadata["contentType"], Equals, "application/json")
|
2015-04-07 18:55:44 -04:00
|
|
|
}
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// test create object fails without name
|
2015-07-02 01:36:33 -04:00
|
|
|
func (s *MyDonutSuite) TestNewObjectFailsWithEmptyName(c *C) {
|
2015-07-09 17:42:04 -04:00
|
|
|
_, err := dd.CreateObject("foo", "", "", 0, nil, nil, nil)
|
2015-04-07 00:42:54 -04:00
|
|
|
c.Assert(err, Not(IsNil))
|
|
|
|
}
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// test create object
|
2015-07-02 01:36:33 -04:00
|
|
|
func (s *MyDonutSuite) TestNewObjectCanBeWritten(c *C) {
|
2015-07-14 12:17:30 -04:00
|
|
|
err := dd.MakeBucket("foo", "private", nil, nil)
|
2015-04-07 00:42:54 -04:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
data := "Hello World"
|
2015-04-07 18:55:44 -04:00
|
|
|
|
|
|
|
hasher := md5.New()
|
|
|
|
hasher.Write([]byte(data))
|
2015-07-01 21:09:44 -04:00
|
|
|
expectedMd5Sum := base64.StdEncoding.EncodeToString(hasher.Sum(nil))
|
2015-04-07 00:42:54 -04:00
|
|
|
reader := ioutil.NopCloser(bytes.NewReader([]byte(data)))
|
|
|
|
|
2015-07-09 17:42:04 -04:00
|
|
|
actualMetadata, err := dd.CreateObject("foo", "obj", expectedMd5Sum, int64(len(data)), reader, map[string]string{"contentType": "application/octet-stream"}, nil)
|
2015-04-07 00:42:54 -04:00
|
|
|
c.Assert(err, IsNil)
|
2015-07-01 21:09:44 -04:00
|
|
|
c.Assert(actualMetadata.MD5Sum, Equals, hex.EncodeToString(hasher.Sum(nil)))
|
2015-04-07 00:42:54 -04:00
|
|
|
|
2015-07-01 21:09:44 -04:00
|
|
|
var buffer bytes.Buffer
|
2015-07-02 01:36:33 -04:00
|
|
|
size, err := dd.GetObject(&buffer, "foo", "obj")
|
2015-04-07 00:42:54 -04:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
c.Assert(size, Equals, int64(len(data)))
|
2015-07-01 21:09:44 -04:00
|
|
|
c.Assert(buffer.Bytes(), DeepEquals, []byte(data))
|
2015-04-07 00:42:54 -04:00
|
|
|
|
2015-07-09 22:34:07 -04:00
|
|
|
actualMetadata, err = dd.GetObjectMetadata("foo", "obj", nil)
|
2015-04-07 00:42:54 -04:00
|
|
|
c.Assert(err, IsNil)
|
2015-07-01 21:09:44 -04:00
|
|
|
c.Assert(hex.EncodeToString(hasher.Sum(nil)), Equals, actualMetadata.MD5Sum)
|
2015-06-26 18:32:24 -04:00
|
|
|
c.Assert(int64(len(data)), Equals, actualMetadata.Size)
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// test list objects
|
2015-07-02 01:36:33 -04:00
|
|
|
func (s *MyDonutSuite) TestMultipleNewObjects(c *C) {
|
2015-07-14 12:17:30 -04:00
|
|
|
c.Assert(dd.MakeBucket("foo5", "private", nil, nil), IsNil)
|
2015-04-07 00:42:54 -04:00
|
|
|
|
|
|
|
one := ioutil.NopCloser(bytes.NewReader([]byte("one")))
|
2015-05-04 02:16:10 -04:00
|
|
|
|
2015-07-09 17:42:04 -04:00
|
|
|
_, err := dd.CreateObject("foo5", "obj1", "", int64(len("one")), one, nil, nil)
|
2015-04-07 00:42:54 -04:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
two := ioutil.NopCloser(bytes.NewReader([]byte("two")))
|
2015-07-09 17:42:04 -04:00
|
|
|
_, err = dd.CreateObject("foo5", "obj2", "", int64(len("two")), two, nil, nil)
|
2015-04-07 00:42:54 -04:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
2015-07-01 21:09:44 -04:00
|
|
|
var buffer1 bytes.Buffer
|
2015-07-02 01:36:33 -04:00
|
|
|
size, err := dd.GetObject(&buffer1, "foo5", "obj1")
|
2015-04-07 00:42:54 -04:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
c.Assert(size, Equals, int64(len([]byte("one"))))
|
2015-07-01 21:09:44 -04:00
|
|
|
c.Assert(buffer1.Bytes(), DeepEquals, []byte("one"))
|
2015-04-07 00:42:54 -04:00
|
|
|
|
2015-07-01 21:09:44 -04:00
|
|
|
var buffer2 bytes.Buffer
|
2015-07-02 01:36:33 -04:00
|
|
|
size, err = dd.GetObject(&buffer2, "foo5", "obj2")
|
2015-04-07 00:42:54 -04:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
c.Assert(size, Equals, int64(len([]byte("two"))))
|
|
|
|
|
2015-07-01 21:09:44 -04:00
|
|
|
c.Assert(buffer2.Bytes(), DeepEquals, []byte("two"))
|
2015-04-07 03:01:19 -04:00
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
/// test list of objects
|
|
|
|
|
|
|
|
// test list objects with prefix and delimiter
|
2015-07-01 21:09:44 -04:00
|
|
|
var resources BucketResourcesMetadata
|
|
|
|
resources.Prefix = "o"
|
|
|
|
resources.Delimiter = "1"
|
|
|
|
resources.Maxkeys = 10
|
2015-07-09 22:34:07 -04:00
|
|
|
objectsMetadata, resources, err := dd.ListObjects("foo5", resources, nil)
|
2015-04-07 00:42:54 -04:00
|
|
|
c.Assert(err, IsNil)
|
2015-07-01 21:09:44 -04:00
|
|
|
c.Assert(resources.IsTruncated, Equals, false)
|
|
|
|
c.Assert(resources.CommonPrefixes[0], Equals, "obj1")
|
2015-04-08 19:28:14 -04:00
|
|
|
|
|
|
|
// test list objects with only delimiter
|
2015-07-01 21:09:44 -04:00
|
|
|
resources.Prefix = ""
|
|
|
|
resources.Delimiter = "1"
|
|
|
|
resources.Maxkeys = 10
|
2015-07-09 22:34:07 -04:00
|
|
|
objectsMetadata, resources, err = dd.ListObjects("foo5", resources, nil)
|
2015-04-08 19:28:14 -04:00
|
|
|
c.Assert(err, IsNil)
|
2015-07-02 01:36:33 -04:00
|
|
|
c.Assert(objectsMetadata[0].Object, Equals, "obj2")
|
2015-07-01 21:09:44 -04:00
|
|
|
c.Assert(resources.IsTruncated, Equals, false)
|
|
|
|
c.Assert(resources.CommonPrefixes[0], Equals, "obj1")
|
2015-04-07 00:42:54 -04:00
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// test list objects with only prefix
|
2015-07-01 21:09:44 -04:00
|
|
|
resources.Prefix = "o"
|
|
|
|
resources.Delimiter = ""
|
|
|
|
resources.Maxkeys = 10
|
2015-07-09 22:34:07 -04:00
|
|
|
objectsMetadata, resources, err = dd.ListObjects("foo5", resources, nil)
|
2015-04-07 00:42:54 -04:00
|
|
|
c.Assert(err, IsNil)
|
2015-07-01 21:09:44 -04:00
|
|
|
c.Assert(resources.IsTruncated, Equals, false)
|
|
|
|
c.Assert(objectsMetadata[0].Object, Equals, "obj1")
|
|
|
|
c.Assert(objectsMetadata[1].Object, Equals, "obj2")
|
2015-04-07 03:01:19 -04:00
|
|
|
|
|
|
|
three := ioutil.NopCloser(bytes.NewReader([]byte("three")))
|
2015-07-09 17:42:04 -04:00
|
|
|
_, err = dd.CreateObject("foo5", "obj3", "", int64(len("three")), three, nil, nil)
|
2015-04-07 03:01:19 -04:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
2015-07-01 21:09:44 -04:00
|
|
|
var buffer bytes.Buffer
|
2015-07-02 01:36:33 -04:00
|
|
|
size, err = dd.GetObject(&buffer, "foo5", "obj3")
|
2015-04-07 03:01:19 -04:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
c.Assert(size, Equals, int64(len([]byte("three"))))
|
2015-07-01 21:09:44 -04:00
|
|
|
c.Assert(buffer.Bytes(), DeepEquals, []byte("three"))
|
2015-04-07 03:01:19 -04:00
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// test list objects with maxkeys
|
2015-07-01 21:09:44 -04:00
|
|
|
resources.Prefix = "o"
|
|
|
|
resources.Delimiter = ""
|
|
|
|
resources.Maxkeys = 2
|
2015-07-09 22:34:07 -04:00
|
|
|
objectsMetadata, resources, err = dd.ListObjects("foo5", resources, nil)
|
2015-04-07 03:01:19 -04:00
|
|
|
c.Assert(err, IsNil)
|
2015-07-01 21:09:44 -04:00
|
|
|
c.Assert(resources.IsTruncated, Equals, true)
|
|
|
|
c.Assert(len(objectsMetadata), Equals, 2)
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|