forked from spf13/afero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcs_mocks.go
269 lines (217 loc) · 5.32 KB
/
gcs_mocks.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
// Copyright © 2021 Vasily Ovchinnikov <vasily@remerge.io>.
//
// A set of stiface-based mocks, replicating the GCS behavior, to make the tests not require any
// internet connection or real buckets.
// It is **not** a comprehensive set of mocks to test anything and everything GCS-related, rather
// a very tailored one for the current implementation - thus the tests, written with the use of
// these mocks are more of regression ones.
// If any GCS behavior changes and breaks the implementation, then it should first be adjusted by
// switching over to a real bucket - and then the mocks have to be adjusted to match the
// implementation.
package gcsfs
import (
"context"
"io"
"os"
"strings"
"cloud.google.com/go/storage"
"github.com/googleapis/google-cloud-go-testing/storage/stiface"
"github.com/spf13/afero"
"google.golang.org/api/iterator"
)
// sets filesystem separators to the one, expected (and hard-coded) in the tests
func normSeparators(s string) string {
return strings.Replace(s, "\\", "/", -1)
}
type clientMock struct {
stiface.Client
fs afero.Fs
}
func newClientMock() *clientMock {
return &clientMock{fs: afero.NewMemMapFs()}
}
func (m *clientMock) Bucket(name string) stiface.BucketHandle {
return &bucketMock{bucketName: name, fs: m.fs}
}
type bucketMock struct {
stiface.BucketHandle
bucketName string
fs afero.Fs
}
func (m *bucketMock) Attrs(context.Context) (*storage.BucketAttrs, error) {
return &storage.BucketAttrs{}, nil
}
func (m *bucketMock) Object(name string) stiface.ObjectHandle {
return &objectMock{name: name, fs: m.fs}
}
func (m *bucketMock) Objects(_ context.Context, q *storage.Query) (it stiface.ObjectIterator) {
return &objectItMock{name: q.Prefix, fs: m.fs}
}
type objectMock struct {
stiface.ObjectHandle
name string
fs afero.Fs
}
func (o *objectMock) NewWriter(_ context.Context) stiface.Writer {
return &writerMock{name: o.name, fs: o.fs}
}
func (o *objectMock) NewRangeReader(_ context.Context, offset, length int64) (stiface.Reader, error) {
if o.name == "" {
return nil, ErrEmptyObjectName
}
file, err := o.fs.Open(o.name)
if err != nil {
return nil, err
}
if offset > 0 {
_, err = file.Seek(offset, io.SeekStart)
if err != nil {
return nil, err
}
}
res := &readerMock{file: file}
if length > -1 {
res.buf = make([]byte, length)
_, err = file.Read(res.buf)
if err != nil {
return nil, err
}
}
return res, nil
}
func (o *objectMock) Delete(_ context.Context) error {
if o.name == "" {
return ErrEmptyObjectName
}
return o.fs.Remove(o.name)
}
func (o *objectMock) Attrs(_ context.Context) (*storage.ObjectAttrs, error) {
if o.name == "" {
return nil, ErrEmptyObjectName
}
info, err := o.fs.Stat(o.name)
if err != nil {
pathError, ok := err.(*os.PathError)
if ok {
if pathError.Err == os.ErrNotExist {
return nil, storage.ErrObjectNotExist
}
}
return nil, err
}
res := &storage.ObjectAttrs{Name: normSeparators(o.name), Size: info.Size(), Updated: info.ModTime()}
if info.IsDir() {
// we have to mock it here, because of FileInfo logic
return nil, ErrObjectDoesNotExist
}
return res, nil
}
type writerMock struct {
stiface.Writer
name string
fs afero.Fs
file afero.File
}
func (w *writerMock) Write(p []byte) (n int, err error) {
if w.name == "" {
return 0, ErrEmptyObjectName
}
if w.file == nil {
w.file, err = w.fs.Create(w.name)
if err != nil {
return 0, err
}
}
return w.file.Write(p)
}
func (w *writerMock) Close() error {
if w.name == "" {
return ErrEmptyObjectName
}
if w.file == nil {
var err error
if strings.HasSuffix(w.name, "/") {
err = w.fs.Mkdir(w.name, 0755)
if err != nil {
return err
}
} else {
_, err = w.Write([]byte{})
if err != nil {
return err
}
}
}
if w.file != nil {
return w.file.Close()
}
return nil
}
type readerMock struct {
stiface.Reader
file afero.File
buf []byte
}
func (r *readerMock) Remain() int64 {
return 0
}
func (r *readerMock) Read(p []byte) (int, error) {
if r.buf != nil {
copy(p, r.buf)
return len(r.buf), nil
}
return r.file.Read(p)
}
func (r *readerMock) Close() error {
return r.file.Close()
}
type objectItMock struct {
stiface.ObjectIterator
name string
fs afero.Fs
dir afero.File
infos []*storage.ObjectAttrs
}
func (it *objectItMock) Next() (*storage.ObjectAttrs, error) {
var err error
if it.dir == nil {
it.dir, err = it.fs.Open(it.name)
if err != nil {
return nil, err
}
var isDir bool
isDir, err = afero.IsDir(it.fs, it.name)
if err != nil {
return nil, err
}
it.infos = []*storage.ObjectAttrs{}
if !isDir {
var info os.FileInfo
info, err = it.dir.Stat()
if err != nil {
return nil, err
}
it.infos = append(it.infos, &storage.ObjectAttrs{Name: normSeparators(info.Name()), Size: info.Size(), Updated: info.ModTime()})
} else {
var fInfos []os.FileInfo
fInfos, err = it.dir.Readdir(0)
if err != nil {
return nil, err
}
if it.name != "" {
it.infos = append(it.infos, &storage.ObjectAttrs{
Prefix: normSeparators(it.name) + "/",
})
}
for _, info := range fInfos {
it.infos = append(it.infos, &storage.ObjectAttrs{Name: normSeparators(info.Name()), Size: info.Size(), Updated: info.ModTime()})
}
}
}
if len(it.infos) == 0 {
return nil, iterator.Done
}
res := it.infos[0]
it.infos = it.infos[1:]
return res, err
}