Debian Codegraph @

cache/cache_test.go

raw
1 /*
2 Copyright The containerd Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 /*
18 Copyright 2019 The Go Authors. All rights reserved.
19 Use of this source code is governed by a BSD-style
20 license that can be found in the NOTICE.md file.
21 */
22
23 package cache
24
25 import (
26 "crypto/sha256"
27 "fmt"
28 "io"
29 "os"
30 "testing"
31 )
32
33 const (
34 sampleData = "0123456789"
35 )
36
37 func TestDirectoryCache(t *testing.T) {
38
39 // with enough memory cache
40 newCache := func() (BlobCache, cleanFunc) {
41 tmp, err := os.MkdirTemp("", "testcache")
42 if err != nil {
43 t.Fatalf("failed to make tempdir: %v", err)
44 }
45 c, err := NewDirectoryCache(tmp, DirectoryCacheConfig{
46 MaxLRUCacheEntry: 10,
47 SyncAdd: true,
48 })
49 if err != nil {
50 t.Fatalf("failed to make cache: %v", err)
51 }
52 return c, func() { os.RemoveAll(tmp) }
53 }
54 testCache(t, "dir-with-enough-mem", newCache)
55
56 // with smaller memory cache
57 newCache = func() (BlobCache, cleanFunc) {
58 tmp, err := os.MkdirTemp("", "testcache")
59 if err != nil {
60 t.Fatalf("failed to make tempdir: %v", err)
61 }
62 c, err := NewDirectoryCache(tmp, DirectoryCacheConfig{
63 MaxLRUCacheEntry: 1,
64 SyncAdd: true,
65 })
66 if err != nil {
67 t.Fatalf("failed to make cache: %v", err)
68 }
69 return c, func() { os.RemoveAll(tmp) }
70 }
71 testCache(t, "dir-with-small-mem", newCache)
72 }
73
74 func TestMemoryCache(t *testing.T) {
75 testCache(t, "memory", func() (BlobCache, cleanFunc) { return NewMemoryCache(), func() {} })
76 }
77
78 type cleanFunc func()
79
80 func testCache(t *testing.T, name string, newCache func() (BlobCache, cleanFunc)) {
81 tests := []struct {
82 name string
83 blobs []string
84 checks []check
85 }{
86 {
87 name: "empty_data",
88 blobs: []string{
89 "",
90 },
91 checks: []check{
92 hit(""),
93 miss(sampleData),
94 },
95 },
96 {
97 name: "data",
98 blobs: []string{
99 sampleData,
100 },
101 checks: []check{
102 hit(sampleData),
103 miss("dummy"),
104 },
105 },
106 {
107 name: "manydata",
108 blobs: []string{
109 sampleData,
110 "test",
111 },
112 checks: []check{
113 hit(sampleData),
114 miss("dummy"),
115 },
116 },
117 {
118 name: "dup_data",
119 blobs: []string{
120 sampleData,
121 sampleData,
122 },
123 checks: []check{
124 hit(sampleData),
125 },
126 },
127 }
128
129 for _, tt := range tests {
130 t.Run(fmt.Sprintf("%s-%s", name, tt.name), func(t *testing.T) {
131 c, clean := newCache()
132 defer clean()
133 for _, blob := range tt.blobs {
134 d := digestFor(blob)
135 w, err := c.Add(d)
136 if err != nil {
137 t.Fatalf("failed to add %v: %v", d, err)
138 }
139 if n, err := w.Write([]byte(blob)); err != nil || n != len(blob) {
140 w.Close()
141 t.Fatalf("failed to write %v (len:%d): %v", d, len(blob), err)
142 }
143 if err := w.Commit(); err != nil {
144 w.Close()
145 t.Fatalf("failed to commit %v (len:%d): %v", d, len(blob), err)
146 }
147 w.Close()
148 }
149 for _, check := range tt.checks {
150 check(t, c)
151 }
152 })
153 }
154 }
155
156 type check func(*testing.T, BlobCache)
157
158 func digestFor(content string) string {
159 sum := sha256.Sum256([]byte(content))
160 return fmt.Sprintf("%x", sum)
161 }
162
163 func hit(sample string) check {
164 return func(t *testing.T, c BlobCache) {
165 // test whole blob
166 key := digestFor(sample)
167 testChunk(t, c, key, 0, sample)
168
169 // test a chunk
170 chunk := len(sample) / 3
171 testChunk(t, c, key, int64(chunk), sample[chunk:2*chunk])
172 }
173 }
174
175 func testChunk(t *testing.T, c BlobCache, key string, offset int64, sample string) {
176 p := make([]byte, len(sample))
177 r, err := c.Get(key)
178 if err != nil {
179 t.Errorf("missed %v", key)
180 return
181 }
182 if n, err := r.ReadAt(p, offset); err != nil && err != io.EOF {
183 t.Errorf("failed to fetch blob %q: %v", key, err)
184 return
185 } else if n != len(sample) {
186 t.Errorf("fetched size %d; want %d", len(p), len(sample))
187 return
188 }
189 if digestFor(sample) != digestFor(string(p)) {
190 t.Errorf("fetched %q; want %q", string(p), sample)
191 }
192 }
193
194 func miss(sample string) check {
195 return func(t *testing.T, c BlobCache) {
196 d := digestFor(sample)
197 if _, err := c.Get(d); err == nil {
198 t.Errorf("hit blob %q but must be missed: %v", d, err)
199 return
200 }
201 }
202 }