-
Notifications
You must be signed in to change notification settings - Fork 342
/
testutils.go
225 lines (196 loc) · 5.51 KB
/
testutils.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
package testutils
import (
"bytes"
"context"
"errors"
"fmt"
"math/rand"
"net/url"
"sort"
"time"
"github.com/treeverse/lakefs/pkg/block"
"github.com/treeverse/lakefs/pkg/catalog"
"github.com/treeverse/lakefs/pkg/graveler"
"github.com/treeverse/lakefs/pkg/ingest/store"
"github.com/treeverse/lakefs/pkg/testutil"
)
type FakeValueIterator struct {
Records []*graveler.ValueRecord
Error error
index int
}
func (f *FakeValueIterator) Next() bool {
if f.Error != nil {
return false
}
f.index++
return f.index < len(f.Records)
}
func (f *FakeValueIterator) SeekGE(id graveler.Key) {
if f.Error != nil {
return
}
for i, ent := range f.Records {
if bytes.Compare(ent.Key, id) >= 0 {
f.index = i - 1
return
}
}
}
func (f *FakeValueIterator) Value() *graveler.ValueRecord {
if f.Error != nil || f.index < 0 || f.index >= len(f.Records) {
return nil
}
return f.Records[f.index]
}
func (f *FakeValueIterator) Err() error {
return f.Error
}
func (f *FakeValueIterator) Close() {}
func NewFakeValueIterator(records []*graveler.ValueRecord) *FakeValueIterator {
return &FakeValueIterator{
Records: records,
Error: nil,
index: -1,
}
}
type FakeEntryIterator struct {
Entries []*catalog.EntryRecord
Error error
index int
}
func NewFakeEntryIterator(entries []*catalog.EntryRecord) *FakeEntryIterator {
return &FakeEntryIterator{
Entries: entries,
index: -1,
}
}
func (f *FakeEntryIterator) Next() bool {
if f.Error != nil {
return false
}
f.index++
return f.index < len(f.Entries)
}
func (f *FakeEntryIterator) SeekGE(id catalog.Path) {
if f.Error != nil {
return
}
for i, ent := range f.Entries {
if ent.Path >= id {
f.index = i - 1
return
}
}
}
func (f *FakeEntryIterator) Value() *catalog.EntryRecord {
if f.Error != nil || f.index < 0 || f.index >= len(f.Entries) {
return nil
}
return f.Entries[f.index]
}
func (f *FakeEntryIterator) Err() error {
return f.Error
}
func (f *FakeEntryIterator) Close() {}
type FakeFactory struct {
Walker *FakeWalker
}
func (f FakeFactory) GetWalker(_ context.Context, op store.WalkerOptions) (*store.WalkerWrapper, error) {
u, _ := url.Parse(op.StorageURI)
return store.NewWrapper(f.Walker, u), nil
}
func NewFakeWalker(count, max int, uriPrefix, expectedAfter, expectedContinuationToken, expectedFromSourceURIWithPrefix string, err error) *FakeWalker {
w := &FakeWalker{
Max: max,
uriPrefix: uriPrefix,
expectedAfter: expectedAfter,
expectedContinuationToken: expectedContinuationToken,
expectedFromSourceURIWithPrefix: expectedFromSourceURIWithPrefix,
err: err,
}
w.createEntries(count)
return w
}
type FakeWalker struct {
Entries []block.ObjectStoreEntry
curr int
Max int
uriPrefix string
expectedAfter string
expectedContinuationToken string
expectedFromSourceURIWithPrefix string
err error
}
func (w *FakeWalker) GetSkippedEntries() []block.ObjectStoreEntry {
return nil
}
const (
randomKeyLength = 64
entrySize = 132
)
func (w *FakeWalker) createEntries(count int) {
ents := make([]block.ObjectStoreEntry, count)
// Use same sequence to overcome Graveler ability to create small ranges.
// Calling test functions rely on Graveler to not break on the first 1000 entries.
// For example, setting "5" here will cause the test to constantly fail.
// Fix Bug #3384
const seed = 6
//nolint:gosec
randGen := rand.New(rand.NewSource(seed))
for i := 0; i < count; i++ {
relativeKey := testutil.RandomString(randGen, randomKeyLength)
fullkey := w.uriPrefix + "/" + relativeKey
ents[i] = block.ObjectStoreEntry{
RelativeKey: relativeKey,
FullKey: fullkey,
Address: w.expectedFromSourceURIWithPrefix + "/" + relativeKey,
ETag: "some_etag",
Mtime: time.Time{},
Size: entrySize,
}
}
sort.Slice(ents, func(i, j int) bool {
return ents[i].RelativeKey < ents[j].RelativeKey
})
w.Entries = ents
}
var errUnexpectedValue = errors.New("unexpected value")
func (w *FakeWalker) Walk(_ context.Context, storageURI *url.URL, op block.WalkOptions, walkFn func(e block.ObjectStoreEntry) error) error {
if w.expectedAfter != op.After {
return fmt.Errorf("(after) expected %s, got %s: %w", w.expectedAfter, op.After, errUnexpectedValue)
}
if w.expectedContinuationToken != op.ContinuationToken {
return fmt.Errorf("(continuationToken) expected %s, got %s: %w", w.expectedContinuationToken, op.ContinuationToken, errUnexpectedValue)
}
if w.expectedFromSourceURIWithPrefix != storageURI.String() {
return fmt.Errorf("(fromSourceURIWithPrefix) expected %s, got %s: %w", w.expectedFromSourceURIWithPrefix, storageURI.String(), errUnexpectedValue)
}
if w.err != nil {
return w.err
}
for i, e := range w.Entries {
w.curr = i
if err := walkFn(e); err != nil {
if i < w.Max {
return fmt.Errorf("didn't expect walk err: %w", err)
}
if i >= w.Max && !errors.Is(err, catalog.ErrItClosed) {
return fmt.Errorf("expected error; expected (%s), got: %w", catalog.ErrItClosed, err)
}
}
}
return nil
}
const ContinuationTokenOpaque = "FakeContToken"
func (w *FakeWalker) Marker() block.Mark {
token := ""
if w.curr < len(w.Entries)-1 {
token = ContinuationTokenOpaque
}
return block.Mark{
LastKey: w.Entries[w.curr].FullKey,
HasMore: w.curr < len(w.Entries)-1,
ContinuationToken: token,
}
}