Debian Codegraph @

internal/graph/memory_test.go

raw
1 /*
2 Copyright The ORAS Authors.
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 */
15
16 package graph
17
18 import (
19 "bytes"
20 "context"
21 "encoding/json"
22 "io"
23 "reflect"
24 "testing"
25
26 "github.com/opencontainers/go-digest"
27 ocispec "github.com/opencontainers/image-spec/specs-go/v1"
28 "oras.land/oras-go/v2/internal/cas"
29 )
30
31 // +------------------------------+
32 // | |
33 // | +-----------+ |
34 // | |A(manifest)| |
35 // | +-----+-----+ |
36 // | | |
37 // | +------------+ |
38 // | | | |
39 // | v v |
40 // | +-----+-----+ +---+----+ |
41 // | |B(manifest)| |C(layer)| |
42 // | +-----+-----+ +--------+ |
43 // | | |
44 // | v |
45 // | +---+----+ |
46 // | |D(layer)| |
47 // | +--------+ |
48 // | |
49 // |------------------------------+
50 func TestMemory_IndexAndRemove(t *testing.T) {
51 testFetcher := cas.NewMemory()
52 testMemory := NewMemory()
53 ctx := context.Background()
54
55 // generate test content
56 var blobs [][]byte
57 var descs []ocispec.Descriptor
58 appendBlob := func(mediaType string, blob []byte) ocispec.Descriptor {
59 blobs = append(blobs, blob)
60 descs = append(descs, ocispec.Descriptor{
61 MediaType: mediaType,
62 Digest: digest.FromBytes(blob),
63 Size: int64(len(blob)),
64 })
65 return descs[len(descs)-1]
66 }
67 generateManifest := func(layers ...ocispec.Descriptor) ocispec.Descriptor {
68 manifest := ocispec.Manifest{
69 Config: ocispec.Descriptor{MediaType: "test config"},
70 Layers: layers,
71 }
72 manifestJSON, err := json.Marshal(manifest)
73 if err != nil {
74 t.Fatal(err)
75 }
76 return appendBlob(ocispec.MediaTypeImageManifest, manifestJSON)
77 }
78 descC := appendBlob("layer node C", []byte("Node C is a layer")) // blobs[0], layer "C"
79 descD := appendBlob("layer node D", []byte("Node D is a layer")) // blobs[1], layer "D"
80 descB := generateManifest(descs[0:2]...) // blobs[2], manifest "B"
81 descA := generateManifest(descs[1:3]...) // blobs[3], manifest "A"
82
83 // prepare the content in the fetcher, so that it can be used to test Index
84 testContents := []ocispec.Descriptor{descC, descD, descB, descA}
85 for i := 0; i < len(blobs); i++ {
86 testFetcher.Push(ctx, testContents[i], bytes.NewReader(blobs[i]))
87 }
88
89 // make sure that testFetcher works
90 rc, err := testFetcher.Fetch(ctx, descA)
91 if err != nil {
92 t.Errorf("testFetcher.Fetch() error = %v", err)
93 }
94 got, err := io.ReadAll(rc)
95 if err != nil {
96 t.Errorf("testFetcher.Fetch().Read() error = %v", err)
97 }
98 err = rc.Close()
99 if err != nil {
100 t.Errorf("testFetcher.Fetch().Close() error = %v", err)
101 }
102 if !bytes.Equal(got, blobs[3]) {
103 t.Errorf("testFetcher.Fetch() = %v, want %v", got, blobs[4])
104 }
105
106 // index and check the information of node D
107 testMemory.Index(ctx, testFetcher, descD)
108 // 1. verify its existence in testMemory.nodes
109 if _, exists := testMemory.nodes[descD.Digest]; !exists {
110 t.Errorf("nodes entry of %s should exist", "D")
111 }
112 // 2. verify that the entry of D exists in testMemory.successors and it's empty
113 successorsD, exists := testMemory.successors[descD.Digest]
114 if !exists {
115 t.Errorf("successor entry of %s should exist", "D")
116 }
117 if successorsD == nil {
118 t.Errorf("successors of %s should be an empty set, not nil", "D")
119 }
120 if len(successorsD) != 0 {
121 t.Errorf("successors of %s should be empty", "D")
122 }
123 // 3. there should be no entry of D in testMemory.predecessors yet
124 _, exists = testMemory.predecessors[descD.Digest]
125 if exists {
126 t.Errorf("predecessor entry of %s should not exist yet", "D")
127 }
128
129 // index and check the information of node C
130 testMemory.Index(ctx, testFetcher, descC)
131 // 1. verify its existence in memory.nodes
132 if _, exists := testMemory.nodes[descC.Digest]; !exists {
133 t.Errorf("nodes entry of %s should exist", "C")
134 }
135 // 2. verify that the entry of C exists in testMemory.successors and it's empty
136 successorsC, exists := testMemory.successors[descC.Digest]
137 if !exists {
138 t.Errorf("successor entry of %s should exist", "C")
139 }
140 if successorsC == nil {
141 t.Errorf("successors of %s should be an empty set, not nil", "C")
142 }
143 if len(successorsC) != 0 {
144 t.Errorf("successors of %s should be empty", "C")
145 }
146 // 3. there should be no entry of C in testMemory.predecessors yet
147 _, exists = testMemory.predecessors[descC.Digest]
148 if exists {
149 t.Errorf("predecessor entry of %s should not exist yet", "C")
150 }
151
152 // index and check the information of node A
153 testMemory.Index(ctx, testFetcher, descA)
154 // 1. verify its existence in testMemory.nodes
155 if _, exists := testMemory.nodes[descA.Digest]; !exists {
156 t.Errorf("nodes entry of %s should exist", "A")
157 }
158 // 2. verify that the entry of A exists in testMemory.successors and it contains
159 // node B and node D
160 successorsA, exists := testMemory.successors[descA.Digest]
161 if !exists {
162 t.Errorf("successor entry of %s should exist", "A")
163 }
164 if successorsA == nil {
165 t.Errorf("successors of %s should be a set, not nil", "A")
166 }
167 if !successorsA.Contains(descB.Digest) {
168 t.Errorf("successors of %s should contain %s", "A", "B")
169 }
170 if !successorsA.Contains(descD.Digest) {
171 t.Errorf("successors of %s should contain %s", "A", "D")
172 }
173 // 3. verify that node A exists in the predecessors lists of its successors.
174 // there should be an entry of D in testMemory.predecessors by now and it
175 // should contain A but not B
176 predecessorsD, exists := testMemory.predecessors[descD.Digest]
177 if !exists {
178 t.Errorf("predecessor entry of %s should exist by now", "D")
179 }
180 if !predecessorsD.Contains(descA.Digest) {
181 t.Errorf("predecessors of %s should contain %s", "D", "A")
182 }
183 if predecessorsD.Contains(descB.Digest) {
184 t.Errorf("predecessors of %s should not contain %s yet", "D", "B")
185 }
186 // there should be an entry of B in testMemory.predecessors now
187 // and it should contain A
188 predecessorsB, exists := testMemory.predecessors[descB.Digest]
189 if !exists {
190 t.Errorf("predecessor entry of %s should exist by now", "B")
191 }
192 if !predecessorsB.Contains(descA.Digest) {
193 t.Errorf("predecessors of %s should contain %s", "B", "A")
194 }
195 // 4. there should be no entry of A in testMemory.predecessors
196 _, exists = testMemory.predecessors[descA.Digest]
197 if exists {
198 t.Errorf("predecessor entry of %s should not exist", "A")
199 }
200
201 // index and check the information of node B
202 testMemory.Index(ctx, testFetcher, descB)
203 // 1. verify its existence in testMemory.nodes
204 if _, exists := testMemory.nodes[descB.Digest]; !exists {
205 t.Errorf("nodes entry of %s should exist", "B")
206 }
207 // 2. verify that the entry of B exists in testMemory.successors and it contains
208 // node C and node D
209 successorsB, exists := testMemory.successors[descB.Digest]
210 if !exists {
211 t.Errorf("successor entry of %s should exist", "B")
212 }
213 if successorsB == nil {
214 t.Errorf("successors of %s should be a set, not nil", "B")
215 }
216 if !successorsB.Contains(descC.Digest) {
217 t.Errorf("successors of %s should contain %s", "B", "C")
218 }
219 if !successorsB.Contains(descD.Digest) {
220 t.Errorf("successors of %s should contain %s", "B", "D")
221 }
222 // 3. verify that node B exists in the predecessors lists of its successors.
223 // there should be an entry of C in testMemory.predecessors by now
224 // and it should contain B
225 predecessorsC, exists := testMemory.predecessors[descC.Digest]
226 if !exists {
227 t.Errorf("predecessor entry of %s should exist by now", "C")
228 }
229 if !predecessorsC.Contains(descB.Digest) {
230 t.Errorf("predecessors of %s should contain %s", "C", "B")
231 }
232 // predecessors of D should have been updated now to have node A and B
233 if !predecessorsD.Contains(descB.Digest) {
234 t.Errorf("predecessors of %s should contain %s", "D", "B")
235 }
236 if !predecessorsD.Contains(descA.Digest) {
237 t.Errorf("predecessors of %s should contain %s", "D", "A")
238 }
239
240 // remove node B and check the stored information
241 testMemory.Remove(descB)
242 // 1. verify that node B no longer exists in testMemory.nodes
243 if _, exists := testMemory.nodes[descB.Digest]; exists {
244 t.Errorf("nodes entry of %s should no longer exist", "B")
245 }
246 // 2. verify B' predecessors info: B's entry in testMemory.predecessors should
247 // still exist, since its predecessor A still exists
248 predecessorsB, exists = testMemory.predecessors[descB.Digest]
249 if !exists {
250 t.Errorf("testDeletableMemory.predecessors should still contain the entry of %s", "B")
251 }
252 if !predecessorsB.Contains(descA.Digest) {
253 t.Errorf("predecessors of %s should still contain %s", "B", "A")
254 }
255 // 3. verify B' successors info: B's entry in testMemory.successors should no
256 // longer exist
257 if _, exists := testMemory.successors[descB.Digest]; exists {
258 t.Errorf("testDeletableMemory.successors should not contain the entry of %s", "B")
259 }
260 // 4. verify B' predecessors' successors info: B should still exist in A's
261 // successors
262 if !successorsA.Contains(descB.Digest) {
263 t.Errorf("successors of %s should still contain %s", "A", "B")
264 }
265 // 5. verify B' successors' predecessors info: C's entry in testMemory.predecessors
266 // should no longer exist, since C's only predecessor B is already deleted
267 if _, exists = testMemory.predecessors[descC.Digest]; exists {
268 t.Errorf("predecessor entry of %s should no longer exist by now, since all its predecessors have been deleted", "C")
269 }
270 // B should no longer exist in D's predecessors
271 if predecessorsD.Contains(descB.Digest) {
272 t.Errorf("predecessors of %s should not contain %s", "D", "B")
273 }
274 // but A still exists in D's predecessors
275 if !predecessorsD.Contains(descA.Digest) {
276 t.Errorf("predecessors of %s should still contain %s", "D", "A")
277 }
278
279 // remove node A and check the stored information
280 testMemory.Remove(descA)
281 // 1. verify that node A no longer exists in testMemory.nodes
282 if _, exists := testMemory.nodes[descA.Digest]; exists {
283 t.Errorf("nodes entry of %s should no longer exist", "A")
284 }
285 // 2. verify A' successors info: A's entry in testMemory.successors should no
286 // longer exist
287 if _, exists := testMemory.successors[descA.Digest]; exists {
288 t.Errorf("testDeletableMemory.successors should not contain the entry of %s", "A")
289 }
290 // 3. verify A' successors' predecessors info: D's entry in testMemory.predecessors
291 // should no longer exist, since all predecessors of D are already deleted
292 if _, exists = testMemory.predecessors[descD.Digest]; exists {
293 t.Errorf("predecessor entry of %s should no longer exist by now, since all its predecessors have been deleted", "D")
294 }
295 // B's entry in testMemory.predecessors should no longer exist, since B's only
296 // predecessor A is already deleted
297 if _, exists = testMemory.predecessors[descB.Digest]; exists {
298 t.Errorf("predecessor entry of %s should no longer exist by now, since all its predecessors have been deleted", "B")
299 }
300 }
301
302 // +-----------------------------------------------+
303 // | |
304 // | +--------+ |
305 // | |A(index)| |
306 // | +---+----+ |
307 // | | |
308 // | -+--------------+--------------+- |
309 // | | | | |
310 // | +-----v-----+ +-----v-----+ +-----v-----+ |
311 // | |B(manifest)| |C(manifest)| |D(manifest)| |
312 // | +--------+--+ ++---------++ +--+--------+ |
313 // | | | | | |
314 // | | | | | |
315 // | v v v v |
316 // | ++------++ ++------++ |
317 // | |E(layer)| |F(layer)| |
318 // | +--------+ +--------+ |
319 // | |
320 // +-----------------------------------------------+
321 func TestMemory_IndexAllAndPredecessors(t *testing.T) {
322 testFetcher := cas.NewMemory()
323 testMemory := NewMemory()
324 ctx := context.Background()
325
326 // generate test content
327 var blobs [][]byte
328 var descriptors []ocispec.Descriptor
329 appendBlob := func(mediaType string, blob []byte) ocispec.Descriptor {
330 blobs = append(blobs, blob)
331 descriptors = append(descriptors, ocispec.Descriptor{
332 MediaType: mediaType,
333 Digest: digest.FromBytes(blob),
334 Size: int64(len(blob)),
335 })
336 return descriptors[len(descriptors)-1]
337 }
338 generateManifest := func(layers ...ocispec.Descriptor) ocispec.Descriptor {
339 manifest := ocispec.Manifest{
340 Config: ocispec.Descriptor{MediaType: "test config"},
341 Layers: layers,
342 }
343 manifestJSON, err := json.Marshal(manifest)
344 if err != nil {
345 t.Fatal(err)
346 }
347 return appendBlob(ocispec.MediaTypeImageManifest, manifestJSON)
348 }
349 generateIndex := func(manifests ...ocispec.Descriptor) ocispec.Descriptor {
350 index := ocispec.Index{
351 Manifests: manifests,
352 }
353 indexJSON, err := json.Marshal(index)
354 if err != nil {
355 t.Fatal(err)
356 }
357 return appendBlob(ocispec.MediaTypeImageIndex, indexJSON)
358 }
359 descE := appendBlob("layer node E", []byte("Node E is a layer")) // blobs[0], layer "E"
360 descF := appendBlob("layer node F", []byte("Node F is a layer")) // blobs[1], layer "F"
361 descB := generateManifest(descriptors[0:1]...) // blobs[2], manifest "B"
362 descC := generateManifest(descriptors[0:2]...) // blobs[3], manifest "C"
363 descD := generateManifest(descriptors[1:2]...) // blobs[4], manifest "D"
364 descA := generateIndex(descriptors[2:5]...) // blobs[5], index "A"
365
366 // prepare the content in the fetcher, so that it can be used to test IndexAll
367 testContents := []ocispec.Descriptor{descE, descF, descB, descC, descD, descA}
368 for i := 0; i < len(blobs); i++ {
369 testFetcher.Push(ctx, testContents[i], bytes.NewReader(blobs[i]))
370 }
371
372 // make sure that testFetcher works
373 rc, err := testFetcher.Fetch(ctx, descA)
374 if err != nil {
375 t.Errorf("testFetcher.Fetch() error = %v", err)
376 }
377 got, err := io.ReadAll(rc)
378 if err != nil {
379 t.Errorf("testFetcher.Fetch().Read() error = %v", err)
380 }
381 err = rc.Close()
382 if err != nil {
383 t.Errorf("testFetcher.Fetch().Close() error = %v", err)
384 }
385 if !bytes.Equal(got, blobs[5]) {
386 t.Errorf("testFetcher.Fetch() = %v, want %v", got, blobs[4])
387 }
388
389 // index node A into testMemory using IndexAll
390 testMemory.IndexAll(ctx, testFetcher, descA)
391
392 // check the information of node A
393 // 1. verify that node A exists in testMemory.nodes
394 if _, exists := testMemory.nodes[descA.Digest]; !exists {
395 t.Errorf("nodes entry of %s should exist", "A")
396 }
397 // 2. verify that there is no entry of A in predecessors
398 if _, exists := testMemory.predecessors[descA.Digest]; exists {
399 t.Errorf("there should be no entry of %s in predecessors", "A")
400 }
401 // 3. verify that A has successors B, C, D
402 successorsA, exists := testMemory.successors[descA.Digest]
403 if !exists {
404 t.Errorf("there should be an entry of %s in successors", "A")
405 }
406 if !successorsA.Contains(descB.Digest) {
407 t.Errorf("successors of %s should contain %s", "A", "B")
408 }
409 if !successorsA.Contains(descC.Digest) {
410 t.Errorf("successors of %s should contain %s", "A", "C")
411 }
412 if !successorsA.Contains(descD.Digest) {
413 t.Errorf("successors of %s should contain %s", "A", "D")
414 }
415
416 // check the information of node B
417 // 1. verify that node B exists in testMemory.nodes
418 if _, exists := testMemory.nodes[descB.Digest]; !exists {
419 t.Errorf("nodes entry of %s should exist", "B")
420 }
421 // 2. verify that B has node A in its predecessors
422 predecessorsB := testMemory.predecessors[descB.Digest]
423 if !predecessorsB.Contains(descA.Digest) {
424 t.Errorf("predecessors of %s should contain %s", "B", "A")
425 }
426 // 3. verify that B has node E in its successors
427 successorsB := testMemory.successors[descB.Digest]
428 if !successorsB.Contains(descE.Digest) {
429 t.Errorf("successors of %s should contain %s", "B", "E")
430 }
431
432 // check the information of node C
433 // 1. verify that node C exists in testMemory.nodes
434 if _, exists := testMemory.nodes[descC.Digest]; !exists {
435 t.Errorf("nodes entry of %s should exist", "C")
436 }
437 // 2. verify that C has node A in its predecessors
438 predecessorsC := testMemory.predecessors[descC.Digest]
439 if !predecessorsC.Contains(descA.Digest) {
440 t.Errorf("predecessors of %s should contain %s", "C", "A")
441 }
442 // 3. verify that C has node E and F in its successors
443 successorsC := testMemory.successors[descC.Digest]
444 if !successorsC.Contains(descE.Digest) {
445 t.Errorf("successors of %s should contain %s", "C", "E")
446 }
447 if !successorsC.Contains(descF.Digest) {
448 t.Errorf("successors of %s should contain %s", "C", "F")
449 }
450
451 // check the information of node D
452 // 1. verify that node D exists in testMemory.nodes
453 if _, exists := testMemory.nodes[descD.Digest]; !exists {
454 t.Errorf("nodes entry of %s should exist", "D")
455 }
456 // 2. verify that D has node A in its predecessors
457 predecessorsD := testMemory.predecessors[descD.Digest]
458 if !predecessorsD.Contains(descA.Digest) {
459 t.Errorf("predecessors of %s should contain %s", "D", "A")
460 }
461 // 3. verify that D has node F in its successors
462 successorsD := testMemory.successors[descD.Digest]
463 if !successorsD.Contains(descF.Digest) {
464 t.Errorf("successors of %s should contain %s", "D", "F")
465 }
466
467 // check the information of node E
468 // 1. verify that node E exists in testMemory.nodes
469 if _, exists := testMemory.nodes[descE.Digest]; !exists {
470 t.Errorf("nodes entry of %s should exist", "E")
471 }
472 // 2. verify that E has node B and C in its predecessors
473 predecessorsE := testMemory.predecessors[descE.Digest]
474 if !predecessorsE.Contains(descB.Digest) {
475 t.Errorf("predecessors of %s should contain %s", "E", "B")
476 }
477 if !predecessorsE.Contains(descC.Digest) {
478 t.Errorf("predecessors of %s should contain %s", "E", "C")
479 }
480 // 3. verify that E has an entry in successors and it's empty
481 successorsE, exists := testMemory.successors[descE.Digest]
482 if !exists {
483 t.Errorf("entry %s should exist in testMemory.successors", "E")
484 }
485 if successorsE == nil {
486 t.Errorf("successors of %s should be an empty set, not nil", "E")
487 }
488 if len(successorsE) != 0 {
489 t.Errorf("successors of %s should be empty", "E")
490 }
491
492 // check the information of node F
493 // 1. verify that node F exists in testMemory.nodes
494 if _, exists := testMemory.nodes[descF.Digest]; !exists {
495 t.Errorf("nodes entry of %s should exist", "F")
496 }
497 // 2. verify that F has node C and D in its predecessors
498 predecessorsF := testMemory.predecessors[descF.Digest]
499 if !predecessorsF.Contains(descC.Digest) {
500 t.Errorf("predecessors of %s should contain %s", "F", "C")
501 }
502 if !predecessorsF.Contains(descD.Digest) {
503 t.Errorf("predecessors of %s should contain %s", "F", "D")
504 }
505 // 3. verify that F has an entry in successors and it's empty
506 successorsF, exists := testMemory.successors[descF.Digest]
507 if !exists {
508 t.Errorf("entry %s should exist in testMemory.successors", "F")
509 }
510 if successorsF == nil {
511 t.Errorf("successors of %s should be an empty set, not nil", "F")
512 }
513 if len(successorsF) != 0 {
514 t.Errorf("successors of %s should be empty", "F")
515 }
516
517 // check that the Predecessors of node C is node A
518 predsC, err := testMemory.Predecessors(ctx, descC)
519 if err != nil {
520 t.Errorf("testFetcher.Predecessors() error = %v", err)
521 }
522 expectedLength := 1
523 if len(predsC) != expectedLength {
524 t.Errorf("%s should have length %d", "predsC", expectedLength)
525 }
526 if !reflect.DeepEqual(predsC[0], descA) {
527 t.Errorf("incorrect predecessor result")
528 }
529
530 // check that the Predecessors of node F are node C and node D
531 predsF, err := testMemory.Predecessors(ctx, descF)
532 if err != nil {
533 t.Errorf("testFetcher.Predecessors() error = %v", err)
534 }
535 expectedLength = 2
536 if len(predsF) != expectedLength {
537 t.Errorf("%s should have length %d", "predsF", expectedLength)
538 }
539 for _, pred := range predsF {
540 if !reflect.DeepEqual(pred, descC) && !reflect.DeepEqual(pred, descD) {
541 t.Errorf("incorrect predecessor results")
542 }
543 }
544
545 // remove node C and check the stored information
546 testMemory.Remove(descC)
547 if predecessorsE.Contains(descC.Digest) {
548 t.Errorf("predecessors of %s should not contain %s", "E", "C")
549 }
550 if predecessorsF.Contains(descC.Digest) {
551 t.Errorf("predecessors of %s should not contain %s", "F", "C")
552 }
553 if !successorsA.Contains(descC.Digest) {
554 t.Errorf("successors of %s should still contain %s", "A", "C")
555 }
556 if _, exists := testMemory.successors[descC.Digest]; exists {
557 t.Errorf("testMemory.successors should not contain the entry of %s", "C")
558 }
559 if _, exists := testMemory.predecessors[descC.Digest]; !exists {
560 t.Errorf("entry %s in predecessors should still exists since it still has at least one predecessor node present", "C")
561 }
562
563 // remove node A and check the stored information
564 testMemory.Remove(descA)
565 if _, exists := testMemory.predecessors[descB.Digest]; exists {
566 t.Errorf("entry %s in predecessors should no longer exists", "B")
567 }
568 if _, exists := testMemory.predecessors[descC.Digest]; exists {
569 t.Errorf("entry %s in predecessors should no longer exists", "C")
570 }
571 if _, exists := testMemory.predecessors[descD.Digest]; exists {
572 t.Errorf("entry %s in predecessors should no longer exists", "D")
573 }
574 if _, exists := testMemory.successors[descA.Digest]; exists {
575 t.Errorf("testDeletableMemory.successors should not contain the entry of %s", "A")
576 }
577
578 // check that the Predecessors of node D is empty
579 predsD, err := testMemory.Predecessors(ctx, descD)
580 if err != nil {
581 t.Errorf("testFetcher.Predecessors() error = %v", err)
582 }
583 if predsD != nil {
584 t.Errorf("%s should be nil", "predsD")
585 }
586
587 // check that the Predecessors of node E is node B
588 predsE, err := testMemory.Predecessors(ctx, descE)
589 if err != nil {
590 t.Errorf("testFetcher.Predecessors() error = %v", err)
591 }
592 expectedLength = 1
593 if len(predsE) != expectedLength {
594 t.Errorf("%s should have length %d", "predsE", expectedLength)
595 }
596 if !reflect.DeepEqual(predsE[0], descB) {
597 t.Errorf("incorrect predecessor result")
598 }
599 }
600
601 func TestMemory_DigestSet(t *testing.T) {
602 testFetcher := cas.NewMemory()
603 testMemory := NewMemory()
604 ctx := context.Background()
605
606 // generate test content
607 var blobs [][]byte
608 var descriptors []ocispec.Descriptor
609 appendBlob := func(mediaType string, blob []byte) ocispec.Descriptor {
610 blobs = append(blobs, blob)
611 descriptors = append(descriptors, ocispec.Descriptor{
612 MediaType: mediaType,
613 Digest: digest.FromBytes(blob),
614 Size: int64(len(blob)),
615 })
616 return descriptors[len(descriptors)-1]
617 }
618 generateManifest := func(layers ...ocispec.Descriptor) ocispec.Descriptor {
619 manifest := ocispec.Manifest{
620 Config: ocispec.Descriptor{MediaType: "test config"},
621 Layers: layers,
622 }
623 manifestJSON, err := json.Marshal(manifest)
624 if err != nil {
625 t.Fatal(err)
626 }
627 return appendBlob(ocispec.MediaTypeImageManifest, manifestJSON)
628 }
629 generateIndex := func(manifests ...ocispec.Descriptor) ocispec.Descriptor {
630 index := ocispec.Index{
631 Manifests: manifests,
632 }
633 indexJSON, err := json.Marshal(index)
634 if err != nil {
635 t.Fatal(err)
636 }
637 return appendBlob(ocispec.MediaTypeImageIndex, indexJSON)
638 }
639 descE := appendBlob("layer node E", []byte("Node E is a layer")) // blobs[0], layer "E"
640 descF := appendBlob("layer node F", []byte("Node F is a layer")) // blobs[1], layer "F"
641 descB := generateManifest(descriptors[0:1]...) // blobs[2], manifest "B"
642 descC := generateManifest(descriptors[0:2]...) // blobs[3], manifest "C"
643 descD := generateManifest(descriptors[1:2]...) // blobs[4], manifest "D"
644 descA := generateIndex(descriptors[2:5]...) // blobs[5], index "A"
645
646 // prepare the content in the fetcher, so that it can be used to test IndexAll
647 testContents := []ocispec.Descriptor{descE, descF, descB, descC, descD, descA}
648 for i := 0; i < len(blobs); i++ {
649 testFetcher.Push(ctx, testContents[i], bytes.NewReader(blobs[i]))
650 }
651
652 // make sure that testFetcher works
653 rc, err := testFetcher.Fetch(ctx, descA)
654 if err != nil {
655 t.Errorf("testFetcher.Fetch() error = %v", err)
656 }
657 got, err := io.ReadAll(rc)
658 if err != nil {
659 t.Errorf("testFetcher.Fetch().Read() error = %v", err)
660 }
661 err = rc.Close()
662 if err != nil {
663 t.Errorf("testFetcher.Fetch().Close() error = %v", err)
664 }
665 if !bytes.Equal(got, blobs[5]) {
666 t.Errorf("testFetcher.Fetch() = %v, want %v", got, blobs[4])
667 }
668
669 // index node A into testMemory using IndexAll
670 testMemory.IndexAll(ctx, testFetcher, descA)
671 digestSet := testMemory.DigestSet()
672 for i := 0; i < len(blobs); i++ {
673 if exists := digestSet.Contains(descriptors[i].Digest); exists != true {
674 t.Errorf("digest of blob[%d] should exist in digestSet", i)
675 }
676 }
677 }
678
679 func TestMemory_Exists(t *testing.T) {
680 testFetcher := cas.NewMemory()
681 testMemory := NewMemory()
682 ctx := context.Background()
683
684 // generate test content
685 var blobs [][]byte
686 var descriptors []ocispec.Descriptor
687 appendBlob := func(mediaType string, blob []byte) ocispec.Descriptor {
688 blobs = append(blobs, blob)
689 descriptors = append(descriptors, ocispec.Descriptor{
690 MediaType: mediaType,
691 Digest: digest.FromBytes(blob),
692 Size: int64(len(blob)),
693 })
694 return descriptors[len(descriptors)-1]
695 }
696 generateManifest := func(layers ...ocispec.Descriptor) ocispec.Descriptor {
697 manifest := ocispec.Manifest{
698 Config: ocispec.Descriptor{MediaType: "test config"},
699 Layers: layers,
700 }
701 manifestJSON, err := json.Marshal(manifest)
702 if err != nil {
703 t.Fatal(err)
704 }
705 return appendBlob(ocispec.MediaTypeImageManifest, manifestJSON)
706 }
707 generateIndex := func(manifests ...ocispec.Descriptor) ocispec.Descriptor {
708 index := ocispec.Index{
709 Manifests: manifests,
710 }
711 indexJSON, err := json.Marshal(index)
712 if err != nil {
713 t.Fatal(err)
714 }
715 return appendBlob(ocispec.MediaTypeImageIndex, indexJSON)
716 }
717 descE := appendBlob("layer node E", []byte("Node E is a layer")) // blobs[0], layer "E"
718 descF := appendBlob("layer node F", []byte("Node F is a layer")) // blobs[1], layer "F"
719 descB := generateManifest(descriptors[0:1]...) // blobs[2], manifest "B"
720 descC := generateManifest(descriptors[0:2]...) // blobs[3], manifest "C"
721 descD := generateManifest(descriptors[1:2]...) // blobs[4], manifest "D"
722 descA := generateIndex(descriptors[2:5]...) // blobs[5], index "A"
723
724 // prepare the content in the fetcher, so that it can be used to test IndexAll
725 testContents := []ocispec.Descriptor{descE, descF, descB, descC, descD, descA}
726 for i := 0; i < len(blobs); i++ {
727 testFetcher.Push(ctx, testContents[i], bytes.NewReader(blobs[i]))
728 }
729
730 // make sure that testFetcher works
731 rc, err := testFetcher.Fetch(ctx, descA)
732 if err != nil {
733 t.Errorf("testFetcher.Fetch() error = %v", err)
734 }
735 got, err := io.ReadAll(rc)
736 if err != nil {
737 t.Errorf("testFetcher.Fetch().Read() error = %v", err)
738 }
739 err = rc.Close()
740 if err != nil {
741 t.Errorf("testFetcher.Fetch().Close() error = %v", err)
742 }
743 if !bytes.Equal(got, blobs[5]) {
744 t.Errorf("testFetcher.Fetch() = %v, want %v", got, blobs[4])
745 }
746
747 // index node A into testMemory using IndexAll
748 testMemory.IndexAll(ctx, testFetcher, descA)
749 for i := 0; i < len(blobs); i++ {
750 if exists := testMemory.Exists(descriptors[i]); exists != true {
751 t.Errorf("digest of blob[%d] should exist in digestSet", i)
752 }
753 }
754 }