github.com/mika/distribution@v2.2.2-0.20160108133430-a75790e3d8e0+incompatible/registry/storage/driver/s3/s3_test.go (about)

     1  package s3
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"strconv"
     7  	"testing"
     8  
     9  	"github.com/AdRoll/goamz/aws"
    10  	"github.com/docker/distribution/context"
    11  	storagedriver "github.com/docker/distribution/registry/storage/driver"
    12  	"github.com/docker/distribution/registry/storage/driver/testsuites"
    13  
    14  	"gopkg.in/check.v1"
    15  )
    16  
    17  // Hook up gocheck into the "go test" runner.
    18  func Test(t *testing.T) { check.TestingT(t) }
    19  
    20  var s3DriverConstructor func(rootDirectory string) (*Driver, error)
    21  var skipS3 func() string
    22  
    23  func init() {
    24  	accessKey := os.Getenv("AWS_ACCESS_KEY")
    25  	secretKey := os.Getenv("AWS_SECRET_KEY")
    26  	bucket := os.Getenv("S3_BUCKET")
    27  	encrypt := os.Getenv("S3_ENCRYPT")
    28  	secure := os.Getenv("S3_SECURE")
    29  	v4auth := os.Getenv("S3_USE_V4_AUTH")
    30  	region := os.Getenv("AWS_REGION")
    31  	root, err := ioutil.TempDir("", "driver-")
    32  	if err != nil {
    33  		panic(err)
    34  	}
    35  	defer os.Remove(root)
    36  
    37  	s3DriverConstructor = func(rootDirectory string) (*Driver, error) {
    38  		encryptBool := false
    39  		if encrypt != "" {
    40  			encryptBool, err = strconv.ParseBool(encrypt)
    41  			if err != nil {
    42  				return nil, err
    43  			}
    44  		}
    45  
    46  		secureBool := true
    47  		if secure != "" {
    48  			secureBool, err = strconv.ParseBool(secure)
    49  			if err != nil {
    50  				return nil, err
    51  			}
    52  		}
    53  
    54  		v4AuthBool := false
    55  		if v4auth != "" {
    56  			v4AuthBool, err = strconv.ParseBool(v4auth)
    57  			if err != nil {
    58  				return nil, err
    59  			}
    60  		}
    61  
    62  		parameters := DriverParameters{
    63  			accessKey,
    64  			secretKey,
    65  			bucket,
    66  			aws.GetRegion(region),
    67  			encryptBool,
    68  			secureBool,
    69  			v4AuthBool,
    70  			minChunkSize,
    71  			rootDirectory,
    72  		}
    73  
    74  		return New(parameters)
    75  	}
    76  
    77  	// Skip S3 storage driver tests if environment variable parameters are not provided
    78  	skipS3 = func() string {
    79  		if accessKey == "" || secretKey == "" || region == "" || bucket == "" || encrypt == "" {
    80  			return "Must set AWS_ACCESS_KEY, AWS_SECRET_KEY, AWS_REGION, S3_BUCKET, and S3_ENCRYPT to run S3 tests"
    81  		}
    82  		return ""
    83  	}
    84  
    85  	testsuites.RegisterSuite(func() (storagedriver.StorageDriver, error) {
    86  		return s3DriverConstructor(root)
    87  	}, skipS3)
    88  }
    89  
    90  func TestEmptyRootList(t *testing.T) {
    91  	if skipS3() != "" {
    92  		t.Skip(skipS3())
    93  	}
    94  
    95  	validRoot, err := ioutil.TempDir("", "driver-")
    96  	if err != nil {
    97  		t.Fatalf("unexpected error creating temporary directory: %v", err)
    98  	}
    99  	defer os.Remove(validRoot)
   100  
   101  	rootedDriver, err := s3DriverConstructor(validRoot)
   102  	if err != nil {
   103  		t.Fatalf("unexpected error creating rooted driver: %v", err)
   104  	}
   105  
   106  	emptyRootDriver, err := s3DriverConstructor("")
   107  	if err != nil {
   108  		t.Fatalf("unexpected error creating empty root driver: %v", err)
   109  	}
   110  
   111  	slashRootDriver, err := s3DriverConstructor("/")
   112  	if err != nil {
   113  		t.Fatalf("unexpected error creating slash root driver: %v", err)
   114  	}
   115  
   116  	filename := "/test"
   117  	contents := []byte("contents")
   118  	ctx := context.Background()
   119  	err = rootedDriver.PutContent(ctx, filename, contents)
   120  	if err != nil {
   121  		t.Fatalf("unexpected error creating content: %v", err)
   122  	}
   123  	defer rootedDriver.Delete(ctx, filename)
   124  
   125  	keys, err := emptyRootDriver.List(ctx, "/")
   126  	for _, path := range keys {
   127  		if !storagedriver.PathRegexp.MatchString(path) {
   128  			t.Fatalf("unexpected string in path: %q != %q", path, storagedriver.PathRegexp)
   129  		}
   130  	}
   131  
   132  	keys, err = slashRootDriver.List(ctx, "/")
   133  	for _, path := range keys {
   134  		if !storagedriver.PathRegexp.MatchString(path) {
   135  			t.Fatalf("unexpected string in path: %q != %q", path, storagedriver.PathRegexp)
   136  		}
   137  	}
   138  }