forked from thanos-io/objstore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.go
314 lines (261 loc) · 9.51 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.
package objstore
import (
"bytes"
"context"
"fmt"
"io"
"math/rand"
"sort"
"strings"
"sync"
"testing"
"time"
"github.com/efficientgo/core/testutil"
)
func CreateTemporaryTestBucketName(t testing.TB) string {
src := rand.NewSource(time.Now().UnixNano())
// Bucket name need to conform: https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-s3-bucket-naming-requirements.html.
name := strings.ReplaceAll(strings.Replace(fmt.Sprintf("test_%x_%s", src.Int63(), strings.ToLower(t.Name())), "_", "-", -1), "/", "-")
if len(name) >= 63 {
name = name[:63]
}
return name
}
// EmptyBucket deletes all objects from bucket. This operation is required to properly delete bucket as a whole.
// It is used for testing only.
// TODO(bplotka): Add retries.
func EmptyBucket(t testing.TB, ctx context.Context, bkt Bucket) {
var wg sync.WaitGroup
queue := []string{""}
for len(queue) > 0 {
elem := queue[0]
queue = queue[1:]
err := bkt.Iter(ctx, elem, func(p string) error {
if strings.HasSuffix(p, DirDelim) {
queue = append(queue, p)
return nil
}
wg.Add(1)
go func() {
if err := bkt.Delete(ctx, p); err != nil {
t.Logf("deleting object %s failed: %s", p, err)
}
wg.Done()
}()
return nil
})
if err != nil {
t.Logf("iterating over bucket objects failed: %s", err)
wg.Wait()
return
}
}
wg.Wait()
}
func WithNoopInstr(bkt Bucket) InstrumentedBucket {
return noopInstrumentedBucket{Bucket: bkt}
}
type noopInstrumentedBucket struct {
Bucket
}
func (b noopInstrumentedBucket) WithExpectedErrs(IsOpFailureExpectedFunc) Bucket {
return b
}
func (b noopInstrumentedBucket) ReaderWithExpectedErrs(IsOpFailureExpectedFunc) BucketReader {
return b
}
func AcceptanceTest(t *testing.T, bkt Bucket) {
ctx := context.Background()
_, err := bkt.Get(ctx, "")
testutil.NotOk(t, err)
testutil.Assert(t, !bkt.IsObjNotFoundErr(err), "expected user error got not found %s", err)
_, err = bkt.Get(ctx, "id1/obj_1.some")
testutil.NotOk(t, err)
testutil.Assert(t, bkt.IsObjNotFoundErr(err), "expected not found error got %s", err)
ok, err := bkt.Exists(ctx, "id1/obj_1.some")
testutil.Ok(t, err)
testutil.Assert(t, !ok, "expected not exits")
_, err = bkt.Attributes(ctx, "id1/obj_1.some")
testutil.NotOk(t, err)
testutil.Assert(t, bkt.IsObjNotFoundErr(err), "expected not found error but got %s", err)
// Upload first object.
testutil.Ok(t, bkt.Upload(ctx, "id1/obj_1.some", strings.NewReader("@test-data@")))
// Double check we can immediately read it.
rc1, err := bkt.Get(ctx, "id1/obj_1.some")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, rc1.Close()) }()
content, err := io.ReadAll(rc1)
testutil.Ok(t, err)
testutil.Equals(t, "@test-data@", string(content))
// Check if we can get the correct size.
attrs, err := bkt.Attributes(ctx, "id1/obj_1.some")
testutil.Ok(t, err)
testutil.Assert(t, attrs.Size == 11, "expected size to be equal to 11")
rc2, err := bkt.GetRange(ctx, "id1/obj_1.some", 1, 3)
testutil.Ok(t, err)
defer func() { testutil.Ok(t, rc2.Close()) }()
content, err = io.ReadAll(rc2)
testutil.Ok(t, err)
testutil.Equals(t, "tes", string(content))
// Unspecified range with offset.
rcUnspecifiedLen, err := bkt.GetRange(ctx, "id1/obj_1.some", 1, -1)
testutil.Ok(t, err)
defer func() { testutil.Ok(t, rcUnspecifiedLen.Close()) }()
content, err = io.ReadAll(rcUnspecifiedLen)
testutil.Ok(t, err)
testutil.Equals(t, "test-data@", string(content))
// Out of band offset. Do not rely on outcome.
// NOTE: For various providers we have different outcome.
// * GCS is giving 416 status code
// * S3 errors immdiately with invalid range error.
// * inmem and filesystem are returning 0 bytes.
//rcOffset, err := bkt.GetRange(ctx, "id1/obj_1.some", 124141, 3)
// Out of band length. We expect to read file fully.
rcLength, err := bkt.GetRange(ctx, "id1/obj_1.some", 3, 9999)
testutil.Ok(t, err)
defer func() { testutil.Ok(t, rcLength.Close()) }()
content, err = io.ReadAll(rcLength)
testutil.Ok(t, err)
testutil.Equals(t, "st-data@", string(content))
ok, err = bkt.Exists(ctx, "id1/obj_1.some")
testutil.Ok(t, err)
testutil.Assert(t, ok, "expected exits")
// Upload other objects.
testutil.Ok(t, bkt.Upload(ctx, "id1/obj_2.some", strings.NewReader("@test-data2@")))
// Upload should be idempotent.
testutil.Ok(t, bkt.Upload(ctx, "id1/obj_2.some", strings.NewReader("@test-data2@")))
testutil.Ok(t, bkt.Upload(ctx, "id1/obj_3.some", strings.NewReader("@test-data3@")))
testutil.Ok(t, bkt.Upload(ctx, "id1/sub/subobj_1.some", strings.NewReader("@test-data4@")))
testutil.Ok(t, bkt.Upload(ctx, "id1/sub/subobj_2.some", strings.NewReader("@test-data5@")))
testutil.Ok(t, bkt.Upload(ctx, "id2/obj_4.some", strings.NewReader("@test-data6@")))
testutil.Ok(t, bkt.Upload(ctx, "obj_5.some", strings.NewReader("@test-data7@")))
// Can we iter over items from top dir?
var seen []string
testutil.Ok(t, bkt.Iter(ctx, "", func(fn string) error {
seen = append(seen, fn)
return nil
}))
expected := []string{"obj_5.some", "id1/", "id2/"}
sort.Strings(expected)
sort.Strings(seen)
testutil.Equals(t, expected, seen)
// Can we iter over items from top dir recursively?
seen = []string{}
testutil.Ok(t, bkt.Iter(ctx, "", func(fn string) error {
seen = append(seen, fn)
return nil
}, WithRecursiveIter))
expected = []string{"id1/obj_1.some", "id1/obj_2.some", "id1/obj_3.some", "id1/sub/subobj_1.some", "id1/sub/subobj_2.some", "id2/obj_4.some", "obj_5.some"}
sort.Strings(expected)
sort.Strings(seen)
testutil.Equals(t, expected, seen)
// Can we iter over items from id1/ dir?
seen = []string{}
testutil.Ok(t, bkt.Iter(ctx, "id1/", func(fn string) error {
seen = append(seen, fn)
return nil
}))
testutil.Equals(t, []string{"id1/obj_1.some", "id1/obj_2.some", "id1/obj_3.some", "id1/sub/"}, seen)
// Can we iter over items from id1/ dir recursively?
seen = []string{}
testutil.Ok(t, bkt.Iter(ctx, "id1/", func(fn string) error {
seen = append(seen, fn)
return nil
}, WithRecursiveIter))
testutil.Equals(t, []string{"id1/obj_1.some", "id1/obj_2.some", "id1/obj_3.some", "id1/sub/subobj_1.some", "id1/sub/subobj_2.some"}, seen)
// Can we iter over items from id1 dir?
seen = []string{}
testutil.Ok(t, bkt.Iter(ctx, "id1", func(fn string) error {
seen = append(seen, fn)
return nil
}))
testutil.Equals(t, []string{"id1/obj_1.some", "id1/obj_2.some", "id1/obj_3.some", "id1/sub/"}, seen)
// Can we iter over items from id1 dir recursively?
seen = []string{}
testutil.Ok(t, bkt.Iter(ctx, "id1", func(fn string) error {
seen = append(seen, fn)
return nil
}, WithRecursiveIter))
testutil.Equals(t, []string{"id1/obj_1.some", "id1/obj_2.some", "id1/obj_3.some", "id1/sub/subobj_1.some", "id1/sub/subobj_2.some"}, seen)
// Can we iter over items from not existing dir?
testutil.Ok(t, bkt.Iter(ctx, "id0", func(fn string) error {
t.Error("Not expected to loop through not existing directory")
t.FailNow()
return nil
}))
testutil.Ok(t, bkt.Delete(ctx, "id1/obj_2.some"))
// Delete is expected to fail on non existing object.
// NOTE: Don't rely on this. S3 is not complying with this as GCS is.
// testutil.NotOk(t, bkt.Delete(ctx, "id1/obj_2.some"))
// Can we iter over items from id1/ dir and see obj2 being deleted?
seen = []string{}
testutil.Ok(t, bkt.Iter(ctx, "id1/", func(fn string) error {
seen = append(seen, fn)
return nil
}))
testutil.Equals(t, []string{"id1/obj_1.some", "id1/obj_3.some", "id1/sub/"}, seen)
testutil.Ok(t, bkt.Delete(ctx, "id2/obj_4.some"))
seen = []string{}
testutil.Ok(t, bkt.Iter(ctx, "", func(fn string) error {
seen = append(seen, fn)
return nil
}))
expected = []string{"obj_5.some", "id1/"}
sort.Strings(expected)
sort.Strings(seen)
testutil.Equals(t, expected, seen)
testutil.Ok(t, bkt.Upload(ctx, "obj_6.som", bytes.NewReader(make([]byte, 1024*1024*200))))
testutil.Ok(t, bkt.Delete(ctx, "obj_6.som"))
}
type delayingBucket struct {
bkt Bucket
delay time.Duration
}
func WithDelay(bkt Bucket, delay time.Duration) Bucket {
return &delayingBucket{bkt: bkt, delay: delay}
}
func (d *delayingBucket) Get(ctx context.Context, name string) (io.ReadCloser, error) {
time.Sleep(d.delay)
return d.bkt.Get(ctx, name)
}
func (d *delayingBucket) Attributes(ctx context.Context, name string) (ObjectAttributes, error) {
time.Sleep(d.delay)
return d.bkt.Attributes(ctx, name)
}
func (d *delayingBucket) Iter(ctx context.Context, dir string, f func(string) error, options ...IterOption) error {
time.Sleep(d.delay)
return d.bkt.Iter(ctx, dir, f, options...)
}
func (d *delayingBucket) GetRange(ctx context.Context, name string, off, length int64) (io.ReadCloser, error) {
time.Sleep(d.delay)
return d.bkt.GetRange(ctx, name, off, length)
}
func (d *delayingBucket) Exists(ctx context.Context, name string) (bool, error) {
time.Sleep(d.delay)
return d.bkt.Exists(ctx, name)
}
func (d *delayingBucket) Upload(ctx context.Context, name string, r io.Reader) error {
time.Sleep(d.delay)
return d.bkt.Upload(ctx, name, r)
}
func (d *delayingBucket) Delete(ctx context.Context, name string) error {
time.Sleep(d.delay)
return d.bkt.Delete(ctx, name)
}
func (d *delayingBucket) Name() string {
time.Sleep(d.delay)
return d.bkt.Name()
}
func (d *delayingBucket) Close() error {
// No delay for a local operation.
return d.bkt.Close()
}
func (d *delayingBucket) IsObjNotFoundErr(err error) bool {
// No delay for a local operation.
return d.bkt.IsObjNotFoundErr(err)
}
func (d *delayingBucket) IsAccessDeniedErr(err error) bool {
return d.bkt.IsAccessDeniedErr(err)
}