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 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
|
/*
Copyright The ORAS Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package graph
import (
"bytes"
"context"
"encoding/json"
"io"
"reflect"
"testing"
"github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2/internal/cas"
"oras.land/oras-go/v2/internal/descriptor"
)
// +------------------------------+
// | |
// | +-----------+ |
// | |A(manifest)| |
// | +-----+-----+ |
// | | |
// | +------------+ |
// | | | |
// | v v |
// | +-----+-----+ +---+----+ |
// | |B(manifest)| |C(layer)| |
// | +-----+-----+ +--------+ |
// | | |
// | v |
// | +---+----+ |
// | |D(layer)| |
// | +--------+ |
// | |
// |------------------------------+
func TestMemory_IndexAndRemove(t *testing.T) {
testFetcher := cas.NewMemory()
testMemory := NewMemory()
ctx := context.Background()
// generate test content
var blobs [][]byte
var descs []ocispec.Descriptor
appendBlob := func(mediaType string, blob []byte) ocispec.Descriptor {
blobs = append(blobs, blob)
descs = append(descs, ocispec.Descriptor{
MediaType: mediaType,
Digest: digest.FromBytes(blob),
Size: int64(len(blob)),
})
return descs[len(descs)-1]
}
generateManifest := func(layers ...ocispec.Descriptor) ocispec.Descriptor {
manifest := ocispec.Manifest{
Config: ocispec.Descriptor{MediaType: "test config"},
Layers: layers,
}
manifestJSON, err := json.Marshal(manifest)
if err != nil {
t.Fatal(err)
}
return appendBlob(ocispec.MediaTypeImageManifest, manifestJSON)
}
descC := appendBlob("layer node C", []byte("Node C is a layer")) // blobs[0], layer "C"
descD := appendBlob("layer node D", []byte("Node D is a layer")) // blobs[1], layer "D"
descB := generateManifest(descs[0:2]...) // blobs[2], manifest "B"
descA := generateManifest(descs[1:3]...) // blobs[3], manifest "A"
// prepare the content in the fetcher, so that it can be used to test Index
testContents := []ocispec.Descriptor{descC, descD, descB, descA}
for i := 0; i < len(blobs); i++ {
testFetcher.Push(ctx, testContents[i], bytes.NewReader(blobs[i]))
}
// make sure that testFetcher works
rc, err := testFetcher.Fetch(ctx, descA)
if err != nil {
t.Errorf("testFetcher.Fetch() error = %v", err)
}
got, err := io.ReadAll(rc)
if err != nil {
t.Errorf("testFetcher.Fetch().Read() error = %v", err)
}
err = rc.Close()
if err != nil {
t.Errorf("testFetcher.Fetch().Close() error = %v", err)
}
if !bytes.Equal(got, blobs[3]) {
t.Errorf("testFetcher.Fetch() = %v, want %v", got, blobs[4])
}
nodeKeyA := descriptor.FromOCI(descA)
nodeKeyB := descriptor.FromOCI(descB)
nodeKeyC := descriptor.FromOCI(descC)
nodeKeyD := descriptor.FromOCI(descD)
// index and check the information of node D
testMemory.Index(ctx, testFetcher, descD)
// 1. verify its existence in testMemory.nodes
if _, exists := testMemory.nodes[nodeKeyD]; !exists {
t.Errorf("nodes entry of %s should exist", "D")
}
// 2. verify that the entry of D exists in testMemory.successors and it's empty
successorsD, exists := testMemory.successors[nodeKeyD]
if !exists {
t.Errorf("successor entry of %s should exist", "D")
}
if successorsD == nil {
t.Errorf("successors of %s should be an empty set, not nil", "D")
}
if len(successorsD) != 0 {
t.Errorf("successors of %s should be empty", "D")
}
// 3. there should be no entry of D in testMemory.predecessors yet
_, exists = testMemory.predecessors[nodeKeyD]
if exists {
t.Errorf("predecessor entry of %s should not exist yet", "D")
}
// index and check the information of node C
testMemory.Index(ctx, testFetcher, descC)
// 1. verify its existence in memory.nodes
if _, exists := testMemory.nodes[nodeKeyC]; !exists {
t.Errorf("nodes entry of %s should exist", "C")
}
// 2. verify that the entry of C exists in testMemory.successors and it's empty
successorsC, exists := testMemory.successors[nodeKeyC]
if !exists {
t.Errorf("successor entry of %s should exist", "C")
}
if successorsC == nil {
t.Errorf("successors of %s should be an empty set, not nil", "C")
}
if len(successorsC) != 0 {
t.Errorf("successors of %s should be empty", "C")
}
// 3. there should be no entry of C in testMemory.predecessors yet
_, exists = testMemory.predecessors[nodeKeyC]
if exists {
t.Errorf("predecessor entry of %s should not exist yet", "C")
}
// index and check the information of node A
testMemory.Index(ctx, testFetcher, descA)
// 1. verify its existence in testMemory.nodes
if _, exists := testMemory.nodes[nodeKeyA]; !exists {
t.Errorf("nodes entry of %s should exist", "A")
}
// 2. verify that the entry of A exists in testMemory.successors and it contains
// node B and node D
successorsA, exists := testMemory.successors[nodeKeyA]
if !exists {
t.Errorf("successor entry of %s should exist", "A")
}
if successorsA == nil {
t.Errorf("successors of %s should be a set, not nil", "A")
}
if !successorsA.Contains(nodeKeyB) {
t.Errorf("successors of %s should contain %s", "A", "B")
}
if !successorsA.Contains(nodeKeyD) {
t.Errorf("successors of %s should contain %s", "A", "D")
}
// 3. verify that node A exists in the predecessors lists of its successors.
// there should be an entry of D in testMemory.predecessors by now and it
// should contain A but not B
predecessorsD, exists := testMemory.predecessors[nodeKeyD]
if !exists {
t.Errorf("predecessor entry of %s should exist by now", "D")
}
if !predecessorsD.Contains(nodeKeyA) {
t.Errorf("predecessors of %s should contain %s", "D", "A")
}
if predecessorsD.Contains(nodeKeyB) {
t.Errorf("predecessors of %s should not contain %s yet", "D", "B")
}
// there should be an entry of B in testMemory.predecessors now
// and it should contain A
predecessorsB, exists := testMemory.predecessors[nodeKeyB]
if !exists {
t.Errorf("predecessor entry of %s should exist by now", "B")
}
if !predecessorsB.Contains(nodeKeyA) {
t.Errorf("predecessors of %s should contain %s", "B", "A")
}
// 4. there should be no entry of A in testMemory.predecessors
_, exists = testMemory.predecessors[nodeKeyA]
if exists {
t.Errorf("predecessor entry of %s should not exist", "A")
}
// index and check the information of node B
testMemory.Index(ctx, testFetcher, descB)
// 1. verify its existence in testMemory.nodes
if _, exists := testMemory.nodes[nodeKeyB]; !exists {
t.Errorf("nodes entry of %s should exist", "B")
}
// 2. verify that the entry of B exists in testMemory.successors and it contains
// node C and node D
successorsB, exists := testMemory.successors[nodeKeyB]
if !exists {
t.Errorf("successor entry of %s should exist", "B")
}
if successorsB == nil {
t.Errorf("successors of %s should be a set, not nil", "B")
}
if !successorsB.Contains(nodeKeyC) {
t.Errorf("successors of %s should contain %s", "B", "C")
}
if !successorsB.Contains(nodeKeyD) {
t.Errorf("successors of %s should contain %s", "B", "D")
}
// 3. verify that node B exists in the predecessors lists of its successors.
// there should be an entry of C in testMemory.predecessors by now
// and it should contain B
predecessorsC, exists := testMemory.predecessors[nodeKeyC]
if !exists {
t.Errorf("predecessor entry of %s should exist by now", "C")
}
if !predecessorsC.Contains(nodeKeyB) {
t.Errorf("predecessors of %s should contain %s", "C", "B")
}
// predecessors of D should have been updated now to have node A and B
if !predecessorsD.Contains(nodeKeyB) {
t.Errorf("predecessors of %s should contain %s", "D", "B")
}
if !predecessorsD.Contains(nodeKeyA) {
t.Errorf("predecessors of %s should contain %s", "D", "A")
}
// remove node B and check the stored information
testMemory.Remove(descB)
// 1. verify that node B no longer exists in testMemory.nodes
if _, exists := testMemory.nodes[nodeKeyB]; exists {
t.Errorf("nodes entry of %s should no longer exist", "B")
}
// 2. verify B' predecessors info: B's entry in testMemory.predecessors should
// still exist, since its predecessor A still exists
predecessorsB, exists = testMemory.predecessors[nodeKeyB]
if !exists {
t.Errorf("testDeletableMemory.predecessors should still contain the entry of %s", "B")
}
if !predecessorsB.Contains(nodeKeyA) {
t.Errorf("predecessors of %s should still contain %s", "B", "A")
}
// 3. verify B' successors info: B's entry in testMemory.successors should no
// longer exist
if _, exists := testMemory.successors[nodeKeyB]; exists {
t.Errorf("testDeletableMemory.successors should not contain the entry of %s", "B")
}
// 4. verify B' predecessors' successors info: B should still exist in A's
// successors
if !successorsA.Contains(nodeKeyB) {
t.Errorf("successors of %s should still contain %s", "A", "B")
}
// 5. verify B' successors' predecessors info: C's entry in testMemory.predecessors
// should no longer exist, since C's only predecessor B is already deleted
if _, exists = testMemory.predecessors[nodeKeyC]; exists {
t.Errorf("predecessor entry of %s should no longer exist by now, since all its predecessors have been deleted", "C")
}
// B should no longer exist in D's predecessors
if predecessorsD.Contains(nodeKeyB) {
t.Errorf("predecessors of %s should not contain %s", "D", "B")
}
// but A still exists in D's predecessors
if !predecessorsD.Contains(nodeKeyA) {
t.Errorf("predecessors of %s should still contain %s", "D", "A")
}
// remove node A and check the stored information
testMemory.Remove(descA)
// 1. verify that node A no longer exists in testMemory.nodes
if _, exists := testMemory.nodes[nodeKeyA]; exists {
t.Errorf("nodes entry of %s should no longer exist", "A")
}
// 2. verify A' successors info: A's entry in testMemory.successors should no
// longer exist
if _, exists := testMemory.successors[nodeKeyA]; exists {
t.Errorf("testDeletableMemory.successors should not contain the entry of %s", "A")
}
// 3. verify A' successors' predecessors info: D's entry in testMemory.predecessors
// should no longer exist, since all predecessors of D are already deleted
if _, exists = testMemory.predecessors[nodeKeyD]; exists {
t.Errorf("predecessor entry of %s should no longer exist by now, since all its predecessors have been deleted", "D")
}
// B's entry in testMemory.predecessors should no longer exist, since B's only
// predecessor A is already deleted
if _, exists = testMemory.predecessors[nodeKeyB]; exists {
t.Errorf("predecessor entry of %s should no longer exist by now, since all its predecessors have been deleted", "B")
}
}
// +-----------------------------------------------+
// | |
// | +--------+ |
// | |A(index)| |
// | +---+----+ |
// | | |
// | -+--------------+--------------+- |
// | | | | |
// | +-----v-----+ +-----v-----+ +-----v-----+ |
// | |B(manifest)| |C(manifest)| |D(manifest)| |
// | +--------+--+ ++---------++ +--+--------+ |
// | | | | | |
// | | | | | |
// | v v v v |
// | ++------++ ++------++ |
// | |E(layer)| |F(layer)| |
// | +--------+ +--------+ |
// | |
// +-----------------------------------------------+
func TestMemory_IndexAllAndPredecessors(t *testing.T) {
testFetcher := cas.NewMemory()
testMemory := NewMemory()
ctx := context.Background()
// generate test content
var blobs [][]byte
var descriptors []ocispec.Descriptor
appendBlob := func(mediaType string, blob []byte) ocispec.Descriptor {
blobs = append(blobs, blob)
descriptors = append(descriptors, ocispec.Descriptor{
MediaType: mediaType,
Digest: digest.FromBytes(blob),
Size: int64(len(blob)),
})
return descriptors[len(descriptors)-1]
}
generateManifest := func(layers ...ocispec.Descriptor) ocispec.Descriptor {
manifest := ocispec.Manifest{
Config: ocispec.Descriptor{MediaType: "test config"},
Layers: layers,
}
manifestJSON, err := json.Marshal(manifest)
if err != nil {
t.Fatal(err)
}
return appendBlob(ocispec.MediaTypeImageManifest, manifestJSON)
}
generateIndex := func(manifests ...ocispec.Descriptor) ocispec.Descriptor {
index := ocispec.Index{
Manifests: manifests,
}
indexJSON, err := json.Marshal(index)
if err != nil {
t.Fatal(err)
}
return appendBlob(ocispec.MediaTypeImageIndex, indexJSON)
}
descE := appendBlob("layer node E", []byte("Node E is a layer")) // blobs[0], layer "E"
descF := appendBlob("layer node F", []byte("Node F is a layer")) // blobs[1], layer "F"
descB := generateManifest(descriptors[0:1]...) // blobs[2], manifest "B"
descC := generateManifest(descriptors[0:2]...) // blobs[3], manifest "C"
descD := generateManifest(descriptors[1:2]...) // blobs[4], manifest "D"
descA := generateIndex(descriptors[2:5]...) // blobs[5], index "A"
// prepare the content in the fetcher, so that it can be used to test IndexAll
testContents := []ocispec.Descriptor{descE, descF, descB, descC, descD, descA}
for i := 0; i < len(blobs); i++ {
testFetcher.Push(ctx, testContents[i], bytes.NewReader(blobs[i]))
}
// make sure that testFetcher works
rc, err := testFetcher.Fetch(ctx, descA)
if err != nil {
t.Errorf("testFetcher.Fetch() error = %v", err)
}
got, err := io.ReadAll(rc)
if err != nil {
t.Errorf("testFetcher.Fetch().Read() error = %v", err)
}
err = rc.Close()
if err != nil {
t.Errorf("testFetcher.Fetch().Close() error = %v", err)
}
if !bytes.Equal(got, blobs[5]) {
t.Errorf("testFetcher.Fetch() = %v, want %v", got, blobs[4])
}
nodeKeyA := descriptor.FromOCI(descA)
nodeKeyB := descriptor.FromOCI(descB)
nodeKeyC := descriptor.FromOCI(descC)
nodeKeyD := descriptor.FromOCI(descD)
nodeKeyE := descriptor.FromOCI(descE)
nodeKeyF := descriptor.FromOCI(descF)
// index node A into testMemory using IndexAll
testMemory.IndexAll(ctx, testFetcher, descA)
// check the information of node A
// 1. verify that node A exists in testMemory.nodes
if _, exists := testMemory.nodes[nodeKeyA]; !exists {
t.Errorf("nodes entry of %s should exist", "A")
}
// 2. verify that there is no entry of A in predecessors
if _, exists := testMemory.predecessors[nodeKeyA]; exists {
t.Errorf("there should be no entry of %s in predecessors", "A")
}
// 3. verify that A has successors B, C, D
successorsA, exists := testMemory.successors[nodeKeyA]
if !exists {
t.Errorf("there should be an entry of %s in successors", "A")
}
if !successorsA.Contains(nodeKeyB) {
t.Errorf("successors of %s should contain %s", "A", "B")
}
if !successorsA.Contains(nodeKeyC) {
t.Errorf("successors of %s should contain %s", "A", "C")
}
if !successorsA.Contains(nodeKeyD) {
t.Errorf("successors of %s should contain %s", "A", "D")
}
// check the information of node B
// 1. verify that node B exists in testMemory.nodes
if _, exists := testMemory.nodes[nodeKeyB]; !exists {
t.Errorf("nodes entry of %s should exist", "B")
}
// 2. verify that B has node A in its predecessors
predecessorsB := testMemory.predecessors[nodeKeyB]
if !predecessorsB.Contains(nodeKeyA) {
t.Errorf("predecessors of %s should contain %s", "B", "A")
}
// 3. verify that B has node E in its successors
successorsB := testMemory.successors[nodeKeyB]
if !successorsB.Contains(nodeKeyE) {
t.Errorf("successors of %s should contain %s", "B", "E")
}
// check the information of node C
// 1. verify that node C exists in testMemory.nodes
if _, exists := testMemory.nodes[nodeKeyC]; !exists {
t.Errorf("nodes entry of %s should exist", "C")
}
// 2. verify that C has node A in its predecessors
predecessorsC := testMemory.predecessors[nodeKeyC]
if !predecessorsC.Contains(nodeKeyA) {
t.Errorf("predecessors of %s should contain %s", "C", "A")
}
// 3. verify that C has node E and F in its successors
successorsC := testMemory.successors[nodeKeyC]
if !successorsC.Contains(nodeKeyE) {
t.Errorf("successors of %s should contain %s", "C", "E")
}
if !successorsC.Contains(nodeKeyF) {
t.Errorf("successors of %s should contain %s", "C", "F")
}
// check the information of node D
// 1. verify that node D exists in testMemory.nodes
if _, exists := testMemory.nodes[nodeKeyD]; !exists {
t.Errorf("nodes entry of %s should exist", "D")
}
// 2. verify that D has node A in its predecessors
predecessorsD := testMemory.predecessors[nodeKeyD]
if !predecessorsD.Contains(nodeKeyA) {
t.Errorf("predecessors of %s should contain %s", "D", "A")
}
// 3. verify that D has node F in its successors
successorsD := testMemory.successors[nodeKeyD]
if !successorsD.Contains(nodeKeyF) {
t.Errorf("successors of %s should contain %s", "D", "F")
}
// check the information of node E
// 1. verify that node E exists in testMemory.nodes
if _, exists := testMemory.nodes[nodeKeyE]; !exists {
t.Errorf("nodes entry of %s should exist", "E")
}
// 2. verify that E has node B and C in its predecessors
predecessorsE := testMemory.predecessors[nodeKeyE]
if !predecessorsE.Contains(nodeKeyB) {
t.Errorf("predecessors of %s should contain %s", "E", "B")
}
if !predecessorsE.Contains(nodeKeyC) {
t.Errorf("predecessors of %s should contain %s", "E", "C")
}
// 3. verify that E has an entry in successors and it's empty
successorsE, exists := testMemory.successors[nodeKeyE]
if !exists {
t.Errorf("entry %s should exist in testMemory.successors", "E")
}
if successorsE == nil {
t.Errorf("successors of %s should be an empty set, not nil", "E")
}
if len(successorsE) != 0 {
t.Errorf("successors of %s should be empty", "E")
}
// check the information of node F
// 1. verify that node F exists in testMemory.nodes
if _, exists := testMemory.nodes[nodeKeyF]; !exists {
t.Errorf("nodes entry of %s should exist", "F")
}
// 2. verify that F has node C and D in its predecessors
predecessorsF := testMemory.predecessors[nodeKeyF]
if !predecessorsF.Contains(nodeKeyC) {
t.Errorf("predecessors of %s should contain %s", "F", "C")
}
if !predecessorsF.Contains(nodeKeyD) {
t.Errorf("predecessors of %s should contain %s", "F", "D")
}
// 3. verify that F has an entry in successors and it's empty
successorsF, exists := testMemory.successors[nodeKeyF]
if !exists {
t.Errorf("entry %s should exist in testMemory.successors", "F")
}
if successorsF == nil {
t.Errorf("successors of %s should be an empty set, not nil", "F")
}
if len(successorsF) != 0 {
t.Errorf("successors of %s should be empty", "F")
}
// check that the Predecessors of node C is node A
predsC, err := testMemory.Predecessors(ctx, descC)
if err != nil {
t.Errorf("testFetcher.Predecessors() error = %v", err)
}
expectedLength := 1
if len(predsC) != expectedLength {
t.Errorf("%s should have length %d", "predsC", expectedLength)
}
if !reflect.DeepEqual(predsC[0], descA) {
t.Errorf("incorrect predecessor result")
}
// check that the Predecessors of node F are node C and node D
predsF, err := testMemory.Predecessors(ctx, descF)
if err != nil {
t.Errorf("testFetcher.Predecessors() error = %v", err)
}
expectedLength = 2
if len(predsF) != expectedLength {
t.Errorf("%s should have length %d", "predsF", expectedLength)
}
for _, pred := range predsF {
if !reflect.DeepEqual(pred, descC) && !reflect.DeepEqual(pred, descD) {
t.Errorf("incorrect predecessor results")
}
}
// remove node C and check the stored information
testMemory.Remove(descC)
if predecessorsE.Contains(nodeKeyC) {
t.Errorf("predecessors of %s should not contain %s", "E", "C")
}
if predecessorsF.Contains(nodeKeyC) {
t.Errorf("predecessors of %s should not contain %s", "F", "C")
}
if !successorsA.Contains(nodeKeyC) {
t.Errorf("successors of %s should still contain %s", "A", "C")
}
if _, exists := testMemory.successors[nodeKeyC]; exists {
t.Errorf("testMemory.successors should not contain the entry of %s", "C")
}
if _, exists := testMemory.predecessors[nodeKeyC]; !exists {
t.Errorf("entry %s in predecessors should still exists since it still has at least one predecessor node present", "C")
}
// remove node A and check the stored information
testMemory.Remove(descA)
if _, exists := testMemory.predecessors[nodeKeyB]; exists {
t.Errorf("entry %s in predecessors should no longer exists", "B")
}
if _, exists := testMemory.predecessors[nodeKeyC]; exists {
t.Errorf("entry %s in predecessors should no longer exists", "C")
}
if _, exists := testMemory.predecessors[nodeKeyD]; exists {
t.Errorf("entry %s in predecessors should no longer exists", "D")
}
if _, exists := testMemory.successors[nodeKeyA]; exists {
t.Errorf("testDeletableMemory.successors should not contain the entry of %s", "A")
}
// check that the Predecessors of node D is empty
predsD, err := testMemory.Predecessors(ctx, descD)
if err != nil {
t.Errorf("testFetcher.Predecessors() error = %v", err)
}
if predsD != nil {
t.Errorf("%s should be nil", "predsD")
}
// check that the Predecessors of node E is node B
predsE, err := testMemory.Predecessors(ctx, descE)
if err != nil {
t.Errorf("testFetcher.Predecessors() error = %v", err)
}
expectedLength = 1
if len(predsE) != expectedLength {
t.Errorf("%s should have length %d", "predsE", expectedLength)
}
if !reflect.DeepEqual(predsE[0], descB) {
t.Errorf("incorrect predecessor result")
}
}
func TestMemory_DigestSet(t *testing.T) {
testFetcher := cas.NewMemory()
testMemory := NewMemory()
ctx := context.Background()
// generate test content
var blobs [][]byte
var descriptors []ocispec.Descriptor
appendBlob := func(mediaType string, blob []byte) ocispec.Descriptor {
blobs = append(blobs, blob)
descriptors = append(descriptors, ocispec.Descriptor{
MediaType: mediaType,
Digest: digest.FromBytes(blob),
Size: int64(len(blob)),
})
return descriptors[len(descriptors)-1]
}
generateManifest := func(layers ...ocispec.Descriptor) ocispec.Descriptor {
manifest := ocispec.Manifest{
Config: ocispec.Descriptor{MediaType: "test config"},
Layers: layers,
}
manifestJSON, err := json.Marshal(manifest)
if err != nil {
t.Fatal(err)
}
return appendBlob(ocispec.MediaTypeImageManifest, manifestJSON)
}
generateIndex := func(manifests ...ocispec.Descriptor) ocispec.Descriptor {
index := ocispec.Index{
Manifests: manifests,
}
indexJSON, err := json.Marshal(index)
if err != nil {
t.Fatal(err)
}
return appendBlob(ocispec.MediaTypeImageIndex, indexJSON)
}
descE := appendBlob("layer node E", []byte("Node E is a layer")) // blobs[0], layer "E"
descF := appendBlob("layer node F", []byte("Node F is a layer")) // blobs[1], layer "F"
descB := generateManifest(descriptors[0:1]...) // blobs[2], manifest "B"
descC := generateManifest(descriptors[0:2]...) // blobs[3], manifest "C"
descD := generateManifest(descriptors[1:2]...) // blobs[4], manifest "D"
descA := generateIndex(descriptors[2:5]...) // blobs[5], index "A"
// prepare the content in the fetcher, so that it can be used to test IndexAll
testContents := []ocispec.Descriptor{descE, descF, descB, descC, descD, descA}
for i := 0; i < len(blobs); i++ {
testFetcher.Push(ctx, testContents[i], bytes.NewReader(blobs[i]))
}
// make sure that testFetcher works
rc, err := testFetcher.Fetch(ctx, descA)
if err != nil {
t.Errorf("testFetcher.Fetch() error = %v", err)
}
got, err := io.ReadAll(rc)
if err != nil {
t.Errorf("testFetcher.Fetch().Read() error = %v", err)
}
err = rc.Close()
if err != nil {
t.Errorf("testFetcher.Fetch().Close() error = %v", err)
}
if !bytes.Equal(got, blobs[5]) {
t.Errorf("testFetcher.Fetch() = %v, want %v", got, blobs[4])
}
// index node A into testMemory using IndexAll
testMemory.IndexAll(ctx, testFetcher, descA)
digestSet := testMemory.DigestSet()
for i := 0; i < len(blobs); i++ {
if exists := digestSet.Contains(descriptors[i].Digest); exists != true {
t.Errorf("digest of blob[%d] should exist in digestSet", i)
}
}
}
func TestMemory_Exists(t *testing.T) {
testFetcher := cas.NewMemory()
testMemory := NewMemory()
ctx := context.Background()
// generate test content
var blobs [][]byte
var descriptors []ocispec.Descriptor
appendBlob := func(mediaType string, blob []byte) ocispec.Descriptor {
blobs = append(blobs, blob)
descriptors = append(descriptors, ocispec.Descriptor{
MediaType: mediaType,
Digest: digest.FromBytes(blob),
Size: int64(len(blob)),
})
return descriptors[len(descriptors)-1]
}
generateManifest := func(layers ...ocispec.Descriptor) ocispec.Descriptor {
manifest := ocispec.Manifest{
Config: ocispec.Descriptor{MediaType: "test config"},
Layers: layers,
}
manifestJSON, err := json.Marshal(manifest)
if err != nil {
t.Fatal(err)
}
return appendBlob(ocispec.MediaTypeImageManifest, manifestJSON)
}
generateIndex := func(manifests ...ocispec.Descriptor) ocispec.Descriptor {
index := ocispec.Index{
Manifests: manifests,
}
indexJSON, err := json.Marshal(index)
if err != nil {
t.Fatal(err)
}
return appendBlob(ocispec.MediaTypeImageIndex, indexJSON)
}
descE := appendBlob("layer node E", []byte("Node E is a layer")) // blobs[0], layer "E"
descF := appendBlob("layer node F", []byte("Node F is a layer")) // blobs[1], layer "F"
descB := generateManifest(descriptors[0:1]...) // blobs[2], manifest "B"
descC := generateManifest(descriptors[0:2]...) // blobs[3], manifest "C"
descD := generateManifest(descriptors[1:2]...) // blobs[4], manifest "D"
descA := generateIndex(descriptors[2:5]...) // blobs[5], index "A"
// prepare the content in the fetcher, so that it can be used to test IndexAll
testContents := []ocispec.Descriptor{descE, descF, descB, descC, descD, descA}
for i := 0; i < len(blobs); i++ {
testFetcher.Push(ctx, testContents[i], bytes.NewReader(blobs[i]))
}
// make sure that testFetcher works
rc, err := testFetcher.Fetch(ctx, descA)
if err != nil {
t.Errorf("testFetcher.Fetch() error = %v", err)
}
got, err := io.ReadAll(rc)
if err != nil {
t.Errorf("testFetcher.Fetch().Read() error = %v", err)
}
err = rc.Close()
if err != nil {
t.Errorf("testFetcher.Fetch().Close() error = %v", err)
}
if !bytes.Equal(got, blobs[5]) {
t.Errorf("testFetcher.Fetch() = %v, want %v", got, blobs[4])
}
// index node A into testMemory using IndexAll
testMemory.IndexAll(ctx, testFetcher, descA)
for i := 0; i < len(blobs); i++ {
if exists := testMemory.Exists(descriptors[i]); exists != true {
t.Errorf("digest of blob[%d] should exist in digestSet", i)
}
}
}
|