From 464d8f498870da50e10faa5ad346f8db2919b642 Mon Sep 17 00:00:00 2001 From: Ram Lavi Date: Sun, 3 Dec 2023 14:28:10 +0200 Subject: [PATCH] e2e: Fail faster if checkup has failed Currently the e2e test waits for a JobComplete status, so if the checkup job fails then the test hangs until the timeout. Check for JobFailed condition and fail with an informative error fail. Signed-off-by: Ram Lavi --- tests/checkup_test.go | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/tests/checkup_test.go b/tests/checkup_test.go index 8b9569d2..b3105eac 100644 --- a/tests/checkup_test.go +++ b/tests/checkup_test.go @@ -21,6 +21,7 @@ package tests import ( "context" + "encoding/json" "fmt" "time" @@ -77,7 +78,21 @@ var _ = Describe("Checkup execution", func() { }) It("should complete successfully", func() { - Eventually(getJobConditions, 15*time.Minute, 5*time.Second).Should( + Eventually(func() []batchv1.JobCondition { + jobConditions, err := getJobConditions() + Expect(err).NotTo(HaveOccurred()) + + for _, jobCondition := range jobConditions { + if jobCondition.Type == batchv1.JobFailed && jobCondition.Status == corev1.ConditionTrue { + configMap, err := client.CoreV1().ConfigMaps(testNamespace).Get(context.Background(), testConfigMapName, metav1.GetOptions{}) + Expect(err).NotTo(HaveOccurred()) + + Fail(fmt.Sprintf("checkup failed: %+v", prettifyData(configMap.Data))) + } + } + + return jobConditions + }, 15*time.Minute, 5*time.Second).Should( ContainElement(MatchFields(IgnoreExtras, Fields{ "Type": Equal(batchv1.JobComplete), "Status": Equal(corev1.ConditionTrue), @@ -92,6 +107,12 @@ var _ = Describe("Checkup execution", func() { }) }) +func prettifyData(data map[string]string) string { + dataPrettyJSON, err := json.MarshalIndent(data, "", "\t") + Expect(err).NotTo(HaveOccurred()) + return string(dataPrettyJSON) +} + func setupCheckupPermissions() { var ( err error @@ -321,11 +342,11 @@ func pointer[T any](v T) *T { return &v } -func getJobConditions() []batchv1.JobCondition { +func getJobConditions() ([]batchv1.JobCondition, error) { checkupJob, err := client.BatchV1().Jobs(testNamespace).Get(context.Background(), testCheckupJobName, metav1.GetOptions{}) if err != nil { - return []batchv1.JobCondition{} + return nil, err } - return checkupJob.Status.Conditions + return checkupJob.Status.Conditions, nil }