minio/pkg/storage/donut/donut_test.go

279 lines
8.0 KiB
Go
Raw Normal View History

/*
* Minimalist 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.
*/
package donut
import (
"bytes"
"crypto/md5"
2015-07-01 21:09:44 -04:00
"encoding/base64"
"encoding/hex"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"testing"
2015-07-01 21:09:44 -04:00
"time"
2015-05-11 19:23:10 -04:00
. "github.com/minio/check"
)
func Test(t *testing.T) { TestingT(t) }
2015-07-01 21:09:44 -04:00
type MySuite struct {
root string
}
var _ = Suite(&MySuite{})
// 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++ {
diskPath := filepath.Join(p, strconv.Itoa(i))
if _, err := os.Stat(diskPath); err != nil {
if os.IsNotExist(err) {
os.MkdirAll(diskPath, 0700)
}
}
nodes["localhost"][i] = diskPath
}
return nodes
}
2015-07-01 21:09:44 -04:00
var d Cache
func (s *MySuite) SetUpSuite(c *C) {
root, err := ioutil.TempDir(os.TempDir(), "donut-")
c.Assert(err, IsNil)
2015-07-01 21:09:44 -04:00
s.root = root
d = NewCache(100000, time.Duration(1*time.Hour), "test", createTestNodeDiskMap(root))
buckets, err := d.ListBuckets()
c.Assert(err, IsNil)
2015-07-01 21:09:44 -04:00
c.Assert(len(buckets), Equals, 0)
}
2015-07-01 21:09:44 -04:00
func (s *MySuite) TearDownSuite(c *C) {
os.RemoveAll(s.root)
}
// test make bucket without name
func (s *MySuite) TestBucketWithoutNameFails(c *C) {
// fail to create new bucket without a name
2015-07-01 21:09:44 -04:00
err := d.MakeBucket("", "private")
c.Assert(err, Not(IsNil))
2015-07-01 21:09:44 -04:00
err = d.MakeBucket(" ", "private")
c.Assert(err, Not(IsNil))
}
// test empty bucket
func (s *MySuite) TestEmptyBucket(c *C) {
2015-07-01 21:09:44 -04:00
c.Assert(d.MakeBucket("foo1", "private"), IsNil)
// check if bucket is empty
2015-07-01 21:09:44 -04:00
var resources BucketResourcesMetadata
resources.Maxkeys = 1
objectsMetadata, resources, err := d.ListObjects("foo1", resources)
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)
}
// test bucket list
func (s *MySuite) TestMakeBucketAndList(c *C) {
// create bucket
2015-07-01 21:09:44 -04:00
err := d.MakeBucket("foo2", "private")
c.Assert(err, IsNil)
// check bucket exists
2015-07-01 21:09:44 -04:00
buckets, err := d.ListBuckets()
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"))
}
// test re-create bucket
func (s *MySuite) TestMakeBucketWithSameNameFails(c *C) {
2015-07-01 21:09:44 -04:00
err := d.MakeBucket("foo3", "private")
c.Assert(err, IsNil)
2015-07-01 21:09:44 -04:00
err = d.MakeBucket("foo3", "private")
c.Assert(err, Not(IsNil))
}
// test make multiple buckets
func (s *MySuite) TestCreateMultipleBucketsAndList(c *C) {
// add a second bucket
2015-07-01 21:09:44 -04:00
err := d.MakeBucket("foo4", "private")
c.Assert(err, IsNil)
2015-07-01 21:09:44 -04:00
err = d.MakeBucket("bar1", "private")
c.Assert(err, IsNil)
2015-07-01 21:09:44 -04:00
buckets, err := d.ListBuckets()
c.Assert(err, IsNil)
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-07-01 21:09:44 -04:00
err = d.MakeBucket("foobar1", "private")
c.Assert(err, IsNil)
2015-07-01 21:09:44 -04:00
buckets, err = d.ListBuckets()
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")
}
// test object create without bucket
func (s *MySuite) TestNewObjectFailsWithoutBucket(c *C) {
2015-07-01 21:09:44 -04:00
_, err := d.CreateObject("unknown", "obj", "", "", 0, nil)
c.Assert(err, Not(IsNil))
}
// test create object metadata
func (s *MySuite) TestNewObjectMetadata(c *C) {
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))
reader := ioutil.NopCloser(bytes.NewReader([]byte(data)))
2015-07-01 21:09:44 -04:00
err := d.MakeBucket("foo6", "private")
c.Assert(err, IsNil)
2015-07-01 21:09:44 -04:00
objectMetadata, err := d.CreateObject("foo6", "obj", "application/json", expectedMd5Sum, int64(len(data)), reader)
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")
}
// test create object fails without name
func (s *MySuite) TestNewObjectFailsWithEmptyName(c *C) {
2015-07-01 21:09:44 -04:00
_, err := d.CreateObject("foo", "", "", "", 0, nil)
c.Assert(err, Not(IsNil))
}
// test create object
func (s *MySuite) TestNewObjectCanBeWritten(c *C) {
2015-07-01 21:09:44 -04:00
err := d.MakeBucket("foo", "private")
c.Assert(err, IsNil)
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))
reader := ioutil.NopCloser(bytes.NewReader([]byte(data)))
2015-07-01 21:09:44 -04:00
actualMetadata, err := d.CreateObject("foo", "obj", "application/octet-stream", expectedMd5Sum, int64(len(data)), reader)
c.Assert(err, IsNil)
2015-07-01 21:09:44 -04:00
c.Assert(actualMetadata.MD5Sum, Equals, hex.EncodeToString(hasher.Sum(nil)))
2015-07-01 21:09:44 -04:00
var buffer bytes.Buffer
size, err := d.GetObject(&buffer, "foo", "obj")
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-07-01 21:09:44 -04:00
actualMetadata, err = d.GetObjectMetadata("foo", "obj")
c.Assert(err, IsNil)
2015-07-01 21:09:44 -04:00
c.Assert(hex.EncodeToString(hasher.Sum(nil)), Equals, actualMetadata.MD5Sum)
c.Assert(int64(len(data)), Equals, actualMetadata.Size)
}
// test list objects
func (s *MySuite) TestMultipleNewObjects(c *C) {
2015-07-01 21:09:44 -04:00
c.Assert(d.MakeBucket("foo5", "private"), IsNil)
one := ioutil.NopCloser(bytes.NewReader([]byte("one")))
2015-07-01 21:09:44 -04:00
_, err := d.CreateObject("foo5", "obj1", "", "", int64(len("one")), one)
c.Assert(err, IsNil)
two := ioutil.NopCloser(bytes.NewReader([]byte("two")))
2015-07-01 21:09:44 -04:00
_, err = d.CreateObject("foo5", "obj2", "", "", int64(len("two")), two)
c.Assert(err, IsNil)
2015-07-01 21:09:44 -04:00
var buffer1 bytes.Buffer
size, err := d.GetObject(&buffer1, "foo5", "obj1")
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-07-01 21:09:44 -04:00
var buffer2 bytes.Buffer
size, err = d.GetObject(&buffer2, "foo5", "obj2")
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"))
/// 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
objectsMetadata, resources, err := d.ListObjects("foo5", resources)
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")
// test list objects with only delimiter
2015-07-01 21:09:44 -04:00
resources.Prefix = ""
resources.Delimiter = "1"
resources.Maxkeys = 10
objectsMetadata, resources, err = d.ListObjects("foo5", resources)
c.Assert(err, IsNil)
2015-07-01 21:09:44 -04:00
c.Assert(objectsMetadata[0].Object, Equals, "obj1")
c.Assert(resources.IsTruncated, Equals, false)
c.Assert(resources.CommonPrefixes[0], Equals, "obj1")
// test list objects with only prefix
2015-07-01 21:09:44 -04:00
resources.Prefix = "o"
resources.Delimiter = ""
resources.Maxkeys = 10
objectsMetadata, resources, err = d.ListObjects("foo5", resources)
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")
three := ioutil.NopCloser(bytes.NewReader([]byte("three")))
2015-07-01 21:09:44 -04:00
_, err = d.CreateObject("foo5", "obj3", "", "", int64(len("three")), three)
c.Assert(err, IsNil)
2015-07-01 21:09:44 -04:00
var buffer bytes.Buffer
size, err = d.GetObject(&buffer, "foo5", "obj3")
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"))
// test list objects with maxkeys
2015-07-01 21:09:44 -04:00
resources.Prefix = "o"
resources.Delimiter = ""
resources.Maxkeys = 2
objectsMetadata, resources, err = d.ListObjects("foo5", resources)
c.Assert(err, IsNil)
2015-07-01 21:09:44 -04:00
c.Assert(resources.IsTruncated, Equals, true)
c.Assert(len(objectsMetadata), Equals, 2)
}