Skip to content

Commit

Permalink
Fix #409
Browse files Browse the repository at this point in the history
- Ignore directory (size <= 0) when listing bucket object
- Add test for empty directory
  • Loading branch information
Martin Guibert authored and lotoussa committed Apr 7, 2021
1 parent 589c908 commit 50386ae
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pkg/iac/terraform/state/enumerator/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package enumerator
import (
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
Expand Down Expand Up @@ -45,7 +46,9 @@ func (s *S3Enumerator) Enumerate() ([]string, error) {
}
err := s.client.ListObjectsV2Pages(input, func(output *s3.ListObjectsV2Output, lastPage bool) bool {
for _, metadata := range output.Contents {
keys = append(keys, strings.Join([]string{bucket, *metadata.Key}, "/"))
if aws.Int64Value(metadata.Size) > 0 {
keys = append(keys, strings.Join([]string{bucket, *metadata.Key}, "/"))
}
}
return !lastPage
})
Expand Down
48 changes: 48 additions & 0 deletions pkg/iac/terraform/state/enumerator/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,31 @@ func TestS3Enumerator_Enumerate(t *testing.T) {
Contents: []*s3.Object{
{
Key: awssdk.String("a/nested/prefix/state1"),
Size: awssdk.Int64(5),
},
{
Key: awssdk.String("a/nested/prefix/state2"),
Size: awssdk.Int64(2),
},
{
Key: awssdk.String("a/nested/prefix/state3"),
Size: awssdk.Int64(1),
},
},
}, false)
callback(&s3.ListObjectsV2Output{
Contents: []*s3.Object{
{
Key: awssdk.String("a/nested/prefix/state4"),
Size: awssdk.Int64(5),
},
{
Key: awssdk.String("a/nested/prefix/state5"),
Size: awssdk.Int64(5),
},
{
Key: awssdk.String("a/nested/prefix/state6"),
Size: awssdk.Int64(5),
},
},
}, true)
Expand All @@ -73,6 +79,48 @@ func TestS3Enumerator_Enumerate(t *testing.T) {
"bucket-name/a/nested/prefix/state6",
},
},
{
name: "test that directories objects are filtered",
config: config.SupplierConfig{
Path: "bucket-name/a/nested/prefix",
},
mocks: func(client *mocks.FakeS3) {
input := &s3.ListObjectsV2Input{
Bucket: awssdk.String("bucket-name"),
Prefix: awssdk.String("a/nested/prefix"),
}
client.On(
"ListObjectsV2Pages",
input,
mock.MatchedBy(func(callback func(res *s3.ListObjectsV2Output, lastPage bool) bool) bool {
callback(&s3.ListObjectsV2Output{
Contents: []*s3.Object{
{
Key: awssdk.String("a/nested/prefix/state1"),
Size: awssdk.Int64(0),
},
{
Key: awssdk.String("a/nested/prefix/state2"),
Size: nil,
},
{
Key: awssdk.String("a/nested/prefix/state3"),
Size: awssdk.Int64(-1),
},
{
Key: awssdk.String("a/nested/prefix/state4"),
Size: awssdk.Int64(1),
},
},
}, true)
return true
}),
).Return(nil)
},
want: []string{
"bucket-name/a/nested/prefix/state4",
},
},
{
name: "test when invalid config used",
config: config.SupplierConfig{
Expand Down

0 comments on commit 50386ae

Please sign in to comment.