github.com/klaytn/klaytn@v1.12.1/storage/database/s3filedb_test.go (about)

     1  // Copyright 2020 The klaytn Authors
     2  // This file is part of the klaytn library.
     3  //
     4  // The klaytn library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The klaytn library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the klaytn library. If not, see <http://www.gnu.org/licenses/>.
    16  //
    17  // For local test, please run the below.
    18  //    $ docker run -d -p 4566:4566 localstack/localstack:0.11.5
    19  
    20  package database
    21  
    22  import (
    23  	"bytes"
    24  	"strings"
    25  	"testing"
    26  
    27  	"github.com/aws/aws-sdk-go/aws"
    28  	"github.com/aws/aws-sdk-go/service/s3"
    29  	"github.com/klaytn/klaytn/common"
    30  	"github.com/klaytn/klaytn/storage"
    31  	"github.com/stretchr/testify/suite"
    32  )
    33  
    34  type SuiteS3FileDB struct {
    35  	suite.Suite
    36  	s3DB           *s3FileDB
    37  	testBucketName *string
    38  }
    39  
    40  func (s *SuiteS3FileDB) SetupSuite() {
    41  	// use local test configs
    42  	region := "us-east-1"
    43  	endpoint := "http://localhost:4566"
    44  	testBucketName := aws.String("test-bucket")
    45  
    46  	s3DB, err := newS3FileDB(region, endpoint, *testBucketName)
    47  	if err != nil {
    48  		s.Fail("failed to create s3Database", "err", err)
    49  	}
    50  
    51  	_, err = s3DB.s3.DeleteBucketPolicy(&s3.DeleteBucketPolicyInput{Bucket: testBucketName})
    52  	if err != nil {
    53  		s.Fail("failed to delete the bucket policy for the test", "err", err, "bucketName", *testBucketName)
    54  	}
    55  
    56  	if err != nil {
    57  		s.Fail("failed to create the test bucket", "err", err)
    58  	}
    59  
    60  	s.s3DB = s3DB
    61  	s.testBucketName = testBucketName
    62  }
    63  
    64  func (s *SuiteS3FileDB) TearDownSuite() {
    65  	if _, err := s.s3DB.s3.DeleteBucket(&s3.DeleteBucketInput{Bucket: s.testBucketName}); err != nil {
    66  		s.Fail("failed to delete the test bucket", "err", err, "bucketName", *s.testBucketName)
    67  	}
    68  }
    69  
    70  func TestSuiteS3FileDB(t *testing.T) {
    71  	storage.SkipLocalTest(t)
    72  
    73  	suite.Run(t, new(SuiteS3FileDB))
    74  }
    75  
    76  func (s *SuiteS3FileDB) TestS3FileDB() {
    77  	testKey := common.MakeRandomBytes(32)
    78  	testVal := common.MakeRandomBytes(1024 * 1024)
    79  
    80  	_, err := s.s3DB.read(testKey)
    81  	if err == nil || !strings.Contains(err.Error(), s3.ErrCodeNoSuchKey) {
    82  		s.Fail("test key already exist", "bucketName", *s.testBucketName)
    83  	}
    84  
    85  	uris, err := s.s3DB.write(item{key: testKey, val: testVal})
    86  	if err != nil {
    87  		s.Fail("Failed to write", "err", err, "bucketName", *s.testBucketName)
    88  	}
    89  	defer s.s3DB.delete(testKey)
    90  
    91  	if uris == "" {
    92  		s.Fail("Unexpected amount of uris are returned", "len(uris)", len(uris))
    93  	}
    94  
    95  	val, err := s.s3DB.read(testKey)
    96  	if err != nil {
    97  		s.Fail("Failed to read", "err", err, "bucketName", *s.testBucketName)
    98  	}
    99  	s.True(bytes.Equal(testVal, val))
   100  
   101  	if s.s3DB.delete(testKey) != nil {
   102  		s.Fail("Failed to delete", "err", err, "bucketName", *s.testBucketName)
   103  	}
   104  }
   105  
   106  func (s *SuiteS3FileDB) TestS3FileDB_Overwrite() {
   107  	testKey := common.MakeRandomBytes(32)
   108  	var testVals [][]byte
   109  	for i := 0; i < 10; i++ {
   110  		testVals = append(testVals, common.MakeRandomBytes(1024*1024))
   111  	}
   112  
   113  	_, err := s.s3DB.read(testKey)
   114  	if err == nil || !strings.Contains(err.Error(), s3.ErrCodeNoSuchKey) {
   115  		s.Fail("test key already exist", "bucketName", *s.testBucketName)
   116  	}
   117  
   118  	var uris []string
   119  	for _, testVal := range testVals {
   120  		uri, err := s.s3DB.write(item{key: testKey, val: testVal})
   121  		if err != nil {
   122  			s.Fail("failed to write the data to s3FileDB", "err", err)
   123  		}
   124  		uris = append(uris, uri)
   125  	}
   126  	defer s.s3DB.delete(testKey)
   127  
   128  	returnedVal, err := s.s3DB.read(testKey)
   129  	if err != nil {
   130  		s.Fail("failed to read the data from s3FileDB", "err", err)
   131  	}
   132  
   133  	s.Equal(testVals[len(testVals)-1], returnedVal)
   134  	s.Equal(len(testVals), len(uris))
   135  }
   136  
   137  func (s *SuiteS3FileDB) TestS3FileDB_EmptyDelete() {
   138  	testKey := common.MakeRandomBytes(256)
   139  	s.NoError(s.s3DB.delete(testKey))
   140  	s.NoError(s.s3DB.delete(testKey))
   141  }