2015-03-22 03:39:39 -04:00
|
|
|
package donut
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2015-03-23 23:40:21 -04:00
|
|
|
"testing"
|
2015-03-23 23:06:15 -04:00
|
|
|
"time"
|
2015-03-23 23:40:21 -04:00
|
|
|
|
2015-04-04 16:37:30 -04:00
|
|
|
. "github.com/minio-io/check"
|
2015-03-22 03:39:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test(t *testing.T) { TestingT(t) }
|
|
|
|
|
|
|
|
type MySuite struct{}
|
|
|
|
|
|
|
|
var _ = Suite(&MySuite{})
|
|
|
|
|
|
|
|
func (s *MySuite) TestEmptyBucket(c *C) {
|
|
|
|
root, err := ioutil.TempDir(os.TempDir(), "donut-")
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
defer os.RemoveAll(root)
|
2015-03-25 00:04:08 -04:00
|
|
|
donut, err := NewDonut(root)
|
2015-03-24 22:58:03 -04:00
|
|
|
c.Assert(err, IsNil)
|
2015-03-22 03:39:39 -04:00
|
|
|
|
|
|
|
// check buckets are empty
|
|
|
|
buckets, err := donut.ListBuckets()
|
|
|
|
c.Assert(err, IsNil)
|
2015-03-22 05:53:21 -04:00
|
|
|
c.Assert(buckets, IsNil)
|
2015-03-22 03:39:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MySuite) TestBucketWithoutNameFails(c *C) {
|
|
|
|
root, err := ioutil.TempDir(os.TempDir(), "donut-")
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
defer os.RemoveAll(root)
|
2015-03-25 00:04:08 -04:00
|
|
|
donut, err := NewDonut(root)
|
2015-03-24 22:58:03 -04:00
|
|
|
c.Assert(err, IsNil)
|
2015-03-22 03:39:39 -04:00
|
|
|
// fail to create new bucket without a name
|
|
|
|
err = donut.CreateBucket("")
|
|
|
|
c.Assert(err, Not(IsNil))
|
|
|
|
|
|
|
|
err = donut.CreateBucket(" ")
|
|
|
|
c.Assert(err, Not(IsNil))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MySuite) TestCreateBucketAndList(c *C) {
|
|
|
|
root, err := ioutil.TempDir(os.TempDir(), "donut-")
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
defer os.RemoveAll(root)
|
2015-03-25 00:04:08 -04:00
|
|
|
donut, err := NewDonut(root)
|
2015-03-24 22:58:03 -04:00
|
|
|
c.Assert(err, IsNil)
|
2015-03-22 03:39:39 -04:00
|
|
|
// create bucket
|
|
|
|
err = donut.CreateBucket("foo")
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
// check bucket exists
|
|
|
|
buckets, err := donut.ListBuckets()
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
c.Assert(buckets, DeepEquals, []string{"foo"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MySuite) TestCreateBucketWithSameNameFails(c *C) {
|
|
|
|
root, err := ioutil.TempDir(os.TempDir(), "donut-")
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
defer os.RemoveAll(root)
|
2015-03-25 00:04:08 -04:00
|
|
|
donut, err := NewDonut(root)
|
2015-03-24 22:58:03 -04:00
|
|
|
c.Assert(err, IsNil)
|
2015-03-22 03:39:39 -04:00
|
|
|
err = donut.CreateBucket("foo")
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
err = donut.CreateBucket("foo")
|
|
|
|
c.Assert(err, Not(IsNil))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MySuite) TestCreateMultipleBucketsAndList(c *C) {
|
|
|
|
root, err := ioutil.TempDir(os.TempDir(), "donut-")
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
defer os.RemoveAll(root)
|
2015-03-25 00:04:08 -04:00
|
|
|
donut, err := NewDonut(root)
|
2015-03-24 22:58:03 -04:00
|
|
|
c.Assert(err, IsNil)
|
2015-03-22 03:39:39 -04:00
|
|
|
// add a second bucket
|
|
|
|
err = donut.CreateBucket("foo")
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
err = donut.CreateBucket("bar")
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
buckets, err := donut.ListBuckets()
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
c.Assert(buckets, DeepEquals, []string{"bar", "foo"})
|
|
|
|
|
|
|
|
err = donut.CreateBucket("foobar")
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
buckets, err = donut.ListBuckets()
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
c.Assert(buckets, DeepEquals, []string{"bar", "foo", "foobar"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MySuite) TestNewObjectFailsWithoutBucket(c *C) {
|
|
|
|
root, err := ioutil.TempDir(os.TempDir(), "donut-")
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
defer os.RemoveAll(root)
|
2015-03-25 00:04:08 -04:00
|
|
|
donut, err := NewDonut(root)
|
2015-03-24 22:58:03 -04:00
|
|
|
c.Assert(err, IsNil)
|
2015-03-22 03:39:39 -04:00
|
|
|
|
|
|
|
writer, err := donut.GetObjectWriter("foo", "obj")
|
|
|
|
c.Assert(err, Not(IsNil))
|
|
|
|
c.Assert(writer, IsNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MySuite) TestNewObjectFailsWithEmptyName(c *C) {
|
|
|
|
root, err := ioutil.TempDir(os.TempDir(), "donut-")
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
defer os.RemoveAll(root)
|
2015-03-25 00:04:08 -04:00
|
|
|
donut, err := NewDonut(root)
|
2015-03-24 22:58:03 -04:00
|
|
|
c.Assert(err, IsNil)
|
2015-03-22 03:39:39 -04:00
|
|
|
|
|
|
|
writer, err := donut.GetObjectWriter("foo", "")
|
|
|
|
c.Assert(err, Not(IsNil))
|
|
|
|
c.Assert(writer, IsNil)
|
|
|
|
|
|
|
|
writer, err = donut.GetObjectWriter("foo", " ")
|
|
|
|
c.Assert(err, Not(IsNil))
|
|
|
|
c.Assert(writer, IsNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MySuite) TestNewObjectCanBeWritten(c *C) {
|
|
|
|
root, err := ioutil.TempDir(os.TempDir(), "donut-")
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
defer os.RemoveAll(root)
|
2015-03-25 00:04:08 -04:00
|
|
|
donut, err := NewDonut(root)
|
2015-03-24 22:58:03 -04:00
|
|
|
c.Assert(err, IsNil)
|
2015-03-22 03:39:39 -04:00
|
|
|
|
|
|
|
err = donut.CreateBucket("foo")
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
writer, err := donut.GetObjectWriter("foo", "obj")
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
data := "Hello World"
|
|
|
|
length, err := writer.Write([]byte(data))
|
|
|
|
c.Assert(length, Equals, len(data))
|
|
|
|
|
|
|
|
expectedMetadata := map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"created": "one",
|
|
|
|
"hello": "world",
|
|
|
|
}
|
|
|
|
|
|
|
|
err = writer.SetMetadata(expectedMetadata)
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
err = writer.Close()
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
actualWriterMetadata, err := writer.GetMetadata()
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
c.Assert(actualWriterMetadata, DeepEquals, expectedMetadata)
|
|
|
|
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
2015-03-23 23:40:21 -04:00
|
|
|
reader, err := donut.GetObjectReader("foo", "obj")
|
2015-03-22 03:39:39 -04:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
var actualData bytes.Buffer
|
|
|
|
_, err = io.Copy(&actualData, reader)
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
c.Assert(actualData.Bytes(), DeepEquals, []byte(data))
|
2015-03-22 15:36:51 -04:00
|
|
|
|
|
|
|
actualMetadata, err := donut.GetObjectMetadata("foo", "obj")
|
|
|
|
c.Assert(err, IsNil)
|
2015-03-23 23:06:15 -04:00
|
|
|
expectedMetadata["sys.md5"] = "b10a8db164e0754105b7a99be72e3fe5"
|
|
|
|
expectedMetadata["sys.size"] = "11"
|
|
|
|
_, err = time.Parse(time.RFC3339Nano, actualMetadata["sys.created"])
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
expectedMetadata["sys.created"] = actualMetadata["sys.created"]
|
2015-03-22 15:36:51 -04:00
|
|
|
c.Assert(actualMetadata, DeepEquals, expectedMetadata)
|
2015-03-22 03:39:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MySuite) TestMultipleNewObjects(c *C) {
|
|
|
|
root, err := ioutil.TempDir(os.TempDir(), "donut-")
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
defer os.RemoveAll(root)
|
2015-03-25 00:04:08 -04:00
|
|
|
donut, err := NewDonut(root)
|
2015-03-24 22:58:03 -04:00
|
|
|
c.Assert(err, IsNil)
|
2015-03-22 03:39:39 -04:00
|
|
|
|
|
|
|
c.Assert(donut.CreateBucket("foo"), IsNil)
|
|
|
|
writer, err := donut.GetObjectWriter("foo", "obj1")
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
writer.Write([]byte("one"))
|
|
|
|
writer.Close()
|
|
|
|
|
|
|
|
writer, err = donut.GetObjectWriter("foo", "obj2")
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
writer.Write([]byte("two"))
|
|
|
|
writer.Close()
|
|
|
|
|
|
|
|
// c.Skip("not complete")
|
|
|
|
|
2015-03-23 23:40:21 -04:00
|
|
|
reader, err := donut.GetObjectReader("foo", "obj1")
|
2015-03-22 03:39:39 -04:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
var readerBuffer1 bytes.Buffer
|
|
|
|
_, err = io.Copy(&readerBuffer1, reader)
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
// c.Skip("Not Implemented")
|
|
|
|
c.Assert(readerBuffer1.Bytes(), DeepEquals, []byte("one"))
|
|
|
|
|
2015-03-23 23:40:21 -04:00
|
|
|
reader, err = donut.GetObjectReader("foo", "obj2")
|
2015-03-22 03:39:39 -04:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
var readerBuffer2 bytes.Buffer
|
|
|
|
_, err = io.Copy(&readerBuffer2, reader)
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
c.Assert(readerBuffer2.Bytes(), DeepEquals, []byte("two"))
|
2015-03-22 18:33:52 -04:00
|
|
|
|
|
|
|
// test list objects
|
|
|
|
listObjects, err := donut.ListObjects("foo")
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
c.Assert(listObjects, DeepEquals, []string{"obj1", "obj2"})
|
2015-03-22 03:39:39 -04:00
|
|
|
}
|
2015-03-23 14:56:16 -04:00
|
|
|
|
|
|
|
func (s *MySuite) TestSysPrefixShouldFail(c *C) {
|
|
|
|
root, err := ioutil.TempDir(os.TempDir(), "donut-")
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
defer os.RemoveAll(root)
|
2015-03-25 00:04:08 -04:00
|
|
|
donut, err := NewDonut(root)
|
2015-03-24 22:58:03 -04:00
|
|
|
c.Assert(err, IsNil)
|
2015-03-23 14:56:16 -04:00
|
|
|
|
|
|
|
c.Assert(donut.CreateBucket("foo"), IsNil)
|
|
|
|
writer, err := donut.GetObjectWriter("foo", "obj1")
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
writer.Write([]byte("one"))
|
|
|
|
metadata := make(map[string]string)
|
|
|
|
metadata["foo"] = "bar"
|
|
|
|
metadata["sys.hello"] = "world"
|
|
|
|
err = writer.SetMetadata(metadata)
|
|
|
|
c.Assert(err, Not(IsNil))
|
|
|
|
writer.Close()
|
|
|
|
}
|