Skip to content

Commit 3dd1f91

Browse files
xiancaoddsharpe
authored andcommitted
Test4additionalcommands (#92)
* Test for new feature, additional build commands allow users to inject custom commands into the Docker build step * add verification code * use build_tag for imagename
1 parent 6866363 commit 3dd1f91

File tree

3 files changed

+120
-19
lines changed

3 files changed

+120
-19
lines changed

imagetool/src/test/java/com/oracle/weblogic/imagetool/integration/BaseTest.java

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class BaseTest {
3232
private static final String IMAGETOOLDIR = "imagetool";
3333
private static final String INSTALLERCACHEDIR = "/scratch/artifacts/imagetool";
3434
protected static String build_tag = "";
35+
protected static final String WDT_MODEL1 = "simple-topology1.yaml";
3536

3637
protected static void initialize() throws Exception {
3738
logger.info("Initializing the tests ...");
@@ -52,6 +53,7 @@ protected static void initialize() throws Exception {
5253
imagetool = "java -cp \"" + getImagetoolHome() + FS + "lib" + FS + "*\" -Djava.util.logging.config.file="
5354
+ getImagetoolHome() + FS + "bin" + FS + "logging.properties com.oracle.weblogic.imagetool.cli.CLIDriver";
5455

56+
// get the build tag from Jenkins build environment variable BUILD_TAG
5557
build_tag = System.getenv("BUILD_TAG");
5658
if (build_tag != null) {
5759
build_tag = build_tag.toLowerCase();
@@ -95,16 +97,18 @@ protected static void cleanup() throws Exception {
9597
// clean up the docker images
9698
command = "docker stop " + dbContainerName;
9799
executeNoVerify(command);
98-
command = "docker rmi -f " + BASE_OS_IMG + ":" + BASE_OS_IMG_TAG + " " + ORACLE_DB_IMG + ":"
99-
+ ORACLE_DB_IMG_TAG;
100-
executeNoVerify(command);
100+
// command = "docker rmi -f " + BASE_OS_IMG + ":" + BASE_OS_IMG_TAG + " " + ORACLE_DB_IMG + ":"
101+
// + ORACLE_DB_IMG_TAG;
102+
// executeNoVerify(command);
101103

102104
command = "docker rmi -f $(docker images | grep " + build_tag + " | tr -s ' ' | cut -d ' ' -f 3)";
103105
executeNoVerify(command);
104106

105107
// clean up the possible left over wlsimgbuilder_temp*
106108
command = "rm -rf " + wlsImgBldDir + FS + "wlsimgbuilder_temp*";
107109
executeNoVerify(command);
110+
command = "rm -rf " + wlsImgBldDir + FS + WDT_MODEL1;
111+
executeNoVerify(command);
108112
}
109113

110114
protected static void pullBaseOSDockerImage() throws Exception {
@@ -162,6 +166,10 @@ protected static String getWDTResourcePath() {
162166
return getProjectRoot() + FS + "src" + FS + "test" + FS + "resources" + FS + "wdt";
163167
}
164168

169+
protected static String getABCResourcePath() {
170+
return getProjectRoot() + FS + "src" + FS + "test" + FS + "resources" + FS + "additionalBuildCommands";
171+
}
172+
165173
protected static void executeNoVerify(String command) throws Exception {
166174
logger.info("executing command: " + command);
167175
ExecCommand.exec(command);
@@ -175,7 +183,8 @@ protected void verifyResult(ExecResult result, String matchString) throws Except
175183

176184
protected void verifyExitValue(ExecResult result, String command) throws Exception {
177185
if (result.exitValue() != 0) {
178-
logger.info(result.stderr());
186+
logger.info("DEBUG: result.exitValue=" + result.exitValue());
187+
logger.info("DEBUG: result.stderr=" + result.stderr());
179188
throw new Exception("executing the following command failed: " + command);
180189
}
181190
}
@@ -189,6 +198,27 @@ protected void verifyDockerImages(String imageTag) throws Exception {
189198
}
190199
}
191200

201+
protected void verifyFileInImage(String imagename, String filename, String expectedContent) throws Exception {
202+
logger.info("verifying the file content in image");
203+
String command = "docker run --rm " + imagename + " bash -c 'cat " + filename + "'";
204+
logger.info("executing command: " + command);
205+
ExecResult result = ExecCommand.exec(command);
206+
if (!result.stdout().contains(expectedContent)) {
207+
logger.info("DEBUG: result.stdout=" + result.stdout());
208+
logger.info("DEBUG: result.stderr=" + result.stderr());
209+
throw new Exception("The image " + imagename + " does not have the expected file content: "
210+
+ expectedContent);
211+
}
212+
}
213+
214+
protected void verifyLabelInImage(String imagename, String label) throws Exception {
215+
ExecResult result = ExecCommand.exec("docker inspect --format '{{ index .Config.Labels}}' "
216+
+ imagename);
217+
if (!result.stdout().contains(label)) {
218+
throw new Exception("The image " + imagename + " does not contain the expected label " + label);
219+
}
220+
}
221+
192222
protected void logTestBegin(String testMethodName) throws Exception {
193223
logger.info("=======================================");
194224
logger.info("BEGIN test " + testMethodName + " ...");

imagetool/src/test/java/com/oracle/weblogic/imagetool/integration/ITImagetool.java

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public class ITImagetool extends BaseTest {
4141
private static final String WDT_ARCHIVE = "archive.zip";
4242
private static final String WDT_VARIABLES = "domain.properties";
4343
private static final String WDT_MODEL = "simple-topology.yaml";
44-
private static final String WDT_MODEL1 = "simple-topology1.yaml";
4544
private static final String WDT_MODEL2 = "simple-topology2.yaml";
4645
private static String oracleSupportUsername;
4746
private static String oracleSupportPassword;
@@ -143,7 +142,7 @@ public void test3CacheAddInstallerWLS() throws Exception {
143142
/**
144143
* create a WLS image with default WLS version.
145144
*
146-
* @throws Exception
145+
* @throws Exception - if any error occurs
147146
*/
148147
@Test
149148
public void test4CreateWLSImg() throws Exception {
@@ -153,9 +152,8 @@ public void test4CreateWLSImg() throws Exception {
153152
String command = imagetool + " create --jdkVersion=" + JDK_VERSION + " --tag "
154153
+ build_tag + ":" + testMethodName;
155154
logger.info("Executing command: " + command);
156-
ExecResult result = ExecCommand.exec(command);
157-
logger.info("DEBUG: result.stdout=" + result.stdout());
158-
logger.info("DEBUG: result.stderr=" + result.stderr());
155+
ExecResult result = ExecCommand.exec(command, true);
156+
verifyExitValue(result, command);
159157

160158
// verify the docker image is created
161159
verifyDockerImages(testMethodName);
@@ -247,7 +245,8 @@ public void test8CreateWLSImgUseCache() throws Exception {
247245
+ BASE_OS_IMG + ":" + BASE_OS_IMG_TAG + " --tag " + build_tag + ":" + testMethodName
248246
+ " --version " + WLS_VERSION;
249247
logger.info("Executing command: " + command);
250-
ExecCommand.exec(command, true);
248+
ExecResult result = ExecCommand.exec(command, true);
249+
verifyExitValue(result, command);
251250

252251
// verify the docker image is created
253252
verifyDockerImages(testMethodName);
@@ -265,10 +264,11 @@ public void test9UpdateWLSImg() throws Exception {
265264
String testMethodName = new Object() {}.getClass().getEnclosingMethod().getName();
266265
logTestBegin(testMethodName);
267266

268-
String command = imagetool + " update --fromImage imagetool:test8CreateWLSImgUseCache --tag "
267+
String command = imagetool + " update --fromImage " + build_tag + ":test8CreateWLSImgUseCache --tag "
269268
+ build_tag + ":" + testMethodName + " --patches " + P27342434_ID;
270269
logger.info("Executing command: " + command);
271-
ExecCommand.exec(command, true);
270+
ExecResult result = ExecCommand.exec(command, true);
271+
verifyExitValue(result, command);
272272

273273
// verify the docker image is created
274274
verifyDockerImages(testMethodName);
@@ -329,7 +329,8 @@ public void testACreateWLSImgUsingWDT() throws Exception {
329329
+ wdtModel + " --wdtVariables " + wdtVariables;
330330

331331
logger.info("Executing command: " + command);
332-
ExecCommand.exec(command, true);
332+
ExecResult result = ExecCommand.exec(command, true);
333+
verifyExitValue(result, command);
333334

334335
// verify the docker image is created
335336
verifyDockerImages(testMethodName);
@@ -363,7 +364,8 @@ public void testBCreateFMWImgFullInternetAccess() throws Exception {
363364
String command = imagetool + " create --version=" + WLS_VERSION + " --tag " + build_tag + ":" + testMethodName
364365
+ " --latestPSU --user " + oracleSupportUsername + " --passwordEnv ORACLE_SUPPORT_PASSWORD --type fmw";
365366
logger.info("Executing command: " + command);
366-
ExecCommand.exec(command, true);
367+
ExecResult result = ExecCommand.exec(command, true);
368+
verifyExitValue(result, command);
367369

368370
// verify the docker image is created
369371
verifyDockerImages(testMethodName);
@@ -399,7 +401,8 @@ public void testCCreateFMWImgNonDefault() throws Exception {
399401
String command = imagetool + " create --jdkVersion " + JDK_VERSION_8u212 + " --version=" + WLS_VERSION_1221
400402
+ " --tag " + build_tag + ":" + testMethodName + " --patches " + P22987840_ID + " --type fmw";
401403
logger.info("Executing command: " + command);
402-
ExecCommand.exec(command, true);
404+
ExecResult result = ExecCommand.exec(command, true);
405+
verifyExitValue(result, command);
403406

404407
// verify the docker image is created
405408
verifyDockerImages(testMethodName);
@@ -444,7 +447,7 @@ public void testDCreateJRFDomainImgUsingWDT() throws Exception {
444447

445448
String wdtArchive = getWDTResourcePath() + FS + WDT_ARCHIVE;
446449
String wdtModel = getWDTResourcePath() + FS + WDT_MODEL1;
447-
String tmpWdtModel = System.getProperty("java.io.tmpdir") + FS + WDT_MODEL1;
450+
String tmpWdtModel = wlsImgBldDir + FS + WDT_MODEL1;
448451

449452
// update wdt model file
450453
Path source = Paths.get(wdtModel);
@@ -463,7 +466,8 @@ public void testDCreateJRFDomainImgUsingWDT() throws Exception {
463466
+ tmpWdtModel + " --wdtDomainType JRF --wdtRunRCU --type fmw";
464467

465468
logger.info("Executing command: " + command);
466-
ExecCommand.exec(command, true);
469+
ExecResult result = ExecCommand.exec(command, true);
470+
verifyExitValue(result, command);
467471

468472
// verify the docker image is created
469473
verifyDockerImages(testMethodName);
@@ -513,14 +517,20 @@ public void testECreateRestricedJRFDomainImgUsingWDT() throws Exception {
513517
+ wdtModel + " --wdtDomainType RestrictedJRF --type fmw --wdtVariables " + wdtVariables;
514518

515519
logger.info("Executing command: " + command);
516-
ExecCommand.exec(command, true);
520+
ExecResult result = ExecCommand.exec(command, true);
521+
verifyExitValue(result, command);
517522

518523
// verify the docker image is created
519524
verifyDockerImages(testMethodName);
520525

521526
logTestEnd(testMethodName);
522527
}
523528

529+
/**
530+
* create wls image using multiple WDT model files
531+
*
532+
* @throws Exception - if any error occurs
533+
*/
524534
@Test
525535
public void testFCreateWLSImgUsingMultiModels() throws Exception {
526536
String testMethodName = new Object() {}.getClass().getEnclosingMethod().getName();
@@ -564,11 +574,47 @@ public void testFCreateWLSImgUsingMultiModels() throws Exception {
564574
+ wdtModel + "," + wdtModel2 + " --wdtVariables " + wdtVariables;
565575

566576
logger.info("Executing command: " + command);
567-
ExecCommand.exec(command, true);
577+
ExecResult result = ExecCommand.exec(command, true);
578+
verifyExitValue(result, command);
568579

569580
// verify the docker image is created
570581
verifyDockerImages(testMethodName);
571582

572583
logTestEnd(testMethodName);
573584
}
585+
586+
/**
587+
* create WLS image with additional build commands
588+
*
589+
* @throws Exception - if any error occurs
590+
*/
591+
@Test
592+
public void testGCreateWLSImgWithAdditionalBuildCommands() throws Exception {
593+
String testMethodName = new Object() {}.getClass().getEnclosingMethod().getName();
594+
logTestBegin(testMethodName);
595+
596+
String imagename = build_tag + ":" + testMethodName;
597+
String abcPath = getABCResourcePath() + FS + "multi-sections.txt";
598+
String command = imagetool + " create --jdkVersion=" + JDK_VERSION + " --tag "
599+
+ imagename + " --additionalBuildCommands " + abcPath;
600+
logger.info("Executing command: " + command);
601+
ExecResult result = ExecCommand.exec(command, true);
602+
verifyExitValue(result, command);
603+
604+
// verify the docker image is created
605+
verifyDockerImages(testMethodName);
606+
607+
// verify the file created in [before-jdk-install] section
608+
verifyFileInImage(imagename, "/u01/jdk/beforeJDKInstall.txt", "before-jdk-install");
609+
// verify the file created in [after-jdk-install] section
610+
verifyFileInImage(imagename, "/u01/jdk/afterJDKInstall.txt", "after-jdk-install");
611+
// verify the file created in [before-fmw-install] section
612+
verifyFileInImage(imagename, "/u01/oracle/beforeFMWInstall.txt", "before-fmw-install");
613+
// verify the file created in [after-fmw-install] section
614+
verifyFileInImage(imagename, "/u01/oracle/afterFMWInstall.txt", "after-fmw-install");
615+
// verify the label is created as in [final-build-commands] section
616+
verifyLabelInImage(imagename, "final-build-commands:finalBuildCommands");
617+
618+
logTestEnd(testMethodName);
619+
}
574620
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 http://oss.oracle.com/licenses/upl.
3+
4+
[before-jdk-install]
5+
RUN echo "before-jdk-install" > /u01/beforeJDKInstall.txt
6+
7+
[after-jdk-install]
8+
RUN echo "after-jdk-install" > /u01/afterJDKInstall.txt
9+
10+
[before-fmw-install]
11+
RUN echo "before-fmw-install" > /u01/beforeFMWInstall.txt
12+
13+
[after-fmw-install]
14+
RUN echo "after-fmw-install" > /u01/afterFMWInstall.txt
15+
16+
[before-wdt-command]
17+
LABEL before-wdt-command="beforeWDTComamnd"
18+
19+
[after-wdt-command]
20+
LABEL after-wdt-command="afterWDTCommand"
21+
22+
[final-build-commands]
23+
LABEL final-build-commands="finalBuildCommands"
24+
COPY --from=JDK_BUILD --chown=oracle:oracle /u01/beforeJDKInstall.txt /u01/afterJDKInstall.txt /u01/jdk/
25+
COPY --from=WLS_BUILD --chown=oracle:oracle /u01/beforeFMWInstall.txt /u01/afterFMWInstall.txt /u01/oracle/

0 commit comments

Comments
 (0)