make these tests table-driven

This commit is contained in:
zhangxiaoyu-zidif 2017-08-31 14:08:46 +08:00
parent a0eb194d81
commit 056d586840

View File

@ -24,43 +24,59 @@ import (
) )
func TestIsJobFinished(t *testing.T) { func TestIsJobFinished(t *testing.T) {
testCases := map[string]struct {
conditionType batch.JobConditionType
conditionStatus v1.ConditionStatus
expectJobNotFinished bool
}{
"Job is completed and condition is true": {
batch.JobComplete,
v1.ConditionTrue,
false,
},
"Job is completed and condition is false": {
batch.JobComplete,
v1.ConditionFalse,
true,
},
"Job is completed and condition is unknown": {
batch.JobComplete,
v1.ConditionUnknown,
true,
},
"Job is failed and condition is true": {
batch.JobFailed,
v1.ConditionTrue,
false,
},
"Job is failed and condition is false": {
batch.JobFailed,
v1.ConditionFalse,
true,
},
"Job is failed and condition is unknown": {
batch.JobFailed,
v1.ConditionUnknown,
true,
},
}
for name, tc := range testCases {
job := &batch.Job{ job := &batch.Job{
Status: batch.JobStatus{ Status: batch.JobStatus{
Conditions: []batch.JobCondition{{ Conditions: []batch.JobCondition{{
Type: batch.JobComplete, Type: tc.conditionType,
Status: v1.ConditionTrue, Status: tc.conditionStatus,
}}, }},
}, },
} }
if !IsJobFinished(job) { if tc.expectJobNotFinished == IsJobFinished(job) {
t.Error("Job was expected to be finished") if tc.expectJobNotFinished {
t.Errorf("test name: %s, job was not expected to be finished", name)
} else {
t.Errorf("test name: %s, job was expected to be finished", name)
} }
job.Status.Conditions[0].Status = v1.ConditionFalse
if IsJobFinished(job) {
t.Error("Job was not expected to be finished")
} }
job.Status.Conditions[0].Status = v1.ConditionUnknown
if IsJobFinished(job) {
t.Error("Job was not expected to be finished")
}
job.Status.Conditions[0].Type = batch.JobFailed
job.Status.Conditions[0].Status = v1.ConditionTrue
if !IsJobFinished(job) {
t.Error("Job was expected to be finished")
}
job.Status.Conditions[0].Status = v1.ConditionFalse
if IsJobFinished(job) {
t.Error("Job was not expected to be finished")
}
job.Status.Conditions[0].Status = v1.ConditionUnknown
if IsJobFinished(job) {
t.Error("Job was not expected to be finished")
} }
} }