@@ -3054,3 +3054,197 @@ func TestTerraformCredentialsHelperConfigMap(t *testing.T) {
3054
3054
})
3055
3055
}
3056
3056
}
3057
+
3058
+ func TestCheckValidateSecretAndConfigMap (t * testing.T ) {
3059
+ ctx := context .Background ()
3060
+ scheme := runtime .NewScheme ()
3061
+ corev1 .AddToScheme (scheme )
3062
+ k8sClient := fake .NewClientBuilder ().WithScheme (scheme ).Build ()
3063
+
3064
+ privateKey := []byte ("aaa" )
3065
+ knownHosts := []byte ("zzz" )
3066
+ secretGitCreds := & corev1.Secret {
3067
+ ObjectMeta : v1.ObjectMeta {
3068
+ Namespace : "default" ,
3069
+ Name : "git-ssh" ,
3070
+ },
3071
+ Data : map [string ][]byte {
3072
+ corev1 .SSHAuthPrivateKey : privateKey ,
3073
+ "known_hosts" : knownHosts ,
3074
+ },
3075
+ Type : corev1 .SecretTypeSSHAuth ,
3076
+ }
3077
+ assert .Nil (t , k8sClient .Create (ctx , secretGitCreds ))
3078
+ assert .Nil (t , k8sClient .Get (ctx , client .ObjectKeyFromObject (secretGitCreds ), secretGitCreds ))
3079
+
3080
+ credentialstfrcjson := []byte ("tfcreds" )
3081
+ terraformrc := []byte ("tfrc" )
3082
+ secretTerraformCredentials := & corev1.Secret {
3083
+ ObjectMeta : v1.ObjectMeta {
3084
+ Namespace : "default" ,
3085
+ Name : "terraform-creds" ,
3086
+ },
3087
+ Data : map [string ][]byte {
3088
+ "credentials.tfrc.json" : credentialstfrcjson ,
3089
+ "terraformrc" : terraformrc ,
3090
+ },
3091
+ Type : corev1 .SecretTypeSSHAuth ,
3092
+ }
3093
+ assert .Nil (t , k8sClient .Create (ctx , secretTerraformCredentials ))
3094
+ assert .Nil (t , k8sClient .Get (ctx , client .ObjectKeyFromObject (secretTerraformCredentials ), secretTerraformCredentials ))
3095
+
3096
+ configMapTerraformRC := & corev1.ConfigMap {
3097
+ ObjectMeta : v1.ObjectMeta {
3098
+ Namespace : "default" ,
3099
+ Name : "terraform-registry-config" ,
3100
+ },
3101
+ Data : map [string ]string {
3102
+ ".terraformrc" : "tfrc" ,
3103
+ },
3104
+ }
3105
+
3106
+ assert .Nil (t , k8sClient .Create (ctx , configMapTerraformRC ))
3107
+ assert .Nil (t , k8sClient .Get (ctx , client .ObjectKeyFromObject (configMapTerraformRC ), configMapTerraformRC ))
3108
+
3109
+ configMapCredentialsHelper := & corev1.ConfigMap {
3110
+ ObjectMeta : v1.ObjectMeta {
3111
+ Namespace : "default" ,
3112
+ Name : "terraform-credentials-helper" ,
3113
+ },
3114
+ Data : map [string ]string {
3115
+ "terraform-credentials-artifactory" : "tfrc" ,
3116
+ },
3117
+ }
3118
+
3119
+ assert .Nil (t , k8sClient .Create (ctx , configMapCredentialsHelper ))
3120
+ assert .Nil (t , k8sClient .Get (ctx , client .ObjectKeyFromObject (configMapCredentialsHelper ), configMapCredentialsHelper ))
3121
+
3122
+ type args struct {
3123
+ k8sClient client.Client
3124
+ meta TFConfigurationMeta
3125
+ }
3126
+
3127
+ type want struct {
3128
+ configMap * corev1.ConfigMap
3129
+ errMsg string
3130
+ }
3131
+
3132
+ testcases := []struct {
3133
+ name string
3134
+ args args
3135
+ want want
3136
+ }{
3137
+ {
3138
+ name : "configmap not found" ,
3139
+ args : args {
3140
+ k8sClient : k8sClient ,
3141
+ meta : TFConfigurationMeta {
3142
+ Name : "a" ,
3143
+ ConfigurationCMName : "b" ,
3144
+ BusyboxImage : "c" ,
3145
+ GitImage : "d" ,
3146
+ Namespace : "e" ,
3147
+ TerraformImage : "f" ,
3148
+ RemoteGit : "g" ,
3149
+ GitCredentialsSecretReference : & corev1.SecretReference {
3150
+ Namespace : "default" ,
3151
+ Name : "git-ssh" ,
3152
+ },
3153
+ TerraformCredentialsSecretReference : & corev1.SecretReference {
3154
+ Namespace : "default" ,
3155
+ Name : "terraform-creds" ,
3156
+ },
3157
+ TerraformRCConfigMapReference : & corev1.SecretReference {
3158
+ Namespace : "default" ,
3159
+ Name : "terraform-registry-config" ,
3160
+ },
3161
+ TerraformCredentialsHelperConfigMapReference : & corev1.SecretReference {
3162
+ Namespace : "default" ,
3163
+ Name : "terraform-credentials-helper" ,
3164
+ },
3165
+ },
3166
+ },
3167
+ want : want {
3168
+ errMsg : "NoError" ,
3169
+ },
3170
+ },
3171
+ {
3172
+ name : "terraform credentials configmap not found" ,
3173
+ args : args {
3174
+ k8sClient : k8sClient ,
3175
+ meta : TFConfigurationMeta {
3176
+ Name : "a" ,
3177
+ ConfigurationCMName : "b" ,
3178
+ BusyboxImage : "c" ,
3179
+ GitImage : "d" ,
3180
+ Namespace : "e" ,
3181
+ TerraformImage : "f" ,
3182
+ RemoteGit : "g" ,
3183
+ GitCredentialsSecretReference : & corev1.SecretReference {
3184
+ Namespace : "default" ,
3185
+ Name : "git-ssh" ,
3186
+ },
3187
+ TerraformCredentialsSecretReference : & corev1.SecretReference {
3188
+ Namespace : "default" ,
3189
+ Name : "terraform-creds" ,
3190
+ },
3191
+ TerraformRCConfigMapReference : & corev1.SecretReference {
3192
+ Namespace : "default" ,
3193
+ Name : "terraform-registry-config" ,
3194
+ },
3195
+ TerraformCredentialsHelperConfigMapReference : & corev1.SecretReference {
3196
+ Namespace : "default" ,
3197
+ Name : "terraform-registry" ,
3198
+ },
3199
+ },
3200
+ },
3201
+ want : want {
3202
+ errMsg : "Failed to get the terraform credentials helper configmap: configmaps \" terraform-registry\" not found" ,
3203
+ },
3204
+ },
3205
+ {
3206
+ name : "terraformrc configmap not found" ,
3207
+ args : args {
3208
+ k8sClient : k8sClient ,
3209
+ meta : TFConfigurationMeta {
3210
+ Name : "a" ,
3211
+ ConfigurationCMName : "b" ,
3212
+ BusyboxImage : "c" ,
3213
+ GitImage : "d" ,
3214
+ Namespace : "e" ,
3215
+ TerraformImage : "f" ,
3216
+ RemoteGit : "g" ,
3217
+ GitCredentialsSecretReference : & corev1.SecretReference {
3218
+ Namespace : "default" ,
3219
+ Name : "git-ssh" ,
3220
+ },
3221
+ TerraformCredentialsSecretReference : & corev1.SecretReference {
3222
+ Namespace : "default" ,
3223
+ Name : "terraform-creds" ,
3224
+ },
3225
+ TerraformRCConfigMapReference : & corev1.SecretReference {
3226
+ Namespace : "default" ,
3227
+ Name : "terraform-registry" ,
3228
+ },
3229
+ TerraformCredentialsHelperConfigMapReference : & corev1.SecretReference {
3230
+ Namespace : "default" ,
3231
+ Name : "terraform-credentials-helper" ,
3232
+ },
3233
+ },
3234
+ },
3235
+ want : want {
3236
+ errMsg : "Failed to get the terraform registry config configmap: configmaps \" terraform-registry\" not found" ,
3237
+ },
3238
+ },
3239
+ }
3240
+
3241
+ for _ , tc := range testcases {
3242
+ t .Run (tc .name , func (t * testing.T ) {
3243
+ err := tc .args .meta .validateSecretAndConfigMap (ctx , k8sClient )
3244
+ if err != nil {
3245
+ assert .EqualError (t , err , tc .want .errMsg )
3246
+ }
3247
+ })
3248
+ }
3249
+
3250
+ }
0 commit comments