@@ -24,6 +24,7 @@ import (
2424
2525 "k8s.io/api/core/v1"
2626 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+ "k8s.io/apimachinery/pkg/runtime"
2728)
2829
2930var (
@@ -104,17 +105,7 @@ func TestBothAppAndCATLSAssetsExist(t *testing.T) {
104105 }
105106
106107 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 )
118109 if err != nil {
119110 t .Fatal (err )
120111 }
@@ -146,17 +137,7 @@ func TestOnlyAppSecretExist(t *testing.T) {
146137 }
147138
148139 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 )
160141 if err == nil {
161142 t .Fatal ("expect error, but got none" )
162143 }
@@ -186,27 +167,83 @@ func TestOnlyCAExist(t *testing.T) {
186167 }
187168
188169 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 )
198173 }
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 )
204187 }
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 )
206191 if err != nil {
207192 t .Fatal (err )
208193 }
209194
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 ) {
210247 // check if appSecret has the correct fields.
211248 if appSecretName != appSecret .Name {
212249 t .Fatalf ("expect the secret name %v, but got %v" , appSecretName , appSecret .Name )
@@ -225,7 +262,7 @@ func TestOnlyCAExist(t *testing.T) {
225262 }
226263
227264 // 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 {})
229266 if err != nil {
230267 t .Fatal (err )
231268 }
@@ -234,3 +271,25 @@ func TestOnlyCAExist(t *testing.T) {
234271 t .Fatalf ("expect %+v, but got %+v" , appSecret , appSecretFromCluster )
235272 }
236273}
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