Skip to content

Commit 52edb58

Browse files
committed
adding lifecycle restart version tests
1 parent 04b4b42 commit 52edb58

File tree

2 files changed

+345
-1
lines changed

2 files changed

+345
-1
lines changed

integration-tests/src/test/java/oracle/kubernetes/operator/ITPodsRestart.java

Lines changed: 145 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
// Copyright 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.
4-
54
package oracle.kubernetes.operator;
65

6+
import static oracle.kubernetes.operator.BaseTest.logger;
7+
8+
import java.nio.charset.Charset;
9+
import java.nio.charset.StandardCharsets;
10+
import java.nio.file.Files;
11+
import java.nio.file.Path;
12+
import java.nio.file.Paths;
713
import java.util.Map;
814
import oracle.kubernetes.operator.utils.Domain;
15+
import oracle.kubernetes.operator.utils.DomainCRD;
16+
import oracle.kubernetes.operator.utils.ExecResult;
917
import oracle.kubernetes.operator.utils.Operator;
1018
import oracle.kubernetes.operator.utils.TestUtils;
1119
import org.junit.AfterClass;
@@ -26,6 +34,8 @@ public class ITPodsRestart extends BaseTest {
2634

2735
private static Domain domain = null;
2836
private static Operator operator1;
37+
private static String KUBE_EXEC_CMD;
38+
private static String restartTmpDir = "";
2939

3040
/**
3141
* This method gets called only once before any of the test methods are executed. It does the
@@ -45,6 +55,8 @@ public static void staticPrepare() throws Exception {
4555
if (operator1 == null) {
4656
operator1 = TestUtils.createOperator(OPERATOR1_YAML);
4757
}
58+
restartTmpDir = BaseTest.getResultDir() + "/restarttemp";
59+
Files.createDirectories(Paths.get(restartTmpDir));
4860

4961
domain = createPodsRestartdomain();
5062
Assert.assertNotNull(domain);
@@ -190,6 +202,138 @@ public void testServerPodsRestartByChangingZImage() throws Exception {
190202
logger.info("SUCCESS - " + testMethodName);
191203
}
192204

205+
@Test
206+
public void testAdminServerRestartVersions() throws Exception {
207+
Assume.assumeFalse(QUICKTEST);
208+
String testMethodName = new Object() {}.getClass().getEnclosingMethod().getName();
209+
logTestBegin(testMethodName);
210+
String originalYaml =
211+
BaseTest.getUserProjectsDir()
212+
+ "/weblogic-domains/"
213+
+ domain.getDomainUid()
214+
+ "/domain.yaml";
215+
try {
216+
DomainCRD crd = new DomainCRD();
217+
String yaml =
218+
crd.addRestartVersionToAdminServer(
219+
TestUtils.exec(
220+
"kubectl get Domain "
221+
+ domain.getDomainUid()
222+
+ " -n "
223+
+ domain.getDomainNS()
224+
+ " --output json")
225+
.stdout(),
226+
"v1.1");
227+
Path path = Paths.get(restartTmpDir, "restart.admin.yaml");
228+
Charset charset = StandardCharsets.UTF_8;
229+
Files.write(path, yaml.getBytes(charset));
230+
logger.info(TestUtils.exec("kubectl apply -f " + path.toString()).stdout());
231+
domain.verifyAdminServerRestarted();
232+
} finally {
233+
TestUtils.exec("kubectl apply -f " + originalYaml);
234+
domain.verifyAdminServerRestarted();
235+
}
236+
logger.info("SUCCESS - " + testMethodName);
237+
}
238+
239+
// @Test
240+
public void testClusterRestartVersions() throws Exception {
241+
Assume.assumeFalse(QUICKTEST);
242+
String testMethodName = new Object() {}.getClass().getEnclosingMethod().getName();
243+
logTestBegin(testMethodName);
244+
ExecResult result;
245+
String originalYaml =
246+
BaseTest.getUserProjectsDir()
247+
+ "/weblogic-domains/"
248+
+ domain.getDomainUid()
249+
+ "/domain.yaml";
250+
try {
251+
result =
252+
TestUtils.exec(
253+
"kubectl get Domain "
254+
+ domain.getDomainUid()
255+
+ " -n "
256+
+ domain.getDomainNS()
257+
+ " --output json");
258+
DomainCRD parser = new DomainCRD();
259+
String yaml =
260+
parser.addRestartVersionToCluster(
261+
result.stdout(), domain.getDomainMap().get("clusterName").toString(), "v1.1");
262+
Path path = Paths.get(restartTmpDir, "restart.cluster.yaml");
263+
Charset charset = StandardCharsets.UTF_8;
264+
Files.write(path, yaml.getBytes(charset));
265+
result = TestUtils.exec("kubectl apply -f " + path.toString());
266+
// TODO - verify that the pod is restarting
267+
} finally {
268+
result = TestUtils.exec("kubectl apply -f " + originalYaml);
269+
}
270+
logger.info("SUCCESS - " + testMethodName);
271+
}
272+
273+
// @Test
274+
public void testMSRestartVersions() throws Exception {
275+
Assume.assumeFalse(QUICKTEST);
276+
String testMethodName = new Object() {}.getClass().getEnclosingMethod().getName();
277+
logTestBegin(testMethodName);
278+
ExecResult result;
279+
String originalYaml =
280+
BaseTest.getUserProjectsDir()
281+
+ "/weblogic-domains/"
282+
+ domain.getDomainUid()
283+
+ "/domain.yaml";
284+
try {
285+
result =
286+
TestUtils.exec(
287+
"kubectl get Domain "
288+
+ domain.getDomainUid()
289+
+ " -n "
290+
+ domain.getDomainNS()
291+
+ " --output json");
292+
DomainCRD parser = new DomainCRD();
293+
String yaml = parser.addRestartVersionToMS(result.stdout(), "managed-server1", "v1.1");
294+
Path path = Paths.get(restartTmpDir, "restart.ms.yaml");
295+
Charset charset = StandardCharsets.UTF_8;
296+
Files.write(path, yaml.getBytes(charset));
297+
result = TestUtils.exec("kubectl apply -f " + path.toString());
298+
// TODO - verify that the pod is restarting
299+
} finally {
300+
result = TestUtils.exec("kubectl apply -f " + originalYaml);
301+
}
302+
logger.info("SUCCESS - " + testMethodName);
303+
}
304+
305+
// @Test
306+
public void testDomainRestartVersions() throws Exception {
307+
Assume.assumeFalse(QUICKTEST);
308+
String testMethodName = new Object() {}.getClass().getEnclosingMethod().getName();
309+
logTestBegin(testMethodName);
310+
ExecResult result;
311+
String originalYaml =
312+
BaseTest.getUserProjectsDir()
313+
+ "/weblogic-domains/"
314+
+ domain.getDomainUid()
315+
+ "/domain.yaml";
316+
try {
317+
result =
318+
TestUtils.exec(
319+
"kubectl get Domain "
320+
+ domain.getDomainUid()
321+
+ " -n "
322+
+ domain.getDomainNS()
323+
+ " --output json");
324+
DomainCRD parser = new DomainCRD();
325+
String yaml = parser.addRestartVersionToDomain(result.stdout(), "v1.1");
326+
Path path = Paths.get(restartTmpDir, "restart.ms.yaml");
327+
Charset charset = StandardCharsets.UTF_8;
328+
Files.write(path, yaml.getBytes(charset));
329+
result = TestUtils.exec("kubectl apply -f " + path.toString());
330+
// TODO - verify that the pod is restarting
331+
} finally {
332+
result = TestUtils.exec("kubectl apply -f " + originalYaml);
333+
}
334+
logger.info("SUCCESS - " + testMethodName);
335+
}
336+
193337
private static Domain createPodsRestartdomain() throws Exception {
194338

195339
Map<String, Object> domainMap = TestUtils.loadYaml(DOMAINONPV_WLST_YAML);
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
package oracle.kubernetes.operator.utils;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.fasterxml.jackson.databind.node.ArrayNode;
6+
import com.fasterxml.jackson.databind.node.ObjectNode;
7+
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
8+
import java.io.IOException;
9+
import java.nio.charset.Charset;
10+
import java.nio.charset.StandardCharsets;
11+
import java.nio.file.Files;
12+
import java.nio.file.Path;
13+
import java.nio.file.Paths;
14+
15+
/** A Parser utility class to manipulate domain.yaml file */
16+
public class DomainCRD {
17+
18+
/**
19+
* A utility method to add restartVersion attribute to domain in domain.yaml
20+
*
21+
* @param domainCRD String representation of the domain.yaml as JSON tree
22+
* @param version Version value for the restartVersion attribute
23+
* @return Yaml String with added restartVersion attribute in domain
24+
* @throws IOException
25+
*/
26+
public String addRestartVersionToDomain(String domainCRD, String version) throws IOException {
27+
ObjectMapper objectMapper = new ObjectMapper();
28+
JsonNode root = objectMapper.readTree(domainCRD);
29+
30+
// modify admin server restartVersion
31+
JsonNode specNode = getSpecNode(root);
32+
((ObjectNode) specNode).put("restartVersion", version);
33+
34+
String jsonAsYaml = new YAMLMapper().writerWithDefaultPrettyPrinter().writeValueAsString(root);
35+
return jsonAsYaml;
36+
}
37+
38+
/**
39+
* A utility method to add restartVersion attribute to administration server in domain.yaml
40+
*
41+
* @param domainCRD String representation of the domain.yaml as JSON tree
42+
* @param version Version value for the restartVersion attribute
43+
* @return Yaml String with added restartVersion attribute in administration server
44+
* @throws IOException
45+
*/
46+
public String addRestartVersionToAdminServer(String domainCRD, String version)
47+
throws IOException {
48+
ObjectMapper objectMapper = new ObjectMapper();
49+
JsonNode root = objectMapper.readTree(domainCRD);
50+
51+
// modify admin server restartVersion
52+
JsonNode adminServerNode = getAdminServerNode(root);
53+
((ObjectNode) adminServerNode).put("restartVersion", version);
54+
55+
String jsonAsYaml = new YAMLMapper().writerWithDefaultPrettyPrinter().writeValueAsString(root);
56+
return jsonAsYaml;
57+
}
58+
59+
/**
60+
* A utility method to add restartVersion attribute to cluster in domain.yaml
61+
*
62+
* @param domainCRD String representation of the domain.yaml as JSON tree
63+
* @param ClusterName Name of the cluster in which to add the restartVersion attribute
64+
* @param version Version value for the restartVersion attribute
65+
* @return Yaml String with added restartVersion attribute in cluster
66+
* @throws IOException when the JSON tree cannot be read
67+
*/
68+
public String addRestartVersionToCluster(String domainCRD, String ClusterName, String version)
69+
throws IOException {
70+
ObjectMapper objectMapper = new ObjectMapper();
71+
JsonNode root = objectMapper.readTree(domainCRD);
72+
73+
// modify cluster restartVersion
74+
JsonNode clusterNode = getClusterNode(root, ClusterName);
75+
((ObjectNode) clusterNode).put("restartVersion", version);
76+
77+
String jsonAsYaml = new YAMLMapper().writerWithDefaultPrettyPrinter().writeValueAsString(root);
78+
return jsonAsYaml;
79+
}
80+
81+
/**
82+
* A utility method to add restartVersion attribute to managed server in domain.yaml
83+
*
84+
* @param domainCRD domainCRD String representation of the domain.yaml as JSON tree
85+
* @param managedServerName Name of the managed server in which to add the restartVersion
86+
* attribute
87+
* @param version Version value for the restartVersion attribute
88+
* @return Yaml String with added restartVersion attribute in managed server
89+
* @throws IOException when the JSON tree cannot be read
90+
*/
91+
public String addRestartVersionToMS(String domainCRD, String managedServerName, String version)
92+
throws IOException {
93+
ObjectMapper objectMapper = new ObjectMapper();
94+
JsonNode root = objectMapper.readTree(domainCRD);
95+
96+
// modify managedserver restartVersion
97+
JsonNode managedServerNode = getManagedServerNode(objectMapper, root, managedServerName);
98+
((ObjectNode) managedServerNode).put("restartVersion", version);
99+
100+
String jsonAsYaml = new YAMLMapper().writerWithDefaultPrettyPrinter().writeValueAsString(root);
101+
return jsonAsYaml;
102+
}
103+
104+
/**
105+
* Gets the spec node entry from Domain CRD JSON tree
106+
*
107+
* @param root - Root JSON node of the Domain CRD JSON tree
108+
* @return - spec node entry from Domain CRD JSON tree
109+
*/
110+
private JsonNode getSpecNode(JsonNode root) {
111+
JsonNode items = root.get("items");
112+
JsonNode spec = null;
113+
for (JsonNode item : items) {
114+
spec = item.path("spec");
115+
}
116+
return spec;
117+
}
118+
119+
/**
120+
* Gets the administration server node entry from Domain CRD JSON tree
121+
*
122+
* @param root - Root JSON node of the Domain CRD JSON tree
123+
* @return - administration server node entry from Domain CRD JSON tree
124+
*/
125+
private JsonNode getAdminServerNode(JsonNode root) {
126+
JsonNode items = root.get("items");
127+
JsonNode adminServer = null;
128+
for (JsonNode item : items) {
129+
adminServer = item.path("spec").path("adminServer");
130+
}
131+
return adminServer;
132+
}
133+
134+
/**
135+
* Gets the cluster node entry from Domain CRD JSON tree for the given cluster name
136+
*
137+
* @param root - Root JSON node of the Domain CRD JSON tree
138+
* @param clusterName - Name of the cluster
139+
* @return - cluster node entry from Domain CRD JSON tree
140+
*/
141+
private JsonNode getClusterNode(JsonNode root, String clusterName) {
142+
ArrayNode items = (ArrayNode) root.get("items");
143+
ArrayNode clusters;
144+
JsonNode clusterNode = null;
145+
for (JsonNode item : items) {
146+
clusters = (ArrayNode) item.path("spec").path("clusters");
147+
for (JsonNode cluster : clusters) {
148+
if (cluster.get("clusterName").asText().equals(clusterName)) {
149+
clusterNode = cluster;
150+
}
151+
}
152+
}
153+
return clusterNode;
154+
}
155+
156+
/**
157+
* Gets the managed server node entry from Domain CRD JSON tree
158+
*
159+
* @param objectMapper - Instance of the ObjectMapper to use for creating a missing node
160+
* @param root - Root JSON node of the Domain CRD JSON tree
161+
* @param managedServerName - Name of the managed server for which to get the JSON node
162+
* @return administration server node entry from Domain CRD JSON tree
163+
*/
164+
private JsonNode getManagedServerNode(
165+
ObjectMapper objectMapper, JsonNode root, String managedServerName) {
166+
ArrayNode items = (ArrayNode) root.get("items");
167+
ArrayNode managedservers = null;
168+
JsonNode managedserverNode = null;
169+
for (JsonNode item : items) {
170+
managedservers = (ArrayNode) item.path("spec").path("managedServers");
171+
if (managedservers.size() != 0) {
172+
for (JsonNode managedserver : managedservers) {
173+
if (managedserver.get("serverName").equals(managedServerName)) {
174+
managedserverNode = managedserver;
175+
}
176+
}
177+
} else {
178+
ObjectNode managedserver = objectMapper.createObjectNode();
179+
managedserver.put("serverName", managedServerName);
180+
managedservers.add(managedserver);
181+
managedserverNode = managedserver;
182+
}
183+
}
184+
return managedserverNode;
185+
}
186+
187+
/**
188+
* Utility method to create a file and write to it.
189+
*
190+
* @param dir - Directory in which to create the file
191+
* @param file - Name of the file to write the content to
192+
* @param content - String content to write to the file
193+
* @throws IOException - When file cannot opened or written
194+
*/
195+
public void writeToFile(String dir, String file, String content) throws IOException {
196+
Path path = Paths.get(dir, file);
197+
Charset charset = StandardCharsets.UTF_8;
198+
Files.write(path, content.getBytes(charset));
199+
}
200+
}

0 commit comments

Comments
 (0)