Skip to content

Commit 709d8af

Browse files
committed
revise the code to supply only fragment of domain yaml file with added property
1 parent 640a71b commit 709d8af

File tree

8 files changed

+147
-319
lines changed

8 files changed

+147
-319
lines changed

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

Lines changed: 58 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -194,161 +194,108 @@ public void testServerPodsRestartByChangingZImage() throws Exception {
194194
/**
195195
* Modify/Add the containerSecurityContext section at ServerPod Level using kubectl apply -f
196196
* cont.security.context.domain.yaml. Verify all the pods re-started. The property tested is:
197-
* serverPod: containerSecurityContext: runAsUser: 1000 fsGroup: 2000
197+
* serverPod: containerSecurityContext: runAsUser: 1000 fsGroup: 1000.
198198
*
199-
* @throws Exception
199+
* @throws Exception - assertion fails due to unmatched value or errors occurred if tested servers
200+
* are not restarted or after restart the server yaml file doesn't include the new added
201+
* property
200202
*/
201203
@Test
202204
public void testServerPodsRestartByChangingContSecurityContext() throws Exception {
203205
Assume.assumeFalse(QUICKTEST);
204206
String testMethodName = new Object() {}.getClass().getEnclosingMethod().getName();
205207
logTestBegin(testMethodName);
206208

209+
// firstly ensure that original domain.yaml doesn't include the property-to-be-added
210+
String domainFileName =
211+
BaseTest.getUserProjectsDir() + "/weblogic-domains/" + domainUid + "/domain.yaml";
212+
boolean result = TestUtils.checkFileIncludeProperty("fsGroup: 1000", domainFileName);
213+
Assert.assertFalse(result);
214+
207215
// domainYaml: the yaml file name with changed property under resources dir
208216
String domainYaml = "cont.security.context.domain.yaml";
209-
try {
210-
logger.info(
211-
"About to testDomainServerPodRestart for Domain: "
212-
+ domain.getDomainUid()
213-
+ " change container securityContext:\n"
214-
+ " runAsUser: 1000\n"
215-
+ " fsGroup: 2000 ");
216-
domain.testDomainServerPodRestart(domainYaml);
217-
domain.findServerPropertyChange("runAsUser: 1000", "admin-server");
218-
domain.findServerPropertyChange("runAsUser: 1000", "managed-server1");
219-
} finally {
220-
// bring back the domain into previous state
221-
restoreDomain();
222-
}
217+
logger.info(
218+
"About to testDomainServerPodRestart for Domain: "
219+
+ domain.getDomainUid()
220+
+ " change container securityContext:\n"
221+
+ " runAsUser: 1000\n"
222+
+ " fsGroup: 1000 ");
223+
domain.testDomainServerPodRestart(domainYaml);
224+
domain.findServerPropertyChange("securityContext", "admin-server");
225+
domain.findServerPropertyChange("securityContext", "managed-server1");
223226

224227
logger.info("SUCCESS - " + testMethodName);
225228
}
226229

227230
/**
228231
* Modify/Add the podSecurityContext section at ServerPod level using kubectl apply -f
229232
* pod.security.context.domain.yaml. Verify all the pods re-started. The property tested is:
230-
* podSecurityContext: runAsUser: 1000 fsGroup: 2000
233+
* podSecurityContext: runAsUser: 1000 fsGroup: 2000.
231234
*
232-
* @throws Exception
235+
* @throws Exception - assertion fails due to unmatched value or errors occurred if tested servers
236+
* are not restarted or after restart the server yaml file doesn't include the new added
237+
* property
233238
*/
234239
@Test
235240
public void testServerPodsRestartByChangingPodSecurityContext() throws Exception {
236241
Assume.assumeFalse(QUICKTEST);
237242
String testMethodName = new Object() {}.getClass().getEnclosingMethod().getName();
238243
logTestBegin(testMethodName);
239244

245+
// firstly ensure that original domain.yaml doesn't include the property-to-be-added
246+
String domainFileName =
247+
BaseTest.getUserProjectsDir() + "/weblogic-domains/" + domainUid + "/domain.yaml";
248+
boolean result = TestUtils.checkFileIncludeProperty("fsGroup: 2000", domainFileName);
249+
Assert.assertFalse(result);
250+
240251
// domainYaml: the yaml file name with changed property under resources dir
241252
String domainYaml = "pod.security.context.domain.yaml";
242-
try {
243-
logger.info(
244-
"About to testDomainServerPodRestart for Domain: "
245-
+ domain.getDomainUid()
246-
+ " change securityContext:\n"
247-
+ " runAsUser: 1000\n"
248-
+ " fsGroup: 2000 ");
249-
domain.testDomainServerPodRestart(domainYaml);
250-
domain.findServerPropertyChange("fsGroup: 2000", "admin-server");
251-
domain.findServerPropertyChange("fsGroup: 2000", "managed-server1");
252-
} finally {
253-
// bring back the domain into previous State
254-
restoreDomain();
255-
}
256253

257-
logger.info("SUCCESS - " + testMethodName);
258-
}
259-
260-
/**
261-
* Modify/Add imagePullSecrets section at Domain Level using kubectl apply -f secret.domain.yaml.
262-
* Verify all pods re-started. The property tested is: imagePullSecrets:- name: imagePullSecr
263-
* value: myImagePullSecret
264-
*
265-
* @throws Exception
266-
*/
267-
@Test
268-
public void testServerPodsRestartByChangingImagePullSecrets() throws Exception {
269-
Assume.assumeFalse(QUICKTEST);
270-
String testMethodName = new Object() {}.getClass().getEnclosingMethod().getName();
271-
logTestBegin(testMethodName);
272-
273-
// domainYaml: the yaml file name with changed property under resources dir
274-
String domainYaml = "secret.domain.yaml";
275-
try {
276-
logger.info(
277-
"About to testDomainServerPodRestart for Domain: "
278-
+ domain.getDomainUid()
279-
+ " change imagePullSecrets:\n"
280-
+ " name: imagePullSecret\n"
281-
+ " value: myImagePullSecret ");
282-
domain.testDomainServerPodRestart(domainYaml);
283-
domain.findServerPropertyChange("myImagePullSecret", "admin-server");
284-
domain.findServerPropertyChange("myImagePullSecret", "managed-server1");
285-
} finally {
286-
// bring back the domain into previous state
287-
restoreDomain();
288-
}
254+
logger.info(
255+
"About to testDomainServerPodRestart for Domain: "
256+
+ domain.getDomainUid()
257+
+ " change securityContext:\n"
258+
+ " runAsUser: 1000\n"
259+
+ " fsGroup: 2000 ");
260+
domain.testDomainServerPodRestart(domainYaml);
261+
domain.findServerPropertyChange("fsGroup: 2000", "admin-server");
262+
domain.findServerPropertyChange("fsGroup: 2000", "managed-server1");
289263

290264
logger.info("SUCCESS - " + testMethodName);
291265
}
292266

293267
/**
294-
* Modify/Add resources at ServerPod level using kubectl apply -f resource.domain.yaml. Verify all
295-
* pods re-started. The property tested is: resources: limits: cpu: "1" requests: cpu: "0.5" args:
296-
* - -cpus - "2"
268+
* Modify/Add resources at ServerPod level using kubectl apply -f domain.yaml. Verify all pods
269+
* re-started. The property tested is: resources: limits: cpu: "1" requests: cpu: "0.5" args: -
270+
* -cpus - "2".
297271
*
298-
* @throws Exception
272+
* @throws Exception - assertion fails due to unmatched value or errors occurred if tested servers
273+
* are not restarted or after restart the server yaml file doesn't include the new added
274+
* property
299275
*/
300276
@Test
301277
public void testServerPodsRestartByChangingResource() throws Exception {
302278
Assume.assumeFalse(QUICKTEST);
303279
String testMethodName = new Object() {}.getClass().getEnclosingMethod().getName();
304280
logTestBegin(testMethodName);
305281

306-
// domainYaml: the yaml file name with changed property under resources dir
307-
String domainYaml = "resource.domain.yaml";
308-
try {
309-
logger.info(
310-
"About to testDomainServerPodRestart for Domain: "
311-
+ domain.getDomainUid()
312-
+ " change resource:\n"
313-
+ " cpu: 500m");
314-
domain.testDomainServerPodRestart(domainYaml);
315-
domain.findServerPropertyChange("cpu: 500m", "admin-server");
316-
domain.findServerPropertyChange("cpu: 500m", "managed-server1");
317-
} finally {
318-
// bring back the domain into previous state
319-
restoreDomain();
320-
}
321-
322-
logger.info("SUCCESS - " + testMethodName);
323-
}
324-
325-
/**
326-
* Modify the annotations at ServerPod level using kubectl apply -f annotation.domain.yaml. Verify
327-
* all pods re-started. The property tested is: annotations: "TSTAMP" --> "DATETIME"
328-
*
329-
* @throws Exception
330-
*/
331-
@Test
332-
public void testServerPodsRestartByChangingAnnotation() throws Exception {
333-
Assume.assumeFalse(QUICKTEST);
334-
String testMethodName = new Object() {}.getClass().getEnclosingMethod().getName();
335-
logTestBegin(testMethodName);
282+
// firstly ensure that original domain.yaml doesn't include the property-to-be-addeded
283+
String domainFileName =
284+
BaseTest.getUserProjectsDir() + "/weblogic-domains/" + domainUid + "/domain.yaml";
285+
boolean result = TestUtils.checkFileIncludeProperty("cpu: 500m", domainFileName);
286+
Assert.assertFalse(result);
336287

337288
// domainYaml: the yaml file name with changed property under resources dir
338-
String domainYaml = "annotation.domain.yaml";
339-
try {
340-
logger.info(
341-
"About to testDomainServerPodRestart for Domain: "
342-
+ domain.getDomainUid()
343-
+ " change annotation"
344-
+ " annotations:\n"
345-
+ " custom: \"DATETIME\"");
346-
domain.testDomainServerPodRestart(domainYaml);
289+
String domainYaml = "resource.domain.yaml";
347290

348-
} finally {
349-
// bring back the domain into previous state
350-
restoreDomain();
351-
}
291+
logger.info(
292+
"About to testDomainServerPodRestart for Domain: "
293+
+ domain.getDomainUid()
294+
+ " change resource:\n"
295+
+ " cpu: 500m");
296+
domain.testDomainServerPodRestart(domainYaml);
297+
domain.findServerPropertyChange("cpu: 500m", "admin-server");
298+
domain.findServerPropertyChange("cpu: 500m", "managed-server1");
352299

353300
logger.info("SUCCESS - " + testMethodName);
354301
}
@@ -373,12 +320,4 @@ private static void destroyPodsRestartdomain() throws Exception {
373320
domain.destroy();
374321
}
375322
}
376-
377-
private static void restoreDomain() throws Exception {
378-
String yamlFile =
379-
BaseTest.getUserProjectsDir() + "/weblogic-domains/" + domainUid + "/domain.yaml";
380-
TestUtils.kubectlapply(yamlFile);
381-
domain.verifyAdminServerRestarted();
382-
domain.verifyManagedServersRestarted();
383-
}
384323
}

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

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -981,23 +981,42 @@ public void findServerPropertyChange(String changedProperty, String serverName)
981981
}
982982

983983
/**
984-
* verify domain server pods get restarted after the property change by kubectl apply -f
985-
* newDomainYamlfileWithChangedProperty
984+
* verify domain server pods get restarted after the property change by kubectl apply -f new
985+
* domain yaml file with added/changed property
986986
*
987-
* @param fileNameWithChangedProperty
988-
* @throws Exception
987+
* @param fileNameWithChangedProperty - the fragment of domain yaml file with new added property
988+
* change
989+
* @throws Exception - IOException when file is copied or errors occurred if the tested server is
990+
* not restarted
989991
*/
990992
public void testDomainServerPodRestart(String fileNameWithChangedProperty) throws Exception {
991993
logger.info("Inside testDomainServerPodRestart domainYamlWithChangedProperty");
992994

993-
// kubectl apply the supplied domain yaml file under resources dir with changed property
994-
String yamlDir = BaseTest.getProjectRoot() + "/integration-tests/src/test/resources/";
995-
TestUtils.kubectlapply(yamlDir + fileNameWithChangedProperty);
995+
// copy the original domain.yaml to domain_new.yaml
996+
TestUtils.copyFile(
997+
BaseTest.getUserProjectsDir() + "/weblogic-domains/" + domainUid + "/domain.yaml",
998+
BaseTest.getUserProjectsDir() + "/weblogic-domains/" + domainUid + "/domain_new.yaml");
999+
1000+
// append the file with changed property to the end of domain_new.yaml
1001+
TestUtils.concatFile(
1002+
BaseTest.getProjectRoot()
1003+
+ "/integration-tests/src/test/resources/"
1004+
+ fileNameWithChangedProperty,
1005+
BaseTest.getUserProjectsDir() + "/weblogic-domains/" + domainUid + "/domain_new.yaml");
1006+
1007+
// kubectl apply the new constructed domain_new.yaml
1008+
TestUtils.kubectlapply(
1009+
BaseTest.getUserProjectsDir() + "/weblogic-domains/" + domainUid + "/domain_new.yaml");
9961010

9971011
// verify the servers in the domain are being restarted in a sequence
9981012
verifyAdminServerRestarted();
9991013
verifyManagedServersRestarted();
10001014

1015+
// make domain.yaml include the new changed property
1016+
TestUtils.copyFile(
1017+
BaseTest.getUserProjectsDir() + "/weblogic-domains/" + domainUid + "/domain_new.yaml",
1018+
BaseTest.getUserProjectsDir() + "/weblogic-domains/" + domainUid + "/domain.yaml");
1019+
10011020
logger.info("Done - testDomainServerPodRestart with domainYamlWithChangedProperty");
10021021
}
10031022

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.io.FileInputStream;
1010
import java.io.FileReader;
1111
import java.io.FileWriter;
12+
import java.io.IOException;
1213
import java.io.InputStream;
1314
import java.nio.file.Files;
1415
import java.nio.file.Paths;
@@ -1202,4 +1203,53 @@ public static void copyFile(String fromFile, String toFile) throws Exception {
12021203
logger.info("Copying file from " + fromFile + " to " + toFile);
12031204
Files.copy(new File(fromFile).toPath(), Paths.get(toFile), StandardCopyOption.REPLACE_EXISTING);
12041205
}
1206+
1207+
/**
1208+
* 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
1213+
*/
1214+
public static void concatFile(String sourceFile, String targetFile) throws Exception {
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+
}
12051255
}

integration-tests/src/test/resources/annotation.domain.yaml

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)