-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paths3_test.go
73 lines (64 loc) · 1.97 KB
/
s3_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"bytes"
"context"
"errors"
"io"
"strings"
"testing"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/stretchr/testify/assert"
)
type mockPutObjectAPI func(ctx context.Context, params *s3.PutObjectInput, optFns ...func(*s3.Options)) (*s3.PutObjectOutput, error)
func (m mockPutObjectAPI) PutObject(ctx context.Context, params *s3.PutObjectInput, optFns ...func(*s3.Options)) (*s3.PutObjectOutput, error) {
return m(ctx, params, optFns...)
}
func TestSaveToS3(t *testing.T) {
ctx := context.Background()
testContents := "hello world!"
contentsBuf := []byte(testContents)
testBucket := "testBucketName"
testKey := "some/key/to/logs.zip"
testParams := PutObjectParams{Bucket: testBucket, Key: testKey, Contents: bytes.NewBuffer(contentsBuf)}
testCases := []struct {
desc string
shouldSucceed bool
want string
mockAPI mockPutObjectAPI
}{
{
desc: "Successful PutObject call",
shouldSucceed: true,
want: "",
mockAPI: mockPutObjectAPI(
func(ctx context.Context, params *s3.PutObjectInput, optFns ...func(*s3.Options)) (*s3.PutObjectOutput, error) {
// Assertions to test the params given to the S3 client
assert.Equal(t, *params.Key, testKey)
assert.Equal(t, *params.Bucket, testBucket)
buf := new(strings.Builder)
io.Copy(buf, params.Body)
assert.Equal(t, buf.String(), testContents)
return &s3.PutObjectOutput{}, nil
}),
},
{
desc: "Failed PutObject call",
shouldSucceed: false,
want: "oh no!",
mockAPI: mockPutObjectAPI(
func(ctx context.Context, params *s3.PutObjectInput, optFns ...func(*s3.Options)) (*s3.PutObjectOutput, error) {
return nil, errors.New("oh no!")
}),
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
err := saveToS3(ctx, tC.mockAPI, testParams)
if tC.shouldSucceed {
assert.NoError(t, err)
} else {
assert.ErrorContains(t, err, tC.want)
}
})
}
}