From f11ddad99de3389aa47f2d90b615edeadf1314b5 Mon Sep 17 00:00:00 2001 From: Maciej Szulik Date: Tue, 24 Sep 2024 13:10:05 +0200 Subject: [PATCH] e2e: add test covering cronjob-scheduled-timestamp annotation added by cronjob --- test/e2e/apps/cronjob.go | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/test/e2e/apps/cronjob.go b/test/e2e/apps/cronjob.go index c2432971602..70c0f8f89a9 100644 --- a/test/e2e/apps/cronjob.go +++ b/test/e2e/apps/cronjob.go @@ -80,7 +80,7 @@ var _ = SIGDescribe("CronJob", func() { ginkgo.By("Ensuring at least two running jobs exists by listing jobs explicitly") jobs, err := f.ClientSet.BatchV1().Jobs(f.Namespace.Name).List(ctx, metav1.ListOptions{}) - framework.ExpectNoError(err, "Failed to list the CronJobs in namespace %s", f.Namespace.Name) + framework.ExpectNoError(err, "Failed to list jobs in namespace %s", f.Namespace.Name) activeJobs, _ := filterActiveJobs(jobs) gomega.Expect(len(activeJobs)).To(gomega.BeNumerically(">=", 2)) @@ -109,7 +109,7 @@ var _ = SIGDescribe("CronJob", func() { ginkgo.By("Ensuring no job exists by listing jobs explicitly") jobs, err := f.ClientSet.BatchV1().Jobs(f.Namespace.Name).List(ctx, metav1.ListOptions{}) - framework.ExpectNoError(err, "Failed to list the CronJobs in namespace %s", f.Namespace.Name) + framework.ExpectNoError(err, "Failed to list jobs in namespace %s", f.Namespace.Name) gomega.Expect(jobs.Items).To(gomega.BeEmpty()) ginkgo.By("Removing cronjob") @@ -140,7 +140,7 @@ var _ = SIGDescribe("CronJob", func() { ginkgo.By("Ensuring exactly one running job exists by listing jobs explicitly") jobs, err := f.ClientSet.BatchV1().Jobs(f.Namespace.Name).List(ctx, metav1.ListOptions{}) - framework.ExpectNoError(err, "Failed to list the CronJobs in namespace %s", f.Namespace.Name) + framework.ExpectNoError(err, "Failed to list jobs in namespace %s", f.Namespace.Name) activeJobs, _ := filterActiveJobs(jobs) gomega.Expect(activeJobs).To(gomega.HaveLen(1)) @@ -213,7 +213,7 @@ var _ = SIGDescribe("CronJob", func() { ginkgo.By("Ensuring at least one running jobs exists by listing jobs explicitly") jobs, err := f.ClientSet.BatchV1().Jobs(f.Namespace.Name).List(ctx, metav1.ListOptions{}) - framework.ExpectNoError(err, "Failed to list the CronJobs in namespace %s", f.Namespace.Name) + framework.ExpectNoError(err, "Failed to list jobs in namespace %s", f.Namespace.Name) activeJobs, _ := filterActiveJobs(jobs) gomega.Expect(activeJobs).ToNot(gomega.BeEmpty()) @@ -261,6 +261,31 @@ var _ = SIGDescribe("CronJob", func() { framework.ExpectNoError(err, "Failed to delete CronJob %s in namespace %s", cronJob.Name, f.Namespace.Name) }) + ginkgo.It("should set the cronjob-scheduled-timestamp annotation on a job", func(ctx context.Context) { + ginkgo.By("Creating a cronjob") + cronJob := newTestCronJob("concurrent", "*/1 * * * ?", batchv1.AllowConcurrent, + nil, nil, nil) + cronJob, err := createCronJob(ctx, f.ClientSet, f.Namespace.Name, cronJob) + framework.ExpectNoError(err, "Failed to create CronJob in namespace %s", f.Namespace.Name) + + ginkgo.By("Ensuring CronJobsScheduledAnnotation annotation exists on the newely created job") + err = waitForJobsAtLeast(ctx, f.ClientSet, f.Namespace.Name, 1) + framework.ExpectNoError(err, "Failed to ensure a job exists in namespace %s", f.Namespace.Name) + + jobs, err := f.ClientSet.BatchV1().Jobs(f.Namespace.Name).List(ctx, metav1.ListOptions{}) + framework.ExpectNoError(err, "Failed to list jobs in namespace %s", f.Namespace.Name) + gomega.Expect(jobs.Items[0].Annotations).Should(gomega.HaveKey(batchv1.CronJobScheduledTimestampAnnotation), + "missing cronjob-scheduled-timestamp annotation") + ginkgo.By("Ensuring CronJobsScheduledAnnotation annotation parses to RFC3339") + timeAnnotation := jobs.Items[0].Annotations[batchv1.CronJobScheduledTimestampAnnotation] + _, err = time.Parse(time.RFC3339, timeAnnotation) + framework.ExpectNoError(err, "Failed to parse cronjob-scheduled-timestamp annotation: %q", timeAnnotation) + + ginkgo.By("Removing cronjob") + err = deleteCronJob(ctx, f.ClientSet, f.Namespace.Name, cronJob.Name) + framework.ExpectNoError(err, "Failed to delete CronJob %s in namespace %s", cronJob.Name, f.Namespace.Name) + }) + // deleted jobs should be removed from the active list ginkgo.It("should remove from active list jobs that have been deleted", func(ctx context.Context) { ginkgo.By("Creating a ForbidConcurrent cronjob")