/
test.go
352 lines (315 loc) · 17.1 KB
/
test.go
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
// Package test provides generic BlobInfoCache test helpers.
package test
import (
"testing"
"github.com/containers/image/v5/internal/blobinfocache"
"github.com/containers/image/v5/internal/testing/mocks"
"github.com/containers/image/v5/types"
digest "github.com/opencontainers/go-digest"
"github.com/stretchr/testify/assert"
)
const (
digestUnknown = digest.Digest("sha256:1111111111111111111111111111111111111111111111111111111111111111")
digestUncompressed = digest.Digest("sha256:2222222222222222222222222222222222222222222222222222222222222222")
digestCompressedA = digest.Digest("sha256:3333333333333333333333333333333333333333333333333333333333333333")
digestCompressedB = digest.Digest("sha256:4444444444444444444444444444444444444444444444444444444444444444")
digestCompressedUnrelated = digest.Digest("sha256:5555555555555555555555555555555555555555555555555555555555555555")
compressorNameU = "compressorName/U"
compressorNameA = "compressorName/A"
compressorNameB = "compressorName/B"
compressorNameCU = "compressorName/CU"
)
// GenericCache runs an implementation-independent set of tests, given a
// newTestCache, which can be called repeatedly and always returns a fresh cache instance
func GenericCache(t *testing.T, newTestCache func(t *testing.T) blobinfocache.BlobInfoCache2) {
for _, s := range []struct {
name string
fn func(t *testing.T, cache blobinfocache.BlobInfoCache2)
}{
{"UncompressedDigest", testGenericUncompressedDigest},
{"RecordDigestUncompressedPair", testGenericRecordDigestUncompressedPair},
{"RecordKnownLocations", testGenericRecordKnownLocations},
{"CandidateLocations", testGenericCandidateLocations},
{"CandidateLocations2", testGenericCandidateLocations2},
} {
t.Run(s.name, func(t *testing.T) {
cache := newTestCache(t)
s.fn(t, cache)
})
}
}
func testGenericUncompressedDigest(t *testing.T, cache blobinfocache.BlobInfoCache2) {
// Nothing is known.
assert.Equal(t, digest.Digest(""), cache.UncompressedDigest(digestUnknown))
cache.RecordDigestUncompressedPair(digestCompressedA, digestUncompressed)
cache.RecordDigestUncompressedPair(digestCompressedB, digestUncompressed)
// Known compressed→uncompressed mapping
assert.Equal(t, digestUncompressed, cache.UncompressedDigest(digestCompressedA))
assert.Equal(t, digestUncompressed, cache.UncompressedDigest(digestCompressedB))
// This implicitly marks digestUncompressed as uncompressed.
assert.Equal(t, digestUncompressed, cache.UncompressedDigest(digestUncompressed))
// Known uncompressed→self mapping
cache.RecordDigestUncompressedPair(digestCompressedUnrelated, digestCompressedUnrelated)
assert.Equal(t, digestCompressedUnrelated, cache.UncompressedDigest(digestCompressedUnrelated))
}
func testGenericRecordDigestUncompressedPair(t *testing.T, cache blobinfocache.BlobInfoCache2) {
for i := 0; i < 2; i++ { // Record the same data twice to ensure redundant writes don’t break things.
// Known compressed→uncompressed mapping
cache.RecordDigestUncompressedPair(digestCompressedA, digestUncompressed)
assert.Equal(t, digestUncompressed, cache.UncompressedDigest(digestCompressedA))
// Two mappings to the same uncompressed digest
cache.RecordDigestUncompressedPair(digestCompressedB, digestUncompressed)
assert.Equal(t, digestUncompressed, cache.UncompressedDigest(digestCompressedB))
// Mapping an uncompressed digest to self
cache.RecordDigestUncompressedPair(digestUncompressed, digestUncompressed)
assert.Equal(t, digestUncompressed, cache.UncompressedDigest(digestUncompressed))
}
}
func testGenericRecordKnownLocations(t *testing.T, cache blobinfocache.BlobInfoCache2) {
transport := mocks.NameImageTransport("==BlobInfocache transport mock")
for i := 0; i < 2; i++ { // Record the same data twice to ensure redundant writes don’t break things.
for _, scopeName := range []string{"A", "B"} { // Run the test in two different scopes to verify they don't affect each other.
scope := types.BICTransportScope{Opaque: scopeName}
for _, digest := range []digest.Digest{digestCompressedA, digestCompressedB} { // Two different digests should not affect each other either.
lr1 := types.BICLocationReference{Opaque: scopeName + "1"}
lr2 := types.BICLocationReference{Opaque: scopeName + "2"}
cache.RecordKnownLocation(transport, scope, digest, lr2)
cache.RecordKnownLocation(transport, scope, digest, lr1)
assert.Equal(t, []types.BICReplacementCandidate{
{Digest: digest, Location: lr1},
{Digest: digest, Location: lr2},
}, cache.CandidateLocations(transport, scope, digest, false))
assert.Equal(t, []blobinfocache.BICReplacementCandidate2{}, cache.CandidateLocations2(transport, scope, digest, false))
}
}
}
}
// candidate is a shorthand for types.BICReplacementCandidate
type candidate struct {
d digest.Digest
cn string
lr string
}
func assertCandidatesMatch(t *testing.T, scopeName string, expected []candidate, actual []types.BICReplacementCandidate) {
e := make([]types.BICReplacementCandidate, len(expected))
for i, ev := range expected {
e[i] = types.BICReplacementCandidate{Digest: ev.d, Location: types.BICLocationReference{Opaque: scopeName + ev.lr}}
}
assert.Equal(t, e, actual)
}
func assertCandidatesMatch2(t *testing.T, scopeName string, expected []candidate, actual []blobinfocache.BICReplacementCandidate2) {
e := make([]blobinfocache.BICReplacementCandidate2, len(expected))
for i, ev := range expected {
e[i] = blobinfocache.BICReplacementCandidate2{Digest: ev.d, CompressorName: ev.cn, Location: types.BICLocationReference{Opaque: scopeName + ev.lr}}
}
assert.Equal(t, e, actual)
}
func testGenericCandidateLocations(t *testing.T, cache blobinfocache.BlobInfoCache2) {
transport := mocks.NameImageTransport("==BlobInfocache transport mock")
cache.RecordDigestUncompressedPair(digestCompressedA, digestUncompressed)
cache.RecordDigestUncompressedPair(digestCompressedB, digestUncompressed)
cache.RecordDigestUncompressedPair(digestUncompressed, digestUncompressed)
digestNameSet := []struct {
n string
d digest.Digest
}{
{"U", digestUncompressed},
{"A", digestCompressedA},
{"B", digestCompressedB},
{"CU", digestCompressedUnrelated},
}
for _, scopeName := range []string{"A", "B"} { // Run the test in two different scopes to verify they don't affect each other.
scope := types.BICTransportScope{Opaque: scopeName}
// Nothing is known.
assert.Equal(t, []types.BICReplacementCandidate{}, cache.CandidateLocations(transport, scope, digestUnknown, false))
assert.Equal(t, []types.BICReplacementCandidate{}, cache.CandidateLocations(transport, scope, digestUnknown, true))
// Record "2" entries before "1" entries; then results should sort "1" (more recent) before "2" (older)
for _, suffix := range []string{"2", "1"} {
for _, e := range digestNameSet {
cache.RecordKnownLocation(transport, scope, e.d, types.BICLocationReference{Opaque: scopeName + e.n + suffix})
}
}
// No substitutions allowed:
for _, e := range digestNameSet {
assertCandidatesMatch(t, scopeName, []candidate{
{d: e.d, lr: e.n + "1"}, {d: e.d, lr: e.n + "2"},
}, cache.CandidateLocations(transport, scope, e.d, false))
}
// With substitutions: The original digest is always preferred, then other compressed, then the uncompressed one.
assertCandidatesMatch(t, scopeName, []candidate{
{d: digestCompressedA, lr: "A1"}, {d: digestCompressedA, lr: "A2"},
{d: digestCompressedB, lr: "B1"}, {d: digestCompressedB, lr: "B2"},
{d: digestUncompressed, lr: "U1"}, // Beyond the replacementAttempts limit: {d: digestUncompressed, lr: "U2"},
}, cache.CandidateLocations(transport, scope, digestCompressedA, true))
assertCandidatesMatch(t, scopeName, []candidate{
{d: digestCompressedB, lr: "B1"}, {d: digestCompressedB, lr: "B2"},
{d: digestCompressedA, lr: "A1"}, {d: digestCompressedA, lr: "A2"},
{d: digestUncompressed, lr: "U1"}, // Beyond the replacementAttempts limit: {d: digestUncompressed, lr: "U2"},
}, cache.CandidateLocations(transport, scope, digestCompressedB, true))
assertCandidatesMatch(t, scopeName, []candidate{
{d: digestUncompressed, lr: "U1"}, {d: digestUncompressed, lr: "U2"},
// "1" entries were added after "2", and A/Bs are sorted in the reverse of digestNameSet order
{d: digestCompressedB, lr: "B1"},
{d: digestCompressedA, lr: "A1"},
{d: digestCompressedB, lr: "B2"},
// Beyond the replacementAttempts limit: {d: digestCompressedA, lr: "A2"},
}, cache.CandidateLocations(transport, scope, digestUncompressed, true))
// Locations are known, but no relationships
assertCandidatesMatch(t, scopeName, []candidate{
{d: digestCompressedUnrelated, lr: "CU1"}, {d: digestCompressedUnrelated, lr: "CU2"},
}, cache.CandidateLocations(transport, scope, digestCompressedUnrelated, true))
}
}
func testGenericCandidateLocations2(t *testing.T, cache blobinfocache.BlobInfoCache2) {
transport := mocks.NameImageTransport("==BlobInfocache transport mock")
cache.RecordDigestUncompressedPair(digestCompressedA, digestUncompressed)
cache.RecordDigestUncompressedPair(digestCompressedB, digestUncompressed)
cache.RecordDigestUncompressedPair(digestUncompressed, digestUncompressed)
digestNameSet := []struct {
n string
d digest.Digest
m string
}{
{"U", digestUncompressed, compressorNameU},
{"A", digestCompressedA, compressorNameA},
{"B", digestCompressedB, compressorNameB},
{"CU", digestCompressedUnrelated, compressorNameCU},
}
for scopeIndex, scopeName := range []string{"A", "B", "C"} { // Run the test in two different scopes to verify they don't affect each other.
scope := types.BICTransportScope{Opaque: scopeName}
// Nothing is known.
assert.Equal(t, []blobinfocache.BICReplacementCandidate2{}, cache.CandidateLocations2(transport, scope, digestUnknown, false))
assert.Equal(t, []blobinfocache.BICReplacementCandidate2{}, cache.CandidateLocations2(transport, scope, digestUnknown, true))
// Record "2" entries before "1" entries; then results should sort "1" (more recent) before "2" (older)
for _, suffix := range []string{"2", "1"} {
for _, e := range digestNameSet {
cache.RecordKnownLocation(transport, scope, e.d, types.BICLocationReference{Opaque: scopeName + e.n + suffix})
}
}
// Clear any "known" compression values, except on the first loop where they've never been set
if scopeIndex != 0 {
for _, e := range digestNameSet {
cache.RecordDigestCompressorName(e.d, blobinfocache.UnknownCompression)
}
}
// No substitutions allowed:
for _, e := range digestNameSet {
assertCandidatesMatch(t, scopeName, []candidate{
{d: e.d, lr: e.n + "1"},
{d: e.d, lr: e.n + "2"},
}, cache.CandidateLocations(transport, scope, e.d, false))
assertCandidatesMatch2(t, scopeName, []candidate{}, cache.CandidateLocations2(transport, scope, e.d, false))
}
// With substitutions: The original digest is always preferred, then other compressed, then the uncompressed one.
assertCandidatesMatch(t, scopeName, []candidate{
{d: digestCompressedA, lr: "A1"},
{d: digestCompressedA, lr: "A2"},
{d: digestCompressedB, lr: "B1"},
{d: digestCompressedB, lr: "B2"},
{d: digestUncompressed, lr: "U1"},
// Beyond the replacementAttempts limit: {d: digestUncompressed, cn: compressorNameCU, lr: "U2"},
}, cache.CandidateLocations(transport, scope, digestCompressedA, true))
// Unknown compression -> no candidates
assertCandidatesMatch2(t, scopeName, []candidate{}, cache.CandidateLocations2(transport, scope, digestCompressedA, true))
assertCandidatesMatch(t, scopeName, []candidate{
{d: digestCompressedB, lr: "B1"},
{d: digestCompressedB, lr: "B2"},
{d: digestCompressedA, lr: "A1"},
{d: digestCompressedA, lr: "A2"},
{d: digestUncompressed, lr: "U1"}, // Beyond the replacementAttempts limit: {d: digestUncompressed, lr: "U2"},
}, cache.CandidateLocations(transport, scope, digestCompressedB, true))
// Unknown compression -> no candidates
assertCandidatesMatch2(t, scopeName, []candidate{}, cache.CandidateLocations2(transport, scope, digestCompressedB, true))
assertCandidatesMatch(t, scopeName, []candidate{
{d: digestUncompressed, lr: "U1"},
{d: digestUncompressed, lr: "U2"},
// "1" entries were added after "2", and A/Bs are sorted in the reverse of digestNameSet order
{d: digestCompressedB, lr: "B1"},
{d: digestCompressedA, lr: "A1"},
{d: digestCompressedB, lr: "B2"},
// Beyond the replacementAttempts limit: {d: digestCompressedA, lr: "A2"},
}, cache.CandidateLocations(transport, scope, digestUncompressed, true))
// Unknown compression -> no candidates
assertCandidatesMatch2(t, scopeName, []candidate{}, cache.CandidateLocations2(transport, scope, digestUncompressed, true))
// Locations are known, but no relationships
assertCandidatesMatch(t, scopeName, []candidate{
{d: digestCompressedUnrelated, lr: "CU1"},
{d: digestCompressedUnrelated, lr: "CU2"},
}, cache.CandidateLocations(transport, scope, digestCompressedUnrelated, true))
// Unknown compression -> no candidates
assertCandidatesMatch2(t, scopeName, []candidate{}, cache.CandidateLocations2(transport, scope, digestCompressedUnrelated, true))
// Set the "known" compression values
for _, e := range digestNameSet {
cache.RecordDigestCompressorName(e.d, e.m)
}
// No substitutions allowed:
for _, e := range digestNameSet {
assertCandidatesMatch(t, scopeName, []candidate{
{d: e.d, lr: e.n + "1"},
{d: e.d, lr: e.n + "2"},
}, cache.CandidateLocations(transport, scope, e.d, false))
assertCandidatesMatch2(t, scopeName, []candidate{
{d: e.d, cn: e.m, lr: e.n + "1"},
{d: e.d, cn: e.m, lr: e.n + "2"},
}, cache.CandidateLocations2(transport, scope, e.d, false))
}
// With substitutions: The original digest is always preferred, then other compressed, then the uncompressed one.
assertCandidatesMatch(t, scopeName, []candidate{
{d: digestCompressedA, lr: "A1"},
{d: digestCompressedA, lr: "A2"},
{d: digestCompressedB, lr: "B1"},
{d: digestCompressedB, lr: "B2"},
{d: digestUncompressed, lr: "U1"},
// Beyond the replacementAttempts limit: {d: digestUncompressed, lr: "U2"},
}, cache.CandidateLocations(transport, scope, digestCompressedA, true))
assertCandidatesMatch2(t, scopeName, []candidate{
{d: digestCompressedA, cn: compressorNameA, lr: "A1"},
{d: digestCompressedA, cn: compressorNameA, lr: "A2"},
{d: digestCompressedB, cn: compressorNameB, lr: "B1"},
{d: digestCompressedB, cn: compressorNameB, lr: "B2"},
{d: digestUncompressed, cn: compressorNameU, lr: "U1"},
// Beyond the replacementAttempts limit: {d: digestUncompressed, cn: compressorNameCU, lr: "U2"},
}, cache.CandidateLocations2(transport, scope, digestCompressedA, true))
assertCandidatesMatch(t, scopeName, []candidate{
{d: digestCompressedB, lr: "B1"},
{d: digestCompressedB, lr: "B2"},
{d: digestCompressedA, lr: "A1"},
{d: digestCompressedA, lr: "A2"},
{d: digestUncompressed, lr: "U1"}, // Beyond the replacementAttempts limit: {d: digestUncompressed, lr: "U2"},
}, cache.CandidateLocations(transport, scope, digestCompressedB, true))
assertCandidatesMatch2(t, scopeName, []candidate{
{d: digestCompressedB, cn: compressorNameB, lr: "B1"},
{d: digestCompressedB, cn: compressorNameB, lr: "B2"},
{d: digestCompressedA, cn: compressorNameA, lr: "A1"},
{d: digestCompressedA, cn: compressorNameA, lr: "A2"},
{d: digestUncompressed, cn: compressorNameU, lr: "U1"}, // Beyond the replacementAttempts limit: {d: digestUncompressed, cn: compressorNameU, lr: "U2"},
}, cache.CandidateLocations2(transport, scope, digestCompressedB, true))
assertCandidatesMatch(t, scopeName, []candidate{
{d: digestUncompressed, lr: "U1"},
{d: digestUncompressed, lr: "U2"},
// "1" entries were added after "2", and A/Bs are sorted in the reverse of digestNameSet order
{d: digestCompressedB, lr: "B1"},
{d: digestCompressedA, lr: "A1"},
{d: digestCompressedB, lr: "B2"},
// Beyond the replacementAttempts limit: {d: digestCompressedA, lr: "A2"},
}, cache.CandidateLocations(transport, scope, digestUncompressed, true))
assertCandidatesMatch2(t, scopeName, []candidate{
{d: digestUncompressed, cn: compressorNameU, lr: "U1"},
{d: digestUncompressed, cn: compressorNameU, lr: "U2"},
// "1" entries were added after "2", and A/Bs are sorted in the reverse of digestNameSet order
{d: digestCompressedB, cn: compressorNameB, lr: "B1"},
{d: digestCompressedA, cn: compressorNameA, lr: "A1"},
{d: digestCompressedB, cn: compressorNameB, lr: "B2"},
// Beyond the replacementAttempts limit: {d: digestCompressedA, cn: compressorNameA, lr: "A2"},
}, cache.CandidateLocations2(transport, scope, digestUncompressed, true))
// Locations are known, but no relationships
assertCandidatesMatch(t, scopeName, []candidate{
{d: digestCompressedUnrelated, lr: "CU1"},
{d: digestCompressedUnrelated, lr: "CU2"},
}, cache.CandidateLocations(transport, scope, digestCompressedUnrelated, true))
assertCandidatesMatch2(t, scopeName, []candidate{
{d: digestCompressedUnrelated, cn: compressorNameCU, lr: "CU1"},
{d: digestCompressedUnrelated, cn: compressorNameCU, lr: "CU2"},
}, cache.CandidateLocations2(transport, scope, digestCompressedUnrelated, true))
}
}