Skip to content

Commit

Permalink
update job is succeeded to check completions (#993)
Browse files Browse the repository at this point in the history
* update job is succeeded to check completions

* Update modules/k8s/job.go

Co-authored-by: Yoriyasu Yano <[email protected]>

Co-authored-by: Yoriyasu Yano <[email protected]>
  • Loading branch information
jeffmhastings and yorinasub17 authored Sep 23, 2021
1 parent fb01cf8 commit 6d96ad5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
11 changes: 9 additions & 2 deletions modules/k8s/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/stretchr/testify/require"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/gruntwork-io/terratest/modules/logger"
Expand Down Expand Up @@ -87,7 +88,13 @@ func WaitUntilJobSucceedE(t testing.TestingT, options *KubectlOptions, jobName s
return nil
}

// IsJobSucceeded returns true if all containers in the job are completed & succeeded
// IsJobSucceeded returns true when the job status condition "Complete" is true. This behavior is documented in the kubernetes API reference:
// https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/job-v1/#JobStatus
func IsJobSucceeded(job *batchv1.Job) bool {
return job.Status.Active == 0 && job.Status.Failed == 0
for _, condition := range job.Status.Conditions {
if condition.Type == batchv1.JobComplete && condition.Status == corev1.ConditionTrue {
return true
}
}
return false
}
28 changes: 22 additions & 6 deletions modules/k8s/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/stretchr/testify/require"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/gruntwork-io/terratest/modules/random"
Expand Down Expand Up @@ -83,9 +84,12 @@ func TestIsJobSucceeded(t *testing.T) {
title: "TestIsJobSucceeded",
job: &batchv1.Job{
Status: batchv1.JobStatus{
Succeeded: 1,
Failed: 0,
Active: 0,
Conditions: []batchv1.JobCondition{
batchv1.JobCondition{
Type: batchv1.JobComplete,
Status: corev1.ConditionTrue,
},
},
},
},
expectedResult: true,
Expand All @@ -94,9 +98,21 @@ func TestIsJobSucceeded(t *testing.T) {
title: "TestIsJobFailed",
job: &batchv1.Job{
Status: batchv1.JobStatus{
Failed: 1,
Active: 0,
Succeeded: 1,
Conditions: []batchv1.JobCondition{
batchv1.JobCondition{
Type: batchv1.JobFailed,
Status: corev1.ConditionTrue,
},
},
},
},
expectedResult: false,
},
{
title: "TestIsJobStarting",
job: &batchv1.Job{
Status: batchv1.JobStatus{
Conditions: []batchv1.JobCondition{},
},
},
expectedResult: false,
Expand Down

0 comments on commit 6d96ad5

Please sign in to comment.