Skip to content

Commit

Permalink
Merge pull request #9 from wolfeidau/feat_cleanup_mocking
Browse files Browse the repository at this point in the history
feat(testing): remove mocking library and follow SDK
  • Loading branch information
wolfeidau authored Sep 16, 2023
2 parents 9f8cb52 + d18c5b8 commit e433a43
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 189 deletions.
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/aws/aws-sdk-go-v2 v1.18.1
github.com/aws/aws-sdk-go-v2/config v1.18.27
github.com/aws/aws-sdk-go-v2/service/s3 v1.35.0
github.com/golang/mock v1.6.0
github.com/rs/zerolog v1.29.1
github.com/stretchr/testify v1.8.4
golang.org/x/net v0.11.0
Expand All @@ -32,8 +31,6 @@ require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/mod v0.11.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/tools v0.10.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
29 changes: 0 additions & 29 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
Expand All @@ -61,41 +59,14 @@ github.com/rs/zerolog v1.29.1/go.mod h1:Le6ESbR7hc+DP6Lt1THiV8CQSdkkNrd3R0XbEgp3
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.11.0 h1:bUO06HqtnRcc/7l71XBe4WcqTZ+3AH1J59zWDDwLKgU=
golang.org/x/mod v0.11.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.11.0 h1:Gi2tvZIJyBtO9SDr1q9h5hEQCp/4L2RQ+ar0qjx2oNU=
golang.org/x/net v0.11.0/go.mod h1:2L/ixqYpgIVXmeoSA/4Lu7BzTG4KIyPIryS4IsOd1oQ=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s=
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.10.0 h1:tvDr/iQoUqNdohiYm0LmmKcBk+q86lb9EprIUFhHHGg=
golang.org/x/tools v0.10.0/go.mod h1:UJwyiVBsOA2uwvK/e5OY3GTpDUJriEd+/YlqAwLPmyM=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
96 changes: 0 additions & 96 deletions mocks/s3api.go

This file was deleted.

2 changes: 1 addition & 1 deletion s3file.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func buildRange(offset, length int64) *string {
} else if length == 0 {
// AWS doesn't support a zero-length read; we'll read 1 byte and then
// ignore it in favor of http.NoBody below.
byteRange = aws.String(fmt.Sprintf("bytes=%d-%d", offset, offset))
byteRange = aws.String(fmt.Sprintf("bytes=%d-%d", offset, offset+1))
} else if length >= 0 {
byteRange = aws.String(fmt.Sprintf("bytes=%d-%d", offset, offset+length-1))
}
Expand Down
95 changes: 77 additions & 18 deletions s3file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand All @@ -34,21 +33,26 @@ func (m mockGetObjectAPI) HeadObject(ctx context.Context, params *s3.HeadObjectI

func TestReadAt(t *testing.T) {

cases := []struct {
client func(t *testing.T) mockGetObjectAPI
type args struct {
bucket string
key string
expect []byte
}

cases := []struct {
client func(t *testing.T) mockGetObjectAPI
args args
expectData []byte
expectedLength int
}{
{
client: func(t *testing.T) mockGetObjectAPI {
return mockGetObjectAPI{
getObject: func(ctx context.Context, params *s3.GetObjectInput, optFns ...func(*s3.Options)) (*s3.GetObjectOutput, error) {
t.Helper()

assert.Equal(t, aws.String("fooBucket"), params.Bucket)
assert.Equal(t, aws.String("barKey"), params.Key)
assert.Equal(t, aws.String("bytes=0-1023"), params.Range)
require.Equal(t, aws.String("fooBucket"), params.Bucket)
require.Equal(t, aws.String("barKey"), params.Key)
require.Equal(t, aws.String("bytes=0-1023"), params.Range)

return &s3.GetObjectOutput{
Body: io.NopCloser(bytes.NewReader(make([]byte, 1024))),
Expand All @@ -57,44 +61,99 @@ func TestReadAt(t *testing.T) {
headObject: func(ctx context.Context, params *s3.HeadObjectInput, optFns ...func(*s3.Options)) (*s3.HeadObjectOutput, error) {
t.Helper()

assert.Equal(t, aws.String("fooBucket"), params.Bucket)
assert.Equal(t, aws.String("barKey"), params.Key)
require.Equal(t, aws.String("fooBucket"), params.Bucket)
require.Equal(t, aws.String("barKey"), params.Key)

return &s3.HeadObjectOutput{
ContentLength: 1024,
}, nil
},
}
},
bucket: "fooBucket",
key: "barKey",
expect: []byte("this is the body foo bar baz"),
args: args{
bucket: "fooBucket",
key: "barKey",
},
expectData: []byte("this is the body foo bar baz"),
expectedLength: 1024,
},
}

for i, tt := range cases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
assert := require.New(t)

// ctx := context.TODO()

sysfs := NewWithClient(tt.bucket, tt.client(t))
sysfs := NewWithClient(tt.args.bucket, tt.client(t))

f, err := sysfs.Open(tt.key)
f, err := sysfs.Open(tt.args.key)
assert.NoError(err)

data := make([]byte, 1024)
data := make([]byte, tt.expectedLength)

if rc, ok := f.(io.ReaderAt); ok {

n, err := rc.ReadAt(data, 0)
assert.NoError(err)
assert.Equal(1024, n)
assert.Equal(tt.expectedLength, n)
}
})
}
}

func Test_buildRange(t *testing.T) {
type args struct {
offset int64
length int64
}
tests := []struct {
name string
args args
want *string
}{
{
name: "should return 0-1023 for offset 0 and length 1024",
args: args{
offset: 0,
length: 1024,
},
want: aws.String("bytes=0-1023"),
},
{
// read the remainder of the file
name: "should return nil for offset 1024 and length -1",
args: args{
offset: 1024,
length: -1,
},
want: aws.String("bytes=1024-"),
},
{
// a nil range will just read the entire file
name: "should return nil for offset 0 and length -1",
args: args{
offset: 0,
length: -1,
},
want: nil,
},
{
// zero read will just read one byte as AWS doesn't support a zero byte range
name: "should return nil for offset 0 and length -1",
args: args{
offset: 1024,
length: 0,
},
want: aws.String("bytes=1024-1025"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := buildRange(tt.args.offset, tt.args.length)
require.Equal(t, aws.ToString(tt.want), aws.ToString(got))
})
}
}

func TestReadAtInt(t *testing.T) {
if os.Getenv("TEST_BUCKET_NAME") == "" {
t.Skip()
Expand Down
Loading

0 comments on commit e433a43

Please sign in to comment.