15
15
package e2e
16
16
17
17
import (
18
+ "io/ioutil"
18
19
"reflect"
19
20
"testing"
20
21
@@ -27,32 +28,53 @@ import (
27
28
28
29
var (
29
30
// TLS test variables.
30
- crKind = "Pod"
31
- crName = "example-pod"
32
- certName = "app-cert"
33
-
31
+ crKind = "Pod"
32
+ crName = "example-pod"
33
+ certName = "app-cert"
34
34
caConfigMapAndSecretName = tlsutil .ToCASecretAndConfigMapName (crKind , crName )
35
- caConfigMap = & v1.ConfigMap {
35
+ appSecretName = tlsutil .ToAppSecretName (crKind , crName , certName )
36
+
37
+ caConfigMap * v1.ConfigMap
38
+ caSecret * v1.Secret
39
+ appSecret * v1.Secret
40
+
41
+ ccfg * tlsutil.CertConfig
42
+ )
43
+
44
+ // setup test variables.
45
+ func init () {
46
+ caCertBytes , err := ioutil .ReadFile ("./testdata/ca.crt" )
47
+ if err != nil {
48
+ panic (err )
49
+ }
50
+ caConfigMap = & v1.ConfigMap {
36
51
ObjectMeta : metav1.ObjectMeta {
37
52
Name : caConfigMapAndSecretName ,
38
53
},
54
+ Data : map [string ]string {tlsutil .TLSCACertKey : string (caCertBytes )},
55
+ }
56
+
57
+ caKeyBytes , err := ioutil .ReadFile ("./testdata/ca.key" )
58
+ if err != nil {
59
+ panic (err )
39
60
}
40
61
caSecret = & v1.Secret {
41
62
ObjectMeta : metav1.ObjectMeta {
42
63
Name : caConfigMapAndSecretName ,
43
64
},
65
+ Data : map [string ][]byte {tlsutil .TLSPrivateCAKeyKey : caKeyBytes },
44
66
}
45
67
46
68
appSecret = & v1.Secret {
47
69
ObjectMeta : metav1.ObjectMeta {
48
- Name : tlsutil . ToAppSecretName ( crKind , crName , certName ) ,
70
+ Name : appSecretName ,
49
71
},
50
72
}
51
73
52
74
ccfg = & tlsutil.CertConfig {
53
75
CertName : certName ,
54
76
}
55
- )
77
+ }
56
78
57
79
// TestBothAppAndCATLSAssetsExist ensures that when both application
58
80
// and CA TLS assets exist in the k8s cluster for a given cr,
@@ -142,3 +164,73 @@ func TestOnlyAppSecretExist(t *testing.T) {
142
164
t .Fatalf ("expect %v, but got %v" , tlsutil .ErrCANotFound .Error (), err .Error ())
143
165
}
144
166
}
167
+
168
+ // TestOnlyCAExist ensures that at the case where only the CA exists in the cluster;
169
+ // GenerateCert can retrieve the CA and uses it to create a new application secret.
170
+ func TestOnlyCAExist (t * testing.T ) {
171
+ f := framework .Global
172
+ ctx := f .NewTestCtx (t )
173
+ defer ctx .Cleanup (t )
174
+ namespace , err := ctx .GetNamespace ()
175
+ if err != nil {
176
+ t .Fatal (err )
177
+ }
178
+
179
+ _ , err = f .KubeClient .CoreV1 ().ConfigMaps (namespace ).Create (caConfigMap )
180
+ if err != nil {
181
+ t .Fatal (err )
182
+ }
183
+ _ , err = f .KubeClient .CoreV1 ().Secrets (namespace ).Create (caSecret )
184
+ if err != nil {
185
+ t .Fatal (err )
186
+ }
187
+
188
+ 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
+ },
198
+ }
199
+ appSvc := & v1.Service {
200
+ ObjectMeta : metav1.ObjectMeta {
201
+ Name : "app-service" ,
202
+ Namespace : namespace ,
203
+ },
204
+ }
205
+ appSecret , _ , _ , err := cg .GenerateCert (mCR , appSvc , ccfg )
206
+ if err != nil {
207
+ t .Fatal (err )
208
+ }
209
+
210
+ // check if appSecret has the correct fields.
211
+ if appSecretName != appSecret .Name {
212
+ t .Fatalf ("expect the secret name %v, but got %v" , appSecretName , appSecret .Name )
213
+ }
214
+ if namespace != appSecret .Namespace {
215
+ t .Fatalf ("expect the secret namespace %v, but got %v" , namespace , appSecret .Namespace )
216
+ }
217
+ if v1 .SecretTypeTLS != appSecret .Type {
218
+ t .Fatalf ("expect the secret type %v, but got %v" , v1 .SecretTypeTLS , appSecret .Type )
219
+ }
220
+ if _ , ok := appSecret .Data [v1 .TLSCertKey ]; ! ok {
221
+ t .Fatalf ("expect the secret to have the data field %v, but got none" , v1 .TLSCertKey )
222
+ }
223
+ if _ , ok := appSecret .Data [v1 .TLSPrivateKeyKey ]; ! ok {
224
+ t .Fatalf ("expect the secret to have the data field %v, but got none" , v1 .TLSPrivateKeyKey )
225
+ }
226
+
227
+ // check if appSecret exists in k8s cluster.
228
+ appSecretFromCluster , err := f .KubeClient .CoreV1 ().Secrets (namespace ).Get (appSecretName , metav1.GetOptions {})
229
+ if err != nil {
230
+ t .Fatal (err )
231
+ }
232
+ // check if appSecret returned from GenerateCert is the same as the one that exists in the k8s.
233
+ if ! reflect .DeepEqual (appSecret , appSecretFromCluster ) {
234
+ t .Fatalf ("expect %+v, but got %+v" , appSecret , appSecretFromCluster )
235
+ }
236
+ }
0 commit comments