Skip to content

Commit a8df423

Browse files
committed
Add unit tests for ServerStatusReader
1 parent fe8b69b commit a8df423

File tree

7 files changed

+345
-1354
lines changed

7 files changed

+345
-1354
lines changed

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2017, 2018, Oracle Corporation and/or its affiliates. All rights reserved.
1+
// Copyright 2017, 2019, Oracle Corporation and/or its affiliates. All rights reserved.
22
// Licensed under the Universal Permissive License v 1.0 as shown at
33
// http://oss.oracle.com/licenses/upl.
44

@@ -117,10 +117,12 @@ static boolean isTerminating(V1Pod pod) {
117117
}
118118

119119
static boolean isReady(V1Pod pod) {
120-
return isReady(pod, false);
120+
boolean ready = getReadyStatus(pod);
121+
if (ready) LOGGER.info(MessageKeys.POD_IS_READY, pod.getMetadata().getName());
122+
return ready;
121123
}
122124

123-
static boolean isReady(V1Pod pod, boolean isStatusCheck) {
125+
static boolean getReadyStatus(V1Pod pod) {
124126
V1PodStatus status = pod.getStatus();
125127
if (status != null) {
126128
if ("Running".equals(status.getPhase())) {
@@ -129,10 +131,6 @@ static boolean isReady(V1Pod pod, boolean isStatusCheck) {
129131
for (V1PodCondition cond : conds) {
130132
if ("Ready".equals(cond.getType())) {
131133
if ("True".equals(cond.getStatus())) {
132-
// Pod is Ready!
133-
if (!isStatusCheck) {
134-
LOGGER.info(MessageKeys.POD_IS_READY, pod.getMetadata().getName());
135-
}
136134
return true;
137135
}
138136
}

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

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
// Copyright 2018, Oracle Corporation and/or its affiliates. All rights reserved.
1+
// Copyright 2018,2019 Oracle Corporation and/or its affiliates. All rights reserved.
22
// Licensed under the Universal Permissive License v 1.0 as shown at
33
// http://oss.oracle.com/licenses/upl.
44

55
package oracle.kubernetes.operator;
66

7+
import static oracle.kubernetes.operator.KubernetesConstants.CONTAINER_NAME;
8+
79
import com.google.common.base.Charsets;
810
import com.google.common.io.CharStreams;
911
import io.kubernetes.client.ApiClient;
1012
import io.kubernetes.client.ApiException;
11-
import io.kubernetes.client.Exec;
1213
import io.kubernetes.client.models.V1Pod;
1314
import java.io.IOException;
1415
import java.io.InputStream;
@@ -20,13 +21,17 @@
2021
import java.util.concurrent.ConcurrentHashMap;
2122
import java.util.concurrent.ConcurrentMap;
2223
import java.util.concurrent.TimeUnit;
24+
import java.util.function.Function;
2325
import oracle.kubernetes.operator.helpers.ClientPool;
2426
import oracle.kubernetes.operator.helpers.DomainPresenceInfo;
2527
import oracle.kubernetes.operator.helpers.ServerKubernetesObjects;
2628
import oracle.kubernetes.operator.logging.LoggingFacade;
2729
import oracle.kubernetes.operator.logging.LoggingFactory;
2830
import oracle.kubernetes.operator.logging.MessageKeys;
2931
import oracle.kubernetes.operator.steps.ReadHealthStep;
32+
import oracle.kubernetes.operator.utils.KubernetesExec;
33+
import oracle.kubernetes.operator.utils.KubernetesExecFactory;
34+
import oracle.kubernetes.operator.utils.KubernetesExecFactoryImpl;
3035
import oracle.kubernetes.operator.work.NextAction;
3136
import oracle.kubernetes.operator.work.Packet;
3237
import oracle.kubernetes.operator.work.Step;
@@ -35,10 +40,12 @@
3540
/** Creates an asynchronous step to read the WebLogic server state from a particular pod */
3641
public class ServerStatusReader {
3742
private static final LoggingFacade LOGGER = LoggingFactory.getLogger("Operator", "Operator");
43+
private static KubernetesExecFactory EXEC_FACTORY = new KubernetesExecFactoryImpl();
44+
private static Function<Step, Step> STEP_FACTORY = ReadHealthStep::createReadHealthStep;
3845

3946
private ServerStatusReader() {}
4047

41-
public static Step createDomainStatusReaderStep(
48+
static Step createDomainStatusReaderStep(
4249
DomainPresenceInfo info, long timeoutSeconds, Step next) {
4350
return new DomainStatusReaderStep(info, timeoutSeconds, next);
4451
}
@@ -47,7 +54,7 @@ private static class DomainStatusReaderStep extends Step {
4754
private final DomainPresenceInfo info;
4855
private final long timeoutSeconds;
4956

50-
public DomainStatusReaderStep(DomainPresenceInfo info, long timeoutSeconds, Step next) {
57+
DomainStatusReaderStep(DomainPresenceInfo info, long timeoutSeconds, Step next) {
5158
super(next);
5259
this.info = info;
5360
this.timeoutSeconds = timeoutSeconds;
@@ -65,13 +72,13 @@ public NextAction apply(Packet packet) {
6572
for (Map.Entry<String, ServerKubernetesObjects> entry : info.getServers().entrySet()) {
6673
String serverName = entry.getKey();
6774
ServerKubernetesObjects sko = entry.getValue();
68-
if (sko != null) {
75+
if (sko != null) { // !! Impossible to have a null value in a concurrent map
6976
V1Pod pod = sko.getPod().get();
7077
if (pod != null) {
7178
Packet p = packet.clone();
7279
startDetails.add(
7380
new StepAndPacket(
74-
createServerStatusReaderStep(sko, pod, serverName, timeoutSeconds, null), p));
81+
createServerStatusReaderStep(sko, pod, serverName, timeoutSeconds), p));
7582
}
7683
}
7784
}
@@ -90,13 +97,12 @@ public NextAction apply(Packet packet) {
9097
* @param pod The pod
9198
* @param serverName Server name
9299
* @param timeoutSeconds Timeout in seconds
93-
* @param next Next step
94100
* @return Created step
95101
*/
96-
public static Step createServerStatusReaderStep(
97-
ServerKubernetesObjects sko, V1Pod pod, String serverName, long timeoutSeconds, Step next) {
102+
private static Step createServerStatusReaderStep(
103+
ServerKubernetesObjects sko, V1Pod pod, String serverName, long timeoutSeconds) {
98104
return new ServerStatusReaderStep(
99-
sko, pod, serverName, timeoutSeconds, new ServerHealthStep(serverName, next));
105+
sko, pod, serverName, timeoutSeconds, new ServerHealthStep(serverName, null));
100106
}
101107

102108
private static class ServerStatusReaderStep extends Step {
@@ -105,7 +111,7 @@ private static class ServerStatusReaderStep extends Step {
105111
private final String serverName;
106112
private final long timeoutSeconds;
107113

108-
public ServerStatusReaderStep(
114+
ServerStatusReaderStep(
109115
ServerKubernetesObjects sko, V1Pod pod, String serverName, long timeoutSeconds, Step next) {
110116
super(next);
111117
this.sko = sko;
@@ -120,7 +126,7 @@ public NextAction apply(Packet packet) {
120126
ConcurrentMap<String, String> serverStateMap =
121127
(ConcurrentMap<String, String>) packet.get(ProcessingConstants.SERVER_STATE_MAP);
122128

123-
if (PodWatcher.isReady(pod, true)) {
129+
if (PodWatcher.getReadyStatus(pod)) {
124130
sko.getLastKnownStatus().set(WebLogicConstants.RUNNING_STATE);
125131
serverStateMap.put(serverName, WebLogicConstants.RUNNING_STATE);
126132
return doNext(packet);
@@ -145,14 +151,10 @@ public NextAction apply(Packet packet) {
145151
ClientPool helper = ClientPool.getInstance();
146152
ApiClient client = helper.take();
147153
try {
148-
proc =
149-
new Exec(client)
150-
.exec(
151-
pod,
152-
new String[] {"/weblogic-operator/scripts/readState.sh"},
153-
KubernetesConstants.CONTAINER_NAME,
154-
stdin,
155-
tty);
154+
KubernetesExec kubernetesExec = EXEC_FACTORY.create(client, pod, CONTAINER_NAME);
155+
kubernetesExec.setStdin(stdin);
156+
kubernetesExec.setTty(tty);
157+
proc = kubernetesExec.exec("/weblogic-operator/scripts/readState.sh");
156158

157159
InputStream in = proc.getInputStream();
158160
if (proc.waitFor(timeoutSeconds, TimeUnit.SECONDS)) {
@@ -179,7 +181,7 @@ public NextAction apply(Packet packet) {
179181
private static class ServerHealthStep extends Step {
180182
private final String serverName;
181183

182-
public ServerHealthStep(String serverName, Step next) {
184+
ServerHealthStep(String serverName, Step next) {
183185
super(next);
184186
this.serverName = serverName;
185187
}
@@ -193,7 +195,7 @@ public NextAction apply(Packet packet) {
193195

194196
if (WebLogicConstants.STATES_SUPPORTING_REST.contains(state)) {
195197
packet.put(ProcessingConstants.SERVER_NAME, serverName);
196-
return doNext(ReadHealthStep.createReadHealthStep(getNext()), packet);
198+
return doNext(STEP_FACTORY.apply(getNext()), packet);
197199
}
198200

199201
return doNext(packet);

0 commit comments

Comments
 (0)