|
1 | 1 | package controllers |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "k8s.io/apimachinery/pkg/api/resource" |
| 4 | + "context" |
5 | 5 | "testing" |
6 | 6 |
|
7 | 7 | "github.com/go-logr/logr" |
| 8 | + logrTesting "github.com/go-logr/logr/testing" |
8 | 9 | oadpv1alpha1 "github.com/openshift/oadp-operator/api/v1alpha1" |
| 10 | + oadpScheme "github.com/openshift/oadp-operator/pkg/scheme" |
9 | 11 | v1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1" |
10 | 12 | corev1 "k8s.io/api/core/v1" |
| 13 | + "k8s.io/apimachinery/pkg/api/resource" |
11 | 14 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 15 | + "k8s.io/apimachinery/pkg/runtime" |
12 | 16 | "k8s.io/apimachinery/pkg/types" |
| 17 | + "k8s.io/apimachinery/pkg/version" |
| 18 | + discoverFake "k8s.io/client-go/discovery/fake" |
| 19 | + clienttesting "k8s.io/client-go/testing" |
13 | 20 | "k8s.io/client-go/tools/record" |
14 | 21 | "k8s.io/utils/pointer" |
15 | 22 | "sigs.k8s.io/controller-runtime/pkg/client" |
| 23 | + clientFake "sigs.k8s.io/controller-runtime/pkg/client/fake" |
16 | 24 | ) |
17 | 25 |
|
18 | 26 | func TestDPAReconciler_ValidateDataProtectionCR(t *testing.T) { |
@@ -707,3 +715,136 @@ func TestDPAReconciler_ValidateDataProtectionCR(t *testing.T) { |
707 | 715 | }) |
708 | 716 | } |
709 | 717 | } |
| 718 | + |
| 719 | +var namespacedName = types.NamespacedName{ |
| 720 | + Namespace: "test-ns", |
| 721 | + Name: "test-DPA-CR", |
| 722 | +} |
| 723 | +func TestDPAReconciler_ValidateVeleroPlugins(t *testing.T) { |
| 724 | + type fields struct { |
| 725 | + objects []runtime.Object |
| 726 | + FakedServerVersion *version.Info |
| 727 | + Scheme *runtime.Scheme |
| 728 | + NamespacedName types.NamespacedName |
| 729 | + EventRecorder record.EventRecorder |
| 730 | + } |
| 731 | + tests := []struct { |
| 732 | + name string |
| 733 | + fields fields |
| 734 | + want bool |
| 735 | + wantErr bool |
| 736 | + }{ |
| 737 | + { |
| 738 | + name: "given valid DPA CR with AWS, CSI Default Plugin and secret", |
| 739 | + fields: fields{ |
| 740 | + objects: []runtime.Object{ |
| 741 | + &oadpv1alpha1.DataProtectionApplication{ |
| 742 | + TypeMeta: metav1.TypeMeta{ |
| 743 | + Kind: "DataProtectionApplication", |
| 744 | + APIVersion: "oadp.openshift.io/v1alpha1", |
| 745 | + }, |
| 746 | + ObjectMeta: metav1.ObjectMeta{ |
| 747 | + Name: "test-DPA-CR", |
| 748 | + Namespace: "test-ns", |
| 749 | + }, |
| 750 | + Spec: oadpv1alpha1.DataProtectionApplicationSpec{ |
| 751 | + Configuration: &oadpv1alpha1.ApplicationConfig{ |
| 752 | + Velero: &oadpv1alpha1.VeleroConfig{ |
| 753 | + DefaultPlugins: []oadpv1alpha1.DefaultPlugin{ |
| 754 | + oadpv1alpha1.DefaultPluginAWS, |
| 755 | + oadpv1alpha1.DefaultPluginCSI, |
| 756 | + }, |
| 757 | + }, |
| 758 | + }, |
| 759 | + }, |
| 760 | + }, |
| 761 | + // secret |
| 762 | + &corev1.Secret{ |
| 763 | + ObjectMeta: metav1.ObjectMeta{ |
| 764 | + Name: "cloud-credentials", |
| 765 | + Namespace: "test-ns", |
| 766 | + }, |
| 767 | + Data: map[string][]byte{ |
| 768 | + "cloud": []byte("test"), |
| 769 | + }, |
| 770 | + }, |
| 771 | + }, |
| 772 | + }, |
| 773 | + want: true, |
| 774 | + wantErr: false, |
| 775 | + }, |
| 776 | + { |
| 777 | + name: "Cluster version too high for CSI plugin", |
| 778 | + fields: fields{ |
| 779 | + objects: []runtime.Object{ |
| 780 | + &oadpv1alpha1.DataProtectionApplication{ |
| 781 | + TypeMeta: metav1.TypeMeta{ |
| 782 | + Kind: "DataProtectionApplication", |
| 783 | + APIVersion: "oadp.openshift.io/v1alpha1", |
| 784 | + }, |
| 785 | + ObjectMeta: metav1.ObjectMeta{ |
| 786 | + Name: "test-DPA-CR", |
| 787 | + Namespace: "test-ns", |
| 788 | + }, |
| 789 | + Spec: oadpv1alpha1.DataProtectionApplicationSpec{ |
| 790 | + Configuration: &oadpv1alpha1.ApplicationConfig{ |
| 791 | + Velero: &oadpv1alpha1.VeleroConfig{ |
| 792 | + DefaultPlugins: []oadpv1alpha1.DefaultPlugin{ |
| 793 | + oadpv1alpha1.DefaultPluginAWS, |
| 794 | + oadpv1alpha1.DefaultPluginCSI, |
| 795 | + }, |
| 796 | + }, |
| 797 | + }, |
| 798 | + }, |
| 799 | + }, |
| 800 | + // secret |
| 801 | + &corev1.Secret{ |
| 802 | + ObjectMeta: metav1.ObjectMeta{ |
| 803 | + Name: "cloud-credentials", |
| 804 | + Namespace: "test-ns", |
| 805 | + }, |
| 806 | + Data: map[string][]byte{ |
| 807 | + "cloud": []byte("test"), |
| 808 | + }, |
| 809 | + }, |
| 810 | + }, |
| 811 | + FakedServerVersion: &version.Info{ |
| 812 | + Major: "1", |
| 813 | + Minor: "24", |
| 814 | + }, |
| 815 | + }, |
| 816 | + want: false, |
| 817 | + wantErr: true, |
| 818 | + }, |
| 819 | + } |
| 820 | + for _, tt := range tests { |
| 821 | + t.Run(tt.name, func(t *testing.T) { |
| 822 | + if tt.fields.FakedServerVersion == nil { |
| 823 | + tt.fields.FakedServerVersion = &version.Info{ |
| 824 | + Major: "1", |
| 825 | + Minor: "23", |
| 826 | + } |
| 827 | + } |
| 828 | + log := logrTesting.TestLogger{T: t} |
| 829 | + scheme := runtime.NewScheme() |
| 830 | + oadpScheme.AddToScheme(scheme, log) |
| 831 | + r := &DPAReconciler{ |
| 832 | + Client: clientFake.NewFakeClientWithScheme(scheme, tt.fields.objects...), |
| 833 | + DiscoveryInterface: &discoverFake.FakeDiscovery{ |
| 834 | + Fake: &clienttesting.Fake{}, |
| 835 | + FakedServerVersion: tt.fields.FakedServerVersion}, |
| 836 | + Log: log, |
| 837 | + NamespacedName: namespacedName, |
| 838 | + Context: context.Background(), |
| 839 | + } |
| 840 | + got, err := r.ValidateVeleroPlugins(r.Log) |
| 841 | + if (err != nil) != tt.wantErr { |
| 842 | + t.Errorf("DPAReconciler.ValidateVeleroPlugins() error = %v, wantErr %v", err, tt.wantErr) |
| 843 | + return |
| 844 | + } |
| 845 | + if got != tt.want { |
| 846 | + t.Errorf("DPAReconciler.ValidateVeleroPlugins() = %v, want %v", got, tt.want) |
| 847 | + } |
| 848 | + }) |
| 849 | + } |
| 850 | +} |
0 commit comments