-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgranular_test.go
More file actions
419 lines (342 loc) · 11.5 KB
/
granular_test.go
File metadata and controls
419 lines (342 loc) · 11.5 KB
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
package granular
import (
"bytes"
"errors"
"fmt"
"path/filepath"
"testing"
"github.com/spf13/afero"
)
func TestBasicCacheOperations(t *testing.T) {
// Setup test cache and filesystem
cache, memFs, tempDir := setupTestCache(t, "granular-test")
// Create a test file
testFilePath := filepath.Join(tempDir, "test.txt")
testContent := []byte("test content")
createTestFile(t, memFs, testFilePath, testContent)
// Create a key with the test file as input
key := Key{
Inputs: []Input{
FileInput{Path: testFilePath, Fs: memFs},
RawInput{
Data: testContent,
Name: "test.txt",
},
},
Extra: map[string]string{"test": "value"},
}
// First get should be a miss
result, hit, err := cache.Get(key)
assertCacheMiss(t, result, hit, err, "first Get")
// Create an output file
outputFilePath := filepath.Join(tempDir, "output.txt")
outputContent := []byte("output content")
createTestFile(t, memFs, outputFilePath, outputContent)
// Store in cache
resultToStore := Result{
Path: outputFilePath,
Metadata: map[string]string{
"data": "test data",
},
}
assertStoreSucceeds(t, cache, key, resultToStore, "initial result")
// Second get should be a hit
resultGet, hit, err := cache.Get(key)
assertCacheHit(t, resultGet, hit, err, "second Get")
assertResultHasPath(t, resultGet, "result from second Get")
// Get and verify the cached file
cachedFilePath := assertFileExists(t, cache, key, filepath.Base(outputFilePath))
assertFileContent(t, memFs, cachedFilePath, outputContent)
// Get and verify the cached data
cachedData := assertDataExists(t, cache, key, "data")
expectedData := []byte("test data")
assertBytesEqual(t, cachedData, expectedData, "Cached data")
// Modify the input file
newContent := []byte("modified content")
createTestFile(t, memFs, testFilePath, newContent)
key2 := Key{
Inputs: []Input{
FileInput{Path: testFilePath, Fs: memFs},
},
Extra: map[string]string{"test": "value"},
}
// Get should be a miss after modification
result, hit, err = cache.Get(key2)
assertCacheMiss(t, result, hit, err, "Get after modification")
// Test cache clear
if err := cache.Clear(); err != nil {
t.Fatalf("Failed to clear cache: %v", err)
}
// Get should be a miss after clear
result, hit, err = cache.Get(key)
assertCacheMiss(t, result, hit, err, "Get after clear")
}
func TestGlobInput(t *testing.T) {
// Setup test cache and filesystem
cache, memFs, tempDir := setupTestCache(t, "granular-glob-test")
// Create test files
testDir := filepath.Join(tempDir, "testfiles")
createTestDir(t, memFs, testDir)
// Create multiple files
for i, content := range []string{"file1", "file2", "file3"} {
filePath := filepath.Join(testDir, fmt.Sprintf("file%d.txt", i+1))
createTestFile(t, memFs, filePath, []byte(content))
}
// Create a key with glob pattern
key := Key{
Inputs: []Input{
GlobInput{Pattern: filepath.Join(testDir, "*.txt"), Fs: memFs},
},
}
// First get should be a miss
result, hit, err := cache.Get(key)
assertCacheMiss(t, result, hit, err, "first Get with glob pattern")
// Store in cache
resultToStore := Result{
Path: "",
Metadata: map[string]string{
"count": "3",
},
}
assertStoreSucceeds(t, cache, key, resultToStore, "glob pattern result")
// Second get should be a hit
resultGet, hit, err := cache.Get(key)
assertCacheHit(t, resultGet, hit, err, "second Get with glob pattern")
// Verify the cached data
assertMetadataValue(t, resultGet, "count", "3")
// Add a new file
newFilePath := filepath.Join(testDir, "file4.txt")
createTestFile(t, memFs, newFilePath, []byte("file4"))
// Get should be a miss after adding a file
result, hit, err = cache.Get(key)
assertCacheMiss(t, result, hit, err, "Get after adding file")
}
func TestRawInput(t *testing.T) {
// Setup test cache and filesystem
cache, _, _ := setupTestCache(t, "granular-raw-test")
// Create a key with raw input
key := Key{
Inputs: []Input{
RawInput{
Data: []byte("test data"),
Name: "test-input",
},
},
Extra: map[string]string{"version": "1.0"},
}
// First get should be a miss
result, hit, err := cache.Get(key)
assertCacheMiss(t, result, hit, err, "first Get with raw input")
// Store in cache
resultToStore := Result{
Path: "",
Metadata: map[string]string{
"result": "computed from raw data",
},
}
assertStoreSucceeds(t, cache, key, resultToStore, "raw input result")
// Second get should be a hit
resultGet, hit, err := cache.Get(key)
assertCacheHit(t, resultGet, hit, err, "second Get with raw input")
// Verify the cached data
assertMetadataValue(t, resultGet, "result", "computed from raw data")
// Modify the raw data
key.Inputs = []Input{
RawInput{
Data: []byte("modified data"),
Name: "test-input",
},
}
// Get should be a miss after modification
result, hit, err = cache.Get(key)
assertCacheMiss(t, result, hit, err, "Get after raw input modification")
}
func TestDirectoryInput(t *testing.T) {
// Setup test cache and filesystem
cache, memFs, tempDir := setupTestCache(t, "granular-dir-test")
// Create test directory structure
testDir := filepath.Join(tempDir, "testdir")
subDir := filepath.Join(testDir, "subdir")
createTestDir(t, memFs, subDir)
// Create files in the directory
files := map[string]string{
filepath.Join(testDir, "file1.txt"): "content1",
filepath.Join(testDir, "file2.log"): "log content",
filepath.Join(subDir, "subfile.txt"): "subcontent",
filepath.Join(subDir, "another.log"): "another log",
filepath.Join(subDir, "important.txt"): "important",
}
for path, content := range files {
createTestFile(t, memFs, path, []byte(content))
}
// Create a key with directory input, excluding log files
key := Key{
Inputs: []Input{
DirectoryInput{
Path: testDir,
Exclude: []string{"*.log"},
Fs: memFs,
},
},
}
// First get should be a miss
result, hit, err := cache.Get(key)
assertCacheMiss(t, result, hit, err, "first Get with directory input")
// Store in cache
resultToStore := Result{
Path: "",
Metadata: map[string]string{
"fileCount": "3", // 3 non-log files
},
}
assertStoreSucceeds(t, cache, key, resultToStore, "directory input result")
// Second get should be a hit
resultGet, hit, err := cache.Get(key)
assertCacheHit(t, resultGet, hit, err, "second Get with directory input")
// Modify a file that should be included
createTestFile(t, memFs, filepath.Join(subDir, "important.txt"), []byte("modified"))
// Get should be a miss after modification
result, hit, err = cache.Get(key)
assertCacheMiss(t, result, hit, err, "Get after modifying included file")
// Modify a file that should be excluded
createTestFile(t, memFs, filepath.Join(testDir, "file2.log"), []byte("new log content"))
// Store in cache again
resultToStore = Result{
Path: "",
Metadata: map[string]string{
"fileCount": "3", // 3 non-log files
},
}
assertStoreSucceeds(t, cache, key, resultToStore, "after log modification")
// Get should be a hit since we only modified an excluded file
resultGet, hit, err = cache.Get(key)
assertCacheHit(t, resultGet, hit, err, "Get after modifying excluded file")
}
// setupTestCache creates a new in-memory filesystem and cache for testing.
// It returns the cache, filesystem, and temporary directory path.
func setupTestCache(t *testing.T, tempDirName string) (*Cache, afero.Fs, string) {
t.Helper()
// Create an in-memory filesystem
memFs := afero.NewMemMapFs()
// Create a temporary directory for the test
tempDir := "/" + tempDirName
if err := memFs.MkdirAll(tempDir, 0o755); err != nil {
t.Fatalf("Failed to create temp directory: %v", err)
}
// Create a cache with the in-memory filesystem
cache, err := New(tempDir, WithFs(memFs))
if err != nil {
t.Fatalf("Failed to create cache: %v", err)
}
return cache, memFs, tempDir
}
// createTestFile creates a file with the given path and content in the filesystem.
func createTestFile(t *testing.T, fs afero.Fs, path string, content []byte) {
t.Helper()
// Ensure parent directory exists
dir := filepath.Dir(path)
if dir != "." && dir != "/" {
createTestDir(t, fs, dir)
}
if err := afero.WriteFile(fs, path, content, 0o644); err != nil {
t.Fatalf("Failed to write file %s: %v", path, err)
}
}
// createTestDir creates a directory with the given path in the filesystem.
func createTestDir(t *testing.T, fs afero.Fs, path string) {
t.Helper()
if err := fs.MkdirAll(path, 0o755); err != nil {
t.Fatalf("Failed to create directory %s: %v", path, err)
}
}
// assertCacheMiss asserts that a cache operation results in a miss.
func assertCacheMiss(t *testing.T, result *Result, hit bool, err error, context string) {
t.Helper()
if err != nil && !errors.Is(err, ErrCacheMiss) {
t.Fatalf("Unexpected error on %s: %v", context, err)
}
if hit {
t.Fatalf("Expected cache miss on %s, got hit", context)
}
}
// assertCacheHit asserts that a cache operation results in a hit.
func assertCacheHit(t *testing.T, result *Result, hit bool, err error, context string) {
t.Helper()
if err != nil {
t.Fatalf("Unexpected error on %s: %v", context, err)
}
if !hit {
t.Fatalf("Expected cache hit on %s, got miss", context)
}
}
// assertFileContent asserts that a file has the expected content.
func assertFileContent(t *testing.T, fs afero.Fs, path string, expected []byte) {
t.Helper()
actual, err := afero.ReadFile(fs, path)
if err != nil {
t.Fatalf("Failed to read file %s: %v", path, err)
}
assertBytesEqual(t, actual, expected, fmt.Sprintf("File content for %s", path))
}
// assertMetadataValue asserts that a metadata value matches the expected value.
func assertMetadataValue(t *testing.T, result *Result, key, expected string) {
t.Helper()
if result.Metadata == nil {
t.Fatalf("Expected metadata to contain key %s, but metadata is nil", key)
}
actual, exists := result.Metadata[key]
if !exists {
t.Fatalf("Expected metadata to contain key %s, but key not found", key)
}
if actual != expected {
t.Fatalf("Metadata value mismatch for key %s:\nExpected: %s\nActual: %s",
key, expected, actual)
}
}
// assertFileExists asserts that a file exists and returns its path.
func assertFileExists(t *testing.T, cache *Cache, key Key, filename string) string {
t.Helper()
path, found, err := cache.GetFile(key, filename)
if err != nil {
t.Fatalf("Failed to GetFile %s: %v", filename, err)
}
if !found {
t.Fatalf("Expected to find file %s, but not found", filename)
}
return path
}
// assertDataExists asserts that data exists and returns its content.
func assertDataExists(t *testing.T, cache *Cache, key Key, dataKey string) []byte {
t.Helper()
data, found, err := cache.GetData(key, dataKey)
if err != nil {
t.Fatalf("Failed to GetData %s: %v", dataKey, err)
}
if !found {
t.Fatalf("Expected to find data %s, but not found", dataKey)
}
return data
}
// assertBytesEqual asserts that two byte slices are equal.
func assertBytesEqual(t *testing.T, actual, expected []byte, context string) {
t.Helper()
if !bytes.Equal(actual, expected) {
t.Fatalf("%s mismatch:\nExpected: %s\nActual: %s",
context, string(expected), string(actual))
}
}
// assertStoreSucceeds asserts that a store operation succeeds.
func assertStoreSucceeds(t *testing.T, cache *Cache, key Key, result Result, context string) {
t.Helper()
err := cache.Store(key, result)
if err != nil {
t.Fatalf("Failed to Store %s: %v", context, err)
}
}
// assertResultHasPath asserts that a result has a non-empty path.
func assertResultHasPath(t *testing.T, result *Result, context string) {
t.Helper()
if result.Path == "" {
t.Fatalf("Expected %s to have a path, got empty path", context)
}
}