Debian Codegraph @

selfseed_test.go

raw
1 package desync
2
3 import (
4 "context"
5 "crypto/md5"
6 "crypto/rand"
7 "os"
8 "path/filepath"
9 "testing"
10
11 "github.com/stretchr/testify/require"
12 )
13
14 func TestSelfSeed(t *testing.T) {
15 // Setup a temporary store
16 store := t.TempDir()
17
18 s, err := NewLocalStore(store, StoreOptions{})
19 require.NoError(t, err)
20
21 // Build a number of fake chunks that can then be used in the test in any order
22 type rawChunk struct {
23 id ChunkID
24 data []byte
25 }
26 size := 1024
27 numChunks := 10
28 chunks := make([]rawChunk, numChunks)
29
30 for i := range numChunks {
31 b := make([]byte, size)
32 rand.Read(b)
33 chunk := NewChunk(b)
34 require.NoError(t, s.StoreChunk(chunk))
35 chunks[i] = rawChunk{chunk.ID(), b}
36 }
37
38 // Define tests with files with different content, by building files out
39 // of sets of byte slices to create duplication or not between the target and
40 // its seeds. Also define a min/max of bytes that should be cloned (from the
41 // self-seed). That number can vary since even with 1 worker goroutine there
42 // another feeder goroutine which can influence timings/results a little.
43 tests := map[string]struct {
44 index []int
45 minCloned int
46 maxCloned int
47 }{
48 "single chunk": {
49 index: []int{0},
50 minCloned: 0,
51 maxCloned: 0,
52 },
53 "repeating single chunk": {
54 index: []int{0, 0, 0, 0, 0},
55 minCloned: 3 * size,
56 maxCloned: 4 * size,
57 },
58 "repeating chunk sequence": {
59 index: []int{0, 1, 2, 0, 1, 2, 2},
60 minCloned: 4 * size,
61 maxCloned: 4 * size,
62 },
63 "repeating chunk sequence mid file": {
64 index: []int{1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3},
65 minCloned: 7 * size,
66 maxCloned: 7 * size,
67 },
68 "repeating chunk sequence reversed": {
69 index: []int{0, 1, 2, 2, 1, 0},
70 minCloned: 2 * size,
71 maxCloned: 3 * size,
72 },
73 "non-repeating chunks": {
74 index: []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
75 minCloned: 0,
76 maxCloned: 0,
77 },
78 }
79
80 for name, test := range tests {
81 t.Run(name, func(t *testing.T) {
82 // Build an index from the target chunks
83 var idx Index
84 var b []byte
85 for i, p := range test.index {
86 chunk := IndexChunk{
87 ID: chunks[p].id,
88 Start: uint64(i * size),
89 Size: uint64(size),
90 }
91 b = append(b, chunks[p].data...)
92 idx.Chunks = append(idx.Chunks, chunk)
93 }
94
95 // Calculate the expected checksum
96 sum := md5.Sum(b)
97
98 // Build a temp target file to extract into
99 dst := filepath.Join(t.TempDir(), "dst")
100
101 // Extract the file
102 stats, err := AssembleFile(context.Background(), dst, idx, s, nil,
103 AssembleOptions{1, InvalidSeedActionBailOut},
104 )
105 require.NoError(t, err)
106
107 // Compare the checksums to that of the input data
108 b, err = os.ReadFile(dst)
109 require.NoError(t, err)
110 require.Equal(t, sum, md5.Sum(b), "checksum of extracted file doesn't match expected")
111
112 // Compare to the expected number of bytes copied or cloned from the seed
113 fromSeed := int(stats.BytesCopied + stats.BytesCloned)
114 require.GreaterOrEqual(t, fromSeed, test.minCloned, "bytes copied/cloned from self-seed")
115 require.LessOrEqual(t, fromSeed, test.maxCloned, "bytes copied/cloned from self-seed")
116 })
117 }
118
119 }