Debian Codegraph @

sparse-file_test.go

raw
1 package desync
2
3 import (
4 "bytes"
5 "crypto/sha256"
6 "errors"
7 "math/rand"
8 "os"
9 "path/filepath"
10 "sync"
11 "testing"
12
13 "github.com/stretchr/testify/require"
14 )
15
16 // flakyStore wraps a Store and injects a fixed number of transient failures
17 // for a specific chunk before delegating to the underlying store.
18 type flakyStore struct {
19 Store
20 mu sync.Mutex
21 failID ChunkID
22 failsLeft int
23 }
24
25 func (s *flakyStore) GetChunk(id ChunkID) (*Chunk, error) {
26 s.mu.Lock()
27 if id == s.failID && s.failsLeft > 0 {
28 s.failsLeft--
29 s.mu.Unlock()
30 return nil, errors.New("injected transient failure")
31 }
32 s.mu.Unlock()
33 return s.Store.GetChunk(id)
34 }
35
36 func TestLoaderChunkRange(t *testing.T) {
37 idx := Index{
38 Chunks: []IndexChunk{
39 {Start: 0, Size: 10, ID: ChunkID{0}},
40 {Start: 10, Size: 10, ID: ChunkID{1}},
41 {Start: 20, Size: 10, ID: ChunkID{2}},
42 },
43 }
44
45 loader := newSparseFileLoader("", idx, nil)
46
47 tests := []struct {
48 // Input ranges
49 start int64
50 length int64
51
52 // Expected output (chunk positions and length)
53 first int
54 last int
55 }{
56 {start: 0, length: 0, first: 0, last: 0}, // empty read at the start
57 {start: 0, length: 1, first: 0, last: 0}, // one byte at the start
58 {start: 10, length: 1, first: 1, last: 1}, // first byte in the 2nd chunk
59 {start: 19, length: 1, first: 1, last: 1}, // last byte in the 2nd chunk
60 {start: 0, length: 20, first: 0, last: 1}, // first two whole chunks
61 {start: 5, length: 10, first: 0, last: 1}, // spanning first two chunks
62 {start: 0, length: 30, first: 0, last: 2}, // whole file
63 {start: 29, length: 0, first: 2, last: 2}, // empty read at the end
64 {start: 29, length: 1, first: 2, last: 2}, // one byte at the end
65 {start: 30, length: 1, first: 2, last: 2}, // read past the end
66 }
67
68 for _, test := range tests {
69 first, chunks := loader.indexRange(test.start, test.length)
70 require.Equal(t, test.first, first, "first chunk")
71 require.Equal(t, test.last, chunks, "number of chunks")
72 }
73 }
74
75 func TestSparseFileRead(t *testing.T) {
76 // Sparse output file
77 sparseFile := filepath.Join(t.TempDir(), "sparse")
78
79 // Open the store
80 s, err := NewLocalStore("testdata/blob1.store", StoreOptions{})
81 require.NoError(t, err)
82 defer s.Close()
83
84 // Read the index
85 indexFile, err := os.Open("testdata/blob1.caibx")
86 require.NoError(t, err)
87 defer indexFile.Close()
88 index, err := IndexFromReader(indexFile)
89 require.NoError(t, err)
90
91 // // Calculate the expected hash
92 b, err := os.ReadFile("testdata/blob1")
93 require.NoError(t, err)
94
95 // Initialize the sparse file and open a handle
96 sparse, err := NewSparseFile(sparseFile, index, s, SparseFileOptions{})
97 require.NoError(t, err)
98 h, err := sparse.Open()
99 require.NoError(t, err)
100 defer h.Close()
101
102 // Read a few random ranges and compare to the expected blob content
103 for range 1000 {
104 length := rand.Intn(int(index.Index.ChunkSizeMax))
105 offset := rand.Intn(int(index.Length()) - length - 1)
106
107 fromSparse := make([]byte, length)
108 fromBlob := make([]byte, length)
109
110 _, err := h.ReadAt(fromSparse, int64(offset))
111 require.NoError(t, err)
112
113 _, err = bytes.NewReader(b).ReadAt(fromBlob, int64(offset))
114 require.NoError(t, err)
115
116 require.Equal(t, fromBlob, fromSparse)
117 }
118
119 // Read the whole file. After this it should match the whole blob
120 whole := make([]byte, index.Length())
121 _, err = h.ReadAt(whole, 0)
122 require.NoError(t, err)
123
124 blobHash := sha256.Sum256(b)
125 sparseHash := sha256.Sum256(whole)
126 require.Equal(t, blobHash, sparseHash)
127 }
128
129 // TestSparseFileRetryAfterFailedLoad ensures that a transient fetch failure
130 // for a chunk does not permanently poison its region of the sparse file. The
131 // failed load must surface an error, and a subsequent read must retry and
132 // return the real chunk data rather than the zeroed (never-written) region.
133 func TestSparseFileRetryAfterFailedLoad(t *testing.T) {
134 sparseFile := filepath.Join(t.TempDir(), "sparse")
135
136 s, err := NewLocalStore("testdata/blob1.store", StoreOptions{})
137 require.NoError(t, err)
138 defer s.Close()
139
140 indexFile, err := os.Open("testdata/blob1.caibx")
141 require.NoError(t, err)
142 defer indexFile.Close()
143 index, err := IndexFromReader(indexFile)
144 require.NoError(t, err)
145
146 b, err := os.ReadFile("testdata/blob1")
147 require.NoError(t, err)
148
149 // Pick the first chunk that actually needs fetching from the store. Null
150 // chunks are served from the truncated sparse file and never hit GetChunk.
151 nullChunk := NewNullChunk(index.Index.ChunkSizeMax)
152 var target IndexChunk
153 found := false
154 for _, c := range index.Chunks {
155 if c.ID != nullChunk.ID {
156 target = c
157 found = true
158 break
159 }
160 }
161 require.True(t, found, "expected at least one non-null chunk")
162
163 // Store that fails the first GetChunk for the target chunk, then works.
164 flaky := &flakyStore{Store: s, failID: target.ID, failsLeft: 1}
165
166 sparse, err := NewSparseFile(sparseFile, index, flaky, SparseFileOptions{})
167 require.NoError(t, err)
168 h, err := sparse.Open()
169 require.NoError(t, err)
170 defer h.Close()
171
172 buf := make([]byte, target.Size)
173
174 // First read of the target chunk's range must surface the transient error.
175 _, err = h.ReadAt(buf, int64(target.Start))
176 require.Error(t, err)
177
178 // The retry must actually load the chunk and return real data, not the
179 // zeroed sparse-file region.
180 _, err = h.ReadAt(buf, int64(target.Start))
181 require.NoError(t, err)
182
183 expected := make([]byte, target.Size)
184 _, err = bytes.NewReader(b).ReadAt(expected, int64(target.Start))
185 require.NoError(t, err)
186 require.Equal(t, expected, buf)
187 }