@@ -78,7 +78,8 @@ func TestUpdateCoreDNS(t *testing.T) {
78
78
Spec : appsv1.DeploymentSpec {
79
79
Template : corev1.PodTemplateSpec {
80
80
ObjectMeta : v1.ObjectMeta {
81
- Name : coreDNSKey ,
81
+ Name : coreDNSKey ,
82
+ Labels : map [string ]string {"app" : coreDNSKey },
82
83
},
83
84
Spec : corev1.PodSpec {
84
85
Containers : []corev1.Container {{
@@ -87,6 +88,9 @@ func TestUpdateCoreDNS(t *testing.T) {
87
88
}},
88
89
},
89
90
},
91
+ Selector : & metav1.LabelSelector {
92
+ MatchLabels : map [string ]string {"app" : coreDNSKey },
93
+ },
90
94
},
91
95
}
92
96
@@ -286,63 +290,74 @@ kind: ClusterConfiguration
286
290
// We are using testEnv as a workload cluster, and given that each test case assumes well known objects with specific
287
291
// Namespace/Name (e.g. The CoderDNS ConfigMap & Deployment, the kubeadm ConfigMap), it is not possible to run the use cases in parallel.
288
292
for _ , tt := range tests {
289
- g := NewWithT (t )
290
- t .Log (tt .name )
291
-
292
- for _ , o := range tt .objs {
293
- // NB. deep copy test object so changes applied during a test does not affect other tests.
294
- o := o .DeepCopyObject ().(client.Object )
295
- g .Expect (testEnv .Create (ctx , o )).To (Succeed ())
296
- }
293
+ t .Run (tt .name , func (t * testing.T ) {
294
+ g := NewWithT (t )
297
295
298
- w := & Workload {
299
- Client : testEnv .GetClient (),
300
- CoreDNSMigrator : tt .migrator ,
301
- }
302
- err := w .UpdateCoreDNS (ctx , tt .kcp )
303
- if tt .expectErr {
304
- g .Expect (err ).To (HaveOccurred ())
305
- return
306
- }
307
- g .Expect (err ).ToNot (HaveOccurred ())
296
+ for _ , o := range tt .objs {
297
+ // NB. deep copy test object so changes applied during a test does not affect other tests.
298
+ o := o .DeepCopyObject ().(client.Object )
299
+ g .Expect (testEnv .Create (ctx , o )).To (Succeed ())
300
+ // this makes sure that the cache is updated with the object
301
+ // to avoid 404 errors leading to test flakes
302
+ g .Eventually (func () bool {
303
+ key , _ := client .ObjectKeyFromObject (o )
304
+ err := testEnv .Get (ctx , key , o )
305
+ return err == nil
306
+ }, "10s" ).Should (BeTrue ())
307
+ }
308
308
309
- // Assert that CoreDNS updates have been made
310
- if tt .expectUpdates {
311
- // assert kubeadmConfigMap
312
- var expectedKubeadmConfigMap corev1.ConfigMap
313
- g .Expect (testEnv .Get (ctx , ctrlclient.ObjectKey {Name : kubeadmConfigKey , Namespace : metav1 .NamespaceSystem }, & expectedKubeadmConfigMap )).To (Succeed ())
314
- g .Expect (expectedKubeadmConfigMap .Data ).To (HaveKeyWithValue ("ClusterConfiguration" , ContainSubstring ("1.7.2" )))
315
- g .Expect (expectedKubeadmConfigMap .Data ).To (HaveKeyWithValue ("ClusterConfiguration" , ContainSubstring ("k8s.gcr.io/some-repo" )))
309
+ // Register cleanup function
310
+ t .Cleanup (func () {
311
+ // Cleanup test objects (and wait for deletion to complete).
312
+ _ = testEnv .Cleanup (ctx , tt .objs ... )
313
+ g .Eventually (func () bool {
314
+ for _ , o := range tt .objs {
315
+ o := o .DeepCopyObject ().(client.Object )
316
+ key , _ := client .ObjectKeyFromObject (o )
317
+ err := testEnv .Get (ctx , key , o )
318
+ if err == nil || (err != nil && ! apierrors .IsNotFound (err )) {
319
+ return false
320
+ }
321
+ }
322
+ return true
323
+ }, "10s" ).Should (BeTrue ())
324
+ })
316
325
317
- // assert CoreDNS corefile
318
- var expectedConfigMap corev1.ConfigMap
319
- g .Expect (testEnv .Get (ctx , ctrlclient.ObjectKey {Name : coreDNSKey , Namespace : metav1 .NamespaceSystem }, & expectedConfigMap )).To (Succeed ())
320
- g .Expect (expectedConfigMap .Data ).To (HaveLen (2 ))
321
- g .Expect (expectedConfigMap .Data ).To (HaveKeyWithValue ("Corefile" , "updated-core-file" ))
322
- g .Expect (expectedConfigMap .Data ).To (HaveKeyWithValue ("Corefile-backup" , expectedCorefile ))
326
+ w := & Workload {
327
+ Client : testEnv .GetClient (),
328
+ CoreDNSMigrator : tt .migrator ,
329
+ }
330
+ err := w .UpdateCoreDNS (ctx , tt .kcp )
323
331
324
- // assert CoreDNS deployment
325
- var actualDeployment appsv1.Deployment
326
- g .Eventually (func () string {
327
- g .Expect (testEnv .Get (ctx , ctrlclient.ObjectKey {Name : coreDNSKey , Namespace : metav1 .NamespaceSystem }, & actualDeployment )).To (Succeed ())
328
- return actualDeployment .Spec .Template .Spec .Containers [0 ].Image
329
- }, "5s" ).Should (Equal ("k8s.gcr.io/some-repo/coredns:1.7.2" ))
330
- }
332
+ if tt .expectErr {
333
+ g .Expect (err ).To (HaveOccurred ())
334
+ return
335
+ }
336
+ g .Expect (err ).ToNot (HaveOccurred ())
331
337
332
- // Cleanup test objects (and wait for deletion to complete).
333
- testEnv .Cleanup (ctx , tt .objs ... )
334
- g .Eventually (func () bool {
335
- for _ , o := range []client.Object {cm , depl , kubeadmCM } {
336
- // NB. deep copy test object so changes applied during a test does not affect other tests.
337
- o := o .DeepCopyObject ().(client.Object )
338
- key , _ := client .ObjectKeyFromObject (o )
339
- err := testEnv .Get (ctx , key , o )
340
- if err == nil || (err != nil && ! apierrors .IsNotFound (err )) {
341
- return false
342
- }
338
+ // Assert that CoreDNS updates have been made
339
+ if tt .expectUpdates {
340
+ // assert kubeadmConfigMap
341
+ var expectedKubeadmConfigMap corev1.ConfigMap
342
+ g .Expect (testEnv .Get (ctx , ctrlclient.ObjectKey {Name : kubeadmConfigKey , Namespace : metav1 .NamespaceSystem }, & expectedKubeadmConfigMap )).To (Succeed ())
343
+ g .Expect (expectedKubeadmConfigMap .Data ).To (HaveKeyWithValue ("ClusterConfiguration" , ContainSubstring ("1.7.2" )))
344
+ g .Expect (expectedKubeadmConfigMap .Data ).To (HaveKeyWithValue ("ClusterConfiguration" , ContainSubstring ("k8s.gcr.io/some-repo" )))
345
+
346
+ // assert CoreDNS corefile
347
+ var expectedConfigMap corev1.ConfigMap
348
+ g .Expect (testEnv .Get (ctx , ctrlclient.ObjectKey {Name : coreDNSKey , Namespace : metav1 .NamespaceSystem }, & expectedConfigMap )).To (Succeed ())
349
+ g .Expect (expectedConfigMap .Data ).To (HaveLen (2 ))
350
+ g .Expect (expectedConfigMap .Data ).To (HaveKeyWithValue ("Corefile" , "updated-core-file" ))
351
+ g .Expect (expectedConfigMap .Data ).To (HaveKeyWithValue ("Corefile-backup" , expectedCorefile ))
352
+
353
+ // assert CoreDNS deployment
354
+ var actualDeployment appsv1.Deployment
355
+ g .Eventually (func () string {
356
+ g .Expect (testEnv .Get (ctx , ctrlclient.ObjectKey {Name : coreDNSKey , Namespace : metav1 .NamespaceSystem }, & actualDeployment )).To (Succeed ())
357
+ return actualDeployment .Spec .Template .Spec .Containers [0 ].Image
358
+ }, "5s" ).Should (Equal ("k8s.gcr.io/some-repo/coredns:1.7.2" ))
343
359
}
344
- return true
345
- }, "10s" ).Should (BeTrue ())
360
+ })
346
361
}
347
362
}
348
363
0 commit comments