Skip to content

Commit 344c7d0

Browse files
committed
adding pod templates test
1 parent b131ce8 commit 344c7d0

File tree

4 files changed

+275
-5
lines changed

4 files changed

+275
-5
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
// Copyright 2019, Oracle Corporation and/or its affiliates. All rights reserved.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at
3+
// http://oss.oracle.com/licenses/upl.
4+
5+
package oracle.kubernetes.operator;
6+
7+
import java.nio.charset.Charset;
8+
import java.nio.charset.StandardCharsets;
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
import java.nio.file.Paths;
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
import java.util.logging.Level;
15+
16+
import org.junit.AfterClass;
17+
import org.junit.BeforeClass;
18+
import org.junit.FixMethodOrder;
19+
import org.junit.Test;
20+
import org.junit.runners.MethodSorters;
21+
22+
import oracle.kubernetes.operator.utils.Domain;
23+
import oracle.kubernetes.operator.utils.DomainCrd;
24+
import oracle.kubernetes.operator.utils.ExecResult;
25+
import oracle.kubernetes.operator.utils.Operator;
26+
import oracle.kubernetes.operator.utils.TestUtils;
27+
28+
/**
29+
* Simple JUnit test file used for testing domain pod templates.
30+
*
31+
* <p>
32+
* This test is used for creating Operator(s) and domain which uses pod templates.
33+
*/
34+
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
35+
public class ItPodTemplates extends BaseTest {
36+
37+
private static Operator operator1;
38+
39+
/**
40+
* This method gets called only once before any of the test methods are
41+
* executed. It does the initialization of the integration test properties
42+
* defined in OperatorIT.properties and setting the resultRoot, pvRoot and
43+
* projectRoot attributes.
44+
*
45+
* @throws Exception
46+
* exception
47+
*/
48+
@BeforeClass
49+
public static void staticPrepare() throws Exception {
50+
// initialize test properties and create the directories
51+
initialize(APP_PROPS_FILE);
52+
}
53+
54+
/**
55+
* Releases k8s cluster lease, archives result, pv directories.
56+
*
57+
* @throws Exception
58+
* exception
59+
*/
60+
@AfterClass
61+
public static void staticUnPrepare() throws Exception {
62+
logger.info("+++++++++++++++++++++++++++++++++---------------------------------+");
63+
logger.info("BEGIN");
64+
logger.info("Run once, release cluster lease");
65+
66+
tearDown(new Object() {
67+
}.getClass().getEnclosingClass().getSimpleName());
68+
69+
logger.info("SUCCESS");
70+
}
71+
72+
/**
73+
* Test pod templates using all the variables $(SERVER_NAME), $(DOMAIN_NAME), $(DOMAIN_UID),
74+
* $(DOMAIN_HOME), $(LOG_HOME) and $(CLUSTER_NAME) in serverPod. Make sure the domain comes up
75+
* successfully.
76+
* @throws Exception when the domain crd creation fails or when updating the serverPod with variables
77+
*
78+
*/
79+
@Test
80+
public void testPodTemplateUsingVariables() throws Exception {
81+
String testMethodName = new Object() {
82+
}.getClass().getEnclosingMethod().getName();
83+
logTestBegin(testMethodName);
84+
logger.info("Creating Operator & waiting for the script to complete execution");
85+
// create operator1
86+
if (operator1 == null) {
87+
operator1 = TestUtils.createOperator(OPERATOR1_YAML);
88+
}
89+
Domain domain = null;
90+
boolean testCompletedSuccessfully = false;
91+
try {
92+
Map<String, Object> domainMap = TestUtils.loadYaml(DOMAINONPV_WLST_YAML);
93+
domainMap.put("domainUID", "podtemplatedomain");
94+
domainMap.put("adminNodePort", new Integer("30713"));
95+
// just create domain yaml, dont apply
96+
domain = TestUtils.createDomain(domainMap, false);
97+
String originalYaml = BaseTest.getUserProjectsDir() + "/weblogic-domains/" + domain.getDomainUid()
98+
+ "/domain.yaml";
99+
100+
// Modify the original domain yaml to include labels in serverPod
101+
// node
102+
DomainCrd crd = new DomainCrd(originalYaml);
103+
// add labels to serverPod
104+
Map<String, String> labelKeyValue = new HashMap();
105+
labelKeyValue.put("servername", "$(SERVER_NAME)");
106+
labelKeyValue.put("domainname", "$(DOMAIN_NAME)");
107+
labelKeyValue.put("domainuid", "$(DOMAIN_UID)");
108+
crd.addObjectNodeToServerPod("labels",labelKeyValue);
109+
110+
//add annotations to serverPod as DOMAIN_HOME and LOG_HOME contains "/" which is not allowed in labels
111+
Map<String, String> envKeyValue = new HashMap();
112+
envKeyValue.put("domainhome", "$(DOMAIN_HOME)");
113+
envKeyValue.put("loghome", "$(LOG_HOME)");
114+
crd.addObjectNodeToServerPod("annotations",envKeyValue);
115+
116+
//add label to cluster serverPod for CLUSTER_NAME
117+
Map<String, String> clusterLabelKeyValue = new HashMap();
118+
clusterLabelKeyValue.put("clustername", "$(CLUSTER_NAME)");
119+
crd.addObjectNodeToClusterServerPod(domain.getClusterName(), "labels", clusterLabelKeyValue);
120+
121+
String modYaml = crd.getYamlTree();
122+
logger.info(modYaml);
123+
124+
// Write the modified yaml to a new file
125+
Path path = Paths.get(BaseTest.getUserProjectsDir() + "/weblogic-domains/" + domain.getDomainUid(),
126+
"domain.modified.yaml");
127+
logger.log(Level.INFO, "Path of the modified domain.yaml :{0}", path.toString());
128+
Charset charset = StandardCharsets.UTF_8;
129+
Files.write(path, modYaml.getBytes(charset));
130+
131+
// Apply the new yaml to update the domain
132+
logger.log(Level.INFO, "kubectl apply -f {0}", path.toString());
133+
ExecResult exec = TestUtils.exec("kubectl apply -f " + path.toString());
134+
logger.info(exec.stdout());
135+
136+
domain.verifyDomainCreated();
137+
testCompletedSuccessfully = true;
138+
} finally {
139+
if (domain != null && !SMOKETEST && (JENKINS || testCompletedSuccessfully)) {
140+
domain.shutdownUsingServerStartPolicy();
141+
}
142+
}
143+
144+
logger.info("SUCCESS - " + testMethodName);
145+
}
146+
}

integration-tests/src/test/java/oracle/kubernetes/operator/utils/Domain.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ public class Domain {
7272
private String imageTag;
7373
private String imageName;
7474
private boolean voyager;
75-
75+
private boolean createDomainResource = true;
76+
7677
public Domain() throws Exception {
7778
domainMap = new HashMap<>();
7879
}
@@ -82,8 +83,18 @@ public Domain(String inputYaml) throws Exception {
8283
this(TestUtils.loadYaml(inputYaml));
8384
}
8485

86+
public Domain(String inputYaml, boolean createDomainResource) throws Exception {
87+
// read input domain yaml to test
88+
this(TestUtils.loadYaml(inputYaml), createDomainResource);
89+
}
90+
8591
public Domain(Map<String, Object> inputDomainMap) throws Exception {
92+
this(inputDomainMap, true);
93+
}
94+
95+
public Domain(Map<String, Object> inputDomainMap, boolean createDomainResource) throws Exception {
8696
initialize(inputDomainMap);
97+
this.createDomainResource = createDomainResource;
8798
createPv();
8899
createSecret();
89100
generateInputYaml();
@@ -1226,7 +1237,8 @@ protected void callCreateDomainScript(String outputDir) throws Exception {
12261237
}
12271238

12281239
// write configOverride and configOverrideSecrets to domain.yaml and/or create domain
1229-
if (domainMap.containsKey("configOverrides") || domainMap.containsKey("domainHomeImageBase")) {
1240+
if (domainMap.containsKey("configOverrides") || domainMap.containsKey("domainHomeImageBase")
1241+
|| createDomainResource) {
12301242
appendToDomainYamlAndCreate();
12311243
}
12321244
}
@@ -1693,7 +1705,7 @@ private String prepareCmdToCallCreateDomainScript(String outputDir) {
16931705

16941706
// skip executing yaml if configOverrides or domain in image
16951707
if (!domainMap.containsKey("configOverrides")
1696-
&& !domainMap.containsKey("domainHomeImageBase")) {
1708+
&& !domainMap.containsKey("domainHomeImageBase") && createDomainResource) {
16971709
createDomainScriptCmd.append(" -e ");
16981710
}
16991711

integration-tests/src/test/java/oracle/kubernetes/operator/utils/DomainCrd.java

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,23 @@ public void addObjectNodeToCluster(String clusterName, Map<String, Object> attri
163163
}
164164
}
165165

166+
/**
167+
* A utility method to add attributes to cluster node's server pod in domain.yaml.
168+
*
169+
* @param clusterName - Name of the cluster to which the attributes to be added
170+
* @param objectName - name of the object in cluster to which the attributes to be added
171+
* @param attributes - A HashMap of key value pairs
172+
*/
173+
public void addObjectNodeToClusterServerPod(String clusterName, String objectName, Map<String, String> attributes) {
174+
175+
JsonNode objectNode = getObjectNodeFromServerPod(getClusterNode(clusterName),objectName);
176+
177+
for (Map.Entry<String, String> entry : attributes.entrySet()) {
178+
Object entryValue = entry.getValue();
179+
((ObjectNode) objectNode).put(entry.getKey(), (String) entryValue);
180+
}
181+
}
182+
166183
/**
167184
* A utility method to add shutdown element and attributes to cluster node in domain.yaml.
168185
*
@@ -196,6 +213,28 @@ public void addObjectNodeToMS(String managedServerName, Map<String, String> attr
196213
}
197214
}
198215

216+
/**
217+
* A utility method to add attributes to server pod node in domain.yaml.
218+
*
219+
* @param objectName
220+
* - Name of the node to which the attributes to be
221+
* added
222+
* @param attributes
223+
* - A HashMap of key value pairs
224+
*/
225+
public void addObjectNodeToServerPod(String objectName, Map<String, String> attributes) {
226+
JsonNode objectNode = getObjectNodeFromServerPod(getSpecNode() ,objectName);
227+
for (Map.Entry<String, String> entry : attributes.entrySet()) {
228+
((ObjectNode) objectNode).put(entry.getKey(), entry.getValue());
229+
}
230+
try {
231+
String jsonString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(objectNode);
232+
System.out.println(jsonString);
233+
} catch (Exception ex) {
234+
// no-op
235+
}
236+
}
237+
199238
/**
200239
* A utility method to add attributes to managed server node in domain.yaml.
201240
*
@@ -263,6 +302,15 @@ private JsonNode getSpecNode() {
263302
return root.path("spec");
264303
}
265304

305+
/**
306+
* Gets the serverPod node entry from Domain CRD JSON tree.
307+
*
308+
* @return serverPod node
309+
*/
310+
private JsonNode getServerPodNode() {
311+
return root.path("spec").path("serverPod");
312+
}
313+
266314
/**
267315
* Gets the administration server node entry from Domain CRD JSON tree.
268316
*
@@ -335,7 +383,60 @@ private JsonNode getManagedServerNode(String managedServerName) {
335383
}
336384
return managedserverNode;
337385
}
338-
386+
387+
/**
388+
* Gets the object node entry from the server pod of the given parent node in Domain CRD JSON tree.
389+
* @param serverPodsParentNode parent node of the server pod
390+
* @param objectName Name of the object for which to get the JSON node
391+
* @return object node
392+
*/
393+
private JsonNode getObjectNodeFromServerPod(JsonNode serverPodsParentNode, String objectName) {
394+
JsonNode serverPodNode = null;
395+
JsonNode objectNode = null;
396+
if (serverPodsParentNode.path("serverPod").isMissingNode()) {
397+
logger.info("Missing serverPod Node");
398+
serverPodNode = objectMapper.createObjectNode();
399+
((ObjectNode) serverPodsParentNode).set("serverPod", serverPodNode);
400+
objectNode = objectMapper.createObjectNode();
401+
((ObjectNode) serverPodNode).set(objectName, objectNode);
402+
403+
} else {
404+
serverPodNode = serverPodsParentNode.path("serverPod");
405+
if(serverPodNode.path(objectName).isMissingNode())
406+
{
407+
logger.info("Creating node with name "+objectName);
408+
objectNode = objectMapper.createObjectNode();
409+
((ObjectNode) serverPodNode).set(objectName, objectNode);
410+
} else {
411+
objectNode = serverPodNode.path(objectName);
412+
}
413+
}
414+
return objectNode;
415+
}
416+
417+
/**
418+
* Gets the object node entry from clusters ServerPod Domain CRD JSON tree.
419+
*
420+
* @param clusterName
421+
* Name of the cluster for which to get the serverPod
422+
* @param objectName
423+
* Name of the object for which to get the JSON node
424+
* @return object node entry from clusters Serverpod of Domain CRD JSON tree
425+
*/
426+
private JsonNode getObjectNodeFromCluster(String clusterName, String objectName) {
427+
JsonNode objectNode = null;
428+
JsonNode clusterNode = getClusterNode(clusterName);
429+
if (clusterNode.path(objectName).isMissingNode()) {
430+
logger.info("Missing clusters "+objectName+" Node");
431+
objectNode = objectMapper.createObjectNode();
432+
((ObjectNode) clusterNode).set(objectName, objectNode);
433+
434+
} else {
435+
objectNode = clusterNode.path(objectName);
436+
}
437+
return objectNode;
438+
}
439+
339440
/**
340441
* Utility method to create a file and write to it.
341442
*

integration-tests/src/test/java/oracle/kubernetes/operator/utils/TestUtils.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,11 +769,22 @@ public static Domain createDomain(String inputYaml) throws Exception {
769769
logger.info("Creating domain with yaml, waiting for the script to complete execution");
770770
return new Domain(inputYaml);
771771
}
772-
772+
773+
public static Domain createDomain(String inputYaml, boolean createDomainResource) throws Exception {
774+
logger.info("Creating domain with yaml, waiting for the script to complete execution");
775+
return new Domain(inputYaml, createDomainResource);
776+
}
777+
773778
public static Domain createDomain(Map<String, Object> inputDomainMap) throws Exception {
774779
logger.info("Creating domain with Map, waiting for the script to complete execution");
775780
return new Domain(inputDomainMap);
776781
}
782+
783+
public static Domain createDomain(Map<String, Object> inputDomainMap, boolean createDomainResource)
784+
throws Exception {
785+
logger.info("Creating domain with Map, waiting for the script to complete execution");
786+
return new Domain(inputDomainMap, createDomainResource);
787+
}
777788

778789
public static Map<String, Object> loadYaml(String yamlFile) throws Exception {
779790
// read input domain yaml to test

0 commit comments

Comments
 (0)