Skip to content

Commit 80f6f34

Browse files
committed
Unit tests
1 parent e3ae206 commit 80f6f34

File tree

3 files changed

+60
-7
lines changed

3 files changed

+60
-7
lines changed

operator/src/main/java/oracle/kubernetes/operator/helpers/PodStepContext.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,9 +412,8 @@ final void updateForDeepSubstitution(V1Pod pod) {
412412
getContainer(pod)
413413
.ifPresent(
414414
c -> {
415-
doDeepSubstitution(deepSubVars(c.getEnv()), pod.getSpec());
416-
}
417-
);
415+
doDeepSubstitution(deepSubVars(c.getEnv()), pod);
416+
});
418417
}
419418

420419
final Map<String, String> deepSubVars(List<V1EnvVar> envVars) {

operator/src/main/java/oracle/kubernetes/operator/helpers/StepContextBase.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
import java.lang.reflect.Method;
99
import java.util.ArrayList;
1010
import java.util.HashMap;
11+
import java.util.Iterator;
1112
import java.util.List;
1213
import java.util.ListIterator;
1314
import java.util.Map;
15+
import java.util.Set;
1416

1517
import io.kubernetes.client.models.V1EnvVar;
1618
import io.kubernetes.client.models.V1Pod;
@@ -100,6 +102,11 @@ protected void doDeepSubstitution(final Map<String, String> substitutionVariable
100102
for (Pair<Method, Method> item : stringBeans) {
101103
item.getRight().invoke(obj, translate(substitutionVariables, (String) item.getLeft().invoke(obj)));
102104
}
105+
106+
List<Pair<Method, Method>> mapBeans = mapBeans(cls);
107+
for (Pair<Method, Method> item : mapBeans) {
108+
item.getRight().invoke(obj, translate(substitutionVariables, (Map) item.getLeft().invoke(obj)));
109+
}
103110
}
104111
} catch (IllegalAccessException | InvocationTargetException e) {
105112
LOGGER.severe(MessageKeys.EXCEPTION, e);
@@ -136,15 +143,23 @@ && isModelOrListClass(m.getReturnType())
136143
}
137144

138145
private List<Pair<Method, Method>> stringBeans(Class cls) {
146+
return typeBeans(cls, String.class);
147+
}
148+
149+
private List<Pair<Method, Method>> mapBeans(Class cls) {
150+
return typeBeans(cls, Map.class);
151+
}
152+
153+
private List<Pair<Method, Method>> typeBeans(Class cls, Class type) {
139154
List<Pair<Method, Method>> results = new ArrayList<>();
140155
Method[] methods = cls.getMethods();
141156
if (methods != null) {
142157
for (Method m : methods) {
143158
if (m.getName().startsWith("get")
144-
&& m.getReturnType().equals(String.class)
159+
&& m.getReturnType().equals(type)
145160
&& m.getParameterCount() == 0) {
146161
try {
147-
Method set = cls.getMethod("set" + m.getName().substring(3), String.class);
162+
Method set = cls.getMethod("set" + m.getName().substring(3), type);
148163
if (set != null) {
149164
results.add(new Pair<>(m, set));
150165
}
@@ -167,6 +182,26 @@ private String translate(final Map<String, String> substitutionVariables, String
167182
return result;
168183
}
169184

185+
private Map<String, Object> translate(final Map<String, String> substitutionVariables, Map<String, Object> rawValue) {
186+
if (rawValue == null)
187+
return null;
188+
189+
Map<String, Object> trans = new HashMap<>();
190+
for (Map.Entry<String, ?> entry : rawValue.entrySet()) {
191+
Object value = entry.getValue();
192+
if (value instanceof String) {
193+
value = translate(substitutionVariables, (String) value);
194+
} else if (value instanceof Map) {
195+
value = translate(substitutionVariables, (Map) value);
196+
} else {
197+
doDeepSubstitution(substitutionVariables, value);
198+
}
199+
trans.put(translate(substitutionVariables, entry.getKey()), value);
200+
}
201+
202+
return trans;
203+
}
204+
170205
protected void addEnvVar(List<V1EnvVar> vars, String name, String value) {
171206
vars.add(new V1EnvVar().name(name).value(value));
172207
}

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
import java.util.HashMap;
1010
import java.util.List;
1111
import java.util.Map;
12+
import java.util.Optional;
1213

14+
import io.kubernetes.client.models.V1Container;
1315
import io.kubernetes.client.models.V1EnvVar;
1416
import io.kubernetes.client.models.V1Pod;
1517
import oracle.kubernetes.operator.LabelConstants;
@@ -35,6 +37,7 @@
3537
import static org.hamcrest.Matchers.anEmptyMap;
3638
import static org.hamcrest.Matchers.contains;
3739
import static org.hamcrest.Matchers.hasEntry;
40+
import static org.hamcrest.Matchers.hasItem;
3841
import static org.hamcrest.Matchers.hasKey;
3942
import static org.hamcrest.Matchers.is;
4043
import static org.hamcrest.Matchers.not;
@@ -174,17 +177,33 @@ public void whenClusterHasAdditionalVolumesWithVariables_createManagedPodWithSub
174177

175178
@Test
176179
public void whenClusterHasLabelsWithVariables_createManagedPodWithSubstitutions() {
180+
V1EnvVar envVar = toEnvVar("TEST_ENV", "test-value");
181+
testSupport.addToPacket(ProcessingConstants.ENVVARS, Arrays.asList(envVar));
182+
177183
testSupport.addToPacket(ProcessingConstants.CLUSTER_NAME, CLUSTER_NAME);
178184
getConfigurator()
185+
.withLogHomeEnabled(true)
186+
.withContainer(new V1Container()
187+
.name("test")
188+
.addCommandItem("/bin/bash")
189+
.addArgsItem("echo")
190+
.addArgsItem("This server is $(SERVER_NAME) and has $(TEST_ENV)"))
179191
.configureCluster(CLUSTER_NAME)
180192
.withPodLabel("myCluster", "my-$(CLUSTER_NAME)")
181193
.withPodLabel("logHome", "$(LOG_HOME)");
182194

195+
V1Pod pod = getCreatedPod();
183196
assertThat(
184-
getCreatedPod().getMetadata().getLabels(),
197+
pod.getMetadata().getLabels(),
185198
allOf(
186-
hasEntry("myCluster", "my" + CLUSTER_NAME),
199+
hasEntry("myCluster", "my-" + CLUSTER_NAME),
187200
hasEntry("logHome", "/shared/logs/" + UID)));
201+
Optional<V1Container> o = pod.getSpec().getContainers()
202+
.stream().filter(c -> "test".equals(c.getName())).findFirst();
203+
assertThat(
204+
o.orElseThrow().getArgs(),
205+
allOf(
206+
hasItem("This server is " + SERVER_NAME + " and has test-value")));
188207
}
189208

190209
@Test

0 commit comments

Comments
 (0)