2015-02-11 03:14:23 +01:00
|
|
|
package s3
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
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"
|
2016-01-21 01:40:58 +01:00
|
|
|
"github.com/docker/goamz/aws"
|
2016-01-29 00:48:49 +01:00
|
|
|
"github.com/docker/goamz/s3"
|
2015-02-11 03:14:23 +01:00
|
|
|
|
|
|
|
"gopkg.in/check.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Hook up gocheck into the "go test" runner.
|
|
|
|
func Test(t *testing.T) { check.TestingT(t) }
|
|
|
|
|
2016-02-02 00:34:36 +01:00
|
|
|
var s3DriverConstructor func(rootDirectory string, storageClass s3.StorageClass) (*Driver, error)
|
2015-06-30 01:39:45 +02:00
|
|
|
var skipS3 func() string
|
2015-02-20 01:28:32 +01:00
|
|
|
|
2015-02-11 03:14:23 +01:00
|
|
|
func init() {
|
|
|
|
accessKey := os.Getenv("AWS_ACCESS_KEY")
|
|
|
|
secretKey := os.Getenv("AWS_SECRET_KEY")
|
|
|
|
bucket := os.Getenv("S3_BUCKET")
|
|
|
|
encrypt := os.Getenv("S3_ENCRYPT")
|
|
|
|
secure := os.Getenv("S3_SECURE")
|
|
|
|
v4auth := os.Getenv("S3_USE_V4_AUTH")
|
|
|
|
region := os.Getenv("AWS_REGION")
|
|
|
|
root, err := ioutil.TempDir("", "driver-")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer os.Remove(root)
|
|
|
|
|
2016-02-02 00:34:36 +01:00
|
|
|
s3DriverConstructor = func(rootDirectory string, storageClass s3.StorageClass) (*Driver, error) {
|
2015-02-11 03:14:23 +01:00
|
|
|
encryptBool := false
|
|
|
|
if encrypt != "" {
|
|
|
|
encryptBool, err = strconv.ParseBool(encrypt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
secureBool := true
|
|
|
|
if secure != "" {
|
|
|
|
secureBool, err = strconv.ParseBool(secure)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 01:28:32 +01:00
|
|
|
v4AuthBool := false
|
2015-02-11 03:14:23 +01:00
|
|
|
if v4auth != "" {
|
|
|
|
v4AuthBool, err = strconv.ParseBool(v4auth)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parameters := DriverParameters{
|
|
|
|
accessKey,
|
|
|
|
secretKey,
|
|
|
|
bucket,
|
2015-02-20 01:28:32 +01:00
|
|
|
aws.GetRegion(region),
|
2015-02-11 03:14:23 +01:00
|
|
|
encryptBool,
|
|
|
|
secureBool,
|
|
|
|
v4AuthBool,
|
|
|
|
minChunkSize,
|
2015-02-20 01:28:32 +01:00
|
|
|
rootDirectory,
|
2016-02-02 00:34:36 +01:00
|
|
|
storageClass,
|
2016-01-21 01:40:58 +01:00
|
|
|
"",
|
2015-02-11 03:14:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return New(parameters)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip S3 storage driver tests if environment variable parameters are not provided
|
2015-06-30 01:39:45 +02:00
|
|
|
skipS3 = func() string {
|
2015-02-11 03:14:23 +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"
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2015-06-30 01:39:45 +02:00
|
|
|
testsuites.RegisterSuite(func() (storagedriver.StorageDriver, error) {
|
2016-02-02 00:34:36 +01:00
|
|
|
return s3DriverConstructor(root, s3.StandardStorage)
|
2015-06-30 01:39:45 +02:00
|
|
|
}, skipS3)
|
2015-02-20 01:28:32 +01:00
|
|
|
}
|
|
|
|
|
2015-06-30 01:39:45 +02:00
|
|
|
func TestEmptyRootList(t *testing.T) {
|
|
|
|
if skipS3() != "" {
|
|
|
|
t.Skip(skipS3())
|
2015-02-20 01:28:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
validRoot, err := ioutil.TempDir("", "driver-")
|
2015-06-30 01:39:45 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating temporary directory: %v", err)
|
|
|
|
}
|
2015-02-20 01:28:32 +01:00
|
|
|
defer os.Remove(validRoot)
|
|
|
|
|
2016-02-02 00:34:36 +01:00
|
|
|
rootedDriver, err := s3DriverConstructor(validRoot, s3.StandardStorage)
|
2015-06-30 01:39:45 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating rooted driver: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-02-02 00:34:36 +01:00
|
|
|
emptyRootDriver, err := s3DriverConstructor("", s3.StandardStorage)
|
2015-06-30 01:39:45 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating empty root driver: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-02-02 00:34:36 +01:00
|
|
|
slashRootDriver, err := s3DriverConstructor("/", s3.StandardStorage)
|
2015-06-30 01:39:45 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating slash root driver: %v", err)
|
|
|
|
}
|
2015-02-20 01:28:32 +01:00
|
|
|
|
|
|
|
filename := "/test"
|
|
|
|
contents := []byte("contents")
|
2015-04-28 00:58:58 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
err = rootedDriver.PutContent(ctx, filename, contents)
|
2015-06-30 01:39:45 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating content: %v", err)
|
|
|
|
}
|
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 {
|
2015-06-30 01:39:45 +02:00
|
|
|
if !storagedriver.PathRegexp.MatchString(path) {
|
|
|
|
t.Fatalf("unexpected string in path: %q != %q", path, storagedriver.PathRegexp)
|
|
|
|
}
|
2015-02-20 01:28:32 +01:00
|
|
|
}
|
|
|
|
|
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 {
|
2015-06-30 01:39:45 +02:00
|
|
|
if !storagedriver.PathRegexp.MatchString(path) {
|
|
|
|
t.Fatalf("unexpected string in path: %q != %q", path, storagedriver.PathRegexp)
|
|
|
|
}
|
2015-02-20 01:28:32 +01:00
|
|
|
}
|
|
|
|
}
|
2016-02-02 00:34:36 +01: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)
|
|
|
|
|
|
|
|
standardDriver, err := s3DriverConstructor(rootDir, s3.StandardStorage)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating driver with standard storage: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rrDriver, err := s3DriverConstructor(rootDir, s3.ReducedRedundancy)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error creating driver with reduced redundancy storage: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
resp, err := standardDriverUnwrapped.Bucket.GetResponse(standardDriverUnwrapped.s3Path(standardFilename))
|
|
|
|
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
|
|
|
|
if storageClass := resp.Header.Get("x-amz-storage-class"); storageClass != "" {
|
|
|
|
t.Fatalf("unexpected storage class for standard file: %v", storageClass)
|
|
|
|
}
|
|
|
|
|
|
|
|
rrDriverUnwrapped := rrDriver.Base.StorageDriver.(*driver)
|
|
|
|
resp, err = rrDriverUnwrapped.Bucket.GetResponse(rrDriverUnwrapped.s3Path(rrFilename))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error retrieving reduced-redundancy storage file: %v", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if storageClass := resp.Header.Get("x-amz-storage-class"); storageClass != string(s3.ReducedRedundancy) {
|
|
|
|
t.Fatalf("unexpected storage class for standard file: %v", storageClass)
|
|
|
|
}
|
|
|
|
}
|