13
13
import io .kubernetes .client .models .V1JobStatus ;
14
14
import io .kubernetes .client .models .V1ObjectMeta ;
15
15
import io .kubernetes .client .util .Watch ;
16
+ import java .util .function .Consumer ;
16
17
import oracle .kubernetes .operator .builders .StubWatchFactory ;
17
18
import oracle .kubernetes .operator .watcher .WatchListener ;
19
+ import oracle .kubernetes .operator .work .FiberTestSupport ;
18
20
import oracle .kubernetes .operator .work .NextAction ;
19
21
import oracle .kubernetes .operator .work .Packet ;
20
22
import oracle .kubernetes .operator .work .Step ;
21
23
import oracle .kubernetes .weblogic .domain .model .Domain ;
22
24
import org .hamcrest .Matchers ;
25
+ import static org .hamcrest .core .IsNull .nullValue ;
26
+ import org .joda .time .DateTime ;
23
27
import org .junit .Test ;
24
28
25
29
import static oracle .kubernetes .operator .LabelConstants .CREATEDBYOPERATOR_LABEL ;
@@ -38,7 +42,8 @@ public class JobWatcherTest extends WatcherTestBase implements WatchListener<V1J
38
42
private static final String NS = "ns1" ;
39
43
private static final String VERSION = "123" ;
40
44
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 ();
42
47
43
48
public void setUp () throws Exception {
44
49
super .setUp ();
@@ -115,6 +120,12 @@ private void makeJobReady(V1Job job) {
115
120
job .status (new V1JobStatus ().conditions (conditions ));
116
121
}
117
122
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
+
118
129
@ Test
119
130
public void whenJobHasNoStatus_reportNotFailed () {
120
131
assertThat (JobWatcher .isFailed (job ), is (false ));
@@ -127,6 +138,34 @@ public void whenJobHasFailedCount_reportFailed() {
127
138
assertThat (JobWatcher .isFailed (job ), is (true ));
128
139
}
129
140
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
+
130
169
@ Test
131
170
public void waitForReady_returnsAStep () {
132
171
AtomicBoolean stopping = new AtomicBoolean (true );
@@ -152,6 +191,38 @@ public void whenWaitForReadyAppliedToReadyJob_performNextStep() {
152
191
assertThat (listeningStep .wasPerformed , is (true ));
153
192
}
154
193
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
+
155
226
@ Test
156
227
public void afterFactoryDefined_createWatcherForDomain () {
157
228
AtomicBoolean stopping = new AtomicBoolean (true );
0 commit comments