0
0
mirror of https://github.com/thegeeklab/wp-s3-action.git synced 2024-06-02 18:39:42 +02:00

fix S3List test and simplify method

This commit is contained in:
Robert Kaussow 2024-05-12 10:39:45 +02:00
parent 7fde17a11d
commit 6f16a196d6
Signed by: xoxys
GPG Key ID: 4E692A2EAECC03C0
3 changed files with 198 additions and 112 deletions

84
aws/cloudfront_test.go Normal file
View File

@ -0,0 +1,84 @@
package aws
import (
"context"
"errors"
"testing"
"github.com/aws/aws-sdk-go-v2/service/cloudfront"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/thegeeklab/wp-s3-action/aws/mocks"
)
func TestCloudfront_Invalidate(t *testing.T) {
t.Parallel()
tests := []struct {
name string
setup func(t *testing.T) (*Cloudfront, CloudfrontInvalidateOpt, func())
wantErr bool
}{
{
name: "invalidate path successfully",
setup: func(t *testing.T) (*Cloudfront, CloudfrontInvalidateOpt, func()) {
t.Helper()
mockClient := mocks.NewMockCloudfrontAPIClient(t)
mockClient.
On("CreateInvalidation", mock.Anything, mock.Anything).
Return(&cloudfront.CreateInvalidationOutput{}, nil)
return &Cloudfront{
client: mockClient,
Distribution: "test-distribution",
}, CloudfrontInvalidateOpt{
Path: "/path/to/invalidate",
}, func() {
mockClient.AssertExpectations(t)
}
},
wantErr: false,
},
{
name: "error when create invalidation fails",
setup: func(t *testing.T) (*Cloudfront, CloudfrontInvalidateOpt, func()) {
t.Helper()
mockClient := mocks.NewMockCloudfrontAPIClient(t)
mockClient.
On("CreateInvalidation", mock.Anything, mock.Anything).
Return(&cloudfront.CreateInvalidationOutput{}, errors.New("create invalidation failed"))
return &Cloudfront{
client: mockClient,
Distribution: "test-distribution",
}, CloudfrontInvalidateOpt{
Path: "/path/to/invalidate",
}, func() {
mockClient.AssertExpectations(t)
}
},
wantErr: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
cf, opt, teardown := tt.setup(t)
defer teardown()
err := cf.Invalidate(context.Background(), opt)
if tt.wantErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
})
}
}

View File

@ -361,26 +361,15 @@ func (u *S3) Delete(ctx context.Context, opt S3DeleteOptions) error {
// List retrieves a list of object keys in the S3 bucket under the specified path.
func (u *S3) List(ctx context.Context, opt S3ListOptions) ([]string, error) {
remote := make([]string, 0)
var remote []string
resp, err := u.client.ListObjects(ctx, &s3.ListObjectsInput{
input := &s3.ListObjectsInput{
Bucket: aws.String(u.Bucket),
Prefix: aws.String(opt.Path),
})
if err != nil {
return remote, err
}
for _, item := range resp.Contents {
remote = append(remote, *item.Key)
}
for *resp.IsTruncated {
resp, err = u.client.ListObjects(ctx, &s3.ListObjectsInput{
Bucket: aws.String(u.Bucket),
Prefix: aws.String(opt.Path),
Marker: aws.String(remote[len(remote)-1]),
})
for {
resp, err := u.client.ListObjects(ctx, input)
if err != nil {
return remote, err
}
@ -388,6 +377,12 @@ func (u *S3) List(ctx context.Context, opt S3ListOptions) ([]string, error) {
for _, item := range resp.Contents {
remote = append(remote, *item.Key)
}
if !*resp.IsTruncated {
break
}
input.Marker = aws.String(remote[len(remote)-1])
}
return remote, nil

View File

@ -24,7 +24,7 @@ func createTempFile(t *testing.T, name string) string {
return name
}
func TestS3Uploader_Upload(t *testing.T) {
func TestS3_Upload(t *testing.T) {
t.Parallel()
tests := []struct {
@ -439,111 +439,118 @@ func TestS3_Delete(t *testing.T) {
}
}
// func TestS3_List(t *testing.T) {
// t.Parallel()
func TestS3_List(t *testing.T) {
t.Parallel()
// tests := []struct {
// name string
// setup func(t *testing.T) (*S3, S3ListOptions, func())
// wantErr bool
// want []string
// }{
// {
// name: "list objects in prefix",
// setup: func(t *testing.T) (*S3, S3ListOptions, func()) {
// t.Helper()
tests := []struct {
name string
setup func(t *testing.T) (*S3, S3ListOptions, func())
wantErr bool
want []string
}{
{
name: "list objects in prefix",
setup: func(t *testing.T) (*S3, S3ListOptions, func()) {
t.Helper()
// mockS3Client := mocks.NewMockS3APIClient(t)
// mockS3Client.On("ListObjects", mock.Anything, mock.Anything).Return(&s3.ListObjectsOutput{
// Contents: []types.Object{
// {Key: aws.String("prefix/file1.txt")},
// {Key: aws.String("prefix/file2.txt")},
// },
// }, nil)
mockS3Client := mocks.NewMockS3APIClient(t)
mockS3Client.On("ListObjects", mock.Anything, mock.Anything).Return(&s3.ListObjectsOutput{
Contents: []types.Object{
{Key: aws.String("prefix/file1.txt")},
{Key: aws.String("prefix/file2.txt")},
},
IsTruncated: aws.Bool(false),
}, nil)
// return &S3{
// client: mockS3Client,
// Bucket: "test-bucket",
// }, S3ListOptions{
// Path: "prefix/",
// }, func() {
// mockS3Client.AssertExpectations(t)
// }
// },
// wantErr: false,
// want: []string{"prefix/file1.txt", "prefix/file2.txt"},
// },
// {
// name: "list objects with pagination",
// setup: func(t *testing.T) (*S3, S3ListOptions, func()) {
// t.Helper()
return &S3{
client: mockS3Client,
Bucket: "test-bucket",
}, S3ListOptions{
Path: "prefix/",
}, func() {
mockS3Client.AssertExpectations(t)
}
},
wantErr: false,
want: []string{"prefix/file1.txt", "prefix/file2.txt"},
},
{
name: "list objects with pagination",
setup: func(t *testing.T) (*S3, S3ListOptions, func()) {
t.Helper()
// mockS3Client := mocks.NewMockS3APIClient(t)
// mockS3Client.On("ListObjects", mock.Anything, mock.MatchedBy(func(input *s3.ListObjectsInput) bool {
// return *input.Marker == ""
// })).Return(&s3.ListObjectsOutput{
// Contents: []types.Object{{Key: aws.String("prefix/file1.txt")}, {Key: aws.String("prefix/file2.txt")}},
// IsTruncated: aws.Bool(true),
// }, nil)
// mockS3Client.On("ListObjects", mock.Anything, mock.MatchedBy(func(input *s3.ListObjectsInput) bool {
// return *input.Marker == "prefix/file2.txt"
// })).Return(&s3.ListObjectsOutput{
// Contents: []types.Object{{Key: aws.String("prefix/file3.txt")}},
// }, nil)
mockS3Client := mocks.NewMockS3APIClient(t)
mockS3Client.On("ListObjects", mock.Anything, mock.MatchedBy(func(input *s3.ListObjectsInput) bool {
return input.Marker == nil
})).Return(&s3.ListObjectsOutput{
Contents: []types.Object{
{Key: aws.String("prefix/file1.txt")},
{Key: aws.String("prefix/file2.txt")},
},
IsTruncated: aws.Bool(true),
}, nil)
mockS3Client.On("ListObjects", mock.Anything, mock.MatchedBy(func(input *s3.ListObjectsInput) bool {
return *input.Marker == "prefix/file2.txt"
})).Return(&s3.ListObjectsOutput{
Contents: []types.Object{
{Key: aws.String("prefix/file3.txt")},
},
IsTruncated: aws.Bool(false),
}, nil)
// return &S3{
// client: mockS3Client,
// Bucket: "test-bucket",
// }, S3ListOptions{
// Path: "prefix/",
// }, func() {
// mockS3Client.AssertExpectations(t)
// }
// },
// wantErr: false,
// want: []string{"prefix/file1.txt", "prefix/file2.txt", "prefix/file3.txt"},
// },
// {
// name: "error when list objects fails",
// setup: func(t *testing.T) (*S3, S3ListOptions, func()) {
// t.Helper()
return &S3{
client: mockS3Client,
Bucket: "test-bucket",
}, S3ListOptions{
Path: "prefix/",
}, func() {
mockS3Client.AssertExpectations(t)
}
},
wantErr: false,
want: []string{"prefix/file1.txt", "prefix/file2.txt", "prefix/file3.txt"},
},
{
name: "error when list objects fails",
setup: func(t *testing.T) (*S3, S3ListOptions, func()) {
t.Helper()
// mockS3Client := mocks.NewMockS3APIClient(t)
// mockS3Client.
// On("ListObjects", mock.Anything, mock.Anything).
// Return(&s3.ListObjectsOutput{}, errors.New("list objects failed"))
mockS3Client := mocks.NewMockS3APIClient(t)
mockS3Client.
On("ListObjects", mock.Anything, mock.Anything).
Return(&s3.ListObjectsOutput{}, errors.New("list objects failed"))
// return &S3{
// client: mockS3Client,
// Bucket: "test-bucket",
// }, S3ListOptions{
// Path: "prefix/",
// }, func() {
// mockS3Client.AssertExpectations(t)
// }
// },
// wantErr: true,
// want: nil,
// },
// }
return &S3{
client: mockS3Client,
Bucket: "test-bucket",
}, S3ListOptions{
Path: "prefix/",
}, func() {
mockS3Client.AssertExpectations(t)
}
},
wantErr: true,
want: nil,
},
}
// for _, tt := range tests {
// tt := tt
// t.Run(tt.name, func(t *testing.T) {
// t.Parallel()
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
// s3, opt, teardown := tt.setup(t)
// defer teardown()
s3, opt, teardown := tt.setup(t)
defer teardown()
// got, err := s3.List(context.Background(), opt)
// if tt.wantErr {
// assert.Error(t, err)
got, err := s3.List(context.Background(), opt)
if tt.wantErr {
assert.Error(t, err)
// return
// }
return
}
// assert.NoError(t, err)
// assert.ElementsMatch(t, tt.want, got)
// })
// }
// }
assert.NoError(t, err)
assert.ElementsMatch(t, tt.want, got)
})
}
}