-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy paths3managerv2_mock.go
67 lines (57 loc) · 1.43 KB
/
s3managerv2_mock.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
package testutils
import (
"context"
"errors"
"fmt"
"io"
"os"
"strings"
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
type S3ManagerV2Mock struct {
S3ManagerMockFileSystem map[string]string
TestDataLocation string
}
func (s *S3ManagerV2Mock) Upload(
ctx context.Context,
uploadInput *s3.PutObjectInput,
opts ...func(uploader *manager.Uploader),
) (*manager.UploadOutput, error) {
if ctx == nil {
return nil, errors.New("cannot create context from nil parent")
}
if uploadInput.Bucket == nil || *uploadInput.Bucket == "" {
return nil, errors.New("invalid bucket")
}
if s.S3ManagerMockFileSystem == nil {
s.S3ManagerMockFileSystem = make(map[string]string)
}
buf := new(strings.Builder)
_, err := io.Copy(buf, uploadInput.Body)
if err != nil {
fmt.Println(err)
}
s.S3ManagerMockFileSystem[*uploadInput.Key] = buf.String()
return &manager.UploadOutput{
Location: *uploadInput.Key,
}, nil
}
func (s *S3ManagerV2Mock) Download(
ctx context.Context,
w io.WriterAt,
input *s3.GetObjectInput,
options ...func(*manager.Downloader),
) (n int64, err error) {
if ctx == nil {
return -1, errors.New("cannot create context from nil parent")
}
if *input.Key == "valid" {
res, _ := os.ReadFile(s.TestDataLocation + "/flag-config.yaml")
_, _ = w.WriteAt(res, 0)
return 1, nil
} else if *input.Key == "no-file" {
return 0, errors.New("no file")
}
return 1, nil
}