github.com/StarfishStorage/goofys@v0.23.2-0.20200415030923-535558486b34/internal/aws_test.go (about)

     1  // Copyright 2016 Ka-Hing Cheung
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package internal
    16  
    17  import (
    18  	. "github.com/kahing/goofys/api/common"
    19  	. "gopkg.in/check.v1"
    20  
    21  	"fmt"
    22  	"syscall"
    23  	"time"
    24  )
    25  
    26  type AwsTest struct {
    27  	s3 *S3Backend
    28  }
    29  
    30  var _ = Suite(&AwsTest{})
    31  
    32  func (s *AwsTest) SetUpSuite(t *C) {
    33  	var err error
    34  	s.s3, err = NewS3("", &FlagStorage{}, &S3Config{
    35  		Region: "us-east-1",
    36  	})
    37  	t.Assert(err, IsNil)
    38  }
    39  
    40  func (s *AwsTest) TestRegionDetection(t *C) {
    41  	s.s3.bucket = "goofys-eu-west-1.kahing.xyz"
    42  
    43  	err, isAws := s.s3.detectBucketLocationByHEAD()
    44  	t.Assert(err, IsNil)
    45  	t.Assert(*s.s3.awsConfig.Region, Equals, "eu-west-1")
    46  	t.Assert(isAws, Equals, true)
    47  }
    48  
    49  func (s *AwsTest) TestBucket404(t *C) {
    50  	s.s3.bucket = RandStringBytesMaskImprSrc(64)
    51  
    52  	err, isAws := s.s3.detectBucketLocationByHEAD()
    53  	t.Assert(err, Equals, syscall.ENXIO)
    54  	t.Assert(isAws, Equals, true)
    55  }
    56  
    57  type S3BucketEventualConsistency struct {
    58  	*S3Backend
    59  }
    60  
    61  func (s *S3BucketEventualConsistency) Init(key string) (err error) {
    62  	// TODO: make Init return errno instead
    63  	NoSuchBucket := fmt.Sprintf("bucket %v does not exist", s.Bucket())
    64  
    65  	for i := 0; i < 10; i++ {
    66  		err = s.S3Backend.Init(key)
    67  		if err != nil && err.Error() == NoSuchBucket {
    68  			s3Log.Infof("waiting for bucket")
    69  			time.Sleep((time.Duration(i) + 1) * 2 * time.Second)
    70  		} else {
    71  			return
    72  		}
    73  	}
    74  
    75  	return
    76  }
    77  
    78  func (s *S3BucketEventualConsistency) ListBlobs(param *ListBlobsInput) (*ListBlobsOutput, error) {
    79  	for i := 0; i < 10; i++ {
    80  		res, err := s.S3Backend.ListBlobs(param)
    81  		switch err {
    82  		case syscall.ENXIO:
    83  			s3Log.Infof("waiting for bucket")
    84  			time.Sleep((time.Duration(i) + 1) * 2 * time.Second)
    85  		default:
    86  			return res, err
    87  		}
    88  	}
    89  
    90  	return nil, syscall.ENXIO
    91  }
    92  
    93  func (s *S3BucketEventualConsistency) DeleteBlob(param *DeleteBlobInput) (*DeleteBlobOutput, error) {
    94  	for i := 0; i < 10; i++ {
    95  		res, err := s.S3Backend.DeleteBlob(param)
    96  		switch err {
    97  		case syscall.ENXIO:
    98  			s3Log.Infof("waiting for bucket")
    99  			time.Sleep((time.Duration(i) + 1) * 2 * time.Second)
   100  		default:
   101  			return res, err
   102  		}
   103  	}
   104  
   105  	return nil, syscall.ENXIO
   106  }
   107  
   108  func (s *S3BucketEventualConsistency) DeleteBlobs(param *DeleteBlobsInput) (*DeleteBlobsOutput, error) {
   109  	for i := 0; i < 10; i++ {
   110  		res, err := s.S3Backend.DeleteBlobs(param)
   111  		switch err {
   112  		case syscall.ENXIO:
   113  			s3Log.Infof("waiting for bucket")
   114  			time.Sleep((time.Duration(i) + 1) * 2 * time.Second)
   115  		default:
   116  			return res, err
   117  		}
   118  	}
   119  
   120  	return nil, syscall.ENXIO
   121  }
   122  
   123  func (s *S3BucketEventualConsistency) CopyBlob(param *CopyBlobInput) (*CopyBlobOutput, error) {
   124  	for i := 0; i < 10; i++ {
   125  		res, err := s.S3Backend.CopyBlob(param)
   126  		switch err {
   127  		case syscall.ENXIO:
   128  			s3Log.Infof("waiting for bucket")
   129  			time.Sleep((time.Duration(i) + 1) * 2 * time.Second)
   130  		default:
   131  			return res, err
   132  		}
   133  	}
   134  
   135  	return nil, syscall.ENXIO
   136  }
   137  
   138  func (s *S3BucketEventualConsistency) PutBlob(param *PutBlobInput) (*PutBlobOutput, error) {
   139  	for i := 0; i < 10; i++ {
   140  		res, err := s.S3Backend.PutBlob(param)
   141  		switch err {
   142  		case syscall.ENXIO:
   143  			param.Body.Seek(0, 0)
   144  			s3Log.Infof("waiting for bucket")
   145  			time.Sleep((time.Duration(i) + 1) * 2 * time.Second)
   146  		default:
   147  			return res, err
   148  		}
   149  	}
   150  
   151  	return nil, syscall.ENXIO
   152  }
   153  
   154  func (s *S3BucketEventualConsistency) RemoveBucket(param *RemoveBucketInput) (*RemoveBucketOutput, error) {
   155  	for i := 0; i < 10; i++ {
   156  		res, err := s.S3Backend.RemoveBucket(param)
   157  		switch err {
   158  		case syscall.ENXIO:
   159  			s3Log.Infof("waiting for bucket")
   160  			time.Sleep((time.Duration(i) + 1) * 2 * time.Second)
   161  		default:
   162  			return res, err
   163  		}
   164  	}
   165  
   166  	return nil, syscall.ENXIO
   167  }