...

Source file src/github.com/docker/distribution/registry/storage/driver/oss/oss_test.go

Documentation: github.com/docker/distribution/registry/storage/driver/oss

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

View as plain text