-
Notifications
You must be signed in to change notification settings - Fork 10
/
testing.go
272 lines (222 loc) · 7.22 KB
/
testing.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package dstore
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"path"
"path/filepath"
"sort"
"strings"
"go.uber.org/zap"
)
type MockStore struct {
OpenObjectFunc func(ctx context.Context, name string) (out io.ReadCloser, err error)
WriteObjectFunc func(ctx context.Context, base string, f io.Reader) error
CopyObjectFunc func(ctx context.Context, src, dest string) error
DeleteObjectFunc func(ctx context.Context, base string) error
FileExistsFunc func(ctx context.Context, base string) (bool, error)
ObjectAttributesFunc func(ctx context.Context, base string) (*ObjectAttributes, error)
ListFilesFunc func(ctx context.Context, prefix string, max int) ([]string, error)
WalkFunc func(ctx context.Context, prefix string, f func(filename string) error) error
PushLocalFileFunc func(ctx context.Context, localFile string, toBaseName string) (err error)
Files map[string][]byte
shouldOverwrite bool
}
func NewMockStore(writeFunc func(base string, f io.Reader) (err error)) *MockStore {
store := &MockStore{Files: make(map[string][]byte)}
if writeFunc != nil {
store.WriteObjectFunc = func(ctx context.Context, base string, f io.Reader) error {
return writeFunc(base, f)
}
}
return store
}
func (s *MockStore) SubStore(subFolder string) (Store, error) {
newFiles := map[string][]byte{}
for k, v := range s.Files {
prefix := filepath.Join(subFolder, "") + string(filepath.Separator)
if strings.HasPrefix(k, prefix) {
newFiles[strings.TrimPrefix(k, prefix)] = v
}
}
return &MockStore{
Files: newFiles,
shouldOverwrite: s.shouldOverwrite,
OpenObjectFunc: s.OpenObjectFunc,
WriteObjectFunc: s.WriteObjectFunc,
CopyObjectFunc: s.CopyObjectFunc,
DeleteObjectFunc: s.DeleteObjectFunc,
FileExistsFunc: s.FileExistsFunc,
ListFilesFunc: s.ListFilesFunc,
WalkFunc: s.WalkFunc,
PushLocalFileFunc: s.PushLocalFileFunc,
}, nil
}
func (s *MockStore) BaseURL() *url.URL {
return &url.URL{Scheme: "mock", Path: "/mock"}
}
// WriteFiles dumps currently know file
func (s *MockStore) WriteFiles(toDirectory string) error {
for name, content := range s.Files {
if err := ioutil.WriteFile(path.Join(toDirectory, name), content, os.ModePerm); err != nil {
return fmt.Errorf("writing file %q: %w", name, err)
}
}
return nil
}
// SetFile sets the content of a file. Set the value "err" to trigger
// an error when reading this file.
func (s *MockStore) SetFile(name string, content []byte) {
isError := string(content) == "err"
zlog.Debug("adding file", zap.String("name", name), zap.Int("content_length", len(content)), zap.Bool("is_error", isError))
s.Files[name] = content
}
func (s *MockStore) OpenObject(ctx context.Context, name string) (out io.ReadCloser, err error) {
if s.OpenObjectFunc != nil {
return s.OpenObjectFunc(ctx, name)
}
zlog.Debug("opening object", zap.String("name", name))
content, exists := s.Files[name]
if !exists {
zlog.Debug("opening object not found", zap.String("name", name))
return nil, io.EOF
}
if string(content) == "err" {
zlog.Debug("opening object error", zap.String("name", name))
return nil, fmt.Errorf("%s errored", name)
}
zlog.Debug("opened object", zap.String("name", name), zap.Int("content_length", len(content)))
return ioutil.NopCloser(bytes.NewReader(content)), nil
}
func (s *MockStore) CopyObject(ctx context.Context, src, dest string) error {
if s.CopyObjectFunc != nil {
return s.CopyObjectFunc(ctx, src, dest)
}
reader, err := s.OpenObject(ctx, src)
if err != nil {
return err
}
defer reader.Close()
return s.WriteObject(ctx, dest, reader)
}
func (s *MockStore) WriteObject(ctx context.Context, base string, f io.Reader) (err error) {
if s.WriteObjectFunc != nil {
return s.WriteObjectFunc(ctx, base, f)
}
zlog.Debug("writing object", zap.String("name", base))
content, exists := s.Files[base]
if !exists {
zlog.Debug("writing object not found, creating new one", zap.String("name", base))
} else {
if !s.shouldOverwrite {
zlog.Debug("writing object not allowing overwrite", zap.String("name", base))
return nil
}
zlog.Debug("writing object found, resetting it due to overwrite true", zap.String("name", base), zap.Int("content_length", len(content)))
}
buffer := bytes.NewBuffer(nil)
_, err = io.Copy(buffer, f)
if err != nil {
return fmt.Errorf("copy object to mock storage: %w", err)
}
s.Files[base] = buffer.Bytes()
zlog.Debug("wrote object", zap.String("name", base), zap.Int("content_length", len(s.Files[base])))
return nil
}
func (s *MockStore) ObjectPath(base string) string {
return base
}
func (s *MockStore) ObjectURL(base string) string {
return base
}
func (s *MockStore) DeleteObject(ctx context.Context, base string) error {
if s.DeleteObjectFunc != nil {
return s.DeleteObjectFunc(ctx, base)
}
zlog.Debug("deleting object", zap.String("name", base))
delete(s.Files, base)
return nil
}
func (s *MockStore) FileExists(ctx context.Context, base string) (bool, error) {
if s.FileExistsFunc != nil {
return s.FileExistsFunc(ctx, base)
}
zlog.Debug("checking if file exists", zap.String("name", base))
content, exists := s.Files[base]
if !exists {
return false, nil
}
scnt := string(content)
if scnt == "err" {
return false, fmt.Errorf("%q errored", base)
}
return scnt != "err", nil
}
func (s *MockStore) ObjectAttributes(ctx context.Context, base string) (*ObjectAttributes, error) {
if s.ObjectAttributesFunc != nil {
return s.ObjectAttributesFunc(ctx, base)
}
return nil, nil
}
func (s *MockStore) ListFiles(ctx context.Context, prefix string, max int) ([]string, error) {
if s.ListFilesFunc != nil {
return s.ListFilesFunc(ctx, prefix, max)
}
return listFiles(ctx, s, prefix, max)
}
func (s *MockStore) SetOverwrite(in bool) {
s.shouldOverwrite = in
}
func (s *MockStore) WalkFrom(ctx context.Context, prefix, startingPoint string, f func(filename string) (err error)) error {
return commonWalkFrom(s, ctx, prefix, startingPoint, f)
}
func (s *MockStore) Walk(ctx context.Context, prefix string, f func(filename string) error) error {
if s.WalkFunc != nil {
return s.WalkFunc(ctx, prefix, f)
}
zlog.Debug("walking files", zap.String("prefix", prefix))
sortedFiles := s.sortedFiles()
for _, file := range sortedFiles {
zlog.Debug("walking file", zap.String("file", file), zap.Bool("has_prefix", strings.HasPrefix(file, prefix)))
if strings.Contains(file, "err") {
return fmt.Errorf("mock err, %s", file)
}
if strings.HasPrefix(file, prefix) {
if err := f(file); err != nil {
if errors.Is(err, StopIteration) {
return nil
}
return err
}
}
}
return nil
}
func (s *MockStore) sortedFiles() []string {
sortedFiles := make([]string, len(s.Files))
i := 0
for file := range s.Files {
sortedFiles[i] = file
i++
}
sort.Sort(sort.StringSlice(sortedFiles))
return sortedFiles
}
func (s *MockStore) PushLocalFile(ctx context.Context, localFile string, toBaseName string) (err error) {
if s.PushLocalFileFunc != nil {
return s.PushLocalFileFunc(ctx, localFile, toBaseName)
}
remove, err := pushLocalFile(ctx, s, localFile, toBaseName)
if err != nil {
return err
}
return remove()
}
func (s *MockStore) Overwrite() bool {
return s.shouldOverwrite
}