github.com/verrazzano/verrazzano@v1.7.0/cluster-operator/controllers/vmc/update_rancher_rolebinding_test.go (about) 1 // Copyright (c) 2022, Oracle and/or its affiliates. 2 // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. 3 4 package vmc 5 6 import ( 7 "context" 8 "fmt" 9 "testing" 10 11 asserts "github.com/stretchr/testify/assert" 12 "github.com/verrazzano/verrazzano/cluster-operator/apis/clusters/v1alpha1" 13 vzconst "github.com/verrazzano/verrazzano/pkg/constants" 14 "github.com/verrazzano/verrazzano/pkg/log/vzlog" 15 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 16 "k8s.io/apimachinery/pkg/runtime/schema" 17 "k8s.io/apimachinery/pkg/types" 18 fakes "sigs.k8s.io/controller-runtime/pkg/client/fake" 19 ) 20 21 func TestUpdateRancherClusterRoleBinding(t *testing.T) { 22 a := asserts.New(t) 23 24 vmcNoID := &v1alpha1.VerrazzanoManagedCluster{} 25 26 clusterID := "testID" 27 vmcID := vmcNoID.DeepCopy() 28 vmcID.Status.RancherRegistration.ClusterID = clusterID 29 30 clusterUserNoData := &unstructured.Unstructured{} 31 clusterUserNoData.SetGroupVersionKind(schema.GroupVersionKind{ 32 Group: APIGroupRancherManagement, 33 Version: APIGroupVersionRancherManagement, 34 Kind: UserKind, 35 }) 36 clusterUserNoData.SetName(vzconst.VerrazzanoClusterRancherUsername) 37 38 clusterUserData := clusterUserNoData.DeepCopy() 39 data := clusterUserData.UnstructuredContent() 40 data[UserUsernameAttribute] = vzconst.VerrazzanoClusterRancherUsername 41 42 tests := []struct { 43 name string 44 vmc *v1alpha1.VerrazzanoManagedCluster 45 expectCreate bool 46 expectErr bool 47 user *unstructured.Unstructured 48 }{ 49 { 50 name: "test nil vmc", 51 expectCreate: false, 52 expectErr: false, 53 user: clusterUserData, 54 }, 55 { 56 name: "test vmc no cluster id", 57 vmc: vmcNoID, 58 expectCreate: false, 59 expectErr: false, 60 user: clusterUserData, 61 }, 62 { 63 name: "test vmc with cluster id", 64 vmc: vmcID, 65 expectCreate: true, 66 expectErr: false, 67 user: clusterUserData, 68 }, 69 { 70 name: "test user doesn't exist", 71 vmc: vmcID, 72 expectCreate: false, 73 expectErr: true, 74 }, 75 { 76 name: "test user no username", 77 vmc: vmcID, 78 expectCreate: false, 79 expectErr: true, 80 user: clusterUserNoData, 81 }, 82 } 83 for _, tt := range tests { 84 t.Run(tt.name, func(t *testing.T) { 85 b := fakes.NewClientBuilder() 86 if tt.user != nil { 87 b = b.WithObjects(tt.user) 88 } 89 c := b.Build() 90 91 r := &VerrazzanoManagedClusterReconciler{ 92 Client: c, 93 log: vzlog.DefaultLogger(), 94 } 95 err := r.updateRancherClusterRoleBindingTemplate(tt.vmc) 96 97 if tt.expectErr { 98 a.Error(err) 99 return 100 } 101 a.NoError(err) 102 103 if tt.expectCreate { 104 name := fmt.Sprintf("crtb-verrazzano-cluster-%s", clusterID) 105 resource := &unstructured.Unstructured{} 106 resource.SetGroupVersionKind(schema.GroupVersionKind{ 107 Group: APIGroupRancherManagement, 108 Version: APIGroupVersionRancherManagement, 109 Kind: ClusterRoleTemplateBindingKind, 110 }) 111 err = c.Get(context.TODO(), types.NamespacedName{Namespace: clusterID, Name: name}, resource) 112 a.NoError(err) 113 a.NotNil(resource) 114 a.Equal(clusterID, resource.Object[ClusterRoleTemplateBindingAttributeClusterName]) 115 a.Equal(vzconst.VerrazzanoClusterRancherUsername, resource.Object[ClusterRoleTemplateBindingAttributeUserName]) 116 a.Equal(vzconst.VerrazzanoClusterRancherName, resource.Object[ClusterRoleTemplateBindingAttributeRoleTemplateName]) 117 } 118 }) 119 } 120 }