@@ -2923,3 +2923,102 @@ func (MockManager) GetLogger() logr.Logger { return logr.Logger{} }
29232923func (MockManager ) GetControllerOptions () v1alpha1.ControllerConfigurationSpec {
29242924 return v1alpha1.ControllerConfigurationSpec {}
29252925}
2926+
2927+ func TestGetVaultSecret (t * testing.T ) {
2928+ controller := Controller {
2929+ Client : newFakeClient (),
2930+ ClientSet : clientsetfake .NewSimpleClientset (),
2931+ }
2932+ clusterOps := & clusteroperationv1alpha1.ClusterOperation {}
2933+
2934+ tests := []struct {
2935+ name string
2936+ setup func ()
2937+ want * apis.SecretRef
2938+ }{
2939+ {
2940+ name : "No HostsConfRef" ,
2941+ setup : func () {
2942+ clusterOps .Spec .HostsConfRef = & apis.ConfigMapRef {}
2943+ },
2944+ want : nil ,
2945+ },
2946+ {
2947+ name : "ConfigMap Not Found" ,
2948+ setup : func () {
2949+ clusterOps .Spec .HostsConfRef = & apis.ConfigMapRef {NameSpace : "default" , Name : "nonexistent" }
2950+ },
2951+ want : nil ,
2952+ },
2953+ {
2954+ name : "No Vault Annotation" ,
2955+ setup : func () {
2956+ cm := & corev1.ConfigMap {
2957+ ObjectMeta : metav1.ObjectMeta {
2958+ Namespace : "default" ,
2959+ Name : "hostsconf" ,
2960+ },
2961+ }
2962+ controller .ClientSet .CoreV1 ().ConfigMaps ("default" ).Create (context .Background (), cm , metav1.CreateOptions {})
2963+ clusterOps .Spec .HostsConfRef = & apis.ConfigMapRef {NameSpace : "default" , Name : "hostsconf" }
2964+ },
2965+ want : nil ,
2966+ },
2967+ {
2968+ name : "Vault Secret Not Found" ,
2969+ setup : func () {
2970+ cm := & corev1.ConfigMap {
2971+ ObjectMeta : metav1.ObjectMeta {
2972+ Namespace : "default" ,
2973+ Name : "hostsconf" ,
2974+ Annotations : map [string ]string {constants .AnnotationHostsConfVaultPasswordRef : "vault-secret" },
2975+ },
2976+ }
2977+ controller .ClientSet .CoreV1 ().ConfigMaps ("default" ).Create (context .Background (), cm , metav1.CreateOptions {})
2978+ clusterOps .Spec .HostsConfRef = & apis.ConfigMapRef {NameSpace : "default" , Name : "hostsconf" }
2979+ },
2980+ want : nil ,
2981+ },
2982+ {
2983+ name : "Successful Retrieval" ,
2984+ setup : func () {
2985+ cm := & corev1.ConfigMap {
2986+ ObjectMeta : metav1.ObjectMeta {
2987+ Namespace : "default" ,
2988+ Name : "hostsconf1" ,
2989+ Annotations : map [string ]string {constants .AnnotationHostsConfVaultPasswordRef : "vault-secret" },
2990+ },
2991+ Data : map [string ]string {
2992+ "vault-password" : "vault-password" ,
2993+ },
2994+ }
2995+ _ , err := controller .ClientSet .CoreV1 ().ConfigMaps ("default" ).Create (context .Background (), cm , metav1.CreateOptions {})
2996+ if err != nil {
2997+ t .Fatalf ("failed to create config map: %v" , err )
2998+ }
2999+ secret := & corev1.Secret {
3000+ ObjectMeta : metav1.ObjectMeta {
3001+ Namespace : "default" ,
3002+ Name : "vault-secret" ,
3003+ },
3004+ }
3005+ _ , err = controller .ClientSet .CoreV1 ().Secrets ("default" ).Create (context .Background (), secret , metav1.CreateOptions {})
3006+ if err != nil {
3007+ t .Fatalf ("failed to create secret: %v" , err )
3008+ }
3009+ clusterOps .Spec .HostsConfRef = & apis.ConfigMapRef {NameSpace : "default" , Name : "hostsconf1" }
3010+ },
3011+ want : & apis.SecretRef {NameSpace : "default" , Name : "vault-secret" },
3012+ },
3013+ }
3014+
3015+ for _ , test := range tests {
3016+ t .Run (test .name , func (t * testing.T ) {
3017+ test .setup ()
3018+ got := controller .getVaultSecret (clusterOps )
3019+ if ! reflect .DeepEqual (got , test .want ) {
3020+ t .Fatalf ("got %v, want %v" , got , test .want )
3021+ }
3022+ })
3023+ }
3024+ }
0 commit comments