2014-10-25 01:37:25 +02:00
|
|
|
package s3
|
|
|
|
|
|
|
|
import (
|
2014-12-19 18:16:51 +01:00
|
|
|
"io/ioutil"
|
2014-10-25 01:37:25 +02:00
|
|
|
"os"
|
2014-10-29 02:15:40 +01:00
|
|
|
"strconv"
|
2014-10-25 01:37:25 +02:00
|
|
|
"testing"
|
|
|
|
|
2015-02-02 20:03:20 +01:00
|
|
|
"github.com/AdRoll/goamz/aws"
|
2015-04-28 00:58:58 +02:00
|
|
|
"github.com/docker/distribution/context"
|
2015-02-11 03:14:23 +01:00
|
|
|
storagedriver "github.com/docker/distribution/registry/storage/driver"
|
|
|
|
"github.com/docker/distribution/registry/storage/driver/testsuites"
|
2014-11-18 00:44:07 +01:00
|
|
|
|
|
|
|
"gopkg.in/check.v1"
|
2014-10-25 01:37:25 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Hook up gocheck into the "go test" runner.
|
2014-11-18 00:44:07 +01:00
|
|
|
func Test(t *testing.T) { check.TestingT(t) }
|
2014-10-25 01:37:25 +02:00
|
|
|
|
2015-02-20 01:28:32 +01:00
|
|
|
type S3DriverConstructor func(rootDirectory string) (*Driver, error)
|
|
|
|
|
2014-10-25 01:37:25 +02:00
|
|
|
func init() {
|
2014-10-29 02:15:40 +01:00
|
|
|
accessKey := os.Getenv("AWS_ACCESS_KEY")
|
|
|
|
secretKey := os.Getenv("AWS_SECRET_KEY")
|
2014-10-25 01:37:25 +02:00
|
|
|
bucket := os.Getenv("S3_BUCKET")
|
|
|
|
encrypt := os.Getenv("S3_ENCRYPT")
|
2014-12-29 21:29:54 +01:00
|
|
|
secure := os.Getenv("S3_SECURE")
|
2015-01-07 10:45:31 +01:00
|
|
|
v4auth := os.Getenv("S3_USE_V4_AUTH")
|
2014-12-19 18:16:51 +01:00
|
|
|
region := os.Getenv("AWS_REGION")
|
|
|
|
root, err := ioutil.TempDir("", "driver-")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2015-01-07 11:18:42 +01:00
|
|
|
defer os.Remove(root)
|
2014-10-25 01:37:25 +02:00
|
|
|
|
2015-02-20 01:28:32 +01:00
|
|
|
s3DriverConstructor := func(rootDirectory string) (*Driver, error) {
|
2015-01-07 11:18:42 +01:00
|
|
|
encryptBool := false
|
2015-01-07 10:45:31 +01:00
|
|
|
if encrypt != "" {
|
|
|
|
encryptBool, err = strconv.ParseBool(encrypt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-10-29 02:15:40 +01:00
|
|
|
}
|
2014-12-29 21:29:54 +01:00
|
|
|
|
|
|
|
secureBool := true
|
|
|
|
if secure != "" {
|
|
|
|
secureBool, err = strconv.ParseBool(secure)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2015-01-07 10:45:31 +01:00
|
|
|
|
2015-02-20 01:28:32 +01:00
|
|
|
v4AuthBool := false
|
2015-01-07 10:45:31 +01:00
|
|
|
if v4auth != "" {
|
|
|
|
v4AuthBool, err = strconv.ParseBool(v4auth)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2015-01-07 11:18:42 +01:00
|
|
|
|
|
|
|
parameters := DriverParameters{
|
|
|
|
accessKey,
|
|
|
|
secretKey,
|
|
|
|
bucket,
|
2015-02-20 01:28:32 +01:00
|
|
|
aws.GetRegion(region),
|
2015-01-07 11:18:42 +01:00
|
|
|
encryptBool,
|
|
|
|
secureBool,
|
|
|
|
v4AuthBool,
|
2015-01-24 01:46:43 +01:00
|
|
|
minChunkSize,
|
2015-02-20 01:28:32 +01:00
|
|
|
rootDirectory,
|
2015-01-07 11:18:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return New(parameters)
|
2014-10-25 01:37:25 +02:00
|
|
|
}
|
|
|
|
|
2014-10-29 02:15:40 +01:00
|
|
|
// Skip S3 storage driver tests if environment variable parameters are not provided
|
2014-10-27 21:24:07 +01:00
|
|
|
skipCheck := func() string {
|
2015-02-04 20:39:41 +01:00
|
|
|
if accessKey == "" || secretKey == "" || region == "" || bucket == "" || encrypt == "" {
|
|
|
|
return "Must set AWS_ACCESS_KEY, AWS_SECRET_KEY, AWS_REGION, S3_BUCKET, and S3_ENCRYPT to run S3 tests"
|
2014-10-27 21:24:07 +01:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2015-02-20 01:28:32 +01:00
|
|
|
driverConstructor := func() (storagedriver.StorageDriver, error) {
|
|
|
|
return s3DriverConstructor(root)
|
|
|
|
}
|
|
|
|
|
|
|
|
testsuites.RegisterInProcessSuite(driverConstructor, skipCheck)
|
|
|
|
|
|
|
|
// s3Constructor := func() (*Driver, error) {
|
|
|
|
// return s3DriverConstructor(aws.GetRegion(region))
|
|
|
|
// }
|
|
|
|
|
|
|
|
RegisterS3DriverSuite(s3DriverConstructor, skipCheck)
|
2014-12-19 18:16:51 +01:00
|
|
|
|
|
|
|
// testsuites.RegisterIPCSuite(driverName, map[string]string{
|
|
|
|
// "accesskey": accessKey,
|
|
|
|
// "secretkey": secretKey,
|
|
|
|
// "region": region.Name,
|
|
|
|
// "bucket": bucket,
|
|
|
|
// "encrypt": encrypt,
|
|
|
|
// }, skipCheck)
|
|
|
|
// }
|
2014-10-25 01:37:25 +02:00
|
|
|
}
|
2015-02-20 01:28:32 +01:00
|
|
|
|
|
|
|
func RegisterS3DriverSuite(s3DriverConstructor S3DriverConstructor, skipCheck testsuites.SkipCheck) {
|
|
|
|
check.Suite(&S3DriverSuite{
|
|
|
|
Constructor: s3DriverConstructor,
|
|
|
|
SkipCheck: skipCheck,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type S3DriverSuite struct {
|
|
|
|
Constructor S3DriverConstructor
|
|
|
|
testsuites.SkipCheck
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *S3DriverSuite) SetUpSuite(c *check.C) {
|
|
|
|
if reason := suite.SkipCheck(); reason != "" {
|
|
|
|
c.Skip(reason)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *S3DriverSuite) TestEmptyRootList(c *check.C) {
|
|
|
|
validRoot, err := ioutil.TempDir("", "driver-")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
defer os.Remove(validRoot)
|
|
|
|
|
|
|
|
rootedDriver, err := suite.Constructor(validRoot)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
emptyRootDriver, err := suite.Constructor("")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
slashRootDriver, err := suite.Constructor("/")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
filename := "/test"
|
|
|
|
contents := []byte("contents")
|
2015-04-28 00:58:58 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
err = rootedDriver.PutContent(ctx, filename, contents)
|
2015-02-20 01:28:32 +01:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-04-28 00:58:58 +02:00
|
|
|
defer rootedDriver.Delete(ctx, filename)
|
2015-02-20 01:28:32 +01:00
|
|
|
|
2015-04-28 00:58:58 +02:00
|
|
|
keys, err := emptyRootDriver.List(ctx, "/")
|
2015-02-20 01:28:32 +01:00
|
|
|
for _, path := range keys {
|
|
|
|
c.Assert(storagedriver.PathRegexp.MatchString(path), check.Equals, true)
|
|
|
|
}
|
|
|
|
|
2015-04-28 00:58:58 +02:00
|
|
|
keys, err = slashRootDriver.List(ctx, "/")
|
2015-02-20 01:28:32 +01:00
|
|
|
for _, path := range keys {
|
|
|
|
c.Assert(storagedriver.PathRegexp.MatchString(path), check.Equals, true)
|
|
|
|
}
|
|
|
|
}
|