|
5 | 5 | "reflect"
|
6 | 6 | "testing"
|
7 | 7 |
|
| 8 | + "github.com/stretchr/testify/require" |
8 | 9 | "k8s.io/apimachinery/pkg/apis/meta/v1"
|
9 | 10 | )
|
10 | 11 |
|
@@ -464,3 +465,298 @@ func TestClusterServiceVersion_GetVersion(t *testing.T) {
|
464 | 465 | })
|
465 | 466 | }
|
466 | 467 | }
|
| 468 | + |
| 469 | +func TestClusterServiceVersion_GetRelatedImages(t *testing.T) { |
| 470 | + type fields struct { |
| 471 | + TypeMeta v1.TypeMeta |
| 472 | + ObjectMeta v1.ObjectMeta |
| 473 | + Spec json.RawMessage |
| 474 | + } |
| 475 | + tests := []struct { |
| 476 | + name string |
| 477 | + fields fields |
| 478 | + want map[string]struct{} |
| 479 | + wantErr bool |
| 480 | + }{ |
| 481 | + { |
| 482 | + name: "no related images", |
| 483 | + fields: fields{ |
| 484 | + TypeMeta: v1.TypeMeta{}, |
| 485 | + ObjectMeta: v1.ObjectMeta{}, |
| 486 | + Spec: json.RawMessage(`{"no": "field"}`), |
| 487 | + }, |
| 488 | + want: map[string]struct{}{}, |
| 489 | + }, |
| 490 | + { |
| 491 | + name: "one related image", |
| 492 | + fields: fields{ |
| 493 | + TypeMeta: v1.TypeMeta{}, |
| 494 | + ObjectMeta: v1.ObjectMeta{}, |
| 495 | + Spec: json.RawMessage(`{"relatedImages": [ |
| 496 | + {"name": "test", "image": "quay.io/etcd/etcd-operator@sha256:123"} |
| 497 | + ]}`), |
| 498 | + }, |
| 499 | + want: map[string]struct{}{"quay.io/etcd/etcd-operator@sha256:123": {}}, |
| 500 | + }, |
| 501 | + { |
| 502 | + name: "multiple related images", |
| 503 | + fields: fields{ |
| 504 | + TypeMeta: v1.TypeMeta{}, |
| 505 | + ObjectMeta: v1.ObjectMeta{}, |
| 506 | + Spec: json.RawMessage(`{"relatedImages": [ |
| 507 | + {"name": "test", "image": "quay.io/etcd/etcd-operator@sha256:123"}, |
| 508 | + {"name": "operand", "image": "quay.io/etcd/etcd@sha256:123"} |
| 509 | + ]}`), |
| 510 | + }, |
| 511 | + want: map[string]struct{}{"quay.io/etcd/etcd-operator@sha256:123": {}, "quay.io/etcd/etcd@sha256:123": {}}, |
| 512 | + }, |
| 513 | + } |
| 514 | + for _, tt := range tests { |
| 515 | + t.Run(tt.name, func(t *testing.T) { |
| 516 | + csv := &ClusterServiceVersion{ |
| 517 | + TypeMeta: tt.fields.TypeMeta, |
| 518 | + ObjectMeta: tt.fields.ObjectMeta, |
| 519 | + Spec: tt.fields.Spec, |
| 520 | + } |
| 521 | + got, err := csv.GetRelatedImages() |
| 522 | + if (err != nil) != tt.wantErr { |
| 523 | + t.Errorf("GetRelatedImages() error = %v, wantErr %v", err, tt.wantErr) |
| 524 | + return |
| 525 | + } |
| 526 | + require.Equal(t, tt.want, got) |
| 527 | + }) |
| 528 | + } |
| 529 | +} |
| 530 | + |
| 531 | +func TestClusterServiceVersion_GetOperatorImages(t *testing.T) { |
| 532 | + type fields struct { |
| 533 | + TypeMeta v1.TypeMeta |
| 534 | + ObjectMeta v1.ObjectMeta |
| 535 | + Spec json.RawMessage |
| 536 | + } |
| 537 | + tests := []struct { |
| 538 | + name string |
| 539 | + fields fields |
| 540 | + want map[string]struct{} |
| 541 | + wantErr bool |
| 542 | + }{ |
| 543 | + { |
| 544 | + name: "bad strategy", |
| 545 | + fields: fields{ |
| 546 | + TypeMeta: v1.TypeMeta{}, |
| 547 | + ObjectMeta: v1.ObjectMeta{}, |
| 548 | + Spec: json.RawMessage(` |
| 549 | + {"install": {"strategy": "nope", "spec": {"deployments":[{"name":"etcd-operator","spec":{"template":{"spec":{"containers":[{ |
| 550 | + "command":["etcd-operator"], |
| 551 | + "image":"quay.io/coreos/etcd-operator@sha256:c0301e4686c3ed4206e370b42de5a3bd2229b9fb4906cf85f3f30650424abec2", |
| 552 | + "name":"etcd-operator" |
| 553 | + }]}}}}]}}}`), |
| 554 | + }, |
| 555 | + }, |
| 556 | + { |
| 557 | + name: "no images", |
| 558 | + fields: fields{ |
| 559 | + TypeMeta: v1.TypeMeta{}, |
| 560 | + ObjectMeta: v1.ObjectMeta{}, |
| 561 | + Spec: json.RawMessage(` |
| 562 | + {"install": {"strategy": "deployment","spec": {"deployments":[{"name":"etcd-operator","spec":{"template":{"spec": |
| 563 | + "containers":[] |
| 564 | + }}}}]}}}`), |
| 565 | + }, |
| 566 | + want: nil, |
| 567 | + wantErr: true, |
| 568 | + }, |
| 569 | + { |
| 570 | + name: "one image", |
| 571 | + fields: fields{ |
| 572 | + TypeMeta: v1.TypeMeta{}, |
| 573 | + ObjectMeta: v1.ObjectMeta{}, |
| 574 | + Spec: json.RawMessage(` |
| 575 | + {"install": {"strategy": "deployment", "spec": {"deployments":[{ |
| 576 | + "name":"etcd-operator", |
| 577 | + "spec":{ |
| 578 | + "template":{ |
| 579 | + "spec":{ |
| 580 | + "containers":[ |
| 581 | + { |
| 582 | + "command":["etcd-operator"], |
| 583 | + "image":"quay.io/coreos/etcd-operator@sha256:c0301e4686c3ed4206e370b42de5a3bd2229b9fb4906cf85f3f30650424abec2", |
| 584 | + "name":"etcd-operator" |
| 585 | + } |
| 586 | + ] |
| 587 | + } |
| 588 | + } |
| 589 | + }}]}}}`), |
| 590 | + }, |
| 591 | + want: map[string]struct{}{"quay.io/coreos/etcd-operator@sha256:c0301e4686c3ed4206e370b42de5a3bd2229b9fb4906cf85f3f30650424abec2":{}}, |
| 592 | + }, |
| 593 | + { |
| 594 | + name: "two container images", |
| 595 | + fields: fields{ |
| 596 | + TypeMeta: v1.TypeMeta{}, |
| 597 | + ObjectMeta: v1.ObjectMeta{}, |
| 598 | + Spec: json.RawMessage(` |
| 599 | + {"install": {"strategy": "deployment", "spec": {"deployments":[{ |
| 600 | + "name":"etcd-operator", |
| 601 | + "spec":{ |
| 602 | + "template":{ |
| 603 | + "spec":{ |
| 604 | + "containers":[ |
| 605 | + { |
| 606 | + "command":["etcd-operator"], |
| 607 | + "image":"quay.io/coreos/etcd-operator@sha256:c0301e4686c3ed4206e370b42de5a3bd2229b9fb4906cf85f3f30650424abec2", |
| 608 | + "name":"etcd-operator" |
| 609 | + }, |
| 610 | + { |
| 611 | + "command":["etcd-operator-2"], |
| 612 | + "image":"quay.io/coreos/etcd-operator-2@sha256:c0301e4686c3ed4206e370b42de5a3bd2229b9fb4906cf85f3f30650424abec2", |
| 613 | + "name":"etcd-operator-2" |
| 614 | + } |
| 615 | + ] |
| 616 | + } |
| 617 | + } |
| 618 | + }}]}}}`), |
| 619 | + }, |
| 620 | + want: map[string]struct{}{"quay.io/coreos/etcd-operator-2@sha256:c0301e4686c3ed4206e370b42de5a3bd2229b9fb4906cf85f3f30650424abec2":{}, "quay.io/coreos/etcd-operator@sha256:c0301e4686c3ed4206e370b42de5a3bd2229b9fb4906cf85f3f30650424abec2":{}}, |
| 621 | + }, |
| 622 | + { |
| 623 | + name: "init container image", |
| 624 | + fields: fields{ |
| 625 | + TypeMeta: v1.TypeMeta{}, |
| 626 | + ObjectMeta: v1.ObjectMeta{}, |
| 627 | + Spec: json.RawMessage(` |
| 628 | + { |
| 629 | + "install": { |
| 630 | + "strategy": "deployment", |
| 631 | + "spec": { |
| 632 | + "deployments":[ |
| 633 | + { |
| 634 | + "name":"etcd-operator", |
| 635 | + "spec":{ |
| 636 | + "template":{ |
| 637 | + "spec":{ |
| 638 | + "initContainers":[ |
| 639 | + { |
| 640 | + "command":["etcd-operator"], |
| 641 | + "image":"quay.io/coreos/etcd-operator@sha256:c0301e4686c3ed4206e370b42de5a3bd2229b9fb4906cf85f3f30650424abec2", |
| 642 | + "name":"etcd-operator" |
| 643 | + } |
| 644 | + ] |
| 645 | + } |
| 646 | + } |
| 647 | + } |
| 648 | + } |
| 649 | + ] |
| 650 | + } |
| 651 | + } |
| 652 | + }`), |
| 653 | + }, |
| 654 | + want: map[string]struct{}{"quay.io/coreos/etcd-operator@sha256:c0301e4686c3ed4206e370b42de5a3bd2229b9fb4906cf85f3f30650424abec2":{}}, |
| 655 | + }, |
| 656 | + { |
| 657 | + name: "two init container images", |
| 658 | + fields: fields{ |
| 659 | + TypeMeta: v1.TypeMeta{}, |
| 660 | + ObjectMeta: v1.ObjectMeta{}, |
| 661 | + Spec: json.RawMessage(` |
| 662 | + { |
| 663 | + "install": { |
| 664 | + "strategy": "deployment", |
| 665 | + "spec": { |
| 666 | + "deployments":[ |
| 667 | + { |
| 668 | + "name":"etcd-operator", |
| 669 | + "spec":{ |
| 670 | + "template":{ |
| 671 | + "spec":{ |
| 672 | + "initContainers":[ |
| 673 | + { |
| 674 | + "command":["etcd-operator"], |
| 675 | + "image":"quay.io/coreos/etcd-operator@sha256:c0301e4686c3ed4206e370b42de5a3bd2229b9fb4906cf85f3f30650424abec2", |
| 676 | + "name":"etcd-operator" |
| 677 | + }, |
| 678 | + { |
| 679 | + "command":["etcd-operator2"], |
| 680 | + "image":"quay.io/coreos/etcd-operator2@sha256:c0301e4686c3ed4206e370b42de5a3bd2229b9fb4906cf85f3f30650424abec2", |
| 681 | + "name":"etcd-operator2" |
| 682 | + } |
| 683 | + ] |
| 684 | + } |
| 685 | + } |
| 686 | + } |
| 687 | + } |
| 688 | + ] |
| 689 | + } |
| 690 | + } |
| 691 | + }`), |
| 692 | + }, |
| 693 | + want: map[string]struct{}{"quay.io/coreos/etcd-operator2@sha256:c0301e4686c3ed4206e370b42de5a3bd2229b9fb4906cf85f3f30650424abec2":{}, "quay.io/coreos/etcd-operator@sha256:c0301e4686c3ed4206e370b42de5a3bd2229b9fb4906cf85f3f30650424abec2":{}}, |
| 694 | + }, |
| 695 | + { |
| 696 | + name: "container and init container", |
| 697 | + fields: fields{ |
| 698 | + TypeMeta: v1.TypeMeta{}, |
| 699 | + ObjectMeta: v1.ObjectMeta{}, |
| 700 | + Spec: json.RawMessage(` |
| 701 | + { |
| 702 | + "install": { |
| 703 | + "strategy": "deployment", |
| 704 | + "spec": { |
| 705 | + "deployments":[ |
| 706 | + { |
| 707 | + "name":"etcd-operator", |
| 708 | + "spec":{ |
| 709 | + "template":{ |
| 710 | + "spec":{ |
| 711 | + "initContainers":[ |
| 712 | + { |
| 713 | + "command":["init-etcd-operator"], |
| 714 | + "image":"quay.io/coreos/init-etcd-operator@sha256:c0301e4686c3ed4206e370b42de5a3bd2229b9fb4906cf85f3f30650424abec2", |
| 715 | + "name":"etcd-operator" |
| 716 | + }, |
| 717 | + { |
| 718 | + "command":["init-etcd-operator2"], |
| 719 | + "image":"quay.io/coreos/init-etcd-operator2@sha256:c0301e4686c3ed4206e370b42de5a3bd2229b9fb4906cf85f3f30650424abec2", |
| 720 | + "name":"etcd-operator2" |
| 721 | + } |
| 722 | + ], |
| 723 | + "containers":[ |
| 724 | + { |
| 725 | + "command":["etcd-operator"], |
| 726 | + "image":"quay.io/coreos/etcd-operator@sha256:c0301e4686c3ed4206e370b42de5a3bd2229b9fb4906cf85f3f30650424abec2", |
| 727 | + "name":"etcd-operator" |
| 728 | + }, |
| 729 | + { |
| 730 | + "command":["etcd-operator2"], |
| 731 | + "image":"quay.io/coreos/etcd-operator2@sha256:c0301e4686c3ed4206e370b42de5a3bd2229b9fb4906cf85f3f30650424abec2", |
| 732 | + "name":"etcd-operator2" |
| 733 | + } |
| 734 | + ] |
| 735 | + } |
| 736 | + } |
| 737 | + } |
| 738 | + } |
| 739 | + ] |
| 740 | + } |
| 741 | + } |
| 742 | + }`), |
| 743 | + }, |
| 744 | + want: map[string]struct{}{"quay.io/coreos/etcd-operator2@sha256:c0301e4686c3ed4206e370b42de5a3bd2229b9fb4906cf85f3f30650424abec2":{}, "quay.io/coreos/etcd-operator@sha256:c0301e4686c3ed4206e370b42de5a3bd2229b9fb4906cf85f3f30650424abec2":struct {}{}, "quay.io/coreos/init-etcd-operator2@sha256:c0301e4686c3ed4206e370b42de5a3bd2229b9fb4906cf85f3f30650424abec2":{}, "quay.io/coreos/init-etcd-operator@sha256:c0301e4686c3ed4206e370b42de5a3bd2229b9fb4906cf85f3f30650424abec2":{}}, |
| 745 | + }, |
| 746 | + } |
| 747 | + for _, tt := range tests { |
| 748 | + t.Run(tt.name, func(t *testing.T) { |
| 749 | + csv := &ClusterServiceVersion{ |
| 750 | + TypeMeta: tt.fields.TypeMeta, |
| 751 | + ObjectMeta: tt.fields.ObjectMeta, |
| 752 | + Spec: tt.fields.Spec, |
| 753 | + } |
| 754 | + got, err := csv.GetOperatorImages() |
| 755 | + if (err != nil) != tt.wantErr { |
| 756 | + t.Errorf("GetOperatorImages() error = %v, wantErr %v", err, tt.wantErr) |
| 757 | + return |
| 758 | + } |
| 759 | + require.Equal(t, tt.want, got) |
| 760 | + }) |
| 761 | + } |
| 762 | +} |
0 commit comments