k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/controller/job/util/utils_test.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package util 18 19 import ( 20 "testing" 21 22 batch "k8s.io/api/batch/v1" 23 v1 "k8s.io/api/core/v1" 24 ) 25 26 func TestFinishedCondition(t *testing.T) { 27 tests := map[string]struct { 28 conditions []batch.JobCondition 29 wantJobFinished bool 30 wantConditionType batch.JobConditionType 31 }{ 32 "Job doesn't have any conditions": { 33 wantJobFinished: false, 34 wantConditionType: "", 35 }, 36 "Job is completed and condition is true": { 37 conditions: []batch.JobCondition{ 38 { 39 Type: batch.JobComplete, 40 Status: v1.ConditionTrue, 41 }, 42 }, 43 wantJobFinished: true, 44 wantConditionType: batch.JobComplete, 45 }, 46 "Job is completed and condition is false": { 47 conditions: []batch.JobCondition{ 48 { 49 Type: batch.JobComplete, 50 Status: v1.ConditionFalse, 51 }, 52 }, 53 wantJobFinished: false, 54 wantConditionType: "", 55 }, 56 "Job is completed and condition is unknown": { 57 conditions: []batch.JobCondition{ 58 { 59 Type: batch.JobComplete, 60 Status: v1.ConditionUnknown, 61 }, 62 }, 63 wantJobFinished: false, 64 wantConditionType: "", 65 }, 66 "Job has multiple conditions, one of them being complete and condition true": { 67 conditions: []batch.JobCondition{ 68 { 69 Type: batch.JobSuspended, 70 Status: v1.ConditionFalse, 71 }, 72 { 73 Type: batch.JobComplete, 74 Status: v1.ConditionTrue, 75 }, 76 { 77 Type: batch.JobFailed, 78 Status: v1.ConditionFalse, 79 }, 80 }, 81 wantJobFinished: true, 82 wantConditionType: batch.JobComplete, 83 }, 84 "Job is failed and condition is true": { 85 conditions: []batch.JobCondition{ 86 { 87 Type: batch.JobFailed, 88 Status: v1.ConditionTrue, 89 }, 90 }, 91 wantJobFinished: true, 92 wantConditionType: batch.JobFailed, 93 }, 94 "Job is failed and condition is false": { 95 conditions: []batch.JobCondition{ 96 { 97 Type: batch.JobFailed, 98 Status: v1.ConditionFalse, 99 }, 100 }, 101 wantJobFinished: false, 102 wantConditionType: "", 103 }, 104 "Job is failed and condition is unknown": { 105 conditions: []batch.JobCondition{ 106 { 107 Type: batch.JobFailed, 108 Status: v1.ConditionUnknown, 109 }, 110 }, 111 wantJobFinished: false, 112 wantConditionType: "", 113 }, 114 "Job has multiple conditions, none of them has condition true": { 115 conditions: []batch.JobCondition{ 116 { 117 Type: batch.JobSuspended, 118 Status: v1.ConditionFalse, 119 }, 120 { 121 Type: batch.JobComplete, 122 Status: v1.ConditionFalse, 123 }, 124 { 125 Type: batch.JobFailed, 126 Status: v1.ConditionFalse, 127 }, 128 }, 129 wantJobFinished: false, 130 wantConditionType: "", 131 }, 132 } 133 134 for name, test := range tests { 135 t.Run(name, func(t *testing.T) { 136 job := &batch.Job{ 137 Status: batch.JobStatus{ 138 Conditions: test.conditions, 139 }, 140 } 141 142 isJobFinished, conditionType := FinishedCondition(job) 143 if isJobFinished != test.wantJobFinished { 144 if test.wantJobFinished { 145 t.Error("Expected the job to be finished") 146 } else { 147 t.Error("Expected the job to be unfinished") 148 } 149 } 150 151 if conditionType != test.wantConditionType { 152 t.Errorf("Unexpected job condition type. got: '%v', want: '%v'", conditionType, test.wantConditionType) 153 } 154 }) 155 } 156 } 157 158 func TestIsJobSucceeded(t *testing.T) { 159 tests := map[string]struct { 160 job batch.Job 161 wantResult bool 162 }{ 163 "job doesn't have any conditions": { 164 wantResult: false, 165 }, 166 "job has Complete=True condition": { 167 job: batch.Job{ 168 Status: batch.JobStatus{ 169 Conditions: []batch.JobCondition{ 170 { 171 Type: batch.JobSuspended, 172 Status: v1.ConditionFalse, 173 }, 174 { 175 Type: batch.JobComplete, 176 Status: v1.ConditionTrue, 177 }, 178 }, 179 }, 180 }, 181 wantResult: true, 182 }, 183 "job has Complete=False condition": { 184 job: batch.Job{ 185 Status: batch.JobStatus{ 186 Conditions: []batch.JobCondition{ 187 { 188 Type: batch.JobFailed, 189 Status: v1.ConditionTrue, 190 }, 191 { 192 Type: batch.JobComplete, 193 Status: v1.ConditionFalse, 194 }, 195 }, 196 }, 197 }, 198 wantResult: false, 199 }, 200 } 201 for name, tc := range tests { 202 t.Run(name, func(t *testing.T) { 203 gotResult := IsJobSucceeded(&tc.job) 204 if tc.wantResult != gotResult { 205 t.Errorf("unexpected result, want=%v, got=%v", tc.wantResult, gotResult) 206 } 207 }) 208 } 209 }