github.com/etecs-ru/gnomock@v0.13.2/preset/localstack/s3_test.go (about)

     1  package localstack_test
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/aws/credentials"
    10  	"github.com/aws/aws-sdk-go/aws/session"
    11  	"github.com/aws/aws-sdk-go/service/s3"
    12  	"github.com/etecs-ru/gnomock"
    13  	"github.com/etecs-ru/gnomock/preset/localstack"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  func TestWithS3Files(t *testing.T) {
    18  	// testdata/s3 includes 100 files in my-bucket/dir folder
    19  	p := localstack.Preset(
    20  		localstack.WithServices(localstack.S3),
    21  		localstack.WithS3Files("testdata/s3"),
    22  		localstack.WithVersion("0.11.0"),
    23  	)
    24  	c, err := gnomock.Start(p)
    25  
    26  	defer func() { require.NoError(t, gnomock.Stop(c)) }()
    27  
    28  	require.NoError(t, err)
    29  
    30  	s3Endpoint := fmt.Sprintf("http://%s/", c.Address(localstack.APIPort))
    31  	config := &aws.Config{
    32  		Region:           aws.String("us-east-1"),
    33  		Endpoint:         aws.String(s3Endpoint),
    34  		S3ForcePathStyle: aws.Bool(true),
    35  		Credentials:      credentials.NewStaticCredentials("a", "b", "c"),
    36  	}
    37  
    38  	sess, err := session.NewSession(config)
    39  	require.NoError(t, err)
    40  
    41  	svc := s3.New(sess)
    42  
    43  	// my-bucket is automatically created, and now includes 100 files
    44  	listInput := &s3.ListObjectsV2Input{
    45  		Bucket:  aws.String("my-bucket"),
    46  		MaxKeys: aws.Int64(40),
    47  	}
    48  	files, err := svc.ListObjectsV2(listInput)
    49  	require.NoError(t, err)
    50  	require.Len(t, files.Contents, 40)
    51  	require.True(t, *files.IsTruncated)
    52  
    53  	for _, f := range files.Contents {
    54  		require.True(t, strings.HasPrefix(*f.Key, "dir/f-"))
    55  	}
    56  
    57  	listInput = &s3.ListObjectsV2Input{
    58  		Bucket:            aws.String("my-bucket"),
    59  		ContinuationToken: files.NextContinuationToken,
    60  		MaxKeys:           aws.Int64(50),
    61  	}
    62  	files, err = svc.ListObjectsV2(listInput)
    63  	require.NoError(t, err)
    64  	require.Len(t, files.Contents, 50)
    65  	require.True(t, *files.IsTruncated)
    66  
    67  	for _, f := range files.Contents {
    68  		require.True(t, strings.HasPrefix(*f.Key, "dir/f-"))
    69  	}
    70  
    71  	// list last batch of files, only 10 left
    72  	listInput = &s3.ListObjectsV2Input{
    73  		Bucket:            aws.String("my-bucket"),
    74  		ContinuationToken: files.NextContinuationToken,
    75  		MaxKeys:           aws.Int64(100),
    76  	}
    77  	files, err = svc.ListObjectsV2(listInput)
    78  	require.NoError(t, err)
    79  	require.Len(t, files.Contents, 10)
    80  	require.False(t, *files.IsTruncated)
    81  
    82  	for _, f := range files.Contents {
    83  		require.True(t, strings.HasPrefix(*f.Key, "dir/f-"))
    84  	}
    85  }