2014-10-21 22:02:20 +00:00
|
|
|
package testsuites
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2014-12-09 02:22:08 +00:00
|
|
|
"crypto/sha1"
|
2014-12-05 19:46:41 +00:00
|
|
|
"io"
|
2014-10-21 22:02:20 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"math/rand"
|
2014-11-20 22:50:51 +00:00
|
|
|
"os"
|
2014-10-21 22:02:20 +00:00
|
|
|
"path"
|
|
|
|
"sort"
|
2014-12-03 04:43:31 +00:00
|
|
|
"sync"
|
2014-10-21 22:02:20 +00:00
|
|
|
"testing"
|
2014-12-04 00:37:46 +00:00
|
|
|
"time"
|
2014-10-21 22:02:20 +00:00
|
|
|
|
|
|
|
"github.com/docker/docker-registry/storagedriver"
|
|
|
|
|
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-12-05 22:05:37 +00:00
|
|
|
panic("ipc testing is disabled for now")
|
|
|
|
|
|
|
|
// NOTE(stevvooe): IPC testing is disabled for now. Uncomment the code
|
|
|
|
// block before and remove the panic when we phase it back in.
|
|
|
|
|
|
|
|
// 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
|
|
|
|
// },
|
|
|
|
// SkipCheck: skipCheck,
|
|
|
|
// }
|
|
|
|
// suite.Teardown = func() error {
|
|
|
|
// if suite.StorageDriver == nil {
|
|
|
|
// return nil
|
|
|
|
// }
|
|
|
|
|
|
|
|
// driverClient := suite.StorageDriver.(*ipc.StorageDriverClient)
|
|
|
|
// return driverClient.Stop()
|
|
|
|
// }
|
|
|
|
// 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-12-11 22:11:47 +00:00
|
|
|
// TearDownTest tears down the gocheck test.
|
|
|
|
// This causes the suite to abort if any files are left around in the storage
|
|
|
|
// driver.
|
|
|
|
func (suite *DriverSuite) TearDownTest(c *check.C) {
|
|
|
|
files, _ := suite.StorageDriver.List("/")
|
|
|
|
if len(files) > 0 {
|
|
|
|
c.Fatalf("Storage driver did not clean up properly. Offending files: %#v", files)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-11 22:54:04 +00:00
|
|
|
// TestValidPaths checks that various valid file paths are accepted by the
|
|
|
|
// storage driver.
|
|
|
|
func (suite *DriverSuite) TestValidPaths(c *check.C) {
|
|
|
|
contents := randomContents(64)
|
|
|
|
validFiles := []string{"/aa", "/a.a", "/0-9/abcdefg", "/abcdefg/z.75", "/abc/1.2.3.4.5-6_zyx/123.z", "/docker/docker-registry"}
|
|
|
|
|
|
|
|
for _, filename := range validFiles {
|
|
|
|
err := suite.StorageDriver.PutContent(filename, contents)
|
|
|
|
defer suite.StorageDriver.Delete(firstPart(filename))
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
received, err := suite.StorageDriver.GetContent(filename)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(received, check.DeepEquals, contents)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestInvalidPaths checks that various invalid file paths are rejected by the
|
|
|
|
// storage driver.
|
|
|
|
func (suite *DriverSuite) TestInvalidPaths(c *check.C) {
|
|
|
|
contents := randomContents(64)
|
2014-12-16 20:01:27 +00:00
|
|
|
invalidFiles := []string{"", "/", "abc", "123.abc", "/abc./abc", "/.abc", "/a--b", "/a-.b", "/_.abc", "/a/bcd", "/abc_123/d", "/Docker/docker-registry"}
|
2014-12-11 22:54:04 +00:00
|
|
|
|
|
|
|
for _, filename := range invalidFiles {
|
|
|
|
err := suite.StorageDriver.PutContent(filename, contents)
|
|
|
|
defer suite.StorageDriver.Delete(firstPart(filename))
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
c.Assert(err, check.FitsTypeOf, storagedriver.InvalidPathError{})
|
|
|
|
|
|
|
|
_, err = suite.StorageDriver.GetContent(filename)
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
c.Assert(err, check.FitsTypeOf, storagedriver.InvalidPathError{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-12-09 02:22:08 +00:00
|
|
|
filename := randomPath(32)
|
2014-10-21 22:02:20 +00:00
|
|
|
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-12-09 02:22:08 +00:00
|
|
|
filename := randomPath(32)
|
2014-10-21 22:02:20 +00:00
|
|
|
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-12-09 02:22:08 +00:00
|
|
|
filename := randomPath(32)
|
|
|
|
contents := randomContents(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-12-09 02:22:08 +00:00
|
|
|
filename := randomPath(32)
|
|
|
|
contents := randomContents(1024 * 1024)
|
|
|
|
suite.writeReadCompare(c, filename, contents)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestWriteReadNonUTF8 tests that non-utf8 data may be written to the storage
|
|
|
|
// driver safely.
|
|
|
|
func (suite *DriverSuite) TestWriteReadNonUTF8(c *check.C) {
|
|
|
|
filename := randomPath(32)
|
|
|
|
contents := []byte{0x80, 0x80, 0x80, 0x80}
|
|
|
|
suite.writeReadCompare(c, filename, contents)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestTruncate tests that putting smaller contents than an original file does
|
|
|
|
// remove the excess contents.
|
|
|
|
func (suite *DriverSuite) TestTruncate(c *check.C) {
|
|
|
|
filename := randomPath(32)
|
|
|
|
contents := randomContents(1024 * 1024)
|
|
|
|
suite.writeReadCompare(c, filename, contents)
|
|
|
|
|
|
|
|
contents = randomContents(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-12-09 02:22:08 +00:00
|
|
|
filename := randomPath(32)
|
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
|
|
|
// TestWriteReadStreams1 tests a simple write-read streaming workflow.
|
2014-11-17 23:44:07 +00:00
|
|
|
func (suite *DriverSuite) TestWriteReadStreams1(c *check.C) {
|
2014-12-09 02:22:08 +00:00
|
|
|
filename := randomPath(32)
|
2014-10-21 22:02:20 +00:00
|
|
|
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-12-09 02:22:08 +00:00
|
|
|
filename := randomPath(32)
|
2014-10-21 22:02:20 +00:00
|
|
|
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-12-09 02:22:08 +00:00
|
|
|
filename := randomPath(32)
|
|
|
|
contents := randomContents(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-12-09 02:22:08 +00:00
|
|
|
filename := randomPath(32)
|
|
|
|
contents := randomContents(1024 * 1024)
|
|
|
|
suite.writeReadCompareStreams(c, filename, contents)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestWriteReadStreamsNonUTF8 tests that non-utf8 data may be written to the
|
|
|
|
// storage driver safely.
|
|
|
|
func (suite *DriverSuite) TestWriteReadStreamsNonUTF8(c *check.C) {
|
|
|
|
filename := randomPath(32)
|
|
|
|
contents := []byte{0x80, 0x80, 0x80, 0x80}
|
2014-11-20 22:50:51 +00:00
|
|
|
suite.writeReadCompareStreams(c, filename, contents)
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-12-09 02:22:08 +00:00
|
|
|
// TestWriteReadLargeStreams tests that a 5GB file may be written to the storage
|
|
|
|
// driver safely.
|
|
|
|
func (suite *DriverSuite) TestWriteReadLargeStreams(c *check.C) {
|
|
|
|
if testing.Short() {
|
|
|
|
c.Skip("Skipping test in short mode")
|
|
|
|
}
|
|
|
|
|
|
|
|
filename := randomPath(32)
|
|
|
|
defer suite.StorageDriver.Delete(firstPart(filename))
|
|
|
|
|
|
|
|
checksum := sha1.New()
|
2014-12-10 02:04:05 +00:00
|
|
|
var offset int64
|
2014-12-09 02:22:08 +00:00
|
|
|
var chunkSize int64 = 1024 * 1024
|
|
|
|
|
|
|
|
for i := 0; i < 5*1024; i++ {
|
|
|
|
contents := randomContents(chunkSize)
|
|
|
|
written, err := suite.StorageDriver.WriteStream(filename, offset, io.TeeReader(bytes.NewReader(contents), checksum))
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(written, check.Equals, chunkSize)
|
|
|
|
offset += chunkSize
|
|
|
|
}
|
|
|
|
reader, err := suite.StorageDriver.ReadStream(filename, 0)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
writtenChecksum := sha1.New()
|
|
|
|
io.Copy(writtenChecksum, reader)
|
|
|
|
|
|
|
|
c.Assert(writtenChecksum.Sum(nil), check.DeepEquals, checksum.Sum(nil))
|
|
|
|
}
|
|
|
|
|
2014-12-04 00:37:46 +00:00
|
|
|
// TestReadStreamWithOffset tests that the appropriate data is streamed when
|
|
|
|
// reading with a given offset.
|
|
|
|
func (suite *DriverSuite) TestReadStreamWithOffset(c *check.C) {
|
2014-12-09 02:22:08 +00:00
|
|
|
filename := randomPath(32)
|
|
|
|
defer suite.StorageDriver.Delete(firstPart(filename))
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-12-04 00:37:46 +00:00
|
|
|
chunkSize := int64(32)
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-12-09 02:22:08 +00:00
|
|
|
contentsChunk1 := randomContents(chunkSize)
|
|
|
|
contentsChunk2 := randomContents(chunkSize)
|
|
|
|
contentsChunk3 := randomContents(chunkSize)
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-12-04 00:37:46 +00:00
|
|
|
err := suite.StorageDriver.PutContent(filename, append(append(contentsChunk1, contentsChunk2...), contentsChunk3...))
|
|
|
|
c.Assert(err, check.IsNil)
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-12-04 00:37:46 +00:00
|
|
|
reader, err := suite.StorageDriver.ReadStream(filename, 0)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-12-04 00:37:46 +00:00
|
|
|
defer reader.Close()
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-12-04 00:37:46 +00:00
|
|
|
readContents, err := ioutil.ReadAll(reader)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-12-04 00:37:46 +00:00
|
|
|
|
|
|
|
c.Assert(readContents, check.DeepEquals, append(append(contentsChunk1, contentsChunk2...), contentsChunk3...))
|
|
|
|
|
|
|
|
reader, err = suite.StorageDriver.ReadStream(filename, chunkSize)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-12-04 00:37:46 +00:00
|
|
|
defer reader.Close()
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-12-04 00:37:46 +00:00
|
|
|
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-12-04 00:37:46 +00:00
|
|
|
c.Assert(readContents, check.DeepEquals, append(contentsChunk2, contentsChunk3...))
|
|
|
|
|
|
|
|
reader, err = suite.StorageDriver.ReadStream(filename, chunkSize*2)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-12-04 00:37:46 +00:00
|
|
|
defer reader.Close()
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-12-04 00:37:46 +00:00
|
|
|
readContents, err = ioutil.ReadAll(reader)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-12-04 00:37:46 +00:00
|
|
|
c.Assert(readContents, check.DeepEquals, contentsChunk3)
|
2014-12-05 19:46:41 +00:00
|
|
|
|
|
|
|
// Ensure we get invalid offest for negative offsets.
|
|
|
|
reader, err = suite.StorageDriver.ReadStream(filename, -1)
|
|
|
|
c.Assert(err, check.FitsTypeOf, storagedriver.InvalidOffsetError{})
|
|
|
|
c.Assert(err.(storagedriver.InvalidOffsetError).Offset, check.Equals, int64(-1))
|
|
|
|
c.Assert(err.(storagedriver.InvalidOffsetError).Path, check.Equals, filename)
|
|
|
|
c.Assert(reader, check.IsNil)
|
|
|
|
|
|
|
|
// Read past the end of the content and make sure we get a reader that
|
|
|
|
// returns 0 bytes and io.EOF
|
|
|
|
reader, err = suite.StorageDriver.ReadStream(filename, chunkSize*3)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
defer reader.Close()
|
|
|
|
|
|
|
|
buf := make([]byte, chunkSize)
|
|
|
|
n, err := reader.Read(buf)
|
|
|
|
c.Assert(err, check.Equals, io.EOF)
|
|
|
|
c.Assert(n, check.Equals, 0)
|
|
|
|
|
|
|
|
// Check the N-1 boundary condition, ensuring we get 1 byte then io.EOF.
|
|
|
|
reader, err = suite.StorageDriver.ReadStream(filename, chunkSize*3-1)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
defer reader.Close()
|
|
|
|
|
|
|
|
n, err = reader.Read(buf)
|
|
|
|
c.Assert(n, check.Equals, 1)
|
|
|
|
|
|
|
|
// We don't care whether the io.EOF comes on the this read or the first
|
|
|
|
// zero read, but the only error acceptable here is io.EOF.
|
|
|
|
if err != nil {
|
|
|
|
c.Assert(err, check.Equals, io.EOF)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Any more reads should result in zero bytes and io.EOF
|
|
|
|
n, err = reader.Read(buf)
|
|
|
|
c.Assert(n, check.Equals, 0)
|
|
|
|
c.Assert(err, check.Equals, io.EOF)
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-12-04 00:37:46 +00:00
|
|
|
// TestContinueStreamAppend tests that a stream write can be appended to without
|
|
|
|
// corrupting the data.
|
|
|
|
func (suite *DriverSuite) TestContinueStreamAppend(c *check.C) {
|
2014-12-09 02:22:08 +00:00
|
|
|
filename := randomPath(32)
|
|
|
|
defer suite.StorageDriver.Delete(firstPart(filename))
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-12-19 17:16:51 +00:00
|
|
|
chunkSize := int64(5 * 1024 * 1024)
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-12-09 02:22:08 +00:00
|
|
|
contentsChunk1 := randomContents(chunkSize)
|
|
|
|
contentsChunk2 := randomContents(chunkSize)
|
|
|
|
contentsChunk3 := randomContents(chunkSize)
|
|
|
|
contentsChunk4 := randomContents(chunkSize)
|
2014-12-04 00:37:46 +00:00
|
|
|
zeroChunk := make([]byte, int64(chunkSize))
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-12-04 00:37:46 +00:00
|
|
|
fullContents := append(append(contentsChunk1, contentsChunk2...), contentsChunk3...)
|
|
|
|
|
|
|
|
nn, err := suite.StorageDriver.WriteStream(filename, 0, bytes.NewReader(contentsChunk1))
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-12-04 00:37:46 +00:00
|
|
|
c.Assert(nn, check.Equals, int64(len(contentsChunk1)))
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-12-04 00:37:46 +00:00
|
|
|
fi, err := suite.StorageDriver.Stat(filename)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-12-04 00:37:46 +00:00
|
|
|
c.Assert(fi, check.NotNil)
|
|
|
|
c.Assert(fi.Size(), check.Equals, int64(len(contentsChunk1)))
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-12-04 00:37:46 +00:00
|
|
|
if fi.Size() > chunkSize {
|
2014-12-11 22:11:47 +00:00
|
|
|
c.Errorf("Offset too large, %d > %d", fi.Size(), chunkSize)
|
2014-12-04 00:37:46 +00:00
|
|
|
}
|
|
|
|
nn, err = suite.StorageDriver.WriteStream(filename, fi.Size(), bytes.NewReader(contentsChunk2))
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-12-04 00:37:46 +00:00
|
|
|
c.Assert(nn, check.Equals, int64(len(contentsChunk2)))
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-12-04 00:37:46 +00:00
|
|
|
fi, err = suite.StorageDriver.Stat(filename)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(fi, check.NotNil)
|
|
|
|
c.Assert(fi.Size(), check.Equals, 2*chunkSize)
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-12-04 00:37:46 +00:00
|
|
|
if fi.Size() > 2*chunkSize {
|
2014-12-11 22:11:47 +00:00
|
|
|
c.Errorf("Offset too large, %d > %d", fi.Size(), 2*chunkSize)
|
2014-12-04 00:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nn, err = suite.StorageDriver.WriteStream(filename, fi.Size(), bytes.NewReader(fullContents[fi.Size():]))
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-12-04 00:37:46 +00:00
|
|
|
c.Assert(nn, check.Equals, int64(len(fullContents[fi.Size():])))
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-12-04 00:37:46 +00:00
|
|
|
received, err := suite.StorageDriver.GetContent(filename)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-12-04 00:37:46 +00:00
|
|
|
c.Assert(received, check.DeepEquals, fullContents)
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-12-04 00:37:46 +00:00
|
|
|
// Writing past size of file extends file (no offest error). We would like
|
|
|
|
// to write chunk 4 one chunk length past chunk 3. It should be successful
|
|
|
|
// and the resulting file will be 5 chunks long, with a chunk of all
|
|
|
|
// zeros.
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-12-04 00:37:46 +00:00
|
|
|
fullContents = append(fullContents, zeroChunk...)
|
|
|
|
fullContents = append(fullContents, contentsChunk4...)
|
|
|
|
|
|
|
|
nn, err = suite.StorageDriver.WriteStream(filename, int64(len(fullContents))-chunkSize, bytes.NewReader(contentsChunk4))
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-12-04 00:37:46 +00:00
|
|
|
c.Assert(nn, check.Equals, chunkSize)
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-12-04 00:37:46 +00:00
|
|
|
fi, err = suite.StorageDriver.Stat(filename)
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-12-04 00:37:46 +00:00
|
|
|
c.Assert(fi, check.NotNil)
|
|
|
|
c.Assert(fi.Size(), check.Equals, int64(len(fullContents)))
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-12-04 00:37:46 +00:00
|
|
|
received, err = suite.StorageDriver.GetContent(filename)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(received), check.Equals, len(fullContents))
|
|
|
|
c.Assert(received[chunkSize*3:chunkSize*4], check.DeepEquals, zeroChunk)
|
|
|
|
c.Assert(received[chunkSize*4:chunkSize*5], check.DeepEquals, contentsChunk4)
|
|
|
|
c.Assert(received, check.DeepEquals, fullContents)
|
|
|
|
|
|
|
|
// Ensure that negative offsets return correct error.
|
|
|
|
nn, err = suite.StorageDriver.WriteStream(filename, -1, bytes.NewReader(zeroChunk))
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
c.Assert(err, check.FitsTypeOf, storagedriver.InvalidOffsetError{})
|
|
|
|
c.Assert(err.(storagedriver.InvalidOffsetError).Path, check.Equals, filename)
|
|
|
|
c.Assert(err.(storagedriver.InvalidOffsetError).Offset, check.Equals, int64(-1))
|
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-12-09 02:22:08 +00:00
|
|
|
filename := randomPath(32)
|
2014-12-10 18:51:07 +00:00
|
|
|
|
2014-10-21 22:02:20 +00:00
|
|
|
_, 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-12-10 18:51:07 +00:00
|
|
|
|
|
|
|
_, err = suite.StorageDriver.ReadStream(filename, 64)
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
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-12-09 02:22:08 +00:00
|
|
|
rootDirectory := "/" + randomFilename(int64(8+rand.Intn(8)))
|
2014-12-11 22:11:47 +00:00
|
|
|
defer suite.StorageDriver.Delete(rootDirectory)
|
2014-10-24 23:36:17 +00:00
|
|
|
|
2014-12-09 02:22:08 +00:00
|
|
|
parentDirectory := rootDirectory + "/" + randomFilename(int64(8+rand.Intn(8)))
|
2014-10-21 22:02:20 +00:00
|
|
|
childFiles := make([]string, 50)
|
|
|
|
for i := 0; i < len(childFiles); i++ {
|
2014-12-09 02:22:08 +00:00
|
|
|
childFile := parentDirectory + "/" + randomFilename(int64(8+rand.Intn(8)))
|
2014-10-21 22:02:20 +00:00
|
|
|
childFiles[i] = childFile
|
2014-12-09 02:22:08 +00:00
|
|
|
err := suite.StorageDriver.PutContent(childFile, randomContents(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-12-05 19:46:41 +00:00
|
|
|
|
|
|
|
// A few checks to add here (check out #819 for more discussion on this):
|
|
|
|
// 1. Ensure that all paths are absolute.
|
|
|
|
// 2. Ensure that listings only include direct children.
|
|
|
|
// 3. Ensure that we only respond to directory listings that end with a slash (maybe?).
|
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-12-09 02:22:08 +00:00
|
|
|
contents := randomContents(32)
|
|
|
|
sourcePath := randomPath(32)
|
|
|
|
destPath := randomPath(32)
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-12-09 02:22:08 +00:00
|
|
|
defer suite.StorageDriver.Delete(firstPart(sourcePath))
|
|
|
|
defer suite.StorageDriver.Delete(firstPart(destPath))
|
2014-10-24 23:36:17 +00:00
|
|
|
|
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-12-10 18:53:51 +00:00
|
|
|
// TestMoveOverwrite checks that a moved object no longer exists at the source
|
|
|
|
// path and overwrites the contents at the destination.
|
|
|
|
func (suite *DriverSuite) TestMoveOverwrite(c *check.C) {
|
|
|
|
sourcePath := randomPath(32)
|
|
|
|
destPath := randomPath(32)
|
|
|
|
sourceContents := randomContents(32)
|
|
|
|
destContents := randomContents(64)
|
|
|
|
|
|
|
|
defer suite.StorageDriver.Delete(firstPart(sourcePath))
|
|
|
|
defer suite.StorageDriver.Delete(firstPart(destPath))
|
|
|
|
|
|
|
|
err := suite.StorageDriver.PutContent(sourcePath, sourceContents)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
err = suite.StorageDriver.PutContent(destPath, destContents)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
err = suite.StorageDriver.Move(sourcePath, destPath)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
received, err := suite.StorageDriver.GetContent(destPath)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(received, check.DeepEquals, sourceContents)
|
|
|
|
|
|
|
|
_, err = suite.StorageDriver.GetContent(sourcePath)
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
c.Assert(err, check.FitsTypeOf, storagedriver.PathNotFoundError{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestMoveNonexistent checks that moving a nonexistent key fails and does not
|
|
|
|
// delete the data at the destination path.
|
2014-11-17 23:44:07 +00:00
|
|
|
func (suite *DriverSuite) TestMoveNonexistent(c *check.C) {
|
2014-12-10 18:53:51 +00:00
|
|
|
contents := randomContents(32)
|
2014-12-09 02:22:08 +00:00
|
|
|
sourcePath := randomPath(32)
|
|
|
|
destPath := randomPath(32)
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-12-10 18:53:51 +00:00
|
|
|
defer suite.StorageDriver.Delete(firstPart(destPath))
|
|
|
|
|
|
|
|
err := suite.StorageDriver.PutContent(destPath, contents)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
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-12-10 18:53:51 +00:00
|
|
|
|
|
|
|
received, err := suite.StorageDriver.GetContent(destPath)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(received, check.DeepEquals, contents)
|
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-12-09 02:22:08 +00:00
|
|
|
filename := randomPath(32)
|
|
|
|
contents := randomContents(32)
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-12-09 02:22:08 +00:00
|
|
|
defer suite.StorageDriver.Delete(firstPart(filename))
|
2014-10-24 23:36:17 +00:00
|
|
|
|
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-12-09 02:22:08 +00:00
|
|
|
filename := randomPath(32)
|
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.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-12-09 02:22:08 +00:00
|
|
|
dirname := randomPath(32)
|
|
|
|
filename1 := randomPath(32)
|
|
|
|
filename2 := randomPath(32)
|
|
|
|
filename3 := randomPath(32)
|
|
|
|
contents := randomContents(32)
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-12-09 02:22:08 +00:00
|
|
|
defer suite.StorageDriver.Delete(firstPart(dirname))
|
2014-10-24 23:36:17 +00:00
|
|
|
|
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
|
|
|
|
2014-12-09 02:22:08 +00:00
|
|
|
err = suite.StorageDriver.PutContent(path.Join(dirname, filename3), contents)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
err = suite.StorageDriver.Delete(path.Join(dirname, filename1))
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
_, err = suite.StorageDriver.GetContent(path.Join(dirname, filename1))
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
c.Assert(err, check.FitsTypeOf, storagedriver.PathNotFoundError{})
|
|
|
|
|
|
|
|
_, err = suite.StorageDriver.GetContent(path.Join(dirname, filename2))
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
_, err = suite.StorageDriver.GetContent(path.Join(dirname, filename3))
|
|
|
|
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-12-09 02:22:08 +00:00
|
|
|
|
|
|
|
_, err = suite.StorageDriver.GetContent(path.Join(dirname, filename3))
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
c.Assert(err, check.FitsTypeOf, storagedriver.PathNotFoundError{})
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-12-06 03:20:42 +00:00
|
|
|
// TestStatCall runs verifies the implementation of the storagedriver's Stat call.
|
2014-12-04 00:37:46 +00:00
|
|
|
func (suite *DriverSuite) TestStatCall(c *check.C) {
|
2014-12-09 02:22:08 +00:00
|
|
|
content := randomContents(4096)
|
|
|
|
dirPath := randomPath(32)
|
|
|
|
fileName := randomFilename(32)
|
2014-12-04 00:37:46 +00:00
|
|
|
filePath := path.Join(dirPath, fileName)
|
|
|
|
|
2014-12-10 18:55:33 +00:00
|
|
|
defer suite.StorageDriver.Delete(firstPart(dirPath))
|
2014-12-09 02:22:08 +00:00
|
|
|
|
2014-12-04 00:37:46 +00:00
|
|
|
// Call on non-existent file/dir, check error.
|
2014-12-10 18:55:33 +00:00
|
|
|
fi, err := suite.StorageDriver.Stat(dirPath)
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
c.Assert(err, check.FitsTypeOf, storagedriver.PathNotFoundError{})
|
|
|
|
c.Assert(fi, check.IsNil)
|
|
|
|
|
|
|
|
fi, err = suite.StorageDriver.Stat(filePath)
|
2014-12-04 00:37:46 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
c.Assert(err, check.FitsTypeOf, storagedriver.PathNotFoundError{})
|
|
|
|
c.Assert(fi, check.IsNil)
|
|
|
|
|
2014-12-09 02:22:08 +00:00
|
|
|
err = suite.StorageDriver.PutContent(filePath, content)
|
2014-12-04 00:37:46 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
// Call on regular file, check results
|
|
|
|
start := time.Now().Truncate(time.Second) // truncated for filesystem
|
|
|
|
fi, err = suite.StorageDriver.Stat(filePath)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
expectedModTime := time.Now()
|
|
|
|
c.Assert(fi, check.NotNil)
|
|
|
|
c.Assert(fi.Path(), check.Equals, filePath)
|
|
|
|
c.Assert(fi.Size(), check.Equals, int64(len(content)))
|
|
|
|
c.Assert(fi.IsDir(), check.Equals, false)
|
|
|
|
|
|
|
|
if start.After(fi.ModTime()) {
|
2014-12-11 22:11:47 +00:00
|
|
|
c.Errorf("modtime %s before file created (%v)", fi.ModTime(), start)
|
2014-12-04 00:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if fi.ModTime().After(expectedModTime) {
|
2014-12-11 22:11:47 +00:00
|
|
|
c.Errorf("modtime %s after file created (%v)", fi.ModTime(), expectedModTime)
|
2014-12-04 00:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call on directory
|
|
|
|
start = time.Now().Truncate(time.Second)
|
|
|
|
fi, err = suite.StorageDriver.Stat(dirPath)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
expectedModTime = time.Now()
|
|
|
|
c.Assert(fi, check.NotNil)
|
|
|
|
c.Assert(fi.Path(), check.Equals, dirPath)
|
|
|
|
c.Assert(fi.Size(), check.Equals, int64(0))
|
|
|
|
c.Assert(fi.IsDir(), check.Equals, true)
|
|
|
|
|
2014-12-19 17:16:51 +00:00
|
|
|
// if start.After(fi.ModTime()) {
|
|
|
|
// c.Errorf("modtime %s before file created (%v)", fi.ModTime(), start)
|
|
|
|
// }
|
2014-12-04 00:37:46 +00:00
|
|
|
|
|
|
|
if fi.ModTime().After(expectedModTime) {
|
2014-12-11 22:11:47 +00:00
|
|
|
c.Errorf("modtime %s after file created (%v)", fi.ModTime(), expectedModTime)
|
2014-12-04 00:37:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-10 18:57:47 +00:00
|
|
|
// TestConcurrentStreamReads checks that multiple clients can safely read from
|
|
|
|
// the same file simultaneously with various offsets.
|
|
|
|
func (suite *DriverSuite) TestConcurrentStreamReads(c *check.C) {
|
|
|
|
var filesize int64 = 128 * 1024 * 1024
|
|
|
|
|
|
|
|
if testing.Short() {
|
|
|
|
filesize = 10 * 1024 * 1024
|
|
|
|
c.Log("Reducing file size to 10MB for short mode")
|
|
|
|
}
|
|
|
|
|
|
|
|
filename := randomPath(32)
|
|
|
|
contents := randomContents(filesize)
|
|
|
|
|
|
|
|
defer suite.StorageDriver.Delete(firstPart(filename))
|
|
|
|
|
|
|
|
err := suite.StorageDriver.PutContent(filename, contents)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
readContents := func() {
|
|
|
|
defer wg.Done()
|
|
|
|
offset := rand.Int63n(int64(len(contents)))
|
|
|
|
reader, err := suite.StorageDriver.ReadStream(filename, offset)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
readContents, err := ioutil.ReadAll(reader)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(readContents, check.DeepEquals, contents[offset:])
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Add(10)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
go readContents()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2014-11-20 22:50:51 +00:00
|
|
|
// TestConcurrentFileStreams checks that multiple *os.File objects can be passed
|
|
|
|
// in to WriteStream concurrently without hanging.
|
|
|
|
func (suite *DriverSuite) TestConcurrentFileStreams(c *check.C) {
|
2014-12-05 22:05:37 +00:00
|
|
|
// if _, isIPC := suite.StorageDriver.(*ipc.StorageDriverClient); isIPC {
|
|
|
|
// c.Skip("Need to fix out-of-process concurrency")
|
|
|
|
// }
|
2014-11-20 22:50:51 +00:00
|
|
|
|
2014-12-11 00:20:14 +00:00
|
|
|
numStreams := 32
|
|
|
|
|
|
|
|
if testing.Short() {
|
|
|
|
numStreams = 8
|
|
|
|
c.Log("Reducing number of streams to 8 for short mode")
|
|
|
|
}
|
|
|
|
|
2014-12-03 04:43:31 +00:00
|
|
|
var wg sync.WaitGroup
|
2014-11-20 22:50:51 +00:00
|
|
|
|
2014-12-03 03:01:00 +00:00
|
|
|
testStream := func(size int64) {
|
2014-12-03 04:43:31 +00:00
|
|
|
defer wg.Done()
|
2014-11-20 22:50:51 +00:00
|
|
|
suite.testFileStreams(c, size)
|
|
|
|
}
|
|
|
|
|
2014-12-11 00:20:14 +00:00
|
|
|
wg.Add(numStreams)
|
|
|
|
for i := numStreams; i > 0; i-- {
|
|
|
|
go testStream(int64(numStreams) * 1024 * 1024)
|
|
|
|
}
|
2014-11-20 22:50:51 +00:00
|
|
|
|
2014-12-03 04:43:31 +00:00
|
|
|
wg.Wait()
|
2014-11-20 22:50:51 +00:00
|
|
|
}
|
|
|
|
|
2014-12-16 20:01:27 +00:00
|
|
|
// BenchmarkPutGetEmptyFiles benchmarks PutContent/GetContent for 0B files
|
|
|
|
func (suite *DriverSuite) BenchmarkPutGetEmptyFiles(c *check.C) {
|
|
|
|
suite.benchmarkPutGetFiles(c, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// BenchmarkPutGet1KBFiles benchmarks PutContent/GetContent for 1KB files
|
|
|
|
func (suite *DriverSuite) BenchmarkPutGet1KBFiles(c *check.C) {
|
|
|
|
suite.benchmarkPutGetFiles(c, 1024)
|
|
|
|
}
|
|
|
|
|
|
|
|
// BenchmarkPutGet1MBFiles benchmarks PutContent/GetContent for 1MB files
|
|
|
|
func (suite *DriverSuite) BenchmarkPutGet1MBFiles(c *check.C) {
|
|
|
|
suite.benchmarkPutGetFiles(c, 1024*1024)
|
|
|
|
}
|
|
|
|
|
|
|
|
// BenchmarkPutGet1GBFiles benchmarks PutContent/GetContent for 1GB files
|
|
|
|
func (suite *DriverSuite) BenchmarkPutGet1GBFiles(c *check.C) {
|
|
|
|
suite.benchmarkPutGetFiles(c, 1024*1024*1024)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *DriverSuite) benchmarkPutGetFiles(c *check.C, size int64) {
|
|
|
|
c.SetBytes(size)
|
|
|
|
parentDir := randomPath(8)
|
|
|
|
defer func() {
|
|
|
|
c.StopTimer()
|
|
|
|
suite.StorageDriver.Delete(firstPart(parentDir))
|
|
|
|
}()
|
|
|
|
|
|
|
|
for i := 0; i < c.N; i++ {
|
|
|
|
filename := path.Join(parentDir, randomPath(32))
|
|
|
|
err := suite.StorageDriver.PutContent(filename, randomContents(size))
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
_, err = suite.StorageDriver.GetContent(filename)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BenchmarkStreamEmptyFiles benchmarks WriteStream/ReadStream for 0B files
|
|
|
|
func (suite *DriverSuite) BenchmarkStreamEmptyFiles(c *check.C) {
|
|
|
|
suite.benchmarkStreamFiles(c, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// BenchmarkStream1KBFiles benchmarks WriteStream/ReadStream for 1KB files
|
|
|
|
func (suite *DriverSuite) BenchmarkStream1KBFiles(c *check.C) {
|
|
|
|
suite.benchmarkStreamFiles(c, 1024)
|
|
|
|
}
|
|
|
|
|
|
|
|
// BenchmarkStream1MBFiles benchmarks WriteStream/ReadStream for 1MB files
|
|
|
|
func (suite *DriverSuite) BenchmarkStream1MBFiles(c *check.C) {
|
|
|
|
suite.benchmarkStreamFiles(c, 1024*1024)
|
|
|
|
}
|
|
|
|
|
|
|
|
// BenchmarkStream1GBFiles benchmarks WriteStream/ReadStream for 1GB files
|
|
|
|
func (suite *DriverSuite) BenchmarkStream1GBFiles(c *check.C) {
|
|
|
|
suite.benchmarkStreamFiles(c, 1024*1024*1024)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *DriverSuite) benchmarkStreamFiles(c *check.C, size int64) {
|
|
|
|
c.SetBytes(size)
|
|
|
|
parentDir := randomPath(8)
|
|
|
|
defer func() {
|
|
|
|
c.StopTimer()
|
|
|
|
suite.StorageDriver.Delete(firstPart(parentDir))
|
|
|
|
}()
|
|
|
|
|
|
|
|
for i := 0; i < c.N; i++ {
|
|
|
|
filename := path.Join(parentDir, randomPath(32))
|
|
|
|
written, err := suite.StorageDriver.WriteStream(filename, 0, bytes.NewReader(randomContents(size)))
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(written, check.Equals, size)
|
|
|
|
|
|
|
|
rc, err := suite.StorageDriver.ReadStream(filename, 0)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
rc.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BenchmarkList5Files benchmarks List for 5 small files
|
|
|
|
func (suite *DriverSuite) BenchmarkList5Files(c *check.C) {
|
|
|
|
suite.benchmarkListFiles(c, 5)
|
|
|
|
}
|
|
|
|
|
|
|
|
// BenchmarkList50Files benchmarks List for 50 small files
|
|
|
|
func (suite *DriverSuite) BenchmarkList50Files(c *check.C) {
|
|
|
|
suite.benchmarkListFiles(c, 50)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *DriverSuite) benchmarkListFiles(c *check.C, numFiles int64) {
|
|
|
|
parentDir := randomPath(8)
|
|
|
|
defer func() {
|
|
|
|
c.StopTimer()
|
|
|
|
suite.StorageDriver.Delete(firstPart(parentDir))
|
|
|
|
}()
|
|
|
|
|
|
|
|
for i := int64(0); i < numFiles; i++ {
|
|
|
|
err := suite.StorageDriver.PutContent(path.Join(parentDir, randomPath(32)), nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.ResetTimer()
|
|
|
|
for i := 0; i < c.N; i++ {
|
|
|
|
files, err := suite.StorageDriver.List(parentDir)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(int64(len(files)), check.Equals, numFiles)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BenchmarkDelete5Files benchmarks Delete for 5 small files
|
|
|
|
func (suite *DriverSuite) BenchmarkDelete5Files(c *check.C) {
|
|
|
|
suite.benchmarkDeleteFiles(c, 5)
|
|
|
|
}
|
|
|
|
|
|
|
|
// BenchmarkDelete50Files benchmarks Delete for 50 small files
|
|
|
|
func (suite *DriverSuite) BenchmarkDelete50Files(c *check.C) {
|
|
|
|
suite.benchmarkDeleteFiles(c, 50)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *DriverSuite) benchmarkDeleteFiles(c *check.C, numFiles int64) {
|
|
|
|
for i := 0; i < c.N; i++ {
|
|
|
|
parentDir := randomPath(8)
|
|
|
|
defer suite.StorageDriver.Delete(firstPart(parentDir))
|
|
|
|
|
|
|
|
c.StopTimer()
|
|
|
|
for j := int64(0); j < numFiles; j++ {
|
|
|
|
err := suite.StorageDriver.PutContent(path.Join(parentDir, randomPath(32)), nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
}
|
|
|
|
c.StartTimer()
|
|
|
|
|
|
|
|
// This is the operation we're benchmarking
|
|
|
|
err := suite.StorageDriver.Delete(firstPart(parentDir))
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-03 03:01:00 +00:00
|
|
|
func (suite *DriverSuite) testFileStreams(c *check.C, size int64) {
|
2014-11-20 22:50:51 +00:00
|
|
|
tf, err := ioutil.TempFile("", "tf")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
defer os.Remove(tf.Name())
|
2014-12-11 00:20:14 +00:00
|
|
|
defer tf.Close()
|
2014-11-20 22:50:51 +00:00
|
|
|
|
2014-12-10 18:57:47 +00:00
|
|
|
filename := randomPath(32)
|
|
|
|
defer suite.StorageDriver.Delete(firstPart(filename))
|
2014-11-20 22:50:51 +00:00
|
|
|
|
2014-12-09 02:22:08 +00:00
|
|
|
contents := randomContents(size)
|
2014-11-20 22:50:51 +00:00
|
|
|
|
|
|
|
_, err = tf.Write(contents)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
tf.Sync()
|
|
|
|
tf.Seek(0, os.SEEK_SET)
|
|
|
|
|
2014-12-19 17:20:07 +00:00
|
|
|
totalRead := int64(0)
|
|
|
|
for totalRead < size {
|
|
|
|
nn, err := suite.StorageDriver.WriteStream(filename, 0, tf)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
totalRead += nn
|
|
|
|
}
|
|
|
|
c.Assert(totalRead, check.Equals, size)
|
2014-11-20 22:50:51 +00:00
|
|
|
|
2014-12-10 18:57:47 +00:00
|
|
|
reader, err := suite.StorageDriver.ReadStream(filename, 0)
|
2014-11-20 22:50:51 +00:00
|
|
|
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-12-09 02:22:08 +00:00
|
|
|
defer suite.StorageDriver.Delete(firstPart(filename))
|
2014-10-24 23:36:17 +00:00
|
|
|
|
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-12-09 02:22:08 +00:00
|
|
|
defer suite.StorageDriver.Delete(firstPart(filename))
|
2014-10-24 23:36:17 +00:00
|
|
|
|
2014-12-04 00:37:46 +00:00
|
|
|
nn, err := suite.StorageDriver.WriteStream(filename, 0, bytes.NewReader(contents))
|
2014-11-17 23:44:07 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-12-04 00:37:46 +00:00
|
|
|
c.Assert(nn, check.Equals, int64(len(contents)))
|
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
|
|
|
}
|
|
|
|
|
2014-12-09 02:22:08 +00:00
|
|
|
var filenameChars = []byte("abcdefghijklmnopqrstuvwxyz0123456789")
|
2014-12-11 22:11:47 +00:00
|
|
|
var separatorChars = []byte("._-")
|
2014-12-09 02:22:08 +00:00
|
|
|
|
|
|
|
func randomPath(length int64) string {
|
2014-12-11 22:11:47 +00:00
|
|
|
path := "/"
|
2014-12-09 02:22:08 +00:00
|
|
|
for int64(len(path)) < length {
|
2014-12-11 22:11:47 +00:00
|
|
|
chunkLength := rand.Int63n(length-int64(len(path)+1)) + 2
|
2014-12-09 02:22:08 +00:00
|
|
|
chunk := randomFilename(chunkLength)
|
|
|
|
path += chunk
|
|
|
|
if length-int64(len(path)) == 1 {
|
|
|
|
path += randomFilename(1)
|
2014-12-11 22:11:47 +00:00
|
|
|
} else if length-int64(len(path)) == 2 {
|
|
|
|
path += randomFilename(2)
|
|
|
|
} else if length-int64(len(path)) > 2 {
|
2014-12-09 02:22:08 +00:00
|
|
|
path += "/"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-12-09 02:22:08 +00:00
|
|
|
func randomFilename(length int64) string {
|
2014-10-21 22:02:20 +00:00
|
|
|
b := make([]byte, length)
|
2014-12-11 22:11:47 +00:00
|
|
|
wasSeparator := true
|
2014-10-21 22:02:20 +00:00
|
|
|
for i := range b {
|
2014-12-11 22:11:47 +00:00
|
|
|
if !wasSeparator && i < len(b)-1 && rand.Intn(4) == 0 {
|
|
|
|
b[i] = separatorChars[rand.Intn(len(separatorChars))]
|
|
|
|
wasSeparator = true
|
|
|
|
} else {
|
|
|
|
b[i] = filenameChars[rand.Intn(len(filenameChars))]
|
|
|
|
wasSeparator = false
|
|
|
|
}
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
return string(b)
|
|
|
|
}
|
2014-12-09 02:22:08 +00:00
|
|
|
|
|
|
|
func randomContents(length int64) []byte {
|
|
|
|
b := make([]byte, length)
|
|
|
|
for i := range b {
|
|
|
|
b[i] = byte(rand.Intn(2 << 8))
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func firstPart(filePath string) string {
|
2014-12-16 20:01:27 +00:00
|
|
|
if filePath == "" {
|
|
|
|
return "/"
|
|
|
|
}
|
2014-12-09 02:22:08 +00:00
|
|
|
for {
|
|
|
|
if filePath[len(filePath)-1] == '/' {
|
|
|
|
filePath = filePath[:len(filePath)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
dir, file := path.Split(filePath)
|
|
|
|
if dir == "" && file == "" {
|
|
|
|
return "/"
|
|
|
|
}
|
2014-12-11 22:11:47 +00:00
|
|
|
if dir == "/" || dir == "" {
|
|
|
|
return "/" + file
|
2014-12-09 02:22:08 +00:00
|
|
|
}
|
|
|
|
if file == "" {
|
|
|
|
return dir
|
|
|
|
}
|
|
|
|
filePath = dir
|
|
|
|
}
|
|
|
|
}
|