Skip to content

Commit a887a3b

Browse files
authored
Changed code to improve output messages on error for files under test/e2e/apps (kubernetes#109944)
* Improving the output of tests in case of error * Better error message Also, the condition in the second case was reversed * Fixing 2 tests whose condition was inverted * Again I got the conditions wrong * Sorry for the confusion * Improved error messages on failures
1 parent cfa6ad5 commit a887a3b

File tree

7 files changed

+98
-32
lines changed

7 files changed

+98
-32
lines changed

test/e2e/apps/cronjob.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,9 @@ var _ = SIGDescribe("CronJob", func() {
261261
ginkgo.By("Ensuring job was deleted")
262262
_, err = e2ejob.GetJob(ctx, f.ClientSet, f.Namespace.Name, job.Name)
263263
framework.ExpectError(err)
264-
framework.ExpectEqual(apierrors.IsNotFound(err), true)
264+
if !apierrors.IsNotFound(err) {
265+
framework.Failf("Failed to delete %s cronjob in namespace %s", cronJob.Name, f.Namespace.Name)
266+
}
265267

266268
ginkgo.By("Ensuring the job is not in the cronjob active list")
267269
err = waitForJobNotActive(ctx, f.ClientSet, f.Namespace.Name, cronJob.Name, job.Name)
@@ -385,10 +387,14 @@ var _ = SIGDescribe("CronJob", func() {
385387
for sawAnnotations := false; !sawAnnotations; {
386388
select {
387389
case evt, ok := <-cjWatch.ResultChan():
388-
framework.ExpectEqual(ok, true, "watch channel should not close")
390+
if !ok {
391+
framework.Fail("watch channel should not close")
392+
}
389393
framework.ExpectEqual(evt.Type, watch.Modified)
390394
watchedCronJob, isCronJob := evt.Object.(*batchv1.CronJob)
391-
framework.ExpectEqual(isCronJob, true, fmt.Sprintf("expected CronJob, got %T", evt.Object))
395+
if !isCronJob {
396+
framework.Failf("expected CronJob, got %T", evt.Object)
397+
}
392398
if watchedCronJob.Annotations["patched"] == "true" {
393399
framework.Logf("saw patched and updated annotations")
394400
sawAnnotations = true
@@ -414,7 +420,9 @@ var _ = SIGDescribe("CronJob", func() {
414420
[]byte(`{"metadata":{"annotations":{"patchedstatus":"true"}},"status":`+string(cjStatusJSON)+`}`),
415421
metav1.PatchOptions{}, "status")
416422
framework.ExpectNoError(err)
417-
framework.ExpectEqual(patchedStatus.Status.LastScheduleTime.Equal(&now1), true, "patched object should have the applied lastScheduleTime status")
423+
if !patchedStatus.Status.LastScheduleTime.Equal(&now1) {
424+
framework.Failf("patched object should have the applied lastScheduleTime %#v, got %#v instead", cjStatus.LastScheduleTime, patchedStatus.Status.LastScheduleTime)
425+
}
418426
framework.ExpectEqual(patchedStatus.Annotations["patchedstatus"], "true", "patched object should have the applied annotation")
419427

420428
ginkgo.By("updating /status")
@@ -431,7 +439,9 @@ var _ = SIGDescribe("CronJob", func() {
431439
return err
432440
})
433441
framework.ExpectNoError(err)
434-
framework.ExpectEqual(updatedStatus.Status.LastScheduleTime.Equal(&now2), true, fmt.Sprintf("updated object status expected to have updated lastScheduleTime %#v, got %#v", statusToUpdate.Status.LastScheduleTime, updatedStatus.Status.LastScheduleTime))
442+
if !updatedStatus.Status.LastScheduleTime.Equal(&now2) {
443+
framework.Failf("updated object status expected to have updated lastScheduleTime %#v, got %#v", statusToUpdate.Status.LastScheduleTime, updatedStatus.Status.LastScheduleTime)
444+
}
435445

436446
ginkgo.By("get /status")
437447
cjResource := schema.GroupVersionResource{Group: "batch", Version: cjVersion, Resource: "cronjobs"}
@@ -444,7 +454,9 @@ var _ = SIGDescribe("CronJob", func() {
444454
// CronJob resource delete operations
445455
expectFinalizer := func(cj *batchv1.CronJob, msg string) {
446456
framework.ExpectNotEqual(cj.DeletionTimestamp, nil, fmt.Sprintf("expected deletionTimestamp, got nil on step: %q, cronjob: %+v", msg, cj))
447-
framework.ExpectEqual(len(cj.Finalizers) > 0, true, fmt.Sprintf("expected finalizers on cronjob, got none on step: %q, cronjob: %+v", msg, cj))
457+
if len(cj.Finalizers) == 0 {
458+
framework.Failf("expected finalizers on cronjob, got none on step: %q, cronjob: %+v", msg, cj)
459+
}
448460
}
449461

450462
ginkgo.By("deleting")
@@ -458,7 +470,9 @@ var _ = SIGDescribe("CronJob", func() {
458470
if err == nil {
459471
expectFinalizer(cj, "deleting cronjob")
460472
} else {
461-
framework.ExpectEqual(apierrors.IsNotFound(err), true, fmt.Sprintf("expected 404, got %v", err))
473+
if !apierrors.IsNotFound(err) {
474+
framework.Failf("expected 404, got %v", err)
475+
}
462476
}
463477

464478
ginkgo.By("deleting a collection")
@@ -467,7 +481,10 @@ var _ = SIGDescribe("CronJob", func() {
467481
cjs, err = cjClient.List(ctx, metav1.ListOptions{LabelSelector: "special-label=" + f.UniqueName})
468482
framework.ExpectNoError(err)
469483
// Should have <= 2 items since some cronjobs might not have been deleted yet due to finalizers
470-
framework.ExpectEqual(len(cjs.Items) <= 2, true, "filtered list should be <= 2")
484+
if len(cjs.Items) > 2 {
485+
framework.Logf("got unexpected filtered list: %v", cjs.Items)
486+
framework.Fail("filtered list should be <= 2")
487+
}
471488
// Validate finalizers
472489
for _, cj := range cjs.Items {
473490
expectFinalizer(&cj, "deleting cronjob collection")

test/e2e/apps/daemon_set.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,9 @@ var _ = SIGDescribe("Daemon set [Serial]", func() {
496496
rollbackPods[pod.Name] = true
497497
}
498498
for _, pod := range existingPods {
499-
framework.ExpectEqual(rollbackPods[pod.Name], true, fmt.Sprintf("unexpected pod %s be restarted", pod.Name))
499+
if !rollbackPods[pod.Name] {
500+
framework.Failf("unexpected pod %s be restarted", pod.Name)
501+
}
500502
}
501503
})
502504

test/e2e/apps/deployment.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,9 @@ var _ = SIGDescribe("Deployment", func() {
325325
break
326326
}
327327
}
328-
framework.ExpectEqual(foundDeployment, true, "unable to find the Deployment in list", deploymentsList)
328+
if !foundDeployment {
329+
framework.Failf("unable to find the Deployment in the following list %v", deploymentsList)
330+
}
329331

330332
ginkgo.By("updating the Deployment")
331333
testDeploymentUpdate := testDeployment
@@ -681,7 +683,9 @@ func stopDeployment(ctx context.Context, c clientset.Interface, ns, deploymentNa
681683
framework.Logf("Ensuring deployment %s was deleted", deploymentName)
682684
_, err = c.AppsV1().Deployments(ns).Get(ctx, deployment.Name, metav1.GetOptions{})
683685
framework.ExpectError(err)
684-
framework.ExpectEqual(apierrors.IsNotFound(err), true)
686+
if !apierrors.IsNotFound(err) {
687+
framework.Failf("Expected deployment %s to be deleted", deploymentName)
688+
}
685689
framework.Logf("Ensuring deployment %s's RSes were deleted", deploymentName)
686690
selector, err := metav1.LabelSelectorAsSelector(deployment.Spec.Selector)
687691
framework.ExpectNoError(err)

test/e2e/apps/disruption.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,9 @@ var _ = SIGDescribe("DisruptionController", func() {
318318
if c.shouldDeny {
319319
err = cs.CoreV1().Pods(ns).EvictV1(ctx, e)
320320
framework.ExpectError(err, "pod eviction should fail")
321-
framework.ExpectEqual(apierrors.HasStatusCause(err, policyv1.DisruptionBudgetCause), true, "pod eviction should fail with DisruptionBudget cause")
321+
if !apierrors.HasStatusCause(err, policyv1.DisruptionBudgetCause) {
322+
framework.Fail("pod eviction should fail with DisruptionBudget cause")
323+
}
322324
} else {
323325
// Only wait for running pods in the "allow" case
324326
// because one of shouldDeny cases relies on the
@@ -362,7 +364,9 @@ var _ = SIGDescribe("DisruptionController", func() {
362364
}
363365
err = cs.CoreV1().Pods(ns).EvictV1(ctx, e)
364366
framework.ExpectError(err, "pod eviction should fail")
365-
framework.ExpectEqual(apierrors.HasStatusCause(err, policyv1.DisruptionBudgetCause), true, "pod eviction should fail with DisruptionBudget cause")
367+
if !apierrors.HasStatusCause(err, policyv1.DisruptionBudgetCause) {
368+
framework.Failf("pod eviction should fail with DisruptionBudget cause. The error was \"%v\"", err)
369+
}
366370

367371
ginkgo.By("Updating the pdb to allow a pod to be evicted")
368372
updatePDBOrDie(ctx, cs, ns, defaultName, func(pdb *policyv1.PodDisruptionBudget) *policyv1.PodDisruptionBudget {
@@ -400,7 +404,9 @@ var _ = SIGDescribe("DisruptionController", func() {
400404
}
401405
err = cs.CoreV1().Pods(ns).EvictV1(ctx, e)
402406
framework.ExpectError(err, "pod eviction should fail")
403-
framework.ExpectEqual(apierrors.HasStatusCause(err, policyv1.DisruptionBudgetCause), true, "pod eviction should fail with DisruptionBudget cause")
407+
if !apierrors.HasStatusCause(err, policyv1.DisruptionBudgetCause) {
408+
framework.Failf("pod eviction should fail with DisruptionBudget cause. The error was \"%v\"", err)
409+
}
404410

405411
ginkgo.By("Deleting the pdb to allow a pod to be evicted")
406412
deletePDBOrDie(ctx, cs, ns, defaultName)

test/e2e/apps/job.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,9 @@ var _ = SIGDescribe("Job", func() {
298298
break
299299
}
300300
}
301-
framework.ExpectEqual(exists, true)
301+
if !exists {
302+
framework.Failf("Expected suspended job to exist. It was not found.")
303+
}
302304

303305
ginkgo.By("Updating the job with suspend=false")
304306
job.Spec.Suspend = pointer.BoolPtr(false)
@@ -354,7 +356,9 @@ var _ = SIGDescribe("Job", func() {
354356
break
355357
}
356358
}
357-
framework.ExpectEqual(exists, true)
359+
if !exists {
360+
framework.Failf("Expected suspended job to exist. It was not found.")
361+
}
358362
})
359363

360364
/*
@@ -494,7 +498,9 @@ var _ = SIGDescribe("Job", func() {
494498
ginkgo.By("Ensuring job was deleted")
495499
_, err = e2ejob.GetJob(ctx, f.ClientSet, f.Namespace.Name, job.Name)
496500
framework.ExpectError(err, "failed to ensure job %s was deleted in namespace: %s", job.Name, f.Namespace.Name)
497-
framework.ExpectEqual(apierrors.IsNotFound(err), true)
501+
if !apierrors.IsNotFound(err) {
502+
framework.Failf("failed to ensure job %s was deleted in namespace: %s", job.Name, f.Namespace.Name)
503+
}
498504
})
499505

500506
/*
@@ -661,7 +667,9 @@ var _ = SIGDescribe("Job", func() {
661667
[]byte(`{"metadata":{"annotations":{"patchedstatus":"true"}},"status":`+string(jStatusJSON)+`}`),
662668
metav1.PatchOptions{}, "status")
663669
framework.ExpectNoError(err)
664-
framework.ExpectEqual(patchedStatus.Status.StartTime.Equal(&now1), true, "patched object should have the applied StartTime status")
670+
if !patchedStatus.Status.StartTime.Equal(&now1) {
671+
framework.Failf("patched object should have the applied StartTime %#v, got %#v instead", jStatus.StartTime, patchedStatus.Status.StartTime)
672+
}
665673
framework.ExpectEqual(patchedStatus.Annotations["patchedstatus"], "true", "patched object should have the applied annotation")
666674

667675
ginkgo.By("updating /status")
@@ -678,7 +686,9 @@ var _ = SIGDescribe("Job", func() {
678686
return err
679687
})
680688
framework.ExpectNoError(err)
681-
framework.ExpectEqual(updatedStatus.Status.StartTime.Equal(&now2), true, fmt.Sprintf("updated object status expected to have updated StartTime %#v, got %#v", statusToUpdate.Status.StartTime, updatedStatus.Status.StartTime))
689+
if !updatedStatus.Status.StartTime.Equal(&now2) {
690+
framework.Failf("updated object status expected to have updated StartTime %#v, got %#v", statusToUpdate.Status.StartTime, updatedStatus.Status.StartTime)
691+
}
682692

683693
ginkgo.By("get /status")
684694
jResource := schema.GroupVersionResource{Group: "batch", Version: "v1", Resource: "jobs"}

test/e2e/apps/rc.go

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,9 @@ var _ = SIGDescribe("ReplicationController", func() {
164164
return true, nil
165165
})
166166
framework.ExpectNoError(err, "Wait until condition with watch events should not return an error")
167-
framework.ExpectEqual(eventFound, true, "failed to find RC %v event", watch.Added)
167+
if !eventFound {
168+
framework.Failf("failed to find RC %v event", watch.Added)
169+
}
168170

169171
ginkgo.By("waiting for available Replicas")
170172
eventFound = false
@@ -187,7 +189,9 @@ var _ = SIGDescribe("ReplicationController", func() {
187189
return true, nil
188190
})
189191
framework.ExpectNoError(err, "Wait for condition with watch events should not return an error")
190-
framework.ExpectEqual(eventFound, true, "RC has not reached ReadyReplicas count of %v", testRcInitialReplicaCount)
192+
if !eventFound {
193+
framework.Failf("RC has not reached ReadyReplicas count of %v", testRcInitialReplicaCount)
194+
}
191195

192196
rcLabelPatchPayload, err := json.Marshal(v1.ReplicationController{
193197
ObjectMeta: metav1.ObjectMeta{
@@ -213,7 +217,9 @@ var _ = SIGDescribe("ReplicationController", func() {
213217
return true, nil
214218
})
215219
framework.ExpectNoError(err, "Wait until condition with watch events should not return an error")
216-
framework.ExpectEqual(eventFound, true, "failed to find RC %v event", watch.Added)
220+
if !eventFound {
221+
framework.Failf("failed to find RC %v event", watch.Added)
222+
}
217223

218224
rcStatusPatchPayload, err := json.Marshal(map[string]interface{}{
219225
"status": map[string]interface{}{
@@ -241,7 +247,9 @@ var _ = SIGDescribe("ReplicationController", func() {
241247
return true, nil
242248
})
243249
framework.ExpectNoError(err, "Wait until condition with watch events should not return an error")
244-
framework.ExpectEqual(eventFound, true, "failed to find RC %v event", watch.Added)
250+
if !eventFound {
251+
framework.Failf("failed to find RC %v event", watch.Added)
252+
}
245253

246254
ginkgo.By("waiting for available Replicas")
247255
_, err = watchUntilWithoutRetry(ctx, retryWatcher, func(watchEvent watch.Event) (bool, error) {
@@ -260,7 +268,9 @@ var _ = SIGDescribe("ReplicationController", func() {
260268
return true, nil
261269
})
262270
framework.ExpectNoError(err, "Failed to find updated ready replica count")
263-
framework.ExpectEqual(eventFound, true, "Failed to find updated ready replica count")
271+
if !eventFound {
272+
framework.Fail("Failed to find updated ready replica count")
273+
}
264274

265275
ginkgo.By("fetching ReplicationController status")
266276
rcStatusUnstructured, err := dc.Resource(rcResource).Namespace(testRcNamespace).Get(ctx, testRcName, metav1.GetOptions{}, "status")
@@ -295,7 +305,9 @@ var _ = SIGDescribe("ReplicationController", func() {
295305
return true, nil
296306
})
297307
framework.ExpectNoError(err, "Wait until condition with watch events should not return an error")
298-
framework.ExpectEqual(eventFound, true, "failed to find RC %v event", watch.Added)
308+
if !eventFound {
309+
framework.Failf("failed to find RC %v event", watch.Added)
310+
}
299311

300312
ginkgo.By("waiting for ReplicationController's scale to be the max amount")
301313
eventFound = false
@@ -316,7 +328,9 @@ var _ = SIGDescribe("ReplicationController", func() {
316328
return true, nil
317329
})
318330
framework.ExpectNoError(err, "Wait until condition with watch events should not return an error")
319-
framework.ExpectEqual(eventFound, true, "Failed to find updated ready replica count")
331+
if !eventFound {
332+
framework.Fail("Failed to find updated ready replica count")
333+
}
320334

321335
// Get the ReplicationController
322336
ginkgo.By("fetching ReplicationController; ensuring that it's patched")
@@ -346,12 +360,16 @@ var _ = SIGDescribe("ReplicationController", func() {
346360
return true, nil
347361
})
348362
framework.ExpectNoError(err, "Wait until condition with watch events should not return an error")
349-
framework.ExpectEqual(eventFound, true, "failed to find RC %v event", watch.Added)
363+
if !eventFound {
364+
framework.Failf("failed to find RC %v event", watch.Added)
365+
}
350366

351367
ginkgo.By("listing all ReplicationControllers")
352368
rcs, err := f.ClientSet.CoreV1().ReplicationControllers("").List(ctx, metav1.ListOptions{LabelSelector: "test-rc-static=true"})
353369
framework.ExpectNoError(err, "failed to list ReplicationController")
354-
framework.ExpectEqual(len(rcs.Items) > 0, true)
370+
if len(rcs.Items) == 0 {
371+
framework.Fail("Expected to find a ReplicationController but none was found")
372+
}
355373

356374
ginkgo.By("checking that ReplicationController has expected values")
357375
foundRc := false
@@ -363,7 +381,10 @@ var _ = SIGDescribe("ReplicationController", func() {
363381
foundRc = true
364382
}
365383
}
366-
framework.ExpectEqual(foundRc, true)
384+
if !foundRc {
385+
framework.Logf("Got unexpected replication controller list %v", rcs.Items)
386+
framework.Failf("could not find ReplicationController %s", testRcName)
387+
}
367388

368389
// Delete ReplicationController
369390
ginkgo.By("deleting ReplicationControllers by collection")
@@ -383,7 +404,9 @@ var _ = SIGDescribe("ReplicationController", func() {
383404
return true, nil
384405
})
385406
framework.ExpectNoError(err, "Wait until condition with watch events should not return an error")
386-
framework.ExpectEqual(eventFound, true, "failed to find RC %v event", watch.Added)
407+
if !eventFound {
408+
framework.Failf("failed to find RC %v event", watch.Added)
409+
}
387410

388411
return actualWatchEvents
389412
}, func() (err error) {

test/e2e/apps/ttl_after_finished.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,17 @@ func testFinishedJob(ctx context.Context, f *framework.Framework) {
9797
framework.ExpectNoError(err)
9898
jobFinishTime := finishTime(job)
9999
finishTimeUTC := jobFinishTime.UTC()
100-
framework.ExpectNotEqual(jobFinishTime.IsZero(), true)
100+
if jobFinishTime.IsZero() {
101+
framework.Fail("Expected job finish time not to be zero.")
102+
}
101103

102104
deleteAtUTC := job.ObjectMeta.DeletionTimestamp.UTC()
103105
framework.ExpectNotEqual(deleteAtUTC, nil)
104106

105107
expireAtUTC := finishTimeUTC.Add(time.Duration(ttl) * time.Second)
106-
framework.ExpectEqual(deleteAtUTC.Before(expireAtUTC), false)
108+
if deleteAtUTC.Before(expireAtUTC) {
109+
framework.Fail("Expected job's deletion time to be after expiration time.")
110+
}
107111
}
108112

109113
// finishTime returns finish time of the specified job.

0 commit comments

Comments
 (0)