MCPcopy
hub / github.com/kopia/kopia / TestContentCacheForMetadata

Function TestContentCacheForMetadata

internal/cache/content_cache_metadata_test.go:16–70  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

14)
15
16func TestContentCacheForMetadata(t *testing.T) {
17 ctx := testlogging.Context(t)
18
19 underlyingData := blobtesting.DataMap{}
20 underlying := blobtesting.NewMapStorage(underlyingData, nil, nil)
21
22 td := testutil.TempDirectory(t)
23
24 metadataCache, err := cache.NewContentCache(ctx, underlying, cache.Options{
25 BaseCacheDirectory: td,
26 CacheSubDir: "subdir",
27 HMACSecret: []byte{1, 2, 3},
28 FetchFullBlobs: true,
29 Sweep: cache.SweepSettings{
30 MaxSizeBytes: 100,
31 },
32 }, nil)
33 require.NoError(t, err)
34
35 cacheStorage := metadataCache.CacheStorage()
36
37 var tmp gather.WriteBuffer
38 defer tmp.Close()
39
40 // get something we don't have in the underlying storage
41 require.ErrorIs(t, metadataCache.GetContent(ctx, "key1", "blob1", 0, 3, &tmp), blob.ErrBlobNotFound)
42
43 require.NoError(t, underlying.PutBlob(ctx, "blob1", gather.FromSlice([]byte{1, 2, 3, 4, 5, 6}), blob.PutOptions{}))
44
45 require.NoError(t, metadataCache.GetContent(ctx, "key1", "blob1", 0, -1, &tmp))
46 require.Equal(t, []byte{1, 2, 3, 4, 5, 6}, tmp.ToByteSlice())
47
48 require.NoError(t, metadataCache.GetContent(ctx, "key1", "blob1", 0, 3, &tmp))
49 require.Equal(t, []byte{1, 2, 3}, tmp.ToByteSlice())
50
51 cacheEntries, err := blob.ListAllBlobs(ctx, cacheStorage, "")
52 require.NoError(t, err)
53 // cache has the entire blob
54 require.Len(t, cacheEntries, 1)
55
56 require.NoError(t, metadataCache.GetContent(ctx, "key1", "blob1", 3, 3, &tmp))
57 require.Equal(t, []byte{4, 5, 6}, tmp.ToByteSlice())
58
59 cacheEntries, err = blob.ListAllBlobs(ctx, cacheStorage, "")
60 require.NoError(t, err)
61 // cache has the entire blob
62 require.Len(t, cacheEntries, 1)
63
64 metadataCache.Close(ctx)
65
66 // get slice with cache miss
67 require.NoError(t, underlying.PutBlob(ctx, "blob2", gather.FromSlice([]byte{1, 2, 3, 4, 5, 6}), blob.PutOptions{}))
68 require.NoError(t, metadataCache.GetContent(ctx, "key1", "blob2", 3, 3, &tmp))
69 require.Equal(t, []byte{4, 5, 6}, tmp.ToByteSlice())
70}
71
72func TestContentCacheForMetadata_Passthrough(t *testing.T) {
73 underlyingData := blobtesting.DataMap{}

Callers

nothing calls this directly

Calls 14

CacheStorageMethod · 0.95
CloseMethod · 0.95
GetContentMethod · 0.95
ToByteSliceMethod · 0.95
CloseMethod · 0.95
ContextFunction · 0.92
NewMapStorageFunction · 0.92
TempDirectoryFunction · 0.92
NewContentCacheFunction · 0.92
FromSliceFunction · 0.92
ListAllBlobsFunction · 0.92
EqualMethod · 0.80

Tested by

no test coverage detected