2014-10-21 22:02:20 +00:00
|
|
|
package testsuites
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
|
|
|
"math/rand"
|
2014-11-20 22:50:51 +00:00
|
|
|
"os"
|
2014-10-21 22:02:20 +00:00
|
|
|
"path"
|
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker-registry/storagedriver"
|
|
|
|
"github.com/docker/docker-registry/storagedriver/ipc"
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
"gopkg.in/check.v1"
|
2014-10-21 22:02:20 +00:00
|
|
|
)
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// Test hooks up gocheck into the "go test" runner.
|
|
|
|
func Test(t *testing.T) { check.TestingT(t) }
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-11-20 22:50:51 +00:00
|
|
|
// RegisterInProcessSuite registers an in-process storage driver test suite with
|
|
|
|
// the go test runner.
|
2014-10-27 20:24:07 +00:00
|
|
|
func RegisterInProcessSuite(driverConstructor DriverConstructor, skipCheck SkipCheck) {
|
2014-11-17 23:44:07 +00:00
|
|
|
check.Suite(&DriverSuite{
|
2014-10-21 22:02:20 +00:00
|
|
|
Constructor: driverConstructor,
|
2014-10-27 20:24:07 +00:00
|
|
|
SkipCheck: skipCheck,
|
2014-10-21 22:02:20 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-11-20 22:50:51 +00:00
|
|
|
// RegisterIPCSuite registers a storage driver test suite which runs the named
|
|
|
|
// driver as a child process with the given parameters.
|
2014-10-27 20:24:07 +00:00
|
|
|
func RegisterIPCSuite(driverName string, ipcParams map[string]string, skipCheck SkipCheck) {
|
2014-10-21 22:02:20 +00:00
|
|
|
suite := &DriverSuite{
|
|
|
|
Constructor: func() (storagedriver.StorageDriver, error) {
|
|
|
|
d, err := ipc.NewDriverClient(driverName, ipcParams)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = d.Start()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return d, nil
|
|
|
|
},
|
2014-10-27 20:24:07 +00:00
|
|
|
SkipCheck: skipCheck,
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
suite.Teardown = func() error {
|
2014-10-27 20:24:07 +00:00
|
|
|
if suite.StorageDriver == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-21 22:02:20 +00:00
|
|
|
driverClient := suite.StorageDriver.(*ipc.StorageDriverClient)
|
|
|
|
return driverClient.Stop()
|
|
|
|
}
|
2014-11-17 23:44:07 +00:00
|
|
|
check.Suite(suite)
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-20 22:50:51 +00:00
|
|
|
// SkipCheck is a function used to determine if a test suite should be skipped.
|
|
|
|
// If a SkipCheck returns a non-empty skip reason, the suite is skipped with
|
|
|
|
// the given reason.
|
2014-10-27 20:24:07 +00:00
|
|
|
type SkipCheck func() (reason string)
|
|
|
|
|
2014-11-20 22:50:51 +00:00
|
|
|
// NeverSkip is a default SkipCheck which never skips the suite.
|
2014-10-29 19:14:19 +00:00
|
|
|
var NeverSkip SkipCheck = func() string { return "" }
|
2014-10-27 20:24:07 +00:00
|
|
|
|
2014-11-20 22:50:51 +00:00
|
|
|
// DriverConstructor is a function which returns a new
|
|
|
|
// storagedriver.StorageDriver.
|
2014-10-21 22:02:20 +00:00
|
|
|
type DriverConstructor func() (storagedriver.StorageDriver, error)
|
2014-10-29 19:14:19 +00:00
|
|
|
|
2014-11-20 22:50:51 +00:00
|
|
|
// DriverTeardown is a function which cleans up a suite's
|
|
|
|
// storagedriver.StorageDriver.
|
2014-10-21 22:02:20 +00:00
|
|
|
type DriverTeardown func() error
|
|
|
|
|
2014-11-20 22:50:51 +00:00
|
|
|
// DriverSuite is a gocheck test suite designed to test a
|
|
|
|
// storagedriver.StorageDriver.
|
|
|
|
// The intended way to create a DriverSuite is with RegisterInProcessSuite or
|
|
|
|
// RegisterIPCSuite.
|
2014-10-21 22:02:20 +00:00
|
|
|
type DriverSuite struct {
|
|
|
|
Constructor DriverConstructor
|
|
|
|
Teardown DriverTeardown
|
2014-10-27 20:24:07 +00:00
|
|
|
SkipCheck
|
2014-10-21 22:02:20 +00:00
|
|
|
storagedriver.StorageDriver
|
|
|
|
}
|
|
|
|
|
2014-11-20 22:50:51 +00:00
|
|
|
// SetUpSuite sets up the gocheck test suite.
|
2014-11-17 23:44:07 +00:00
|
|
|
func (suite *DriverSuite) SetUpSuite(c *check.C) {
|
2014-10-27 20:24:07 +00:00
|
|
|
if reason := suite.SkipCheck(); reason != "" {
|
|
|
|
c.Skip(reason)
|
|
|
|
}
|
2014-10-21 22:02:20 +00:00
|
|
|
d, err := suite.Constructor()
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-10-21 22:02:20 +00:00
|
|
|
suite.StorageDriver = d
|
|
|
|
}
|
|
|
|
|
2014-11-20 22:50:51 +00:00
|
|
|
// TearDownSuite tears down the gocheck test suite.
|
2014-11-17 23:44:07 +00:00
|
|
|
func (suite *DriverSuite) TearDownSuite(c *check.C) {
|
2014-10-21 22:02:20 +00:00
|
|
|
if suite.Teardown != nil {
|
|
|
|
err := suite.Teardown()
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-20 22:50:51 +00:00
|
|
|
// TestWriteRead1 tests a simple write-read workflow.
|
2014-11-17 23:44:07 +00:00
|
|
|
func (suite *DriverSuite) TestWriteRead1(c *check.C) {
|
2014-10-21 22:02:20 +00:00
|
|
|
filename := randomString(32)
|
|
|
|
contents := []byte("a")
|
2014-11-20 22:50:51 +00:00
|
|
|
suite.writeReadCompare(c, filename, contents)
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-20 22:50:51 +00:00
|
|
|
// TestWriteRead2 tests a simple write-read workflow with unicode data.
|
2014-11-17 23:44:07 +00:00
|
|
|
func (suite *DriverSuite) TestWriteRead2(c *check.C) {
|
2014-10-21 22:02:20 +00:00
|
|
|
filename := randomString(32)
|
|
|
|
contents := []byte("\xc3\x9f")
|
2014-11-20 22:50:51 +00:00
|
|
|
suite.writeReadCompare(c, filename, contents)
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-20 22:50:51 +00:00
|
|
|
// TestWriteRead3 tests a simple write-read workflow with a small string.
|
2014-11-17 23:44:07 +00:00
|
|
|
func (suite *DriverSuite) TestWriteRead3(c *check.C) {
|
2014-10-21 22:02:20 +00:00
|
|
|
filename := randomString(32)
|
|
|
|
contents := []byte(randomString(32))
|
2014-11-20 22:50:51 +00:00
|
|
|
suite.writeReadCompare(c, filename, contents)
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-20 22:50:51 +00:00
|
|
|
// TestWriteRead4 tests a simple write-read workflow with 1MB of data.
|
2014-11-17 23:44:07 +00:00
|
|
|
func (suite *DriverSuite) TestWriteRead4(c *check.C) {
|
2014-10-21 22:02:20 +00:00
|
|
|
filename := randomString(32)
|
|
|
|
contents := []byte(randomString(1024 * 1024))
|
2014-11-20 22:50:51 +00:00
|
|
|
suite.writeReadCompare(c, filename, contents)
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-20 22:50:51 +00:00
|
|
|
// TestReadNonexistent tests reading content from an empty path.
|
2014-11-17 23:44:07 +00:00
|
|
|
func (suite *DriverSuite) TestReadNonexistent(c *check.C) {
|
2014-10-21 22:02:20 +00:00
|
|
|
filename := randomString(32)
|
|
|
|
_, err := suite.StorageDriver.GetContent(filename)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
2014-11-19 01:41:48 +00:00
|
|
|
c.Assert(err, check.FitsTypeOf, storagedriver.PathNotFoundError{})
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-20 22:50:51 +00:00
|
|
|
// TestWriteReadStreams1 tests a simple write-read streaming workflow.
|
2014-11-17 23:44:07 +00:00
|
|
|
func (suite *DriverSuite) TestWriteReadStreams1(c *check.C) {
|
2014-10-21 22:02:20 +00:00
|
|
|
filename := randomString(32)
|
|
|
|
contents := []byte("a")
|
2014-11-20 22:50:51 +00:00
|
|
|
suite.writeReadCompareStreams(c, filename, contents)
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// TestWriteReadStreams2 tests a simple write-read streaming workflow with
|
2014-11-20 22:50:51 +00:00
|
|
|
// unicode data.
|
2014-11-17 23:44:07 +00:00
|
|
|
func (suite *DriverSuite) TestWriteReadStreams2(c *check.C) {
|
2014-10-21 22:02:20 +00:00
|
|
|
filename := randomString(32)
|
|
|
|
contents := []byte("\xc3\x9f")
|
2014-11-20 22:50:51 +00:00
|
|
|
suite.writeReadCompareStreams(c, filename, contents)
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// TestWriteReadStreams3 tests a simple write-read streaming workflow with a
|
2014-11-20 22:50:51 +00:00
|
|
|
// small amount of data.
|
2014-11-17 23:44:07 +00:00
|
|
|
func (suite *DriverSuite) TestWriteReadStreams3(c *check.C) {
|
2014-10-21 22:02:20 +00:00
|
|
|
filename := randomString(32)
|
|
|
|
contents := []byte(randomString(32))
|
2014-11-20 22:50:51 +00:00
|
|
|
suite.writeReadCompareStreams(c, filename, contents)
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// TestWriteReadStreams4 tests a simple write-read streaming workflow with 1MB
|
2014-11-20 22:50:51 +00:00
|
|
|
// of data.
|
2014-11-17 23:44:07 +00:00
|
|
|
func (suite *DriverSuite) TestWriteReadStreams4(c *check.C) {
|
2014-10-21 22:02:20 +00:00
|
|
|
filename := randomString(32)
|
|
|
|
contents := []byte(randomString(1024 * 1024))
|
2014-11-20 22:50:51 +00:00
|
|
|
suite.writeReadCompareStreams(c, filename, contents)
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// TestContinueStreamAppend tests that a stream write can be appended to without
|
2014-11-20 22:50:51 +00:00
|
|
|
// corrupting the data.
|
2014-11-17 23:44:07 +00:00
|
|
|
func (suite *DriverSuite) TestContinueStreamAppend(c *check.C) {
|
2014-10-21 22:02:20 +00:00
|
|
|
filename := randomString(32)
|
2014-10-24 23:36:17 +00:00
|
|
|
defer suite.StorageDriver.Delete(filename)
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-10-27 20:24:07 +00:00
|
|
|
chunkSize := uint64(10 * 1024 * 1024)
|
2014-10-21 22:02:20 +00:00
|
|
|
|
|
|
|
contentsChunk1 := []byte(randomString(chunkSize))
|
|
|
|
contentsChunk2 := []byte(randomString(chunkSize))
|
|
|
|
contentsChunk3 := []byte(randomString(chunkSize))
|
|
|
|
|
|
|
|
fullContents := append(append(contentsChunk1, contentsChunk2...), contentsChunk3...)
|
|
|
|
|
|
|
|
err := suite.StorageDriver.WriteStream(filename, 0, 3*chunkSize, ioutil.NopCloser(bytes.NewReader(contentsChunk1)))
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-11-07 20:58:48 +00:00
|
|
|
offset, err := suite.StorageDriver.CurrentSize(filename)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-10-21 22:02:20 +00:00
|
|
|
if offset > chunkSize {
|
|
|
|
c.Fatalf("Offset too large, %d > %d", offset, chunkSize)
|
|
|
|
}
|
|
|
|
err = suite.StorageDriver.WriteStream(filename, offset, 3*chunkSize, ioutil.NopCloser(bytes.NewReader(fullContents[offset:2*chunkSize])))
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-11-07 20:58:48 +00:00
|
|
|
offset, err = suite.StorageDriver.CurrentSize(filename)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-10-21 22:02:20 +00:00
|
|
|
if offset > 2*chunkSize {
|
|
|
|
c.Fatalf("Offset too large, %d > %d", offset, 2*chunkSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = suite.StorageDriver.WriteStream(filename, offset, 3*chunkSize, ioutil.NopCloser(bytes.NewReader(fullContents[offset:])))
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-10-21 22:02:20 +00:00
|
|
|
|
|
|
|
received, err := suite.StorageDriver.GetContent(filename)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(received, check.DeepEquals, fullContents)
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// TestReadStreamWithOffset tests that the appropriate data is streamed when
|
2014-11-20 22:50:51 +00:00
|
|
|
// reading with a given offset.
|
2014-11-17 23:44:07 +00:00
|
|
|
func (suite *DriverSuite) TestReadStreamWithOffset(c *check.C) {
|
2014-10-21 22:02:20 +00:00
|
|
|
filename := randomString(32)
|
2014-10-24 23:36:17 +00:00
|
|
|
defer suite.StorageDriver.Delete(filename)
|
2014-10-21 22:02:20 +00:00
|
|
|
|
|
|
|
chunkSize := uint64(32)
|
|
|
|
|
|
|
|
contentsChunk1 := []byte(randomString(chunkSize))
|
|
|
|
contentsChunk2 := []byte(randomString(chunkSize))
|
|
|
|
contentsChunk3 := []byte(randomString(chunkSize))
|
|
|
|
|
|
|
|
err := suite.StorageDriver.PutContent(filename, append(append(contentsChunk1, contentsChunk2...), contentsChunk3...))
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-10-21 22:02:20 +00:00
|
|
|
|
|
|
|
reader, err := suite.StorageDriver.ReadStream(filename, 0)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-10-21 22:02:20 +00:00
|
|
|
defer reader.Close()
|
|
|
|
|
|
|
|
readContents, err := ioutil.ReadAll(reader)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(readContents, check.DeepEquals, append(append(contentsChunk1, contentsChunk2...), contentsChunk3...))
|
2014-10-21 22:02:20 +00:00
|
|
|
|
|
|
|
reader, err = suite.StorageDriver.ReadStream(filename, chunkSize)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-10-21 22:02:20 +00:00
|
|
|
defer reader.Close()
|
|
|
|
|
|
|
|
readContents, err = ioutil.ReadAll(reader)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(readContents, check.DeepEquals, append(contentsChunk2, contentsChunk3...))
|
2014-10-21 22:02:20 +00:00
|
|
|
|
|
|
|
reader, err = suite.StorageDriver.ReadStream(filename, chunkSize*2)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-10-21 22:02:20 +00:00
|
|
|
defer reader.Close()
|
|
|
|
|
|
|
|
readContents, err = ioutil.ReadAll(reader)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(readContents, check.DeepEquals, contentsChunk3)
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// TestReadNonexistentStream tests that reading a stream for a nonexistent path
|
2014-11-20 22:50:51 +00:00
|
|
|
// fails.
|
2014-11-17 23:44:07 +00:00
|
|
|
func (suite *DriverSuite) TestReadNonexistentStream(c *check.C) {
|
2014-10-21 22:02:20 +00:00
|
|
|
filename := randomString(32)
|
|
|
|
_, err := suite.StorageDriver.ReadStream(filename, 0)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
2014-11-19 01:41:48 +00:00
|
|
|
c.Assert(err, check.FitsTypeOf, storagedriver.PathNotFoundError{})
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-20 22:50:51 +00:00
|
|
|
// TestList checks the returned list of keys after populating a directory tree.
|
2014-11-17 23:44:07 +00:00
|
|
|
func (suite *DriverSuite) TestList(c *check.C) {
|
2014-11-20 22:11:49 +00:00
|
|
|
rootDirectory := "/" + randomString(uint64(8+rand.Intn(8)))
|
2014-10-24 23:36:17 +00:00
|
|
|
defer suite.StorageDriver.Delete(rootDirectory)
|
|
|
|
|
2014-10-21 22:02:20 +00:00
|
|
|
parentDirectory := rootDirectory + "/" + randomString(uint64(8+rand.Intn(8)))
|
|
|
|
childFiles := make([]string, 50)
|
|
|
|
for i := 0; i < len(childFiles); i++ {
|
|
|
|
childFile := parentDirectory + "/" + randomString(uint64(8+rand.Intn(8)))
|
|
|
|
childFiles[i] = childFile
|
|
|
|
err := suite.StorageDriver.PutContent(childFile, []byte(randomString(32)))
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
sort.Strings(childFiles)
|
|
|
|
|
2014-11-20 22:11:49 +00:00
|
|
|
keys, err := suite.StorageDriver.List("/")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(keys, check.DeepEquals, []string{rootDirectory})
|
|
|
|
|
|
|
|
keys, err = suite.StorageDriver.List(rootDirectory)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(keys, check.DeepEquals, []string{parentDirectory})
|
2014-10-21 22:02:20 +00:00
|
|
|
|
|
|
|
keys, err = suite.StorageDriver.List(parentDirectory)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-10-21 22:02:20 +00:00
|
|
|
|
|
|
|
sort.Strings(keys)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(keys, check.DeepEquals, childFiles)
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// TestMove checks that a moved object no longer exists at the source path and
|
2014-11-20 22:50:51 +00:00
|
|
|
// does exist at the destination.
|
2014-11-17 23:44:07 +00:00
|
|
|
func (suite *DriverSuite) TestMove(c *check.C) {
|
2014-10-21 22:02:20 +00:00
|
|
|
contents := []byte(randomString(32))
|
|
|
|
sourcePath := randomString(32)
|
|
|
|
destPath := randomString(32)
|
|
|
|
|
2014-10-24 23:36:17 +00:00
|
|
|
defer suite.StorageDriver.Delete(sourcePath)
|
|
|
|
defer suite.StorageDriver.Delete(destPath)
|
|
|
|
|
2014-10-21 22:02:20 +00:00
|
|
|
err := suite.StorageDriver.PutContent(sourcePath, contents)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-10-21 22:02:20 +00:00
|
|
|
|
|
|
|
err = suite.StorageDriver.Move(sourcePath, destPath)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-10-21 22:02:20 +00:00
|
|
|
|
|
|
|
received, err := suite.StorageDriver.GetContent(destPath)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(received, check.DeepEquals, contents)
|
2014-10-21 22:02:20 +00:00
|
|
|
|
|
|
|
_, err = suite.StorageDriver.GetContent(sourcePath)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
2014-11-19 01:41:48 +00:00
|
|
|
c.Assert(err, check.FitsTypeOf, storagedriver.PathNotFoundError{})
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// TestMoveNonexistent checks that moving a nonexistent key fails
|
|
|
|
func (suite *DriverSuite) TestMoveNonexistent(c *check.C) {
|
2014-10-21 22:02:20 +00:00
|
|
|
sourcePath := randomString(32)
|
|
|
|
destPath := randomString(32)
|
|
|
|
|
|
|
|
err := suite.StorageDriver.Move(sourcePath, destPath)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
2014-11-19 01:41:48 +00:00
|
|
|
c.Assert(err, check.FitsTypeOf, storagedriver.PathNotFoundError{})
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// TestDelete checks that the delete operation removes data from the storage
|
|
|
|
// driver
|
|
|
|
func (suite *DriverSuite) TestDelete(c *check.C) {
|
2014-10-21 22:02:20 +00:00
|
|
|
filename := randomString(32)
|
|
|
|
contents := []byte(randomString(32))
|
|
|
|
|
2014-10-24 23:36:17 +00:00
|
|
|
defer suite.StorageDriver.Delete(filename)
|
|
|
|
|
2014-10-21 22:02:20 +00:00
|
|
|
err := suite.StorageDriver.PutContent(filename, contents)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-10-21 22:02:20 +00:00
|
|
|
|
|
|
|
err = suite.StorageDriver.Delete(filename)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-10-21 22:02:20 +00:00
|
|
|
|
|
|
|
_, err = suite.StorageDriver.GetContent(filename)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
2014-11-19 01:41:48 +00:00
|
|
|
c.Assert(err, check.FitsTypeOf, storagedriver.PathNotFoundError{})
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-20 22:50:51 +00:00
|
|
|
// TestDeleteNonexistent checks that removing a nonexistent key fails.
|
2014-11-17 23:44:07 +00:00
|
|
|
func (suite *DriverSuite) TestDeleteNonexistent(c *check.C) {
|
2014-10-21 22:02:20 +00:00
|
|
|
filename := randomString(32)
|
|
|
|
err := suite.StorageDriver.Delete(filename)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
2014-11-19 01:41:48 +00:00
|
|
|
c.Assert(err, check.FitsTypeOf, storagedriver.PathNotFoundError{})
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-20 22:50:51 +00:00
|
|
|
// TestDeleteFolder checks that deleting a folder removes all child elements.
|
2014-11-17 23:44:07 +00:00
|
|
|
func (suite *DriverSuite) TestDeleteFolder(c *check.C) {
|
2014-10-21 22:02:20 +00:00
|
|
|
dirname := randomString(32)
|
|
|
|
filename1 := randomString(32)
|
|
|
|
filename2 := randomString(32)
|
|
|
|
contents := []byte(randomString(32))
|
|
|
|
|
2014-10-24 23:36:17 +00:00
|
|
|
defer suite.StorageDriver.Delete(path.Join(dirname, filename1))
|
|
|
|
defer suite.StorageDriver.Delete(path.Join(dirname, filename2))
|
|
|
|
|
2014-10-21 22:02:20 +00:00
|
|
|
err := suite.StorageDriver.PutContent(path.Join(dirname, filename1), contents)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-10-21 22:02:20 +00:00
|
|
|
|
|
|
|
err = suite.StorageDriver.PutContent(path.Join(dirname, filename2), contents)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-10-21 22:02:20 +00:00
|
|
|
|
|
|
|
err = suite.StorageDriver.Delete(dirname)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-10-21 22:02:20 +00:00
|
|
|
|
|
|
|
_, err = suite.StorageDriver.GetContent(path.Join(dirname, filename1))
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
2014-11-19 01:41:48 +00:00
|
|
|
c.Assert(err, check.FitsTypeOf, storagedriver.PathNotFoundError{})
|
2014-10-21 22:02:20 +00:00
|
|
|
|
|
|
|
_, err = suite.StorageDriver.GetContent(path.Join(dirname, filename2))
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
2014-11-19 01:41:48 +00:00
|
|
|
c.Assert(err, check.FitsTypeOf, storagedriver.PathNotFoundError{})
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-20 22:50:51 +00:00
|
|
|
// TestConcurrentFileStreams checks that multiple *os.File objects can be passed
|
|
|
|
// in to WriteStream concurrently without hanging.
|
|
|
|
// TODO(bbland): fix this test...
|
|
|
|
func (suite *DriverSuite) TestConcurrentFileStreams(c *check.C) {
|
|
|
|
if _, isIPC := suite.StorageDriver.(*ipc.StorageDriverClient); isIPC {
|
|
|
|
c.Skip("Need to fix out-of-process concurrency")
|
|
|
|
}
|
|
|
|
|
|
|
|
doneChan := make(chan struct{})
|
|
|
|
|
|
|
|
testStream := func(size int) {
|
|
|
|
suite.testFileStreams(c, size)
|
|
|
|
doneChan <- struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
go testStream(8 * 1024 * 1024)
|
|
|
|
go testStream(4 * 1024 * 1024)
|
|
|
|
go testStream(2 * 1024 * 1024)
|
|
|
|
go testStream(1024 * 1024)
|
|
|
|
go testStream(1024)
|
|
|
|
go testStream(64)
|
|
|
|
|
|
|
|
for i := 0; i < 6; i++ {
|
|
|
|
<-doneChan
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *DriverSuite) testFileStreams(c *check.C, size int) {
|
|
|
|
tf, err := ioutil.TempFile("", "tf")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
defer os.Remove(tf.Name())
|
|
|
|
|
|
|
|
tfName := path.Base(tf.Name())
|
|
|
|
defer suite.StorageDriver.Delete(tfName)
|
|
|
|
|
|
|
|
contents := []byte(randomString(uint64(size)))
|
|
|
|
|
|
|
|
_, err = tf.Write(contents)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
tf.Sync()
|
|
|
|
tf.Seek(0, os.SEEK_SET)
|
|
|
|
|
|
|
|
err = suite.StorageDriver.WriteStream(tfName, 0, uint64(size), tf)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
reader, err := suite.StorageDriver.ReadStream(tfName, 0)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
defer reader.Close()
|
|
|
|
|
|
|
|
readContents, err := ioutil.ReadAll(reader)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
c.Assert(readContents, check.DeepEquals, contents)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *DriverSuite) writeReadCompare(c *check.C, filename string, contents []byte) {
|
2014-10-24 23:36:17 +00:00
|
|
|
defer suite.StorageDriver.Delete(filename)
|
|
|
|
|
2014-10-21 22:02:20 +00:00
|
|
|
err := suite.StorageDriver.PutContent(filename, contents)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-10-21 22:02:20 +00:00
|
|
|
|
|
|
|
readContents, err := suite.StorageDriver.GetContent(filename)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(readContents, check.DeepEquals, contents)
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-20 22:50:51 +00:00
|
|
|
func (suite *DriverSuite) writeReadCompareStreams(c *check.C, filename string, contents []byte) {
|
2014-10-24 23:36:17 +00:00
|
|
|
defer suite.StorageDriver.Delete(filename)
|
|
|
|
|
2014-10-21 22:02:20 +00:00
|
|
|
err := suite.StorageDriver.WriteStream(filename, 0, uint64(len(contents)), ioutil.NopCloser(bytes.NewReader(contents)))
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-10-21 22:02:20 +00:00
|
|
|
|
|
|
|
reader, err := suite.StorageDriver.ReadStream(filename, 0)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-10-21 22:02:20 +00:00
|
|
|
defer reader.Close()
|
|
|
|
|
|
|
|
readContents, err := ioutil.ReadAll(reader)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(readContents, check.DeepEquals, contents)
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var pathChars = []byte("abcdefghijklmnopqrstuvwxyz")
|
|
|
|
|
|
|
|
func randomString(length uint64) string {
|
|
|
|
b := make([]byte, length)
|
|
|
|
for i := range b {
|
|
|
|
b[i] = pathChars[rand.Intn(len(pathChars))]
|
|
|
|
}
|
|
|
|
return string(b)
|
|
|
|
}
|