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

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