github.com/grailbio/base@v0.0.11/file/s3file/list_bucket_test.go (about)

     1  package s3file
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	awsrequest "github.com/aws/aws-sdk-go/aws/request"
     9  	"github.com/aws/aws-sdk-go/service/s3"
    10  	"github.com/aws/aws-sdk-go/service/s3/s3iface"
    11  	"github.com/grailbio/testutil/assert"
    12  )
    13  
    14  func TestS3BucketLister(t *testing.T) {
    15  	lister := s3BucketLister{
    16  		ctx:    context.Background(),
    17  		scheme: "s3",
    18  		clients: []s3iface.S3API{
    19  			listBucketsFakeClient{},
    20  			listBucketsFakeClient{
    21  				buckets: []*s3.Bucket{
    22  					{Name: aws.String("bucketA")},
    23  					{Name: aws.String("bucketC")},
    24  				},
    25  			},
    26  			listBucketsFakeClient{
    27  				buckets: []*s3.Bucket{
    28  					{Name: aws.String("bucketC")},
    29  					{Name: aws.String("bucketB")},
    30  				},
    31  			},
    32  		},
    33  	}
    34  
    35  	assert.NoError(t, lister.Err())
    36  
    37  	assert.True(t, lister.Scan())
    38  	assert.EQ(t, lister.Path(), "s3://bucketA") // expect alphabetical order
    39  	assert.EQ(t, lister.IsDir(), true)
    40  	_ = lister.Info() // expect nothing, but it must not panic
    41  	assert.NoError(t, lister.Err())
    42  
    43  	assert.True(t, lister.Scan())
    44  	assert.EQ(t, lister.Path(), "s3://bucketB")
    45  	assert.EQ(t, lister.IsDir(), true)
    46  	_ = lister.Info()
    47  	assert.NoError(t, lister.Err())
    48  
    49  	assert.True(t, lister.Scan())
    50  	assert.EQ(t, lister.Path(), "s3://bucketC")
    51  	assert.EQ(t, lister.IsDir(), true)
    52  	_ = lister.Info()
    53  	assert.NoError(t, lister.Err())
    54  
    55  	assert.False(t, lister.Scan())
    56  	assert.NoError(t, lister.Err())
    57  }
    58  
    59  type listBucketsFakeClient struct {
    60  	buckets       []*s3.Bucket // stub response
    61  	s3iface.S3API              // all other methods panic with nil dereference
    62  }
    63  
    64  func (c listBucketsFakeClient) ListBucketsWithContext(
    65  	aws.Context, *s3.ListBucketsInput, ...awsrequest.Option,
    66  ) (*s3.ListBucketsOutput, error) {
    67  	return &s3.ListBucketsOutput{Buckets: c.buckets}, nil
    68  }