Skip to content

Commit df9b9a0

Browse files
committed
refactor the code
1 parent 56adad0 commit df9b9a0

File tree

3 files changed

+57
-123
lines changed

3 files changed

+57
-123
lines changed

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

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
package oracle.kubernetes.operator;
66

7+
import java.nio.file.Files;
8+
import java.nio.file.Paths;
79
import java.util.Map;
810
import oracle.kubernetes.operator.utils.Domain;
911
import oracle.kubernetes.operator.utils.Operator;
@@ -85,10 +87,10 @@ public void testServerPodsRestartByChangingEnvProperty() throws Exception {
8587
logTestBegin(testMethodName);
8688

8789
logger.info(
88-
"About to testDomainServerPodRestart for Domain: "
90+
"About to verifyDomainServerPodRestart for Domain: "
8991
+ domain.getDomainUid()
9092
+ " env property: StdoutDebugEnabled=false to StdoutDebugEnabled=true");
91-
domain.testDomainServerPodRestart(
93+
domain.verifyDomainServerPodRestart(
9294
"\"-Dweblogic.StdoutDebugEnabled=false\"", "\"-Dweblogic.StdoutDebugEnabled=true\"");
9395

9496
logger.info("SUCCESS - " + testMethodName);
@@ -108,10 +110,10 @@ public void testServerPodsRestartByChangingLogHomeEnabled() throws Exception {
108110
logTestBegin(testMethodName);
109111

110112
logger.info(
111-
"About to testDomainServerPodRestart for Domain: "
113+
"About to verifyDomainServerPodRestart for Domain: "
112114
+ domain.getDomainUid()
113115
+ " logHomeEnabled: true --> logHomeEnabled: false");
114-
domain.testDomainServerPodRestart("logHomeEnabled: true", "logHomeEnabled: false");
116+
domain.verifyDomainServerPodRestart("logHomeEnabled: true", "logHomeEnabled: false");
115117

116118
logger.info("SUCCESS - " + testMethodName);
117119
}
@@ -130,10 +132,10 @@ public void testServerPodsRestartByChangingImagePullPolicy() throws Exception {
130132
logTestBegin(testMethodName);
131133

132134
logger.info(
133-
"About to testDomainServerPodRestart for Domain: "
135+
"About to verifyDomainServerPodRestart for Domain: "
134136
+ domain.getDomainUid()
135137
+ " imagePullPolicy: IfNotPresent --> imagePullPolicy: Never ");
136-
domain.testDomainServerPodRestart(
138+
domain.verifyDomainServerPodRestart(
137139
"imagePullPolicy: \"IfNotPresent\"", "imagePullPolicy: \"Never\" ");
138140

139141
logger.info("SUCCESS - " + testMethodName);
@@ -153,10 +155,10 @@ public void testServerPodsRestartByChangingIncludeServerOutInPodLog() throws Exc
153155
logTestBegin(testMethodName);
154156

155157
logger.info(
156-
"About to testDomainServerPodRestart for Domain: "
158+
"About to verifyDomainServerPodRestart for Domain: "
157159
+ domain.getDomainUid()
158160
+ " includeServerOutInPodLog: true --> includeServerOutInPodLog: false");
159-
domain.testDomainServerPodRestart(
161+
domain.verifyDomainServerPodRestart(
160162
"includeServerOutInPodLog: true", "includeServerOutInPodLog: false");
161163

162164
logger.info("SUCCESS - " + testMethodName);
@@ -177,12 +179,12 @@ public void testServerPodsRestartByChangingZImage() throws Exception {
177179

178180
try {
179181
logger.info(
180-
"About to testDomainServerPodRestart for Domain: "
182+
"About to verifyDomainServerPodRestart for Domain: "
181183
+ domain.getDomainUid()
182184
+ " Image property: store/oracle/weblogic:12.2.1.3 to store/oracle/weblogic:duplicate");
183185

184186
TestUtils.exec("docker tag store/oracle/weblogic:12.2.1.3 store/oracle/weblogic:duplicate");
185-
domain.testDomainServerPodRestart(
187+
domain.verifyDomainServerPodRestart(
186188
"\"store/oracle/weblogic:12.2.1.3\"", "\"store/oracle/weblogic:duplicate\"");
187189
} finally {
188190
TestUtils.exec("docker rmi -f store/oracle/weblogic:duplicate");
@@ -209,18 +211,19 @@ public void testServerPodsRestartByChangingContSecurityContext() throws Exceptio
209211
// firstly ensure that original domain.yaml doesn't include the property-to-be-added
210212
String domainFileName =
211213
BaseTest.getUserProjectsDir() + "/weblogic-domains/" + domainUid + "/domain.yaml";
212-
boolean result = TestUtils.checkFileIncludeProperty("fsGroup: 1000", domainFileName);
214+
boolean result =
215+
(new String(Files.readAllBytes(Paths.get(domainFileName)))).contains("fsGroup: 1000");
213216
Assert.assertFalse(result);
214217

215218
// domainYaml: the yaml file name with changed property under resources dir
216219
String domainYaml = "cont.security.context.domain.yaml";
217220
logger.info(
218-
"About to testDomainServerPodRestart for Domain: "
221+
"About to verifyDomainServerPodRestart for Domain: "
219222
+ domain.getDomainUid()
220223
+ " change container securityContext:\n"
221224
+ " runAsUser: 1000\n"
222225
+ " fsGroup: 1000 ");
223-
domain.testDomainServerPodRestart(domainYaml);
226+
domain.verifyDomainServerPodRestart(domainYaml);
224227
domain.findServerPropertyChange("securityContext", "admin-server");
225228
domain.findServerPropertyChange("securityContext", "managed-server1");
226229

@@ -245,19 +248,20 @@ public void testServerPodsRestartByChangingPodSecurityContext() throws Exception
245248
// firstly ensure that original domain.yaml doesn't include the property-to-be-added
246249
String domainFileName =
247250
BaseTest.getUserProjectsDir() + "/weblogic-domains/" + domainUid + "/domain.yaml";
248-
boolean result = TestUtils.checkFileIncludeProperty("fsGroup: 2000", domainFileName);
251+
boolean result =
252+
(new String(Files.readAllBytes(Paths.get(domainFileName)))).contains("fsGroup: 2000");
249253
Assert.assertFalse(result);
250254

251255
// domainYaml: the yaml file name with changed property under resources dir
252256
String domainYaml = "pod.security.context.domain.yaml";
253257

254258
logger.info(
255-
"About to testDomainServerPodRestart for Domain: "
259+
"About to verifyDomainServerPodRestart for Domain: "
256260
+ domain.getDomainUid()
257261
+ " change securityContext:\n"
258262
+ " runAsUser: 1000\n"
259263
+ " fsGroup: 2000 ");
260-
domain.testDomainServerPodRestart(domainYaml);
264+
domain.verifyDomainServerPodRestart(domainYaml);
261265
domain.findServerPropertyChange("fsGroup: 2000", "admin-server");
262266
domain.findServerPropertyChange("fsGroup: 2000", "managed-server1");
263267

@@ -282,18 +286,19 @@ public void testServerPodsRestartByChangingResource() throws Exception {
282286
// firstly ensure that original domain.yaml doesn't include the property-to-be-addeded
283287
String domainFileName =
284288
BaseTest.getUserProjectsDir() + "/weblogic-domains/" + domainUid + "/domain.yaml";
285-
boolean result = TestUtils.checkFileIncludeProperty("cpu: 500m", domainFileName);
289+
boolean result =
290+
(new String(Files.readAllBytes(Paths.get(domainFileName)))).contains("cpu: 500m");
286291
Assert.assertFalse(result);
287292

288293
// domainYaml: the yaml file name with changed property under resources dir
289294
String domainYaml = "resource.domain.yaml";
290295

291296
logger.info(
292-
"About to testDomainServerPodRestart for Domain: "
297+
"About to verifyDomainServerPodRestart for Domain: "
293298
+ domain.getDomainUid()
294299
+ " change resource:\n"
295300
+ " cpu: 500m");
296-
domain.testDomainServerPodRestart(domainYaml);
301+
domain.verifyDomainServerPodRestart(domainYaml);
297302
domain.findServerPropertyChange("cpu: 500m", "admin-server");
298303
domain.findServerPropertyChange("cpu: 500m", "managed-server1");
299304

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

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ private void createPV() throws Exception {
878878
* @param newPropertyString
879879
* @throws Exception
880880
*/
881-
public void testDomainServerPodRestart(String oldPropertyString, String newPropertyString)
881+
public void verifyDomainServerPodRestart(String oldPropertyString, String newPropertyString)
882882
throws Exception {
883883
logger.info("Inside testDomainServerPodRestart");
884884
String content =
@@ -949,35 +949,22 @@ public void findServerPropertyChange(String changedProperty, String serverName)
949949
+ serverName
950950
+ " -o yaml -n "
951951
+ domainNS
952-
+ " > "
953-
+ outDir
954-
+ serverName
955-
+ ".yaml");
952+
+ "|"
953+
+ "grep "
954+
+ "\""
955+
+ changedProperty
956+
+ "\"");
956957
logger.info("kubectl execut with command: " + command.toString());
957958
TestUtils.exec(command.toString());
958959

959-
try {
960-
String content = new String(Files.readAllBytes(Paths.get(outDir + serverName + ".yaml")));
961-
boolean result = content.indexOf(changedProperty) >= 0;
962-
logger.info(
963-
"Server name: "
964-
+ serverName
965-
+ " Search result for "
966-
+ changedProperty
967-
+ " is: "
968-
+ result);
969-
if (!result) {
970-
throw new Exception(
971-
"FAILURE: didn't find the property: "
972-
+ changedProperty
973-
+ "in the "
974-
+ serverName
975-
+ ".yaml file");
976-
}
977-
} finally {
978-
String cmdString = "rm -rf " + outDir + serverName + ".yaml";
979-
TestUtils.exec(cmdString);
960+
String result = ((TestUtils.exec(command.toString())).stdout());
961+
logger.info(
962+
"in the method findServerPropertyChange, " + command.toString() + " return " + result);
963+
if (!result.contains(changedProperty)) {
964+
throw new Exception(
965+
"FAILURE: didn't find the property: " + changedProperty + " for the server" + serverName);
980966
}
967+
981968
logger.info("Done - findServerPropertyChange");
982969
}
983970

@@ -990,33 +977,39 @@ public void findServerPropertyChange(String changedProperty, String serverName)
990977
* @throws Exception - IOException when file is copied or errors occurred if the tested server is
991978
* not restarted
992979
*/
993-
public void testDomainServerPodRestart(String fileNameWithChangedProperty) throws Exception {
980+
public void verifyDomainServerPodRestart(String fileNameWithChangedProperty) throws Exception {
994981
logger.info("Inside testDomainServerPodRestart domainYamlWithChangedProperty");
995982

983+
String newDomainYamlFile =
984+
BaseTest.getUserProjectsDir() + "/weblogic-domains/" + domainUid + "/domain_new.yaml";
985+
String domainYamlFile =
986+
BaseTest.getUserProjectsDir() + "/weblogic-domains/" + domainUid + "/domain.yaml";
987+
String fileWithChangedProperty =
988+
BaseTest.getProjectRoot()
989+
+ "/integration-tests/src/test/resources/"
990+
+ fileNameWithChangedProperty;
991+
996992
// copy the original domain.yaml to domain_new.yaml
997-
TestUtils.copyFile(
998-
BaseTest.getUserProjectsDir() + "/weblogic-domains/" + domainUid + "/domain.yaml",
999-
BaseTest.getUserProjectsDir() + "/weblogic-domains/" + domainUid + "/domain_new.yaml");
993+
TestUtils.copyFile(domainYamlFile, newDomainYamlFile);
1000994

1001995
// append the file with changed property to the end of domain_new.yaml
1002-
TestUtils.concatFile(
1003-
BaseTest.getProjectRoot()
1004-
+ "/integration-tests/src/test/resources/"
1005-
+ fileNameWithChangedProperty,
1006-
BaseTest.getUserProjectsDir() + "/weblogic-domains/" + domainUid + "/domain_new.yaml");
996+
Files.write(
997+
Paths.get(newDomainYamlFile),
998+
Files.readAllBytes(Paths.get(fileWithChangedProperty)),
999+
StandardOpenOption.APPEND);
10071000

10081001
// kubectl apply the new constructed domain_new.yaml
1009-
TestUtils.kubectlapply(
1010-
BaseTest.getUserProjectsDir() + "/weblogic-domains/" + domainUid + "/domain_new.yaml");
1002+
StringBuffer command = new StringBuffer();
1003+
command.append("kubectl apply -f ").append(newDomainYamlFile);
1004+
logger.info("kubectl execut with command: " + command.toString());
1005+
TestUtils.exec(command.toString());
10111006

10121007
// verify the servers in the domain are being restarted in a sequence
10131008
verifyAdminServerRestarted();
10141009
verifyManagedServersRestarted();
10151010

10161011
// make domain.yaml include the new changed property
1017-
TestUtils.copyFile(
1018-
BaseTest.getUserProjectsDir() + "/weblogic-domains/" + domainUid + "/domain_new.yaml",
1019-
BaseTest.getUserProjectsDir() + "/weblogic-domains/" + domainUid + "/domain.yaml");
1012+
TestUtils.copyFile(newDomainYamlFile, domainYamlFile);
10201013

10211014
logger.info("Done - testDomainServerPodRestart with domainYamlWithChangedProperty");
10221015
}

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

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import java.io.FileInputStream;
1010
import java.io.FileReader;
1111
import java.io.FileWriter;
12-
import java.io.IOException;
1312
import java.io.InputStream;
1413
import java.nio.file.Files;
1514
import java.nio.file.Paths;
@@ -499,20 +498,6 @@ public static void kubectlexec(String podName, String namespace, String scriptPa
499498
}
500499
}
501500

502-
/**
503-
* The method to execute kubectl apply -f yamlFile.
504-
*
505-
* @param yamlFile - yaml file name
506-
* @throws Exception - errors occurred while execute kubectl apply
507-
*/
508-
public static void kubectlapply(String yamlFile) throws Exception {
509-
510-
StringBuffer command = new StringBuffer();
511-
command.append("kubectl apply -f ").append(yamlFile);
512-
logger.info("kubectl execut with command: " + command.toString());
513-
exec(command.toString());
514-
}
515-
516501
public static int makeOperatorPostRestCall(Operator operator, String url, String jsonObjStr)
517502
throws Exception {
518503
return makeOperatorRestCall(operator, url, jsonObjStr);
@@ -1203,53 +1188,4 @@ public static void copyFile(String fromFile, String toFile) throws Exception {
12031188
logger.info("Copying file from " + fromFile + " to " + toFile);
12041189
Files.copy(new File(fromFile).toPath(), Paths.get(toFile), StandardCopyOption.REPLACE_EXISTING);
12051190
}
1206-
1207-
/**
1208-
* Method to append source file to the end of target file.
1209-
*
1210-
* @param sourceFile - source file name
1211-
* @param targetFile - target file name
1212-
* @throws Exception - IOException during FileReader or FileWrite close call
1213-
*/
1214-
public static void concatFile(String sourceFile, String targetFile) throws IOException {
1215-
FileReader fr = null;
1216-
FileWriter fw = null;
1217-
try {
1218-
fr = new FileReader(sourceFile);
1219-
fw = new FileWriter(targetFile, true);
1220-
int c = fr.read();
1221-
while (c != -1) {
1222-
fw.write(c);
1223-
c = fr.read();
1224-
}
1225-
} catch (IOException e) {
1226-
e.printStackTrace();
1227-
} finally {
1228-
if (fr != null) {
1229-
fr.close();
1230-
}
1231-
if (fw != null) {
1232-
fw.close();
1233-
}
1234-
}
1235-
}
1236-
1237-
/**
1238-
* check whether a certain property(String)is included in the file
1239-
*
1240-
* @param property
1241-
* @param fileName
1242-
* @return result of above checking
1243-
*/
1244-
public static boolean checkFileIncludeProperty(String property, String fileName) {
1245-
boolean result = false;
1246-
try {
1247-
String content = new String(Files.readAllBytes(Paths.get(fileName)));
1248-
result = content.indexOf(property) >= 0;
1249-
logger.info("the result for checkFileIncludeProperty " + fileName + " result: " + result);
1250-
} catch (IOException e) {
1251-
e.printStackTrace();
1252-
}
1253-
return result;
1254-
}
12551191
}

0 commit comments

Comments
 (0)