1313import io .kubernetes .client .models .V1JobStatus ;
1414import io .kubernetes .client .models .V1ObjectMeta ;
1515import io .kubernetes .client .util .Watch ;
16+ import java .util .function .Consumer ;
1617import oracle .kubernetes .operator .builders .StubWatchFactory ;
1718import oracle .kubernetes .operator .watcher .WatchListener ;
19+ import oracle .kubernetes .operator .work .FiberTestSupport ;
1820import oracle .kubernetes .operator .work .NextAction ;
1921import oracle .kubernetes .operator .work .Packet ;
2022import oracle .kubernetes .operator .work .Step ;
2123import oracle .kubernetes .weblogic .domain .model .Domain ;
2224import org .hamcrest .Matchers ;
25+ import static org .hamcrest .core .IsNull .nullValue ;
26+ import org .joda .time .DateTime ;
2327import org .junit .Test ;
2428
2529import static oracle .kubernetes .operator .LabelConstants .CREATEDBYOPERATOR_LABEL ;
@@ -38,7 +42,8 @@ public class JobWatcherTest extends WatcherTestBase implements WatchListener<V1J
3842 private static final String NS = "ns1" ;
3943 private static final String VERSION = "123" ;
4044 private Packet packet ;
41- private V1Job job = new V1Job ().metadata (new V1ObjectMeta ().name ("test" ));
45+ private V1Job job = new V1Job ().metadata (new V1ObjectMeta ().name ("test" ).creationTimestamp (new DateTime ()));
46+ private FiberTestSupport fiberTestSupport = new FiberTestSupport ();
4247
4348 public void setUp () throws Exception {
4449 super .setUp ();
@@ -115,6 +120,12 @@ private void makeJobReady(V1Job job) {
115120 job .status (new V1JobStatus ().conditions (conditions ));
116121 }
117122
123+ private void makeJobFailed (V1Job job , String reason ) {
124+ List <V1JobCondition > conditions =
125+ Collections .singletonList (new V1JobCondition ().type ("Failed" ).status ("True" ).reason (reason ));
126+ job .status (new V1JobStatus ().failed (1 ).conditions (conditions ));
127+ }
128+
118129 @ Test
119130 public void whenJobHasNoStatus_reportNotFailed () {
120131 assertThat (JobWatcher .isFailed (job ), is (false ));
@@ -127,6 +138,34 @@ public void whenJobHasFailedCount_reportFailed() {
127138 assertThat (JobWatcher .isFailed (job ), is (true ));
128139 }
129140
141+ @ Test
142+ public void whenJobHasFailedReason_getFailedReasonReturnsIt () {
143+ makeJobFailed (job , "DeadlineExceeded" );
144+
145+ assertThat (JobWatcher .getFailedReason (job ), is ("DeadlineExceeded" ));
146+ }
147+
148+ @ Test
149+ public void whenJobHasNoFailedReason_getFailedReasonReturnsNull () {
150+ makeJobFailed (job , null );
151+
152+ assertThat (JobWatcher .getFailedReason (job ), nullValue ());
153+ }
154+
155+ @ Test
156+ public void whenJobHasNoFailedCondition_getFailedReasonReturnsNull () {
157+ job .status (new V1JobStatus ().addConditionsItem (new V1JobCondition ().type ("Complete" ).status ("True" )));
158+
159+ assertThat (JobWatcher .getFailedReason (job ), nullValue ());
160+ }
161+
162+ @ Test
163+ public void whenJobHasNoJobCondition_getFailedReasonReturnsNull () {
164+ job .status (new V1JobStatus ().conditions (Collections .EMPTY_LIST ));
165+
166+ assertThat (JobWatcher .getFailedReason (job ), nullValue ());
167+ }
168+
130169 @ Test
131170 public void waitForReady_returnsAStep () {
132171 AtomicBoolean stopping = new AtomicBoolean (true );
@@ -152,6 +191,38 @@ public void whenWaitForReadyAppliedToReadyJob_performNextStep() {
152191 assertThat (listeningStep .wasPerformed , is (true ));
153192 }
154193
194+ @ Test
195+ public void whenReceivedDeadlineExceededResponse_doNotPerformNextStep () {
196+ doReceivedResponseTest ((j ) -> makeJobFailed (j , "DeadlineExceeded" ), false );
197+ }
198+
199+ @ Test
200+ public void whenReceivedFailedWithNoReasonResponse_performNextStep () {
201+ doReceivedResponseTest ((j ) -> makeJobFailed (j , null ), true );
202+ }
203+
204+ @ Test
205+ public void whenReceivedCompleteResponse_performNextStep () {
206+ doReceivedResponseTest ((j ) -> makeJobReady (j ), true );
207+ }
208+
209+ private void doReceivedResponseTest (Consumer <V1Job > jobStatusUpdater , final boolean expectedResult ) {
210+ AtomicBoolean stopping = new AtomicBoolean (false );
211+ JobWatcher watcher =
212+ JobWatcher .create (this , "ns" , Integer .toString (INITIAL_RESOURCE_VERSION ), tuning , stopping );
213+
214+ ListeningTerminalStep listeningStep = new ListeningTerminalStep (stopping );
215+ Step step = watcher .waitForReady (job , listeningStep );
216+
217+ // run WaitForReadyStep.apply() and the doSuspend() inside apply() to set up Complete callback
218+ fiberTestSupport .runSteps (step );
219+
220+ jobStatusUpdater .accept (job );
221+
222+ watcher .receivedResponse (new Watch .Response <>("MODIFIED" , job ));
223+ assertThat (listeningStep .wasPerformed , is (expectedResult ));
224+ }
225+
155226 @ Test
156227 public void afterFactoryDefined_createWatcherForDomain () {
157228 AtomicBoolean stopping = new AtomicBoolean (true );
0 commit comments