@@ -105,8 +105,7 @@ func TestBothAppAndCATLSAssetsExist(t *testing.T) {
105
105
}
106
106
107
107
cg := tlsutil .NewSDKCertGenerator (f .KubeClient )
108
- appCR := toDummyCR (namespace )
109
- actualAppSecret , actualCaConfigMap , actualCaSecret , err := cg .GenerateCert (appCR , nil , ccfg )
108
+ actualAppSecret , actualCaConfigMap , actualCaSecret , err := cg .GenerateCert (newDummyCR (namespace ), nil , ccfg )
110
109
if err != nil {
111
110
t .Fatal (err )
112
111
}
@@ -138,8 +137,7 @@ func TestOnlyAppSecretExist(t *testing.T) {
138
137
}
139
138
140
139
cg := tlsutil .NewSDKCertGenerator (f .KubeClient )
141
- appCR := toDummyCR (namespace )
142
- _ , _ , _ , err = cg .GenerateCert (appCR , nil , ccfg )
140
+ _ , _ , _ , err = cg .GenerateCert (newDummyCR (namespace ), nil , ccfg )
143
141
if err == nil {
144
142
t .Fatal ("expect error, but got none" )
145
143
}
@@ -169,18 +167,83 @@ func TestOnlyCAExist(t *testing.T) {
169
167
}
170
168
171
169
cg := tlsutil .NewSDKCertGenerator (f .KubeClient )
172
- appCR := toDummyCR (namespace )
173
- appSvc := & v1.Service {
174
- ObjectMeta : metav1.ObjectMeta {
175
- Name : "app-service" ,
176
- Namespace : namespace ,
177
- },
170
+ appSecret , _ , _ , err := cg .GenerateCert (newDummyCR (namespace ), newAppSvc (namespace ), ccfg )
171
+ if err != nil {
172
+ t .Fatal (err )
173
+ }
174
+
175
+ verifyAppSecret (t , appSecret , namespace )
176
+ }
177
+
178
+ // TestNoneOfCaAndAppSecretExist ensures that when none of the CA and Application TLS assets
179
+ // exist, GenerateCert() creates both and put them into the k8s cluster.
180
+ func TestNoneOfCaAndAppSecretExist (t * testing.T ) {
181
+ f := framework .Global
182
+ ctx := f .NewTestCtx (t )
183
+ defer ctx .Cleanup (t )
184
+ namespace , err := ctx .GetNamespace ()
185
+ if err != nil {
186
+ t .Fatal (err )
178
187
}
179
- appSecret , _ , _ , err := cg .GenerateCert (appCR , appSvc , ccfg )
188
+
189
+ cg := tlsutil .NewSDKCertGenerator (f .KubeClient )
190
+ appSecret , caConfigMap , caSecret , err := cg .GenerateCert (newDummyCR (namespace ), newAppSvc (namespace ), ccfg )
180
191
if err != nil {
181
192
t .Fatal (err )
182
193
}
183
194
195
+ verifyAppSecret (t , appSecret , namespace )
196
+ verifyCaConfigMap (t , caConfigMap , namespace )
197
+ verifyCASecret (t , caSecret , namespace )
198
+ }
199
+
200
+ func verifyCASecret (t * testing.T , caSecret * v1.Secret , namespace string ) {
201
+ // check if caConfigMap has the correct fields.
202
+ if caConfigMapAndSecretName != caSecret .Name {
203
+ t .Fatalf ("expect the ca config name %v, but got %v" , caConfigMapAndSecretName , caConfigMap .Name )
204
+ }
205
+ if namespace != caSecret .Namespace {
206
+ t .Fatalf ("expect the ca config namespace %v, but got %v" , namespace , appSecret .Namespace )
207
+ }
208
+ if _ , ok := caSecret .Data [tlsutil .TLSPrivateCAKeyKey ]; ! ok {
209
+ t .Fatalf ("expect the ca config to have the data field %v, but got none" , tlsutil .TLSPrivateCAKeyKey )
210
+ }
211
+
212
+ // check if caConfigMap exists in k8s cluster.
213
+ caSecretFromCluster , err := framework .Global .KubeClient .CoreV1 ().Secrets (namespace ).Get (caConfigMapAndSecretName , metav1.GetOptions {})
214
+ if err != nil {
215
+ t .Fatal (err )
216
+ }
217
+ // check if caSecret returned from GenerateCert is the same as the one that exists in the k8s.
218
+ if ! reflect .DeepEqual (caSecret , caSecretFromCluster ) {
219
+ t .Fatalf ("expect %+v, but got %+v" , caSecret , caSecretFromCluster )
220
+ }
221
+ }
222
+
223
+ func verifyCaConfigMap (t * testing.T , caConfigMap * v1.ConfigMap , namespace string ) {
224
+ // check if caConfigMap has the correct fields.
225
+ if caConfigMapAndSecretName != caConfigMap .Name {
226
+ t .Fatalf ("expect the ca config name %v, but got %v" , caConfigMapAndSecretName , caConfigMap .Name )
227
+ }
228
+ if namespace != caConfigMap .Namespace {
229
+ t .Fatalf ("expect the ca config namespace %v, but got %v" , namespace , appSecret .Namespace )
230
+ }
231
+ if _ , ok := caConfigMap .Data [tlsutil .TLSCACertKey ]; ! ok {
232
+ t .Fatalf ("expect the ca config to have the data field %v, but got none" , tlsutil .TLSCACertKey )
233
+ }
234
+
235
+ // check if caConfigMap exists in k8s cluster.
236
+ caConfigMapFromCluster , err := framework .Global .KubeClient .CoreV1 ().ConfigMaps (namespace ).Get (caConfigMapAndSecretName , metav1.GetOptions {})
237
+ if err != nil {
238
+ t .Fatal (err )
239
+ }
240
+ // check if caConfigMap returned from GenerateCert is the same as the one that exists in the k8s.
241
+ if ! reflect .DeepEqual (caConfigMap , caConfigMapFromCluster ) {
242
+ t .Fatalf ("expect %+v, but got %+v" , caConfigMap , caConfigMapFromCluster )
243
+ }
244
+ }
245
+
246
+ func verifyAppSecret (t * testing.T , appSecret * v1.Secret , namespace string ) {
184
247
// check if appSecret has the correct fields.
185
248
if appSecretName != appSecret .Name {
186
249
t .Fatalf ("expect the secret name %v, but got %v" , appSecretName , appSecret .Name )
@@ -199,7 +262,7 @@ func TestOnlyCAExist(t *testing.T) {
199
262
}
200
263
201
264
// check if appSecret exists in k8s cluster.
202
- appSecretFromCluster , err := f .KubeClient .CoreV1 ().Secrets (namespace ).Get (appSecretName , metav1.GetOptions {})
265
+ appSecretFromCluster , err := framework . Global .KubeClient .CoreV1 ().Secrets (namespace ).Get (appSecretName , metav1.GetOptions {})
203
266
if err != nil {
204
267
t .Fatal (err )
205
268
}
@@ -209,8 +272,8 @@ func TestOnlyCAExist(t *testing.T) {
209
272
}
210
273
}
211
274
212
- // use Pod as a dummy runtime object for the CR input of GenerateCert().
213
- func toDummyCR (namespace string ) runtime.Object {
275
+ // newDummyCR returns a dummy runtime object for the CR input of GenerateCert().
276
+ func newDummyCR (namespace string ) runtime.Object {
214
277
return & v1.Pod {
215
278
TypeMeta : metav1.TypeMeta {
216
279
Kind : crKind ,
@@ -221,3 +284,12 @@ func toDummyCR(namespace string) runtime.Object {
221
284
},
222
285
}
223
286
}
287
+
288
+ func newAppSvc (namespace string ) * v1.Service {
289
+ return & v1.Service {
290
+ ObjectMeta : metav1.ObjectMeta {
291
+ Name : "app-service" ,
292
+ Namespace : namespace ,
293
+ },
294
+ }
295
+ }
0 commit comments