@@ -24,6 +24,7 @@ import (
24
24
25
25
"k8s.io/api/core/v1"
26
26
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27
+ "k8s.io/apimachinery/pkg/runtime"
27
28
)
28
29
29
30
var (
@@ -104,17 +105,7 @@ func TestBothAppAndCATLSAssetsExist(t *testing.T) {
104
105
}
105
106
106
107
cg := tlsutil .NewSDKCertGenerator (f .KubeClient )
107
- // Use Pod as a dummy runtime object for the CR input of GenerateCert().
108
- mCR := & v1.Pod {
109
- TypeMeta : metav1.TypeMeta {
110
- Kind : crKind ,
111
- },
112
- ObjectMeta : metav1.ObjectMeta {
113
- Name : crName ,
114
- Namespace : namespace ,
115
- },
116
- }
117
- actualAppSecret , actualCaConfigMap , actualCaSecret , err := cg .GenerateCert (mCR , nil , ccfg )
108
+ actualAppSecret , actualCaConfigMap , actualCaSecret , err := cg .GenerateCert (newDummyCR (namespace ), nil , ccfg )
118
109
if err != nil {
119
110
t .Fatal (err )
120
111
}
@@ -146,17 +137,7 @@ func TestOnlyAppSecretExist(t *testing.T) {
146
137
}
147
138
148
139
cg := tlsutil .NewSDKCertGenerator (f .KubeClient )
149
- // Use Pod as a dummy runtime object for the CR input of GenerateCert().
150
- mCR := & v1.Pod {
151
- TypeMeta : metav1.TypeMeta {
152
- Kind : crKind ,
153
- },
154
- ObjectMeta : metav1.ObjectMeta {
155
- Name : crName ,
156
- Namespace : namespace ,
157
- },
158
- }
159
- _ , _ , _ , err = cg .GenerateCert (mCR , nil , ccfg )
140
+ _ , _ , _ , err = cg .GenerateCert (newDummyCR (namespace ), nil , ccfg )
160
141
if err == nil {
161
142
t .Fatal ("expect error, but got none" )
162
143
}
@@ -186,27 +167,83 @@ func TestOnlyCAExist(t *testing.T) {
186
167
}
187
168
188
169
cg := tlsutil .NewSDKCertGenerator (f .KubeClient )
189
- // Use Pod as a dummy runtime object for the CR input of GenerateCert().
190
- mCR := & v1.Pod {
191
- TypeMeta : metav1.TypeMeta {
192
- Kind : crKind ,
193
- },
194
- ObjectMeta : metav1.ObjectMeta {
195
- Name : crName ,
196
- Namespace : namespace ,
197
- },
170
+ appSecret , _ , _ , err := cg .GenerateCert (newDummyCR (namespace ), newAppSvc (namespace ), ccfg )
171
+ if err != nil {
172
+ t .Fatal (err )
198
173
}
199
- appSvc := & v1.Service {
200
- ObjectMeta : metav1.ObjectMeta {
201
- Name : "app-service" ,
202
- Namespace : namespace ,
203
- },
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 )
204
187
}
205
- appSecret , _ , _ , err := cg .GenerateCert (mCR , appSvc , ccfg )
188
+
189
+ cg := tlsutil .NewSDKCertGenerator (f .KubeClient )
190
+ appSecret , caConfigMap , caSecret , err := cg .GenerateCert (newDummyCR (namespace ), newAppSvc (namespace ), ccfg )
206
191
if err != nil {
207
192
t .Fatal (err )
208
193
}
209
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 ) {
210
247
// check if appSecret has the correct fields.
211
248
if appSecretName != appSecret .Name {
212
249
t .Fatalf ("expect the secret name %v, but got %v" , appSecretName , appSecret .Name )
@@ -225,7 +262,7 @@ func TestOnlyCAExist(t *testing.T) {
225
262
}
226
263
227
264
// check if appSecret exists in k8s cluster.
228
- appSecretFromCluster , err := f .KubeClient .CoreV1 ().Secrets (namespace ).Get (appSecretName , metav1.GetOptions {})
265
+ appSecretFromCluster , err := framework . Global .KubeClient .CoreV1 ().Secrets (namespace ).Get (appSecretName , metav1.GetOptions {})
229
266
if err != nil {
230
267
t .Fatal (err )
231
268
}
@@ -234,3 +271,25 @@ func TestOnlyCAExist(t *testing.T) {
234
271
t .Fatalf ("expect %+v, but got %+v" , appSecret , appSecretFromCluster )
235
272
}
236
273
}
274
+
275
+ // newDummyCR returns a dummy runtime object for the CR input of GenerateCert().
276
+ func newDummyCR (namespace string ) runtime.Object {
277
+ return & v1.Pod {
278
+ TypeMeta : metav1.TypeMeta {
279
+ Kind : crKind ,
280
+ },
281
+ ObjectMeta : metav1.ObjectMeta {
282
+ Name : crName ,
283
+ Namespace : namespace ,
284
+ },
285
+ }
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