-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_test.go
1260 lines (1121 loc) · 36.5 KB
/
run_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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright © 2018 Chad Netzer <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package hardlinkable
import (
"fmt"
"io/ioutil"
"math/big"
"math/rand"
"os"
"path"
"reflect"
"sort"
"strings"
"syscall"
"testing"
"time"
"github.com/pkg/xattr"
)
// ShuffleString returns a random shuffle of a given string (for test case
// generation uses; complex unicode will likely confuse it)
func ShuffleString(s string) string {
b := []rune(s)
dest := make([]rune, len(b))
perm := rand.Perm(len(b))
for i, v := range perm {
dest[v] = b[i]
}
return string(dest)
}
// Algorithm from http://www.quickperm.org/
// Output of emptyset is nil
func permutations(a []string) <-chan []string {
out := make(chan []string)
go func() {
defer close(out)
if len(a) == 0 {
return
}
// Output initial (ie. non-permuted) ordering as first result
r := make([]string, len(a))
copy(r, a)
out <- r
// Init permutation index array
N := len(a)
p := make([]int, N+1)
for i := 0; i < N+1; i++ {
p[i] = i
}
// Loop over a, swapping and updating perm array
i := 1
for i < N {
var j int
p[i]--
j = (i % 2) * p[i]
a[j], a[i] = a[i], a[j]
for i = 1; p[i] == 0; i++ {
p[i] = i
}
r := make([]string, len(a))
copy(r, a)
out <- r
}
}()
return out
}
func TestPermutations(t *testing.T) {
testData := []struct {
p []string
want int
}{
{[]string{}, 0},
{[]string{"a"}, 1},
{[]string{"a", "b"}, 2},
{[]string{"a", "b", "c"}, 6},
{[]string{"c", "b", "a"}, 6},
{[]string{"b", "a", "c"}, 6},
{[]string{"a", "b", "c", "d"}, 24},
{[]string{"a", "b", "c", "d", "e"}, 120},
}
for _, d := range testData {
seen := make(map[string]struct{})
for v := range permutations(d.p) {
// convert v to string (for storing to map for testing)
j := strings.Join(v, " ")
if _, ok := seen[j]; ok {
t.Errorf("Repeated permutation found: %v\n", v)
}
seen[j] = struct{}{}
}
if len(seen) != d.want {
t.Errorf("Expected %v permutations for %v, got: %v", d.want, d.p, len(seen))
}
}
}
// Does not output the empty set
func powerset(s []string) <-chan []string {
out := make(chan []string)
go func() {
defer close(out)
prevSets := [][]string{[]string{}}
for _, s := range s {
for _, p := range prevSets {
newSet := append([]string{}, p...)
newSet = append(newSet, s)
prevSets = append(prevSets, newSet)
out <- newSet
}
}
}()
return out
}
func TestPowersets(t *testing.T) {
testData := []struct {
p []string
want int
}{
{[]string{}, 0},
{[]string{"a"}, 1},
{[]string{"a", "b"}, 3},
{[]string{"a", "b", "c"}, 7},
{[]string{"b", "c", "a"}, 7},
{[]string{"c", "a", "b"}, 7},
{[]string{"a", "b", "c", "d"}, 15},
{[]string{"a", "b", "c", "d", "e"}, 31},
}
for _, d := range testData {
N := len(d.p)
lengthCounts := map[int]int{}
seen := map[string]struct{}{}
for v := range powerset(d.p) {
// convert v to string (for storing to map for testing)
j := strings.Join(v, " ")
if _, ok := seen[j]; ok {
t.Errorf("Repeated powerset found: %v\n", v)
}
seen[j] = struct{}{}
lengthCounts[len(v)]++
}
if len(seen) != d.want {
t.Errorf("Expected %v powersets for %v, got: %v", d.want, d.p, len(seen))
}
var z big.Int
possibleLengthCounts := map[int]int{}
for i := 1; i <= N; i++ {
// Compute binomial coeffs (ie. Pascal's triangle) for input length.
n := int(z.Binomial(int64(N), int64(i)).Int64())
possibleLengthCounts[i] = n
}
if !reflect.DeepEqual(lengthCounts, possibleLengthCounts) {
t.Errorf("Incorrect powerset lengths: expected %v, got %v", possibleLengthCounts, lengthCounts)
}
}
}
func powersetPerms(s []string) <-chan []string {
out := make(chan []string)
go func() {
defer close(out)
for v := range powerset(s) {
for p := range permutations(v) {
out <- p
}
}
}()
return out
}
func TestPowersetPerms(t *testing.T) {
testData := []struct {
p []string
want int
}{
// "want" lengths from https://oeis.org/A007526
// a(n) = n*(a(n) + 1)
{[]string{"a"}, 1},
{[]string{"a", "b"}, 4},
{[]string{"b", "a"}, 4},
{[]string{"a", "b", "c"}, 15},
{[]string{"c", "a", "b"}, 15},
{[]string{"b", "c", "a"}, 15},
{[]string{"a", "b", "c", "d"}, 64},
{[]string{"a", "b", "c", "d", "e"}, 325},
}
for _, d := range testData {
seen := map[string]struct{}{}
for v := range powersetPerms(d.p) {
j := strings.Join(v, "")
if _, ok := seen[j]; ok {
t.Errorf("Repeated powersetPerm found: %v\n", v)
}
seen[j] = struct{}{}
}
if len(seen) != d.want {
t.Errorf("Expected %v powersetPerms for %v, got: %v", d.want, d.p, len(seen))
}
}
}
func setUp(name string, t *testing.T) string {
topdir, err := ioutil.TempDir("", "hardlinkable")
if err != nil {
t.Fatalf("Couldn't create temp dir for %v tests: %v", topdir, err)
}
if os.Chdir(topdir) != nil {
t.Fatalf("Couldn't chdir to temp dir for %v tests", topdir)
}
return topdir
}
type stringSet map[string]struct{}
func newSet(args ...string) stringSet {
set := make(stringSet)
for _, s := range args {
set[s] = struct{}{}
}
return set
}
// Keeps it simple and doesn't worry about performance
func intersection(a, b stringSet) stringSet {
r := make(stringSet)
for s := range a {
if _, ok := b[s]; ok {
r[s] = struct{}{}
}
}
return r
}
func simpleRun(name string, t *testing.T, opts Options, numLinkPaths int, dirs ...string) *Results {
result, err := Run(dirs, opts)
if err != nil {
t.Errorf("%v: Run() returned error: %v\n", name, err)
}
if !result.RunSuccessful {
t.Errorf("%v: Run() was not successful (aborted early)", name)
}
if len(result.LinkPaths) != numLinkPaths {
t.Errorf("%v: len(LinkPaths) expected %v: got: %v\n", name, numLinkPaths, len(result.LinkPaths))
}
return &result
}
type paths []string
type pathContents map[string]string // pathname:contents
type existingLinks map[string]paths // pathname:pathnames
type linkedPaths []paths
type linkedPathsOptions []linkedPaths
// provided with a map of filenames:content, create the files
func simpleFileMaker(t *testing.T, m pathContents) {
now := time.Now()
for name, content := range m {
dirname := path.Dir(name)
err := os.MkdirAll(dirname, 0755)
if err != nil {
t.Fatalf("Couldn't create test dir '%v': %v", name, err)
}
if err := ioutil.WriteFile(name, []byte(content), 0644); err != nil {
t.Fatalf("Couldn't create test file '%v'", name)
}
if err := os.Chtimes(name, now, now); err != nil {
t.Fatalf("Couldn't Chtimes() on test file '%v'", name)
}
}
}
// provided with a map of filenames:content, create the files
func simpleLinkMaker(t *testing.T, src string, dsts ...string) {
for _, dst := range dsts {
dirname := path.Dir(dst)
err := os.MkdirAll(dirname, 0755)
if err != nil {
t.Fatalf("Couldn't create test dir '%v': %v", dirname, err)
}
if err := os.Link(src, dst); err != nil {
t.Fatalf("Couldn't create test link '%v' to '%v'", src, dst)
}
}
}
func nlinkVal(pathname string) uint32 {
l, err := os.Lstat(pathname)
if err != nil {
return 0
}
statT, ok := l.Sys().(*syscall.Stat_t)
if !ok {
return 0
}
return uint32(statT.Nlink)
}
func verifyLinkPaths(name string, t *testing.T, r *Results, p paths) bool {
if len(p) == 0 && len(r.LinkPaths) > 0 {
t.Errorf("%v: Expected empty LinkPaths, got: %v\n", name, r.LinkPaths)
return false
}
if len(p) == 0 {
return true
}
pathsSet := newSet(p...)
for _, l := range r.LinkPaths {
lSet := newSet(l...)
overlap := intersection(pathsSet, lSet)
if len(overlap) == len(p) {
return true
}
}
return false
}
func verifyInodeCounts(name string, t *testing.T, r *Results, inoRemovedCount int64, inoRemovedBytes uint64, nlinkCount uint32, filenames ...string) {
if r.InodeRemovedCount != inoRemovedCount {
t.Errorf("%v: InodeRemovedCount expected: %v, got: %v\n", name, inoRemovedCount, r.InodeRemovedCount)
}
if r.InodeRemovedByteAmount != inoRemovedBytes {
t.Errorf("%v: InodeRemovedByteAmount expected: %v, got: %v\n", name, inoRemovedBytes, r.InodeRemovedByteAmount)
}
for _, filename := range filenames {
if nlinkVal(filename) != nlinkCount {
t.Errorf("%v: Inode nlink count for '%v' expected: %v, got: %v\n", name, filename, nlinkCount, nlinkVal(filename))
}
}
}
func verifyContents(name string, t *testing.T, m pathContents) {
for pathname, content := range m {
readContent, err := ioutil.ReadFile(pathname)
if err != nil {
t.Fatalf("%v: Couldn't read test file contents: %v\n", name, pathname)
}
if content != string(readContent) {
t.Errorf("%v: Mismatched content for: %v, expected: %v, got: %v\n",
name, pathname, content, string(readContent))
}
}
}
func numNonEmpty(lpo linkedPathsOptions) int {
var count int
if len(lpo) == 0 {
return 0
}
for _, s := range lpo[0] {
if len(s) > 0 {
count++
}
}
return count
}
func TestRunLinkingTable(t *testing.T) {
tsts := []struct {
name string
opts Options
c pathContents
l existingLinks
lpo linkedPathsOptions
inoRemovedCount int
inoRemovedBytes int
nlinkCounts map[int]paths
}{
{
name: "testname: 'Linking Disabled'",
opts: SetupOptions(),
c: pathContents{"f1": "X", "f2": "X"},
l: existingLinks{"f2": paths{"f3"}},
lpo: linkedPathsOptions{
linkedPaths{paths{"f1", "f2"}},
linkedPaths{paths{"f1", "f3"}},
},
inoRemovedCount: 1,
inoRemovedBytes: 1,
nlinkCounts: map[int]paths{
1: paths{"f1"},
2: paths{"f2", "f3"},
},
},
{
name: "testname: 'No Files'",
opts: SetupOptions(LinkingEnabled),
c: pathContents{},
l: existingLinks{},
lpo: linkedPathsOptions{linkedPaths{paths{}}},
inoRemovedCount: 0,
inoRemovedBytes: 0,
nlinkCounts: map[int]paths{},
},
{
name: "testname: 'One File'",
opts: SetupOptions(LinkingEnabled),
c: pathContents{"f1": "X"},
l: existingLinks{},
lpo: linkedPathsOptions{linkedPaths{paths{}}},
inoRemovedCount: 0,
inoRemovedBytes: 0,
nlinkCounts: map[int]paths{1: paths{"f1"}},
},
{
name: "testname: 'Two Equal Files'",
opts: SetupOptions(LinkingEnabled),
c: pathContents{"f1": "X", "f2": "X"},
l: existingLinks{},
lpo: linkedPathsOptions{linkedPaths{paths{"f1", "f2"}}},
inoRemovedCount: 1,
inoRemovedBytes: 1,
nlinkCounts: map[int]paths{2: paths{"f1", "f2"}},
},
{
name: "testname: 'Two Unequal Files'",
opts: SetupOptions(LinkingEnabled),
c: pathContents{"f1": "X", "f2": "Y"},
l: existingLinks{},
lpo: linkedPathsOptions{linkedPaths{paths{}}},
inoRemovedCount: 0,
inoRemovedBytes: 0,
nlinkCounts: map[int]paths{1: paths{"f1", "f2"}},
},
{
name: "testname: 'Two Equal Files One Existing Link'",
opts: SetupOptions(LinkingEnabled),
c: pathContents{"f1": "X", "f2": "X"},
l: existingLinks{"f2": paths{"f3"}},
lpo: linkedPathsOptions{
linkedPaths{paths{"f1", "f2"}},
linkedPaths{paths{"f1", "f3"}},
},
inoRemovedCount: 1,
inoRemovedBytes: 1,
nlinkCounts: map[int]paths{3: paths{"f1", "f2", "f3"}},
},
{
name: "testname: 'Two Groups of Equal Files'",
opts: SetupOptions(LinkingEnabled),
c: pathContents{"f1": "X", "f2": "X", "f3": "YY", "f4": "YY", "f5": "YY"},
l: existingLinks{},
lpo: linkedPathsOptions{
linkedPaths{paths{"f1", "f2"}, paths{"f3", "f4", "f5"}},
},
inoRemovedCount: 3,
inoRemovedBytes: 5,
nlinkCounts: map[int]paths{
2: paths{"f1", "f2"},
3: paths{"f3", "f4", "f5"},
},
},
{
name: "testname: 'One File With Two Existing Links'",
opts: SetupOptions(LinkingEnabled),
c: pathContents{"f1": "X"},
l: existingLinks{"f1": paths{"f2", "f3"}},
lpo: linkedPathsOptions{linkedPaths{paths{}}},
inoRemovedCount: 0,
inoRemovedBytes: 0,
nlinkCounts: map[int]paths{3: paths{"f1", "f2", "f3"}},
},
{
name: "testname: 'Two Files With One Existing Link'",
opts: SetupOptions(LinkingEnabled),
c: pathContents{"f1": "X", "f2": "X"},
l: existingLinks{"f1": paths{"f3"}},
lpo: linkedPathsOptions{
linkedPaths{paths{"f1", "f2"}},
linkedPaths{paths{"f1", "f3"}},
},
inoRemovedCount: 1,
inoRemovedBytes: 1,
nlinkCounts: map[int]paths{3: paths{"f1", "f2", "f3"}},
},
{
name: "testname: 'Equal Filenames Only'",
opts: SetupOptions(LinkingEnabled, SameName),
c: pathContents{"A/f1": "X", "B/f1": "X", "B/f2": "X"},
l: existingLinks{"A/f1": paths{"A/f0", "A/f3", "B/a0", "B/f100"}},
lpo: linkedPathsOptions{linkedPaths{paths{"A/f1", "B/f1"}}},
inoRemovedCount: 1,
inoRemovedBytes: 1,
nlinkCounts: map[int]paths{6: paths{"A/f1", "B/f1", "A/f0", "A/f3", "B/a0", "B/f100"}},
},
{
name: "testname: 'Equal Filenames No Removed Inodes test'",
opts: SetupOptions(LinkingEnabled, SameName),
c: pathContents{"A/f1": "X", "B/f1": "X", "B/f2": "X"},
l: existingLinks{
"A/f1": paths{"A/f0", "A/f3"},
},
lpo: linkedPathsOptions{linkedPaths{paths{"A/f1", "B/f1"}}},
inoRemovedCount: 1,
inoRemovedBytes: 1,
nlinkCounts: map[int]paths{
4: paths{"A/f1", "B/f1", "A/f0", "A/f3"},
},
},
{
name: "testname: 'Equal Filenames No Removed Inodes'",
opts: SetupOptions(LinkingEnabled, SameName),
c: pathContents{"A/f1": "X", "B/f1": "X", "B/f2": "X"},
l: existingLinks{
"A/f1": paths{"A/f0", "A/f3"},
"B/f1": paths{"B/a0", "B/f100"},
},
lpo: linkedPathsOptions{linkedPaths{paths{"A/f1", "B/f1"}}},
inoRemovedCount: 0,
inoRemovedBytes: 0,
nlinkCounts: map[int]paths{
4: paths{"A/f1", "B/f1"},
},
},
}
for _, tst := range tsts {
func() {
topdir := setUp("Run", t)
defer os.RemoveAll(topdir)
simpleFileMaker(t, tst.c)
for src, dsts := range tst.l {
simpleLinkMaker(t, src, dsts...)
}
result := simpleRun(tst.name, t, tst.opts, numNonEmpty(tst.lpo), ".")
verified := false
VerifiedTest:
for _, lp := range tst.lpo {
for _, l := range lp {
if verifyLinkPaths(tst.name, t, result, l) {
verified = true
break VerifiedTest
}
}
}
if !verified {
t.Errorf("%v: Couldn't find expected LinkPaths: %v in results: %v\n", tst.name, tst.lpo, result.LinkPaths)
}
// Note the values of the result, reporting what could be done, and the
// difference from the nlinks read off the disk (which should be
// unaltered, since linking was disabled)
for k, v := range tst.nlinkCounts {
verifyInodeCounts(tst.name, t, result,
int64(tst.inoRemovedCount),
uint64(tst.inoRemovedBytes),
uint32(k), v...)
}
verifyContents(tst.name, t, tst.c)
}()
}
}
func TestRunLinkedFileOutsideOfWalk(t *testing.T) {
topdir := setUp("Run", t)
defer os.RemoveAll(topdir)
opts := SetupOptions(LinkingEnabled)
name := "testname: 'LinkedFileOutsideOfWalk'"
// Test linked file outside of walked tree
m := pathContents{"A/f1": "X"}
simpleFileMaker(t, m)
simpleLinkMaker(t, "A/f1", "B/f2")
result := simpleRun(name, t, opts, 0, "A")
verifyInodeCounts(name, t, result, 0, 0, 2, "A/f1")
m["B/f2"] = "X"
verifyContents(name, t, m)
if result.ExistingLinkCount != 0 {
t.Errorf("Out of tree links counted, expected 0, got %v\n", result.ExistingLinkCount)
}
if result.ExistingLinkByteAmount != 0 {
t.Errorf("Out of tree linked bytes, expected 0, got %v\n", result.ExistingLinkByteAmount)
}
}
func TestRunTwoDifferentTimes(t *testing.T) {
topdir := setUp("Run", t)
defer os.RemoveAll(topdir)
opts := SetupOptions(LinkingEnabled)
name := "testname: 'Two Different File Times'"
m := pathContents{"f1": "X", "f2": "X"}
simpleFileMaker(t, m)
now := time.Now()
then := now.AddDate(-1, 0, 0)
if err := os.Chtimes("f2", then, then); err != nil {
t.Fatalf("Failure to set time on test file: 'f2'\n")
}
result := simpleRun(name, t, opts, 0, ".")
verifyLinkPaths(name, t, result, paths{})
verifyInodeCounts(name, t, result, 0, 0, 1, "f1", "f2")
verifyContents(name, t, m)
}
func TestRunTwoDifferentTimesIgnoreTime(t *testing.T) {
topdir := setUp("Run", t)
defer os.RemoveAll(topdir)
opts := SetupOptions(LinkingEnabled, IgnoreTime)
name := "testname: 'Two Unequal File Times w/ IgnoreTime'"
m := pathContents{"f1": "X", "f2": "X"}
simpleFileMaker(t, m)
now := time.Now()
then := now.AddDate(-1, 0, 0)
if err := os.Chtimes("f2", then, then); err != nil {
t.Fatalf("Failure to set time on test file: 'f2'\n")
}
result := simpleRun(name, t, opts, 1, ".")
verifyLinkPaths(name, t, result, paths{"f1", "f2"})
verifyInodeCounts(name, t, result, 1, 1, 2, "f1", "f2")
verifyContents(name, t, m)
}
func TestRunIgnorePerm(t *testing.T) {
topdir := setUp("Run", t)
defer os.RemoveAll(topdir)
opts := SetupOptions(LinkingEnabled, IgnorePerm)
name := "testname: 'Two Unequal File Modes w/ IgnorePerm'"
m := pathContents{"f1": "X", "f2": "X"}
simpleFileMaker(t, m)
if err := os.Chmod("f1", 0644); err != nil {
t.Fatalf("Couldn't set file 'f1' mode to '0644': %v", err)
}
if err := os.Chmod("f2", 0755); err != nil {
t.Fatalf("Couldn't set file 'f2' mode to '0755': %v", err)
}
result := simpleRun(name, t, opts, 1, ".")
verifyLinkPaths(name, t, result, paths{"f1", "f2"})
verifyInodeCounts(name, t, result, 1, 1, 2, "f1", "f2")
verifyContents(name, t, m)
}
func TestRunExcludeFiles(t *testing.T) {
topdir := setUp("Run", t)
defer os.RemoveAll(topdir)
opts := SetupOptions(LinkingEnabled)
opts.FileExcludes = append(opts.FileExcludes, `.*\.ext$`, `^prefix_.*`)
name := "testname: 'Exclude Files'"
m := pathContents{"f1": "X", "f2": "X", "f3.ext": "X", "prefix_f4": "X"}
simpleFileMaker(t, m)
result := simpleRun(name, t, opts, 1, ".")
verifyLinkPaths(name, t, result, paths{"f1", "f2"})
verifyInodeCounts(name, t, result, 1, 1, 2, "f1", "f2")
verifyContents(name, t, m)
}
func TestRunExcludeDirs(t *testing.T) {
topdir := setUp("Run", t)
defer os.RemoveAll(topdir)
opts := SetupOptions(LinkingEnabled)
opts.DirExcludes = append(opts.DirExcludes, `^A.*`, `.*B$`)
name := "testname: 'Exclude Dirs'"
m := pathContents{"Aetc/f1": "X", "preB/f2": "X", "etcA/f1": "X", "Bpre/f2": "X"}
simpleFileMaker(t, m)
result := simpleRun(name, t, opts, 1, ".")
verifyLinkPaths(name, t, result, paths{"etcA/f1", "Bpre/f2"})
verifyInodeCounts(name, t, result, 1, 1, 2, "etcA/f1", "Bpre/f2")
verifyContents(name, t, m)
}
func TestRunIncludeFiles(t *testing.T) {
topdir := setUp("Run", t)
defer os.RemoveAll(topdir)
opts := SetupOptions(LinkingEnabled)
opts.FileIncludes = append(opts.FileIncludes, `.*\.ext$`, `^prefix_.*`)
name := "testname: 'Include Files'"
m := pathContents{"f1": "X", "f2": "X", "f3.ext": "X", "prefix_f4": "X"}
simpleFileMaker(t, m)
result := simpleRun(name, t, opts, 1, ".")
verifyLinkPaths(name, t, result, paths{"f3.ext", "prefix_f4"})
verifyInodeCounts(name, t, result, 1, 1, 2, "f3.ext", "prefix_f4")
verifyContents(name, t, m)
}
func TestRunReincludeExcludedFiles(t *testing.T) {
topdir := setUp("Run", t)
defer os.RemoveAll(topdir)
opts := SetupOptions(LinkingEnabled)
opts.FileExcludes = append(opts.FileExcludes, `.*\.ext$`, `^prefix_.*`)
opts.FileIncludes = append(opts.FileIncludes, `^prefix_.*`)
name := "testname: 'Include Files'"
m := pathContents{"f1": "X", "f2": "X", "f3.ext": "X", "prefix_f4": "X"}
simpleFileMaker(t, m)
result := simpleRun(name, t, opts, 1, ".")
verifyLinkPaths(name, t, result, paths{"f1", "f2", "prefix_f4"})
verifyInodeCounts(name, t, result, 2, 2, 3, "f1", "f2", "prefix_f4")
verifyContents(name, t, m)
}
func TestRunMinMaxSize(t *testing.T) {
topdir := setUp("Run", t)
defer os.RemoveAll(topdir)
opts := SetupOptions(LinkingEnabled, MinFileSize(2), MaxFileSize(2))
name := "testname: 'Min/Max File Sizes'"
m := pathContents{"f1": "X", "f2": "X", "f3": "YY", "f4": "YY", "f5": "ZZZ", "f6": "ZZZ"}
simpleFileMaker(t, m)
result := simpleRun(name, t, opts, 1, ".")
verifyLinkPaths(name, t, result, paths{"f3", "f4"})
verifyInodeCounts(name, t, result, 1, 2, 2, "f3", "f4")
verifyContents(name, t, m)
}
func TestRunExcludedMinMaxSize(t *testing.T) {
topdir := setUp("Run", t)
defer os.RemoveAll(topdir)
opts := SetupOptions(LinkingEnabled, MinFileSize(2), MaxFileSize(2))
name := "testname: 'Excluded Min/Max File Sizes'"
m := pathContents{"f1": "X", "f2": "X", "f5": "ZZZ", "f6": "ZZZ"}
simpleFileMaker(t, m)
result := simpleRun(name, t, opts, 0, ".")
verifyLinkPaths(name, t, result, paths{})
verifyInodeCounts(name, t, result, 0, 0, 1, "f1", "f2", "f5", "f6")
verifyContents(name, t, m)
}
func TestRunZeroMinSize(t *testing.T) {
topdir := setUp("Run", t)
defer os.RemoveAll(topdir)
opts := SetupOptions(LinkingEnabled, MinFileSize(0), MaxFileSize(1))
name := "testname: 'Zero Min File Size'"
m := pathContents{"f1": "", "f2": ""}
simpleFileMaker(t, m)
result := simpleRun(name, t, opts, 1, ".")
verifyLinkPaths(name, t, result, paths{"f1", "f2"})
verifyInodeCounts(name, t, result, 1, 0, 2, "f1", "f2")
verifyContents(name, t, m)
}
func TestRunCrossedMinMaxSize(t *testing.T) {
topdir := setUp("Run", t)
defer os.RemoveAll(topdir)
const min = 2
const max = 1
opts := SetupOptions(LinkingEnabled, MinFileSize(min), MaxFileSize(max))
result, err := Run([]string{"."}, opts)
if err == nil {
t.Errorf("Run succeeded with incorrect min(%v) and max(%v) size options\n", min, max)
}
if result.RunSuccessful {
t.Errorf("Run result was 'successful' with improper min(%v) and max(%v) size options\n", min, max)
}
}
func TestRunEqualXAttrs(t *testing.T) {
topdir := setUp("Run", t)
defer os.RemoveAll(topdir)
opts := SetupOptions(LinkingEnabled)
name := "testname: 'Equal Xattrs'"
m := pathContents{"f1": "X", "f2": "X"}
simpleFileMaker(t, m)
if err := xattr.Set("f1", "user.foo", []byte{'b', 'a', 'r'}); err != nil {
t.Fatalf("Couldn't set xattr on test file: 'f1', 'user.foo':'bar' %v\n", err)
}
if err := xattr.Set("f2", "user.foo", []byte{'b', 'a', 'r'}); err != nil {
t.Fatalf("Couldn't set xattr on test file: 'f2', 'user.foo':'bar' %v\n", err)
}
if err := xattr.Set("f1", "user.baz", []byte{'a', 'b', 'c'}); err != nil {
t.Fatalf("Couldn't set xattr on test file: 'f1', 'user.baz':'abc' %v\n", err)
}
if err := xattr.Set("f2", "user.baz", []byte{'a', 'b', 'c'}); err != nil {
t.Fatalf("Couldn't set xattr on test file: 'f2', 'user.baz':'abc' %v\n", err)
}
result := simpleRun(name, t, opts, 1, ".")
verifyLinkPaths(name, t, result, paths{"f1", "f2"})
verifyInodeCounts(name, t, result, 1, 1, 2, "f1", "f2")
verifyContents(name, t, m)
}
func TestRunUnequalXAttrs(t *testing.T) {
topdir := setUp("Run", t)
defer os.RemoveAll(topdir)
opts := SetupOptions(LinkingEnabled)
name := "testname: 'Unequal Xattrs'"
m := pathContents{"f1": "X", "f2": "X"}
simpleFileMaker(t, m)
if err := xattr.Set("f1", "user.foo", []byte{'b', 'a', 'r'}); err != nil {
t.Fatalf("Couldn't set xattr on test file: 'f1', 'user.foo':'bar' %v\n", err)
}
if err := xattr.Set("f2", "user.baz", []byte{'a', 'b', 'c'}); err != nil {
t.Fatalf("Couldn't set xattr on test file: 'f2', 'user.baz':'abc' %v\n", err)
}
result := simpleRun(name, t, opts, 0, ".")
verifyLinkPaths(name, t, result, paths{})
verifyInodeCounts(name, t, result, 0, 0, 1, "f1", "f2")
verifyContents(name, t, m)
}
func TestRunEqualXAttrsIgnoreXAttr(t *testing.T) {
topdir := setUp("Run", t)
defer os.RemoveAll(topdir)
opts := SetupOptions(LinkingEnabled, IgnoreXAttr)
name := "testname: 'Unequal Xattrs w/ IgnoreXattr'"
m := pathContents{"f1": "X", "f2": "X"}
simpleFileMaker(t, m)
if err := xattr.Set("f1", "user.foo", []byte{'b', 'a', 'r'}); err != nil {
t.Fatalf("Couldn't set xattr on test file: 'f1', 'user.foo':'bar' %v\n", err)
}
if err := xattr.Set("f2", "user.foo", []byte{'x', 'y', 'z'}); err != nil {
t.Fatalf("Couldn't set xattr on test file: 'f2', 'user.foo':'xyz' %v\n", err)
}
if err := xattr.Set("f2", "user.baz", []byte{'a', 'b', 'c'}); err != nil {
t.Fatalf("Couldn't set xattr on test file: 'f2', 'user.baz':'abc' %v\n", err)
}
result := simpleRun(name, t, opts, 1, ".")
verifyLinkPaths(name, t, result, paths{"f1", "f2"})
verifyInodeCounts(name, t, result, 1, 1, 2, "f1", "f2")
verifyContents(name, t, m)
}
func TestRunLinearVsDigestSearch(t *testing.T) {
topdir := setUp("Run", t)
defer os.RemoveAll(topdir)
// Note - linking disabled to allow re-running multiple times
opts := SetupOptions(LinkingDisabled)
m := pathContents{
"f1": "X", "f2": "X",
"f3": "YY", "f4": "YY",
"f5": "ZZZ", "f6": "ZZZ",
"a1": "A", "a2": "A", "a3": "A", "a4": "A", "a5": "A",
"a6": "A", "a7": "A", "a8": "A", "a9": "A", "a10": "A",
"b1": "B", "b2": "B", "b3": "B", "b4": "B", "b5": "B",
"b6": "B", "b7": "B", "b8": "B", "b9": "B", "b10": "B",
"c1": "C", "c2": "C", "c3": "C", "c4": "C", "c5": "C",
"c6": "C", "c7": "C", "c8": "C", "c9": "C", "c10": "C",
}
simpleFileMaker(t, m)
// Confirm that results match for different max linear search lengths
for i := -1; i < 12; i++ {
name := fmt.Sprintf("testname: 'Linear Vs Digest Search' val=%v", i)
opts.SearchThresh = i
result := simpleRun(name, t, opts, 6, ".")
verifyLinkPaths(name, t, result, paths{"f1", "f2"})
verifyLinkPaths(name, t, result, paths{"f3", "f4"})
verifyLinkPaths(name, t, result, paths{"f5", "f6"})
verifyInodeCounts(name, t, result, 30, 33, 1)
verifyContents(name, t, m)
}
}
type PathnameSet map[string]struct{} // string = pathname
type Clusters []PathnameSet
func newPathnameSet(s string) PathnameSet {
ps := PathnameSet{}
ps[s] = struct{}{}
return ps
}
// Add newPath to the cluster containing prevPath
func (c Clusters) addToCluster(prevPath, newPath string) {
for _, m := range c {
if _, ok := m[prevPath]; ok {
m[newPath] = struct{}{}
break
}
}
}
type randTestVals struct {
minSize int
maxSize int
numDirs int64
numFiles int64
numNewLinks int64
numExistingLinks int64
numInodes int64
numNlinks int64
linkPathsBytes uint64
existingLinksBytes uint64
pc pathContents // pathname:contents map
contentPaths map[string][]string // contents:[]pathname map
contentClusters map[string]Clusters // contents:Clusters
// A set of all the file contents we've used, and their usage count
contents map[string]int // contents:fileCount
}
func newRandTestVals() *randTestVals {
return &randTestVals{
pc: make(pathContents),
contentPaths: make(map[string][]string),
contentClusters: make(map[string]Clusters),
contents: make(map[string]int),
}
}
func setupRandTestFiles(t *testing.T, topdir string, samename bool) *randTestVals {
r := newRandTestVals()
// Use "go test -count=1" to disable test result caching, otherwise the
// tests will not run with a new seed.
seed := time.Now().UnixNano()
rand.Seed(seed)
const maxContentLen = (1 << 18)
const maxContentIndex = maxContentLen - 1
// Generate a bunch of random bytes
contentSrc := make([]byte, maxContentLen)
rand.Read(contentSrc)
// Setup min/max file sizes
if samename {
// Using samename option with excluded files due to size restrictions
// affects the end result of the linking (because the on-disk files are
// sorted by nlink, which are not reflected in the cluster sorting.
// For now, work around by disabling file size restrictions for
// samename mode.
r.maxSize = 0
r.minSize = 0
} else {
r.maxSize = rand.Intn(maxContentIndex) + 1
r.minSize = rand.Intn(r.maxSize)
}
dirnameChars := ShuffleString("ABC")
filenameChars := ShuffleString("abcde")
for dirs := range powersetPerms(strings.Split(dirnameChars, "")) {
newDir := true
for files := range powersetPerms(strings.Split(filenameChars, "")) {
dirname := strings.Join(dirs, "")
filename := strings.Join(files, "")
pathname := path.Join(dirname, filename)