mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-28 05:57:25 +00:00
Merge pull request #78245 from k-toyoda-pi/add_test_cronjob_failedJobsHistoryLimit
add e2e test for cronjob failedJobsHistoryLimit
This commit is contained in:
commit
1c4a7dbe7b
@ -51,6 +51,7 @@ var _ = SIGDescribe("CronJob", func() {
|
|||||||
|
|
||||||
// Pod will complete instantly
|
// Pod will complete instantly
|
||||||
successCommand := []string{"/bin/true"}
|
successCommand := []string{"/bin/true"}
|
||||||
|
failureCommand := []string{"/bin/false"}
|
||||||
|
|
||||||
ginkgo.BeforeEach(func() {
|
ginkgo.BeforeEach(func() {
|
||||||
framework.SkipIfMissingResource(f.DynamicClient, CronJobGroupVersionResourceBeta, f.Namespace.Name)
|
framework.SkipIfMissingResource(f.DynamicClient, CronJobGroupVersionResourceBeta, f.Namespace.Name)
|
||||||
@ -60,7 +61,7 @@ var _ = SIGDescribe("CronJob", func() {
|
|||||||
ginkgo.It("should schedule multiple jobs concurrently", func() {
|
ginkgo.It("should schedule multiple jobs concurrently", func() {
|
||||||
ginkgo.By("Creating a cronjob")
|
ginkgo.By("Creating a cronjob")
|
||||||
cronJob := newTestCronJob("concurrent", "*/1 * * * ?", batchv1beta1.AllowConcurrent,
|
cronJob := newTestCronJob("concurrent", "*/1 * * * ?", batchv1beta1.AllowConcurrent,
|
||||||
sleepCommand, nil)
|
sleepCommand, nil, nil)
|
||||||
cronJob, err := createCronJob(f.ClientSet, f.Namespace.Name, cronJob)
|
cronJob, err := createCronJob(f.ClientSet, f.Namespace.Name, cronJob)
|
||||||
framework.ExpectNoError(err, "Failed to create CronJob in namespace %s", f.Namespace.Name)
|
framework.ExpectNoError(err, "Failed to create CronJob in namespace %s", f.Namespace.Name)
|
||||||
|
|
||||||
@ -83,7 +84,7 @@ var _ = SIGDescribe("CronJob", func() {
|
|||||||
ginkgo.It("should not schedule jobs when suspended [Slow]", func() {
|
ginkgo.It("should not schedule jobs when suspended [Slow]", func() {
|
||||||
ginkgo.By("Creating a suspended cronjob")
|
ginkgo.By("Creating a suspended cronjob")
|
||||||
cronJob := newTestCronJob("suspended", "*/1 * * * ?", batchv1beta1.AllowConcurrent,
|
cronJob := newTestCronJob("suspended", "*/1 * * * ?", batchv1beta1.AllowConcurrent,
|
||||||
sleepCommand, nil)
|
sleepCommand, nil, nil)
|
||||||
t := true
|
t := true
|
||||||
cronJob.Spec.Suspend = &t
|
cronJob.Spec.Suspend = &t
|
||||||
cronJob, err := createCronJob(f.ClientSet, f.Namespace.Name, cronJob)
|
cronJob, err := createCronJob(f.ClientSet, f.Namespace.Name, cronJob)
|
||||||
@ -107,7 +108,7 @@ var _ = SIGDescribe("CronJob", func() {
|
|||||||
ginkgo.It("should not schedule new jobs when ForbidConcurrent [Slow]", func() {
|
ginkgo.It("should not schedule new jobs when ForbidConcurrent [Slow]", func() {
|
||||||
ginkgo.By("Creating a ForbidConcurrent cronjob")
|
ginkgo.By("Creating a ForbidConcurrent cronjob")
|
||||||
cronJob := newTestCronJob("forbid", "*/1 * * * ?", batchv1beta1.ForbidConcurrent,
|
cronJob := newTestCronJob("forbid", "*/1 * * * ?", batchv1beta1.ForbidConcurrent,
|
||||||
sleepCommand, nil)
|
sleepCommand, nil, nil)
|
||||||
cronJob, err := createCronJob(f.ClientSet, f.Namespace.Name, cronJob)
|
cronJob, err := createCronJob(f.ClientSet, f.Namespace.Name, cronJob)
|
||||||
framework.ExpectNoError(err, "Failed to create CronJob in namespace %s", f.Namespace.Name)
|
framework.ExpectNoError(err, "Failed to create CronJob in namespace %s", f.Namespace.Name)
|
||||||
|
|
||||||
@ -139,7 +140,7 @@ var _ = SIGDescribe("CronJob", func() {
|
|||||||
ginkgo.It("should replace jobs when ReplaceConcurrent", func() {
|
ginkgo.It("should replace jobs when ReplaceConcurrent", func() {
|
||||||
ginkgo.By("Creating a ReplaceConcurrent cronjob")
|
ginkgo.By("Creating a ReplaceConcurrent cronjob")
|
||||||
cronJob := newTestCronJob("replace", "*/1 * * * ?", batchv1beta1.ReplaceConcurrent,
|
cronJob := newTestCronJob("replace", "*/1 * * * ?", batchv1beta1.ReplaceConcurrent,
|
||||||
sleepCommand, nil)
|
sleepCommand, nil, nil)
|
||||||
cronJob, err := createCronJob(f.ClientSet, f.Namespace.Name, cronJob)
|
cronJob, err := createCronJob(f.ClientSet, f.Namespace.Name, cronJob)
|
||||||
framework.ExpectNoError(err, "Failed to create CronJob in namespace %s", f.Namespace.Name)
|
framework.ExpectNoError(err, "Failed to create CronJob in namespace %s", f.Namespace.Name)
|
||||||
|
|
||||||
@ -171,7 +172,7 @@ var _ = SIGDescribe("CronJob", func() {
|
|||||||
ginkgo.It("should not emit unexpected warnings", func() {
|
ginkgo.It("should not emit unexpected warnings", func() {
|
||||||
ginkgo.By("Creating a cronjob")
|
ginkgo.By("Creating a cronjob")
|
||||||
cronJob := newTestCronJob("concurrent", "*/1 * * * ?", batchv1beta1.AllowConcurrent,
|
cronJob := newTestCronJob("concurrent", "*/1 * * * ?", batchv1beta1.AllowConcurrent,
|
||||||
nil, nil)
|
nil, nil, nil)
|
||||||
cronJob, err := createCronJob(f.ClientSet, f.Namespace.Name, cronJob)
|
cronJob, err := createCronJob(f.ClientSet, f.Namespace.Name, cronJob)
|
||||||
framework.ExpectNoError(err, "Failed to create CronJob in namespace %s", f.Namespace.Name)
|
framework.ExpectNoError(err, "Failed to create CronJob in namespace %s", f.Namespace.Name)
|
||||||
|
|
||||||
@ -194,7 +195,7 @@ var _ = SIGDescribe("CronJob", func() {
|
|||||||
ginkgo.It("should remove from active list jobs that have been deleted", func() {
|
ginkgo.It("should remove from active list jobs that have been deleted", func() {
|
||||||
ginkgo.By("Creating a ForbidConcurrent cronjob")
|
ginkgo.By("Creating a ForbidConcurrent cronjob")
|
||||||
cronJob := newTestCronJob("forbid", "*/1 * * * ?", batchv1beta1.ForbidConcurrent,
|
cronJob := newTestCronJob("forbid", "*/1 * * * ?", batchv1beta1.ForbidConcurrent,
|
||||||
sleepCommand, nil)
|
sleepCommand, nil, nil)
|
||||||
cronJob, err := createCronJob(f.ClientSet, f.Namespace.Name, cronJob)
|
cronJob, err := createCronJob(f.ClientSet, f.Namespace.Name, cronJob)
|
||||||
framework.ExpectNoError(err, "Failed to create CronJob in namespace %s", f.Namespace.Name)
|
framework.ExpectNoError(err, "Failed to create CronJob in namespace %s", f.Namespace.Name)
|
||||||
|
|
||||||
@ -229,12 +230,33 @@ var _ = SIGDescribe("CronJob", func() {
|
|||||||
framework.ExpectNoError(err, "Failed to remove %s cronjob in namespace %s", cronJob.Name, f.Namespace.Name)
|
framework.ExpectNoError(err, "Failed to remove %s cronjob in namespace %s", cronJob.Name, f.Namespace.Name)
|
||||||
})
|
})
|
||||||
|
|
||||||
// cleanup of successful finished jobs, with limit of one successful job
|
// cleanup of successful/failed finished jobs, with successfulJobsHistoryLimit and failedJobsHistoryLimit
|
||||||
ginkgo.It("should delete successful finished jobs with limit of one successful job", func() {
|
ginkgo.It("should delete successful/failed finished jobs with limit of one job", func() {
|
||||||
ginkgo.By("Creating a AllowConcurrent cronjob with custom history limits")
|
|
||||||
successLimit := int32(1)
|
testCases := []struct {
|
||||||
cronJob := newTestCronJob("concurrent-limit", "*/1 * * * ?", batchv1beta1.AllowConcurrent,
|
description string
|
||||||
successCommand, &successLimit)
|
command []string
|
||||||
|
successLimit int32
|
||||||
|
failedLimit int32
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
description: "successful-jobs-history-limit",
|
||||||
|
command: successCommand,
|
||||||
|
successLimit: 1, // keep one successful job
|
||||||
|
failedLimit: 0, // keep none failed job
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "failed-jobs-history-limit",
|
||||||
|
command: failureCommand,
|
||||||
|
successLimit: 0, // keep none succcessful job
|
||||||
|
failedLimit: 1, // keep one failed job
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, t := range testCases {
|
||||||
|
ginkgo.By(fmt.Sprintf("Creating a AllowConcurrent cronjob with custom %s", t.description))
|
||||||
|
cronJob := newTestCronJob(t.description, "*/1 * * * ?", batchv1beta1.AllowConcurrent,
|
||||||
|
t.command, &t.successLimit, &t.failedLimit)
|
||||||
cronJob, err := createCronJob(f.ClientSet, f.Namespace.Name, cronJob)
|
cronJob, err := createCronJob(f.ClientSet, f.Namespace.Name, cronJob)
|
||||||
framework.ExpectNoError(err, "Failed to create allowconcurrent cronjob with custom history limits in namespace %s", f.Namespace.Name)
|
framework.ExpectNoError(err, "Failed to create allowconcurrent cronjob with custom history limits in namespace %s", f.Namespace.Name)
|
||||||
|
|
||||||
@ -267,14 +289,16 @@ var _ = SIGDescribe("CronJob", func() {
|
|||||||
ginkgo.By("Removing cronjob")
|
ginkgo.By("Removing cronjob")
|
||||||
err = deleteCronJob(f.ClientSet, f.Namespace.Name, cronJob.Name)
|
err = deleteCronJob(f.ClientSet, f.Namespace.Name, cronJob.Name)
|
||||||
framework.ExpectNoError(err, "Failed to remove the %s cronjob in namespace %s", cronJob.Name, f.Namespace.Name)
|
framework.ExpectNoError(err, "Failed to remove the %s cronjob in namespace %s", cronJob.Name, f.Namespace.Name)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
// newTestCronJob returns a cronjob which does one of several testing behaviors.
|
// newTestCronJob returns a cronjob which does one of several testing behaviors.
|
||||||
func newTestCronJob(name, schedule string, concurrencyPolicy batchv1beta1.ConcurrencyPolicy,
|
func newTestCronJob(name, schedule string, concurrencyPolicy batchv1beta1.ConcurrencyPolicy,
|
||||||
command []string, successfulJobsHistoryLimit *int32) *batchv1beta1.CronJob {
|
command []string, successfulJobsHistoryLimit *int32, failedJobsHistoryLimit *int32) *batchv1beta1.CronJob {
|
||||||
parallelism := int32(1)
|
parallelism := int32(1)
|
||||||
completions := int32(1)
|
completions := int32(1)
|
||||||
|
backofflimit := int32(1)
|
||||||
sj := &batchv1beta1.CronJob{
|
sj := &batchv1beta1.CronJob{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: name,
|
Name: name,
|
||||||
@ -289,6 +313,7 @@ func newTestCronJob(name, schedule string, concurrencyPolicy batchv1beta1.Concur
|
|||||||
Spec: batchv1.JobSpec{
|
Spec: batchv1.JobSpec{
|
||||||
Parallelism: ¶llelism,
|
Parallelism: ¶llelism,
|
||||||
Completions: &completions,
|
Completions: &completions,
|
||||||
|
BackoffLimit: &backofflimit,
|
||||||
Template: v1.PodTemplateSpec{
|
Template: v1.PodTemplateSpec{
|
||||||
Spec: v1.PodSpec{
|
Spec: v1.PodSpec{
|
||||||
RestartPolicy: v1.RestartPolicyOnFailure,
|
RestartPolicy: v1.RestartPolicyOnFailure,
|
||||||
@ -319,6 +344,7 @@ func newTestCronJob(name, schedule string, concurrencyPolicy batchv1beta1.Concur
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
sj.Spec.SuccessfulJobsHistoryLimit = successfulJobsHistoryLimit
|
sj.Spec.SuccessfulJobsHistoryLimit = successfulJobsHistoryLimit
|
||||||
|
sj.Spec.FailedJobsHistoryLimit = failedJobsHistoryLimit
|
||||||
if command != nil {
|
if command != nil {
|
||||||
sj.Spec.JobTemplate.Spec.Template.Spec.Containers[0].Command = command
|
sj.Spec.JobTemplate.Spec.Template.Spec.Containers[0].Command = command
|
||||||
}
|
}
|
||||||
@ -334,7 +360,8 @@ func getCronJob(c clientset.Interface, ns, name string) (*batchv1beta1.CronJob,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func deleteCronJob(c clientset.Interface, ns, name string) error {
|
func deleteCronJob(c clientset.Interface, ns, name string) error {
|
||||||
return c.BatchV1beta1().CronJobs(ns).Delete(name, nil)
|
propagationPolicy := metav1.DeletePropagationBackground // Also delete jobs and pods related to cronjob
|
||||||
|
return c.BatchV1beta1().CronJobs(ns).Delete(name, &metav1.DeleteOptions{PropagationPolicy: &propagationPolicy})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for at least given amount of active jobs.
|
// Wait for at least given amount of active jobs.
|
||||||
|
Loading…
Reference in New Issue
Block a user