forked from grailbio/reflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileset_test.go
454 lines (425 loc) · 12.1 KB
/
fileset_test.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
// Copyright 2018 GRAIL, Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package reflow_test
import (
"bytes"
"fmt"
"io"
"reflect"
"strings"
"testing"
"encoding/json"
"github.com/grailbio/base/digest"
"github.com/grailbio/reflow"
"github.com/grailbio/reflow/test/testutil"
)
var (
file1 = reflow.File{ID: reflow.Digester.FromString("foo"), Size: 3}
file2 = reflow.File{ID: reflow.Digester.FromString("bar"), Size: 3}
file3 = reflow.File{
ID: reflow.Digester.FromString("a/b/c"), Size: 5,
Assertions: reflow.AssertionsFromEntry(reflow.AssertionKey{"a/b/c", "n"}, map[string]string{"tag": "v"}),
}
fs1 = reflow.Fileset{Map: map[string]reflow.File{
"foo": file1,
"bar": file2,
}}
fs2 = reflow.Fileset{Map: map[string]reflow.File{
"a/b/c": file3,
"bar": file2,
}}
vlist = reflow.Fileset{List: []reflow.Fileset{fs1, fs2}}
)
const vlistSHA256 = "sha256:d60e67ce9e89548b502a5ad7968e99caed0d388f0a991b906f41a7ba65adb31f"
func TestValueDigest(t *testing.T) {
if fs1.Digest() == fs2.Digest() {
t.Errorf("did not w v1, v2 to have same digest")
}
if got, want := vlist.Digest().String(), vlistSHA256; got != want {
t.Errorf("got %v, want %v", got, want)
}
}
func TestValueFile(t *testing.T) {
files := vlist.Files()
expected := map[digest.Digest]reflow.File{file1.Digest(): file1, file2.Digest(): file2, file3.Digest(): file3}
for _, f := range files {
if _, ok := expected[f.Digest()]; !ok {
t.Errorf("unexpected file %v", f)
}
delete(expected, f.Digest())
}
if len(expected) != 0 {
t.Errorf("expected additional files %v", expected)
}
}
func TestValuePullup(t *testing.T) {
got := vlist.Pullup()
want := reflow.Fileset{Map: map[string]reflow.File{"foo": file1, "bar": file2, "a/b/c": file3}}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, want %v", got, want)
}
}
func TestValueN(t *testing.T) {
if got, want := vlist.N(), 4; got != want {
t.Errorf("got %v, want %v", got, want)
}
}
func TestEmpty(t *testing.T) {
empty := []reflow.Fileset{
{},
{List: make([]reflow.Fileset, 1)},
{List: []reflow.Fileset{{}, {Map: map[string]reflow.File{}}, {List: make([]reflow.Fileset, 100)}}},
}
for i, fs := range empty {
if !fs.Empty() {
t.Errorf("expected empty %d %v", i, fs)
}
}
}
func TestAnyEmpty(t *testing.T) {
empty := []reflow.Fileset{
{},
{List: make([]reflow.Fileset, 1)},
{List: []reflow.Fileset{{}, {Map: map[string]reflow.File{}}, {List: make([]reflow.Fileset, 100)}}},
{List: []reflow.Fileset{{Map: map[string]reflow.File{".": reflow.File{}}}, {}}},
}
for i, fs := range empty {
if !fs.AnyEmpty() {
t.Errorf("expected anyempty %d %v", i, fs)
}
}
}
func TestEqual(t *testing.T) {
const N = 1000
var last reflow.Fileset
fuzz := testutil.NewFuzz(nil)
for i := 0; i < N; i++ {
fs := fuzz.Fileset(true, i%2 == 0)
if !fs.Equal(fs) {
t.Errorf("fileset %v not equal to self", fs)
}
if fs.Equal(last) {
t.Errorf("fileset %v equal to %v", fs, last)
}
last = fs
}
}
func TestEqual2(t *testing.T) {
fid := reflow.Digester.FromString("foo")
f1 := reflow.File{
ID: fid,
Assertions: reflow.AssertionsFromEntry(reflow.AssertionKey{"s1", "t"}, map[string]string{"tag": "v"}),
}
f2 := reflow.File{
ID: fid,
Assertions: reflow.AssertionsFromEntry(reflow.AssertionKey{"s2", "t"}, map[string]string{"tag": "v"}),
}
fempty := reflow.File{ID: fid, Assertions: aempty}
fnil := reflow.File{ID: fid}
f3, f3dup := reflow.File{Source: "same_source", ETag: "etag"}, reflow.File{Source: "same_source", ETag: "etag"}
f3mid := reflow.File{Source: "same_source", ETag: "etag", ContentHash: reflow.Digester.FromString("mid")}
f3middup := reflow.File{Source: "same_source", ETag: "etag", ContentHash: reflow.Digester.FromString("mid")}
f4mid := reflow.File{Source: "same_source", ETag: "etag", ContentHash: reflow.Digester.FromString("mid2")}
tests := []struct {
a, b reflow.File
w bool
}{
{reflow.File{Source: "a"}, reflow.File{Source: "b"}, false},
{reflow.File{Source: "a"}, reflow.File{Source: "a"}, false},
{reflow.File{Source: "a", ETag: "e"}, reflow.File{Source: "a"}, false},
{reflow.File{Source: "a", ETag: "e"}, reflow.File{Source: "a", ETag: "e"}, true},
{f1, f2, true},
{fempty, fnil, true}, {fnil, fempty, true},
{f1, fempty, true}, {fempty, f1, true},
{f1, fnil, true}, {fnil, f1, true},
{f3, f3dup, true}, {f3, f3mid, true}, {f3mid, f3middup, true}, {f3dup, f3middup, true},
{f3mid, f4mid, false}, {f3middup, f4mid, false},
}
for _, tt := range tests {
if got, want := tt.a.Equal(tt.b), tt.w; got != want {
t.Errorf("File<%s> equal File<%s>: got %v, want %v", tt.a, tt.b, got, want)
}
}
}
func TestJson(t *testing.T) {
const N = 1000
fuzz := testutil.NewFuzz(nil)
for _, aok := range []bool{true, false} {
for i := 0; i < N; i++ {
fs := fuzz.Fileset(true, aok)
b, err := json.Marshal(fs)
if err != nil {
t.Errorf("marshal %v", err)
}
var newFs reflow.Fileset
if err := json.Unmarshal(b, &newFs); err != nil {
t.Errorf("unmarshal %v", err)
}
if got, want := newFs, fs; !got.Equal(want) {
t.Errorf("got %v, want %v", got, want)
}
}
}
}
func TestSubst(t *testing.T) {
fuzz := testutil.NewFuzz(nil)
for _, aok := range []bool{true, false} {
fs := fuzz.Fileset(true, aok)
_, ok := fs.Subst(nil)
if ok {
t.Fatal("unexpected resolved fileset")
}
// Create a substitution map:
sub := make(map[digest.Digest]reflow.File)
for _, file := range fs.Files() {
if !file.IsRef() {
continue
}
sub[file.Digest()] = fuzz.File(false, aok)
}
fs, ok = fs.Subst(sub)
if !ok {
t.Error("expected resolved fileset")
}
for _, file := range fs.Files() {
if file.IsRef() {
t.Errorf("unexpected reference file %v", file)
}
}
}
}
func TestDiff(t *testing.T) {
for _, tt := range []struct {
a, b reflow.Fileset
wantD bool
want string
}{
{fs1, fs1, false, ""},
{vlist, vlist, false, ""},
{
reflow.Fileset{List: []reflow.Fileset{vlist, fs2}, Map: map[string]reflow.File{"foo": file1}},
reflow.Fileset{List: []reflow.Fileset{vlist, fs2}, Map: map[string]reflow.File{"foo": file1}},
false, "",
},
{
vlist, fs1, true,
`
"bar" = void -> fcde2b2e
"foo" = void -> 2c26b46b
[0]:val<bar=fcde2b2e, ...6B> -> empty
[1]:val<a/b/c=d76a7b72, ...8B> -> empty`,
},
{
fs1, fs2, true,
`
"a/b/c" = void -> d76a7b72
"foo" = 2c26b46b -> void`,
},
{
reflow.Fileset{List: []reflow.Fileset{fs1, fs2}, Map: map[string]reflow.File{"foo": file1}},
reflow.Fileset{List: []reflow.Fileset{fs2, fs1}},
true,
`
"foo" = 2c26b46b -> void
[0]:"a/b/c" = void -> d76a7b72
[0]:"foo" = 2c26b46b -> void
[1]:"a/b/c" = d76a7b72 -> void
[1]:"foo" = void -> 2c26b46b`,
},
} {
got, gotD := tt.a.Diff(tt.b)
if tt.wantD != gotD {
t.Errorf("got %v, want %v", gotD, tt.wantD)
}
if want := strings.TrimPrefix(tt.want, "\n"); got != want {
t.Errorf("got %s, want %s", got, want)
}
}
const N = 100
fuzz := testutil.NewFuzz(nil)
for i := 0; i < N; i++ {
fs := fuzz.FilesetDeep(fuzz.Intn(10)+1, 5, false, false)
if diffStr, diff := fs.Diff(fs); diff || diffStr != "" {
t.Errorf("got different, want no different:\n%v", diffStr)
}
}
}
func TestAssertions(t *testing.T) {
fuzz := testutil.NewFuzz(nil)
fs := fuzz.Fileset(true, true)
a := reflow.AssertionsFromMap(map[reflow.AssertionKey]map[string]string{
{"s1", "t"}: {"tag": "v"},
{"s2", "t"}: {"tag": "v"},
})
existingByPath := make(map[string]*reflow.Assertions)
for _, f := range fs.Files() {
existingByPath[f.Source] = f.Assertions
}
_ = fs.AddAssertions(a)
wantAll := reflow.NewAssertions()
for _, f := range fs.Files() {
ea := existingByPath[f.Source]
got, want := f.Assertions, ea
if err := want.AddFrom(a); err != nil {
t.Error(err)
}
if !got.Equal(want) {
t.Errorf("got %v, want %v", got, want)
}
if err := wantAll.AddFrom(ea); err != nil {
t.Error(err)
}
}
_ = wantAll.AddFrom(a)
gotAll, err := reflow.MergeAssertions(fs.Assertions()...)
if err != nil {
t.Errorf("unexpected %v", err)
}
if got, want := gotAll, wantAll; !got.Equal(want) {
t.Errorf("got %v, want %v", got, want)
}
src := "some/random/file"
file := reflow.File{Source: "some/random/file", ETag: "etag", Size: int64(len(src))}
a = reflow.AssertionsFromEntry(reflow.AssertionKey{src, "test"}, map[string]string{"etag": "etag"})
fs = reflow.Fileset{Map: map[string]reflow.File{src: file}}
if err := fs.AddAssertions(a); err != nil {
t.Errorf("unexpected %v", err)
}
fsa, err := reflow.MergeAssertions(fs.Assertions()...)
if err != nil {
t.Errorf("unexpected %v", err)
}
if got, want := fsa, a; !got.Equal(want) {
t.Errorf("got %v, want %v", got, want)
}
if got, want := fs.Map[src].Assertions, a; !got.Equal(want) {
t.Errorf("got %v, want %v", got, want)
}
}
func createFilesets(nfs, nfiles, depth int) []reflow.Fileset {
fuzz := testutil.NewFuzz(nil)
fss := make([]reflow.Fileset, nfs)
for i := 0; i < nfs; i++ {
fss[i] = fuzz.FilesetDeep(nfiles, depth, false, true)
}
return fss
}
func createAssertions(n int) *reflow.Assertions {
fuzz := testutil.NewFuzz(nil)
m := make(map[reflow.AssertionKey]map[string]string, n)
for i := 0; i < n; i++ {
gk := reflow.AssertionKey{Subject: "subject-" + fuzz.StringMinLen(80, ""), Namespace: "namespace"}
m[gk] = map[string]string{
"object1": "value-" + fuzz.String(""),
"object2": "value-" + fuzz.String(""),
"object3": "value-" + fuzz.String(""),
}
}
return reflow.AssertionsFromMap(m)
}
var benchCombinations = []struct{ nfiles, nass int }{
{1, 100000},
{1, 10000},
{1, 1000},
{10, 10000},
{100, 1000},
}
func BenchmarkFileset(b *testing.B) {
fuzz := testutil.NewFuzz(nil)
for _, nums := range []struct{ nfiles, nass int }{
{1, 100000},
{1000, 10000},
{100000, 1000},
} {
fs := fuzz.FilesetDeep(nums.nfiles, 0, false, false)
a := createAssertions(nums.nass)
for _, s := range []struct {
name string
fn func() error
}{
{"writeassertions", func() error { _, err := reflow.MergeAssertions(fs.Assertions()...); return err }},
{"addassertions", func() error { return fs.AddAssertions(a) }},
} {
s, nums := s, nums
b.Run(fmt.Sprintf("%s-nfiles-%d-nass-%d", s.name, nums.nfiles, nums.nass), func(b *testing.B) {
for i := 0; i < b.N; i++ {
if err := s.fn(); err != nil {
b.Fatal(err)
}
}
})
}
}
}
func BenchmarkMarshal(b *testing.B) {
fuzz := testutil.NewFuzz(nil)
for _, nums := range benchCombinations {
fs := fuzz.FilesetDeep(nums.nfiles, 0, false, false)
a := createAssertions(nums.nass)
if err := fs.AddAssertions(a); err != nil {
b.Fatal(err)
}
want, err := json.Marshal(fs)
if err != nil {
b.Fatal(err)
}
for _, s := range []struct {
name string
marshaller func(v interface{}) ([]byte, error)
}{
{"json-std-lib", json.Marshal},
} {
s, nums := s, nums
b.Run(fmt.Sprintf("%s-nfiles-%d-nass-%d", s.name, nums.nfiles, nums.nass), func(b *testing.B) {
for i := 0; i < b.N; i++ {
got, err := s.marshaller(fs)
if err != nil {
b.Fatal(err)
}
if !bytes.Equal(got, want) {
b.Error("mismatch")
}
}
})
}
}
}
func BenchmarkUnmarshal(b *testing.B) {
fuzz := testutil.NewFuzz(nil)
for _, nums := range benchCombinations {
fs := fuzz.FilesetDeep(nums.nfiles, 0, false, false)
a := createAssertions(nums.nass)
if err := fs.AddAssertions(a); err != nil {
b.Fatal(err)
}
fssb, err := json.Marshal(fs)
if err != nil {
b.Fatal(err)
}
r := bytes.NewReader(fssb)
for _, s := range []struct {
name string
// unmarshaller func(data []byte, v interface{}) error
decode func(r io.Reader, v interface{}) error
}{
{"json-std-lib", func(r io.Reader, v interface{}) error {
return json.NewDecoder(r).Decode(v)
}},
} {
s, nums := s, nums
b.Run(fmt.Sprintf("%s-nfiles-%d-nass-%d", s.name, nums.nfiles, nums.nass), func(b *testing.B) {
for i := 0; i < b.N; i++ {
var gotfs reflow.Fileset
if err := s.decode(r, &gotfs); err != nil && err != io.EOF {
b.Fatal(err)
}
if got, want := gotfs, fs; !got.Equal(want) {
b.Error("mismatch")
}
r.Reset(fssb)
}
})
}
}
}