Skip to content

Commit 1c3b6a0

Browse files
committed
added unit test to test that wait for ready timeout executes make right domain step.
1 parent 0616eb6 commit 1c3b6a0

File tree

5 files changed

+90
-19
lines changed

5 files changed

+90
-19
lines changed

operator/src/main/java/oracle/kubernetes/operator/PodWatcher.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,19 +336,20 @@ Step createReadAsyncStep(String name, String namespace, String domainUid, Respon
336336
}
337337

338338
protected DefaultResponseStep<V1Pod> resumeIfReady(Callback callback) {
339-
return new DefaultResponseStep<>(null) {
339+
return new DefaultResponseStep<>(getNext()) {
340340
@Override
341341
public NextAction onSuccess(Packet packet, CallResponse<V1Pod> callResponse) {
342342

343343
DomainPresenceInfo info = packet.getSpi(DomainPresenceInfo.class);
344344
String serverName = (String)packet.get(SERVER_NAME);
345+
String resource = initialResource == null ? resourceName : getMetadata(initialResource).getName();
345346
if ((info != null) && (callResponse != null)) {
346347
Optional.ofNullable(callResponse.getResult()).ifPresent(result ->
347348
info.setServerPodFromEvent(getPodLabel(result), result));
348349
if (onReadNotFoundForCachedResource(getServerPod(info, serverName), isNotFoundOnRead(callResponse))) {
349350
LOGGER.fine(EXECUTE_MAKE_RIGHT_DOMAIN, serverName, callback.getRecheckCount());
350-
return doNext(new CallBuilder().readDomainAsync(info.getDomainUid(),
351-
info.getNamespace(), new MakeRightDomainStep(callback,null)), packet);
351+
removeCallback(resource, callback);
352+
return doNext(NEXT_STEP_FACTORY.createMakeDomainRightStep(callback, info, resource, getNext()), packet);
352353
}
353354
}
354355

@@ -366,9 +367,9 @@ public NextAction onSuccess(Packet packet, CallResponse<V1Pod> callResponse) {
366367
getWatchBackstopRecheckDelaySeconds(), TimeUnit.SECONDS);
367368
} else {
368369
LOGGER.fine(EXECUTE_MAKE_RIGHT_DOMAIN, serverName, callback.getRecheckCount());
370+
removeCallback(resource, callback);
369371
// Watch backstop recheck count is more than configured recheck count, proceed to make-right step.
370-
return doNext(new CallBuilder().readDomainAsync(info.getDomainUid(),
371-
info.getNamespace(), new MakeRightDomainStep(callback, null)), packet);
372+
return doNext(NEXT_STEP_FACTORY.createMakeDomainRightStep(callback, info, resource, getNext()), packet);
372373
}
373374
}
374375

operator/src/main/java/oracle/kubernetes/operator/WaitForReadyStep.java

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import io.kubernetes.client.openapi.models.V1ObjectMeta;
1212
import oracle.kubernetes.operator.calls.CallResponse;
13+
import oracle.kubernetes.operator.helpers.CallBuilder;
1314
import oracle.kubernetes.operator.helpers.DomainPresenceInfo;
1415
import oracle.kubernetes.operator.helpers.ResponseStep;
1516
import oracle.kubernetes.operator.logging.LoggingFacade;
@@ -34,6 +35,15 @@ abstract class WaitForReadyStep<T> extends Step {
3435
private static final int DEFAULT_RECHECK_SECONDS = 5;
3536
private static final int DEFAULT_RECHECK_COUNT = 60;
3637

38+
static NextStepFactory NEXT_STEP_FACTORY =
39+
(callback, info, name, next) -> createMakeDomainRightStep(callback, info, name, next);
40+
41+
protected static Step createMakeDomainRightStep(WaitForReadyStep.Callback callback,
42+
DomainPresenceInfo info, String name, Step next) {
43+
return new CallBuilder().readDomainAsync(info.getDomainUid(),
44+
info.getNamespace(), new MakeRightDomainStep(callback, name, null));
45+
}
46+
3747
static int getWatchBackstopRecheckDelaySeconds() {
3848
return Optional.ofNullable(TuningParameters.getInstance())
3949
.map(TuningParameters::getWatchTuning)
@@ -48,8 +58,8 @@ static int getWatchBackstopRecheckCount() {
4858
.orElse(DEFAULT_RECHECK_COUNT);
4959
}
5060

51-
private final T initialResource;
52-
private final String resourceName;
61+
final T initialResource;
62+
final String resourceName;
5363

5464
/**
5565
* Creates a step which will only proceed once the specified resource is ready.
@@ -236,24 +246,26 @@ public NextAction apply(Packet packet) {
236246

237247
}
238248

239-
class MakeRightDomainStep extends DefaultResponseStep {
240-
private final Callback callback;
249+
static class MakeRightDomainStep extends DefaultResponseStep {
250+
private final WaitForReadyStep.Callback callback;
251+
private final String name;
241252

242-
MakeRightDomainStep(Callback callback, Step next) {
253+
MakeRightDomainStep(WaitForReadyStep.Callback callback, String name, Step next) {
243254
super(next);
244255
this.callback = callback;
256+
this.name = name;
245257
}
246258

247259
@Override
248260
public NextAction onSuccess(Packet packet, CallResponse callResponse) {
249-
String name = initialResource == null ? resourceName : getMetadata(initialResource).getName();
250261
MakeRightDomainOperation makeRightDomainOperation =
251262
(MakeRightDomainOperation)packet.get(MAKE_RIGHT_DOMAIN_OPERATION);
252-
makeRightDomainOperation.clear();
253-
makeRightDomainOperation.setLiveInfo(new DomainPresenceInfo((Domain)callResponse.getResult()));
254-
makeRightDomainOperation.withExplicitRecheck().interrupt().execute();
255-
removeCallback(name, callback);
256-
callback.fiber.terminate(null, packet);
263+
if (makeRightDomainOperation != null) {
264+
makeRightDomainOperation.clear();
265+
makeRightDomainOperation.setLiveInfo(new DomainPresenceInfo((Domain) callResponse.getResult()));
266+
makeRightDomainOperation.withExplicitRecheck().interrupt().execute();
267+
}
268+
callback.fiber.terminate(new Exception("timeout exceeded"), packet);
257269
return super.onSuccess(packet, callResponse);
258270
}
259271
}
@@ -311,4 +323,11 @@ private void handleResourceReady(AsyncFiber fiber, Packet packet, T resource) {
311323
fiber.terminate(createTerminationException(resource), packet);
312324
}
313325
}
326+
327+
// an interface to provide a hook for unit testing.
328+
interface NextStepFactory {
329+
Step createMakeDomainRightStep(WaitForReadyStep.Callback callback,
330+
DomainPresenceInfo info, String name, Step next);
331+
}
332+
314333
}

operator/src/test/java/oracle/kubernetes/operator/PodWatcherTest.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
import static oracle.kubernetes.operator.LabelConstants.CREATEDBYOPERATOR_LABEL;
3535
import static oracle.kubernetes.operator.LabelConstants.DOMAINUID_LABEL;
3636
import static oracle.kubernetes.operator.helpers.LegalNames.DEFAULT_INTROSPECTOR_JOB_NAME_SUFFIX;
37+
import static oracle.kubernetes.operator.logging.MessageKeys.EXECUTE_MAKE_RIGHT_DOMAIN;
3738
import static oracle.kubernetes.operator.logging.MessageKeys.INTROSPECTOR_POD_FAILED;
39+
import static oracle.kubernetes.utils.LogMatcher.containsFine;
3840
import static oracle.kubernetes.utils.LogMatcher.containsInfo;
3941
import static org.hamcrest.Matchers.both;
4042
import static org.hamcrest.Matchers.hasEntry;
@@ -69,14 +71,19 @@ public void setUp() throws Exception {
6971

7072
private String[] getMessageKeys() {
7173
return new String[] {
72-
getPodFailedMessageKey()
74+
getPodFailedMessageKey(),
75+
getMakeRightDomainStepKey()
7376
};
7477
}
7578

7679
private String getPodFailedMessageKey() {
7780
return INTROSPECTOR_POD_FAILED;
7881
}
7982

83+
private String getMakeRightDomainStepKey() {
84+
return EXECUTE_MAKE_RIGHT_DOMAIN;
85+
}
86+
8087
@Override
8188
public void receivedResponse(Watch.Response<V1Pod> response) {
8289
recordCallBack(response);
@@ -245,6 +252,16 @@ public void whenPodCreatedAndReadyLater_runNextStep() {
245252
assertThat(terminalStep.wasRun(), is(true));
246253
}
247254

255+
@Test
256+
public void whenPodCreatedAndNotReadyAfterTimeout_executeMakeRightDomain() {
257+
executeWaitForReady();
258+
259+
testSupport.setTime(10, TimeUnit.SECONDS);
260+
261+
assertThat(terminalStep.wasRun(), is(true));
262+
assertThat(logRecords, containsFine(getMakeRightDomainStepKey()));
263+
}
264+
248265
@Test
249266
public void whenPodNotReadyLater_dontRunNextStep() {
250267
sendPodModifiedWatchAfterWaitForReady(this::dontChangePod);
@@ -318,6 +335,19 @@ private void sendPodModifiedWatchAfterResourceCreatedAndWaitForReady(Function<V1
318335
}
319336
}
320337

338+
// Starts the waitForReady step with an uncreated pod and sends a watch indicating that the pod has changed
339+
private void executeWaitForReady() {
340+
AtomicBoolean stopping = new AtomicBoolean(false);
341+
PodWatcher watcher = createWatcher(stopping);
342+
343+
try {
344+
testSupport.addDomainPresenceInfo(new DomainPresenceInfo(NS, "domain1"));
345+
testSupport.runSteps(watcher.waitForReady(NAME, terminalStep));
346+
} finally {
347+
stopping.set(true);
348+
}
349+
}
350+
321351
// Simulates a pod that is ready but where Kubernetes has failed to send the watch event
322352
@SafeVarargs
323353
private void makeModifiedPodReadyWithNoWatchEvent(Function<V1Pod,V1Pod>... modifiers) {

operator/src/test/java/oracle/kubernetes/operator/WatcherTestBase.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@
99
import java.util.concurrent.atomic.AtomicBoolean;
1010

1111
import com.meterware.simplestub.Memento;
12+
import com.meterware.simplestub.StaticStubSupport;
1213
import io.kubernetes.client.openapi.models.V1ObjectMeta;
1314
import io.kubernetes.client.util.Watch;
1415
import oracle.kubernetes.operator.TuningParameters.WatchTuning;
1516
import oracle.kubernetes.operator.builders.StubWatchFactory;
1617
import oracle.kubernetes.operator.builders.WatchEvent;
18+
import oracle.kubernetes.operator.helpers.DomainPresenceInfo;
19+
import oracle.kubernetes.operator.helpers.TuningParametersStub;
20+
import oracle.kubernetes.operator.work.Step;
1721
import oracle.kubernetes.utils.TestUtils;
1822
import oracle.kubernetes.utils.TestUtils.ConsoleHandlerMemento;
1923
import org.junit.jupiter.api.AfterEach;
@@ -80,8 +84,10 @@ protected ConsoleHandlerMemento configureOperatorLogger() {
8084
return TestUtils.silenceOperatorLogger().ignoringLoggedExceptions(hasNextException);
8185
}
8286

83-
final void addMemento(Memento memento) {
87+
final void addMemento(Memento memento) throws NoSuchFieldException {
8488
mementos.add(memento);
89+
mementos.add(TuningParametersStub.install());
90+
mementos.add(TestStepFactory.install());
8591
}
8692

8793
@AfterEach
@@ -253,4 +259,19 @@ private Watcher<?> createAndRunWatcher(String nameSpace, AtomicBoolean stopping,
253259
}
254260

255261
protected abstract Watcher<?> createWatcher(String ns, AtomicBoolean stopping, BigInteger rv);
262+
263+
static class TestStepFactory implements WaitForReadyStep.NextStepFactory {
264+
265+
private static TestStepFactory factory = new TestStepFactory();
266+
267+
private static Memento install() throws NoSuchFieldException {
268+
return StaticStubSupport.install(WaitForReadyStep.class, "NEXT_STEP_FACTORY", factory);
269+
}
270+
271+
@Override
272+
public Step createMakeDomainRightStep(WaitForReadyStep.Callback callback,
273+
DomainPresenceInfo info, String name, Step next) {
274+
return next;
275+
}
276+
}
256277
}

operator/src/test/java/oracle/kubernetes/operator/helpers/TuningParametersStub.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public CallBuilderTuning getCallBuilderTuning() {
7979

8080
@Override
8181
public WatchTuning getWatchTuning() {
82-
return null;
82+
return new TuningParameters.WatchTuning(30, 0, 5, 1);
8383
}
8484

8585
@Override

0 commit comments

Comments
 (0)