2014-10-24 23:37:25 +00:00
|
|
|
package s3
|
|
|
|
|
|
|
|
import (
|
2016-08-16 00:12:24 +00:00
|
|
|
"bytes"
|
2014-12-19 17:16:51 +00:00
|
|
|
"io/ioutil"
|
2016-08-16 00:12:24 +00:00
|
|
|
"math/rand"
|
2014-10-24 23:37:25 +00:00
|
|
|
"os"
|
2014-10-29 01:15:40 +00:00
|
|
|
"strconv"
|
2014-10-24 23:37:25 +00:00
|
|
|
"testing"
|
|
|
|
|
2016-08-16 00:12:24 +00:00
|
|
|
"gopkg.in/check.v1"
|
|
|
|
|
2016-01-22 02:17:53 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/s3"
|
|
|
|
|
2015-04-27 22:58:58 +00:00
|
|
|
"github.com/docker/distribution/context"
|
2015-02-11 02:14:23 +00:00
|
|
|
storagedriver "github.com/docker/distribution/registry/storage/driver"
|
|
|
|
"github.com/docker/distribution/registry/storage/driver/testsuites"
|
2014-10-24 23:37:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Hook up gocheck into the "go test" runner.
|
2014-11-17 23:44:07 +00:00
|
|
|
func Test(t *testing.T) { check.TestingT(t) }
|
2014-10-24 23:37:25 +00:00
|
|
|
|
2016-01-22 02:17:53 +00:00
|
|
|
var s3DriverConstructor func(rootDirectory, storageClass string) (*Driver, error)
|
2015-06-29 23:39:45 +00:00
|
|
|
var skipS3 func() string
|
2015-02-20 00:28:32 +00:00
|
|
|
|
2014-10-24 23:37:25 +00:00
|
|
|
func init() {
|
2014-10-29 01:15:40 +00:00
|
|
|
accessKey := os.Getenv("AWS_ACCESS_KEY")
|
|
|
|
secretKey := os.Getenv("AWS_SECRET_KEY")
|
2014-10-24 23:37:25 +00:00
|
|
|
bucket := os.Getenv("S3_BUCKET")
|
|
|
|
encrypt := os.Getenv("S3_ENCRYPT")
|
2016-03-10 00:52:59 +00:00
|
|
|
keyID := os.Getenv("S3_KEY_ID")
|
2014-12-29 20:29:54 +00:00
|
|
|
secure := os.Getenv("S3_SECURE")
|
2016-09-01 20:52:40 +00:00
|
|
|
v4Auth := os.Getenv("S3_V4_AUTH")
|
2014-12-19 17:16:51 +00:00
|
|
|
region := os.Getenv("AWS_REGION")
|
2016-10-06 00:47:12 +00:00
|
|
|
objectACL := os.Getenv("S3_OBJECT_ACL")
|
2014-12-19 17:16:51 +00:00
|
|
|
root, err := ioutil.TempDir("", "driver-")
|
2016-03-05 18:46:44 +00:00
|
|
|
regionEndpoint := os.Getenv("REGION_ENDPOINT")
|
2014-12-19 17:16:51 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2015-01-07 10:18:42 +00:00
|
|
|
defer os.Remove(root)
|
2014-10-24 23:37:25 +00:00
|
|
|
|
2016-01-22 02:17:53 +00:00
|
|
|
s3DriverConstructor = func(rootDirectory, storageClass string) (*Driver, error) {
|
2015-01-07 10:18:42 +00:00
|
|
|
encryptBool := false
|
2015-01-07 09:45:31 +00:00
|
|
|
if encrypt != "" {
|
|
|
|
encryptBool, err = strconv.ParseBool(encrypt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-10-29 01:15:40 +00:00
|
|
|
}
|
2014-12-29 20:29:54 +00:00
|
|
|
|
|
|
|
secureBool := true
|
|
|
|
if secure != "" {
|
|
|
|
secureBool, err = strconv.ParseBool(secure)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2015-01-07 09:45:31 +00:00
|
|
|
|
2016-09-01 20:52:40 +00:00
|
|
|
v4Bool := true
|
|
|
|
if v4Auth != "" {
|
|
|
|
v4Bool, err = strconv.ParseBool(v4Auth)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-07 10:18:42 +00:00
|
|
|
parameters := DriverParameters{
|
|
|
|
accessKey,
|
|
|
|
secretKey,
|
|
|
|
bucket,
|
2016-01-22 02:17:53 +00:00
|
|
|
region,
|
2016-03-05 18:46:44 +00:00
|
|
|
regionEndpoint,
|
2015-01-07 10:18:42 +00:00
|
|
|
encryptBool,
|
2016-03-10 00:52:59 +00:00
|
|
|
keyID,
|
2015-01-07 10:18:42 +00:00
|
|
|
secureBool,
|
2016-09-01 20:52:40 +00:00
|
|
|
v4Bool,
|
2015-01-24 00:46:43 +00:00
|
|
|
minChunkSize,
|
2016-08-16 00:12:24 +00:00
|
|
|
defaultMultipartCopyChunkSize,
|
|
|
|
defaultMultipartCopyMaxConcurrency,
|
|
|
|
defaultMultipartCopyThresholdSize,
|
2015-02-20 00:28:32 +00:00
|
|
|
rootDirectory,
|
2016-02-01 23:34:36 +00:00
|
|
|
storageClass,
|
2016-01-22 02:17:53 +00:00
|
|
|
driverName + "-test",
|
2016-10-06 00:47:12 +00:00
|
|
|
objectACL,
|
2015-01-07 10:18:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return New(parameters)
|
2014-10-24 23:37:25 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 01:15:40 +00:00
|
|
|
// Skip S3 storage driver tests if environment variable parameters are not provided
|
2015-06-29 23:39:45 +00:00
|
|
|
skipS3 = func() string {
|
2015-02-04 19:39:41 +00: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 20:24:07 +00:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2015-06-29 23:39:45 +00:00
|
|
|
testsuites.RegisterSuite(func() (storagedriver.StorageDriver, error) {
|
2016-01-22 02:17:53 +00:00
|
|
|
return s3DriverConstructor(root, s3.StorageClassStandard)
|
2015-06-29 23:39:45 +00:00
|
|
|
}, skipS3)
|
2015-02-20 00:28:32 +00:00
|
|
|
}
|
|
|
|
|
2015-06-29 23:39:45 +00:00
|
|
|
func TestEmptyRootList(t *testing.T) {
|
|
|
|
if skipS3() != "" {
|
|
|
|
t.Skip(skipS3())
|
2015-02-20 00:28:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
validRoot, err := ioutil.TempDir("", "driver-")
|
2015-06-29 23:39:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating temporary directory: %v", err)
|
|
|
|
}
|
2015-02-20 00:28:32 +00:00
|
|
|
defer os.Remove(validRoot)
|
|
|
|
|
2016-01-22 02:17:53 +00:00
|
|
|
rootedDriver, err := s3DriverConstructor(validRoot, s3.StorageClassStandard)
|
2015-06-29 23:39:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating rooted driver: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:17:53 +00:00
|
|
|
emptyRootDriver, err := s3DriverConstructor("", s3.StorageClassStandard)
|
2015-06-29 23:39:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating empty root driver: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:17:53 +00:00
|
|
|
slashRootDriver, err := s3DriverConstructor("/", s3.StorageClassStandard)
|
2015-06-29 23:39:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating slash root driver: %v", err)
|
|
|
|
}
|
2015-02-20 00:28:32 +00:00
|
|
|
|
|
|
|
filename := "/test"
|
|
|
|
contents := []byte("contents")
|
2015-04-27 22:58:58 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
err = rootedDriver.PutContent(ctx, filename, contents)
|
2015-06-29 23:39:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating content: %v", err)
|
|
|
|
}
|
2015-04-27 22:58:58 +00:00
|
|
|
defer rootedDriver.Delete(ctx, filename)
|
2015-02-20 00:28:32 +00:00
|
|
|
|
2015-04-27 22:58:58 +00:00
|
|
|
keys, err := emptyRootDriver.List(ctx, "/")
|
2015-02-20 00:28:32 +00:00
|
|
|
for _, path := range keys {
|
2015-06-29 23:39:45 +00:00
|
|
|
if !storagedriver.PathRegexp.MatchString(path) {
|
|
|
|
t.Fatalf("unexpected string in path: %q != %q", path, storagedriver.PathRegexp)
|
|
|
|
}
|
2015-02-20 00:28:32 +00:00
|
|
|
}
|
|
|
|
|
2015-04-27 22:58:58 +00:00
|
|
|
keys, err = slashRootDriver.List(ctx, "/")
|
2015-02-20 00:28:32 +00:00
|
|
|
for _, path := range keys {
|
2015-06-29 23:39:45 +00:00
|
|
|
if !storagedriver.PathRegexp.MatchString(path) {
|
|
|
|
t.Fatalf("unexpected string in path: %q != %q", path, storagedriver.PathRegexp)
|
|
|
|
}
|
2015-02-20 00:28:32 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-01 23:34:36 +00:00
|
|
|
|
|
|
|
func TestStorageClass(t *testing.T) {
|
|
|
|
if skipS3() != "" {
|
|
|
|
t.Skip(skipS3())
|
|
|
|
}
|
|
|
|
|
|
|
|
rootDir, err := ioutil.TempDir("", "driver-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating temporary directory: %v", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(rootDir)
|
|
|
|
|
2016-01-22 02:17:53 +00:00
|
|
|
standardDriver, err := s3DriverConstructor(rootDir, s3.StorageClassStandard)
|
2016-02-01 23:34:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating driver with standard storage: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:17:53 +00:00
|
|
|
rrDriver, err := s3DriverConstructor(rootDir, s3.StorageClassReducedRedundancy)
|
2016-02-01 23:34:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating driver with reduced redundancy storage: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-09-01 20:52:40 +00:00
|
|
|
if _, err = s3DriverConstructor(rootDir, noStorageClass); err != nil {
|
|
|
|
t.Fatalf("unexpected error creating driver without storage class: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-02-01 23:34:36 +00:00
|
|
|
standardFilename := "/test-standard"
|
|
|
|
rrFilename := "/test-rr"
|
|
|
|
contents := []byte("contents")
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
err = standardDriver.PutContent(ctx, standardFilename, contents)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating content: %v", err)
|
|
|
|
}
|
|
|
|
defer standardDriver.Delete(ctx, standardFilename)
|
|
|
|
|
|
|
|
err = rrDriver.PutContent(ctx, rrFilename, contents)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating content: %v", err)
|
|
|
|
}
|
|
|
|
defer rrDriver.Delete(ctx, rrFilename)
|
|
|
|
|
|
|
|
standardDriverUnwrapped := standardDriver.Base.StorageDriver.(*driver)
|
2016-01-22 02:17:53 +00:00
|
|
|
resp, err := standardDriverUnwrapped.S3.GetObject(&s3.GetObjectInput{
|
|
|
|
Bucket: aws.String(standardDriverUnwrapped.Bucket),
|
|
|
|
Key: aws.String(standardDriverUnwrapped.s3Path(standardFilename)),
|
|
|
|
})
|
2016-02-01 23:34:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error retrieving standard storage file: %v", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
// Amazon only populates this header value for non-standard storage classes
|
2016-01-22 02:17:53 +00:00
|
|
|
if resp.StorageClass != nil {
|
|
|
|
t.Fatalf("unexpected storage class for standard file: %v", resp.StorageClass)
|
2016-02-01 23:34:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rrDriverUnwrapped := rrDriver.Base.StorageDriver.(*driver)
|
2016-01-22 02:17:53 +00:00
|
|
|
resp, err = rrDriverUnwrapped.S3.GetObject(&s3.GetObjectInput{
|
|
|
|
Bucket: aws.String(rrDriverUnwrapped.Bucket),
|
2016-02-17 01:48:07 +00:00
|
|
|
Key: aws.String(rrDriverUnwrapped.s3Path(rrFilename)),
|
2016-01-22 02:17:53 +00:00
|
|
|
})
|
2016-02-01 23:34:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error retrieving reduced-redundancy storage file: %v", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2016-01-22 02:17:53 +00:00
|
|
|
if resp.StorageClass == nil {
|
|
|
|
t.Fatalf("unexpected storage class for reduced-redundancy file: %v", s3.StorageClassStandard)
|
|
|
|
} else if *resp.StorageClass != s3.StorageClassReducedRedundancy {
|
|
|
|
t.Fatalf("unexpected storage class for reduced-redundancy file: %v", *resp.StorageClass)
|
2016-02-01 23:34:36 +00:00
|
|
|
}
|
2016-01-22 02:17:53 +00:00
|
|
|
|
2016-02-01 23:34:36 +00:00
|
|
|
}
|
2016-06-28 00:39:25 +00:00
|
|
|
|
|
|
|
func TestOverThousandBlobs(t *testing.T) {
|
|
|
|
if skipS3() != "" {
|
|
|
|
t.Skip(skipS3())
|
|
|
|
}
|
|
|
|
|
|
|
|
rootDir, err := ioutil.TempDir("", "driver-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating temporary directory: %v", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(rootDir)
|
|
|
|
|
|
|
|
standardDriver, err := s3DriverConstructor(rootDir, s3.StorageClassStandard)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating driver with standard storage: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
for i := 0; i < 1005; i++ {
|
|
|
|
filename := "/thousandfiletest/file" + strconv.Itoa(i)
|
|
|
|
contents := []byte("contents")
|
|
|
|
err = standardDriver.PutContent(ctx, filename, contents)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating content: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// cant actually verify deletion because read-after-delete is inconsistent, but can ensure no errors
|
|
|
|
err = standardDriver.Delete(ctx, "/thousandfiletest")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error deleting thousand files: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2016-08-16 00:12:24 +00:00
|
|
|
|
|
|
|
func TestMoveWithMultipartCopy(t *testing.T) {
|
|
|
|
if skipS3() != "" {
|
|
|
|
t.Skip(skipS3())
|
|
|
|
}
|
|
|
|
|
|
|
|
rootDir, err := ioutil.TempDir("", "driver-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating temporary directory: %v", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(rootDir)
|
|
|
|
|
|
|
|
d, err := s3DriverConstructor(rootDir, s3.StorageClassStandard)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating driver: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
sourcePath := "/source"
|
|
|
|
destPath := "/dest"
|
|
|
|
|
|
|
|
defer d.Delete(ctx, sourcePath)
|
|
|
|
defer d.Delete(ctx, destPath)
|
|
|
|
|
|
|
|
// An object larger than d's MultipartCopyThresholdSize will cause d.Move() to perform a multipart copy.
|
|
|
|
multipartCopyThresholdSize := d.baseEmbed.Base.StorageDriver.(*driver).MultipartCopyThresholdSize
|
|
|
|
contents := make([]byte, 2*multipartCopyThresholdSize)
|
|
|
|
rand.Read(contents)
|
|
|
|
|
|
|
|
err = d.PutContent(ctx, sourcePath, contents)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating content: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.Move(ctx, sourcePath, destPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error moving file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
received, err := d.GetContent(ctx, destPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error getting content: %v", err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(contents, received) {
|
|
|
|
t.Fatal("content differs")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = d.GetContent(ctx, sourcePath)
|
|
|
|
switch err.(type) {
|
|
|
|
case storagedriver.PathNotFoundError:
|
|
|
|
default:
|
|
|
|
t.Fatalf("unexpected error getting content: %v", err)
|
|
|
|
}
|
|
|
|
}
|