@@ -230,80 +230,66 @@ var _ = SIGDescribe("CronJob", func() {
230
230
framework .ExpectNoError (err , "Failed to remove %s cronjob in namespace %s" , cronJob .Name , f .Namespace .Name )
231
231
})
232
232
233
- // cleanup of successful finished jobs, with limit of one successful job
234
- ginkgo .It ("should delete successful finished jobs with limit of one successful job" , func () {
235
- ginkgo .By ("Creating a AllowConcurrent cronjob with custom history limits" )
236
- successLimit := int32 (1 )
237
- cronJob := newTestCronJob ("concurrent-limit" , "*/1 * * * ?" , batchv1beta1 .AllowConcurrent ,
238
- successCommand , & successLimit , nil )
239
- cronJob , err := createCronJob (f .ClientSet , f .Namespace .Name , cronJob )
240
- framework .ExpectNoError (err , "Failed to create allowconcurrent cronjob with custom history limits in namespace %s" , f .Namespace .Name )
241
-
242
- // Job is going to complete instantly: do not check for an active job
243
- // as we are most likely to miss it
244
-
245
- ginkgo .By ("Ensuring a finished job exists" )
246
- err = waitForAnyFinishedJob (f .ClientSet , f .Namespace .Name )
247
- framework .ExpectNoError (err , "Failed to ensure a finished cronjob exists in namespace %s" , f .Namespace .Name )
248
-
249
- ginkgo .By ("Ensuring a finished job exists by listing jobs explicitly" )
250
- jobs , err := f .ClientSet .BatchV1 ().Jobs (f .Namespace .Name ).List (metav1.ListOptions {})
251
- framework .ExpectNoError (err , "Failed to ensure a finished cronjob exists by listing jobs explicitly in namespace %s" , f .Namespace .Name )
252
- _ , finishedJobs := filterActiveJobs (jobs )
253
- framework .ExpectEqual (len (finishedJobs ), 1 )
254
-
255
- // Job should get deleted when the next job finishes the next minute
256
- ginkgo .By ("Ensuring this job and its pods does not exist anymore" )
257
- err = waitForJobToDisappear (f .ClientSet , f .Namespace .Name , finishedJobs [0 ])
258
- framework .ExpectNoError (err , "Failed to ensure that job does not exists anymore in namespace %s" , f .Namespace .Name )
259
- err = waitForJobsPodToDisappear (f .ClientSet , f .Namespace .Name , finishedJobs [0 ])
260
- framework .ExpectNoError (err , "Failed to ensure that pods for job does not exists anymore in namespace %s" , f .Namespace .Name )
261
-
262
- ginkgo .By ("Ensuring there is 1 finished job by listing jobs explicitly" )
263
- jobs , err = f .ClientSet .BatchV1 ().Jobs (f .Namespace .Name ).List (metav1.ListOptions {})
264
- framework .ExpectNoError (err , "Failed to ensure there is one finished job by listing job explicitly in namespace %s" , f .Namespace .Name )
265
- _ , finishedJobs = filterActiveJobs (jobs )
266
- framework .ExpectEqual (len (finishedJobs ), 1 )
267
-
268
- ginkgo .By ("Removing cronjob" )
269
- err = deleteCronJob (f .ClientSet , f .Namespace .Name , cronJob .Name )
270
- framework .ExpectNoError (err , "Failed to remove the %s cronjob in namespace %s" , cronJob .Name , f .Namespace .Name )
271
- })
272
-
273
- // cleanup of failed finished jobs, with limit of one failed job by failedJobsHistoryLimit
274
- ginkgo .It ("should delete failed finished jobs with failedJobsHistoryLimit 1" , func () {
275
- ginkgo .By ("Creating a AllowConcurrent cronjob with custom history limits" )
276
- failedLimit := int32 (1 )
277
- cronJob := newTestCronJob ("concurrent-limit" , "*/1 * * * ?" , batchv1beta1 .AllowConcurrent ,
278
- failureCommand , nil , & failedLimit )
279
- cronJob , err := createCronJob (f .ClientSet , f .Namespace .Name , cronJob )
280
- framework .ExpectNoError (err , "Failed to create allowconcurrent cronjob with custom history limits in namespace %s" , f .Namespace .Name )
281
-
282
- ginkgo .By ("Ensuring a finished job exists" )
283
- err = waitForAnyFinishedJob (f .ClientSet , f .Namespace .Name )
284
- framework .ExpectNoError (err , "Failed to ensure a finished cronjob exists in namespace %s" , f .Namespace .Name )
285
-
286
- ginkgo .By ("Ensuring a finished job exists by listing jobs explicitly" )
287
- jobs , err := f .ClientSet .BatchV1 ().Jobs (f .Namespace .Name ).List (metav1.ListOptions {})
288
- framework .ExpectNoError (err , "Failed to ensure a finished cronjob exists by listing jobs explicitly in namespace %s" , f .Namespace .Name )
289
- _ , finishedJobs := filterActiveJobs (jobs )
290
- gomega .Expect (len (finishedJobs )).To (gomega .Equal (1 ))
291
-
292
- ginkgo .By ("Ensuring this job and its pods does not exist anymore" )
293
- err = waitForJobToDisappear (f .ClientSet , f .Namespace .Name , finishedJobs [0 ])
294
- framework .ExpectNoError (err , "Failed to ensure that job does not exists anymore in namespace %s" , f .Namespace .Name )
295
- err = waitForJobsPodToDisappear (f .ClientSet , f .Namespace .Name , finishedJobs [0 ])
296
- framework .ExpectNoError (err , "Failed to ensure that pods for job does not exists anymore in namespace %s" , f .Namespace .Name )
297
-
298
- ginkgo .By ("Ensuring there is 1 finished job by listing jobs explicitly" )
299
- jobs , err = f .ClientSet .BatchV1 ().Jobs (f .Namespace .Name ).List (metav1.ListOptions {})
300
- framework .ExpectNoError (err , "Failed to ensure there is one finished job by listing job explicitly in namespace %s" , f .Namespace .Name )
301
- _ , finishedJobs = filterActiveJobs (jobs )
302
- gomega .Expect (len (finishedJobs )).To (gomega .Equal (1 ))
233
+ // cleanup of successful/failed finished jobs, with successfulJobsHistoryLimit and failedJobsHistoryLimit
234
+ ginkgo .It ("should delete successful/failed finished jobs with limit of one job" , func () {
235
+
236
+ testCases := []struct {
237
+ description string
238
+ command []string
239
+ successLimit int32
240
+ failedLimit int32
241
+ }{
242
+ {
243
+ description : "successful-jobs-history-limit" ,
244
+ command : successCommand ,
245
+ successLimit : 1 , // keep one successful job
246
+ failedLimit : 0 , // keep none failed job
247
+ },
248
+ {
249
+ description : "failed-jobs-history-limit" ,
250
+ command : failureCommand ,
251
+ successLimit : 0 , // keep none succcessful job
252
+ failedLimit : 1 , // keep one failed job
253
+ },
254
+ }
303
255
304
- ginkgo .By ("Removing cronjob" )
305
- err = deleteCronJob (f .ClientSet , f .Namespace .Name , cronJob .Name )
306
- framework .ExpectNoError (err , "Failed to remove the %s cronjob in namespace %s" , cronJob .Name , f .Namespace .Name )
256
+ for _ , t := range testCases {
257
+ ginkgo .By (fmt .Sprintf ("Creating a AllowConcurrent cronjob with custom %s" , t .description ))
258
+ cronJob := newTestCronJob (t .description , "*/1 * * * ?" , batchv1beta1 .AllowConcurrent ,
259
+ t .command , & t .successLimit , & t .failedLimit )
260
+ cronJob , err := createCronJob (f .ClientSet , f .Namespace .Name , cronJob )
261
+ framework .ExpectNoError (err , "Failed to create allowconcurrent cronjob with custom history limits in namespace %s" , f .Namespace .Name )
262
+
263
+ // Job is going to complete instantly: do not check for an active job
264
+ // as we are most likely to miss it
265
+
266
+ ginkgo .By ("Ensuring a finished job exists" )
267
+ err = waitForAnyFinishedJob (f .ClientSet , f .Namespace .Name )
268
+ framework .ExpectNoError (err , "Failed to ensure a finished cronjob exists in namespace %s" , f .Namespace .Name )
269
+
270
+ ginkgo .By ("Ensuring a finished job exists by listing jobs explicitly" )
271
+ jobs , err := f .ClientSet .BatchV1 ().Jobs (f .Namespace .Name ).List (metav1.ListOptions {})
272
+ framework .ExpectNoError (err , "Failed to ensure a finished cronjob exists by listing jobs explicitly in namespace %s" , f .Namespace .Name )
273
+ _ , finishedJobs := filterActiveJobs (jobs )
274
+ framework .ExpectEqual (len (finishedJobs ), 1 )
275
+
276
+ // Job should get deleted when the next job finishes the next minute
277
+ ginkgo .By ("Ensuring this job and its pods does not exist anymore" )
278
+ err = waitForJobToDisappear (f .ClientSet , f .Namespace .Name , finishedJobs [0 ])
279
+ framework .ExpectNoError (err , "Failed to ensure that job does not exists anymore in namespace %s" , f .Namespace .Name )
280
+ err = waitForJobsPodToDisappear (f .ClientSet , f .Namespace .Name , finishedJobs [0 ])
281
+ framework .ExpectNoError (err , "Failed to ensure that pods for job does not exists anymore in namespace %s" , f .Namespace .Name )
282
+
283
+ ginkgo .By ("Ensuring there is 1 finished job by listing jobs explicitly" )
284
+ jobs , err = f .ClientSet .BatchV1 ().Jobs (f .Namespace .Name ).List (metav1.ListOptions {})
285
+ framework .ExpectNoError (err , "Failed to ensure there is one finished job by listing job explicitly in namespace %s" , f .Namespace .Name )
286
+ _ , finishedJobs = filterActiveJobs (jobs )
287
+ framework .ExpectEqual (len (finishedJobs ), 1 )
288
+
289
+ ginkgo .By ("Removing cronjob" )
290
+ err = deleteCronJob (f .ClientSet , f .Namespace .Name , cronJob .Name )
291
+ framework .ExpectNoError (err , "Failed to remove the %s cronjob in namespace %s" , cronJob .Name , f .Namespace .Name )
292
+ }
307
293
})
308
294
})
309
295
@@ -374,7 +360,8 @@ func getCronJob(c clientset.Interface, ns, name string) (*batchv1beta1.CronJob,
374
360
}
375
361
376
362
func deleteCronJob (c clientset.Interface , ns , name string ) error {
377
- return c .BatchV1beta1 ().CronJobs (ns ).Delete (name , nil )
363
+ propagationPolicy := metav1 .DeletePropagationBackground // Also delete jobs and pods related to cronjob
364
+ return c .BatchV1beta1 ().CronJobs (ns ).Delete (name , & metav1.DeleteOptions {PropagationPolicy : & propagationPolicy })
378
365
}
379
366
380
367
// Wait for at least given amount of active jobs.
0 commit comments