From 013cd277df01d577bb5a65c51feff97cabb5d339 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 | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/tests/checkup_test.go b/tests/checkup_test.go index 8b9569d2..0a72b01a 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), @@ -86,12 +101,19 @@ var _ = Describe("Checkup execution", func() { configMap, err := client.CoreV1().ConfigMaps(testNamespace).Get(context.Background(), testConfigMapName, metav1.GetOptions{}) Expect(err).NotTo(HaveOccurred()) + By(fmt.Sprintf("examining the configmap data: %+v", prettifyData(configMap.Data))) Expect(configMap.Data).NotTo(BeNil()) Expect(configMap.Data["status.succeeded"]).To(Equal("true"), fmt.Sprintf("should succeed %+v", configMap.Data)) Expect(configMap.Data["status.failureReason"]).To(BeEmpty(), fmt.Sprintf("should be empty %+v", configMap.Data)) }) }) +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 +343,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 }