Skip to content

Commit 7481c9f

Browse files
committed
e2e: add TestNoneOfCaAndAppSecretExist
1 parent 6995c3a commit 7481c9f

File tree

1 file changed

+86
-14
lines changed

1 file changed

+86
-14
lines changed

test/e2e/tls_util_test.go

Lines changed: 86 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ func TestBothAppAndCATLSAssetsExist(t *testing.T) {
105105
}
106106

107107
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)
110109
if err != nil {
111110
t.Fatal(err)
112111
}
@@ -138,8 +137,7 @@ func TestOnlyAppSecretExist(t *testing.T) {
138137
}
139138

140139
cg := tlsutil.NewSDKCertGenerator(f.KubeClient)
141-
appCR := toDummyCR(namespace)
142-
_, _, _, err = cg.GenerateCert(appCR, nil, ccfg)
140+
_, _, _, err = cg.GenerateCert(newDummyCR(namespace), nil, ccfg)
143141
if err == nil {
144142
t.Fatal("expect error, but got none")
145143
}
@@ -169,18 +167,83 @@ func TestOnlyCAExist(t *testing.T) {
169167
}
170168

171169
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)
178187
}
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)
180191
if err != nil {
181192
t.Fatal(err)
182193
}
183194

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) {
184247
// check if appSecret has the correct fields.
185248
if appSecretName != appSecret.Name {
186249
t.Fatalf("expect the secret name %v, but got %v", appSecretName, appSecret.Name)
@@ -199,7 +262,7 @@ func TestOnlyCAExist(t *testing.T) {
199262
}
200263

201264
// 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{})
203266
if err != nil {
204267
t.Fatal(err)
205268
}
@@ -209,8 +272,8 @@ func TestOnlyCAExist(t *testing.T) {
209272
}
210273
}
211274

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 {
214277
return &v1.Pod{
215278
TypeMeta: metav1.TypeMeta{
216279
Kind: crKind,
@@ -221,3 +284,12 @@ func toDummyCR(namespace string) runtime.Object {
221284
},
222285
}
223286
}
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

Comments
 (0)