Skip to content

Commit c3cc268

Browse files
committed
first imagetool test
1 parent f35b4a2 commit c3cc268

File tree

6 files changed

+334
-2
lines changed

6 files changed

+334
-2
lines changed

imagetool/pom.xml

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
<artifactId>junit</artifactId>
4040
<scope>test</scope>
4141
</dependency>
42+
<dependency>
43+
<groupId>com.google.guava</groupId>
44+
<artifactId>guava</artifactId>
45+
<scope>test</scope>
46+
</dependency>
4247
</dependencies>
4348

4449
<build>
@@ -121,6 +126,34 @@
121126
<groupId>org.apache.maven.plugins</groupId>
122127
<artifactId>maven-checkstyle-plugin</artifactId>
123128
</plugin>
129+
<plugin>
130+
<groupId>org.apache.maven.plugins</groupId>
131+
<artifactId>maven-failsafe-plugin</artifactId>
132+
<version>3.0.0-M3</version>
133+
<configuration>
134+
<encoding>UTF-8</encoding>
135+
<failIfNoTests>false</failIfNoTests>
136+
<argLine>
137+
-Xmx1024m -XX:MaxPermSize=256m
138+
</argLine>
139+
</configuration>
140+
<executions>
141+
<execution>
142+
<id>integration-tests</id>
143+
<goals>
144+
<goal>integration-test</goal>
145+
<goal>verify</goal>
146+
</goals>
147+
<phase>integration-test</phase>
148+
<configuration>
149+
<systemPropertyVariables>
150+
<java.util.logging.SimpleFormatter.format>%1$tm-%1$td-%1$tY %1$tH:%1$tM:%1$tS %4$-6s %2$s %5$s%6$s%n</java.util.logging.SimpleFormatter.format>
151+
</systemPropertyVariables>
152+
<trimStackTrace>false</trimStackTrace>
153+
</configuration>
154+
</execution>
155+
</executions>
156+
</plugin>
124157
</plugins>
125158
</build>
126-
</project>
159+
</project>
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package com.oracle.weblogic.imagetool.integration;
2+
3+
import com.oracle.weblogic.imagetool.integration.utils.ExecCommand;
4+
import com.oracle.weblogic.imagetool.integration.utils.ExecResult;
5+
6+
import java.io.File;
7+
import java.util.logging.Logger;
8+
9+
public class BaseTest {
10+
11+
12+
protected static final Logger logger = Logger.getLogger(ITImagetool.class.getName());
13+
protected static final String VERSION = "1.0.1";
14+
protected static final String PS = File.pathSeparator;
15+
protected static final String FS = File.separator;
16+
17+
private static String projectRoot = "";
18+
protected static String wlsImgBldDir = "";
19+
protected static String wlsImgCacheDir = "";
20+
protected static String imagetool = "";
21+
private static String imagetoolZipfile = "";
22+
23+
24+
protected static void initialize() throws Exception {
25+
projectRoot = System.getProperty("user.dir");
26+
27+
if(System.getenv("WLSIMG_BLDDIR") != null) {
28+
wlsImgBldDir = System.getenv("WLSIMG_BLDDIR");
29+
} else {
30+
wlsImgBldDir = System.getenv("HOME");
31+
}
32+
if(System.getenv("WLSIMG_CACHEDIR") != null) {
33+
wlsImgCacheDir = System.getenv("WLSIMG_CACHEDIR");
34+
} else {
35+
wlsImgCacheDir = System.getenv("HOME") + FS + "cache";
36+
}
37+
38+
imagetoolZipfile = "imagetool-" + VERSION + "-SNAPSHOT.zip";
39+
40+
imagetool = "java -cp " + getImagetoolHome() + FS + "lib" + FS + "imagetool.jar" + PS +
41+
getImagetoolHome() + FS + "lib" + FS + "* -Djava.util.logging.config.file=" +
42+
getImagetoolHome() + FS + "bin" + FS + "logging.properties com.oracle.weblogic.imagetool.cli.CLIDriver";
43+
44+
logger.info("DEBUG: WLSIMG_BLDDIR=" + wlsImgBldDir);
45+
logger.info("DEBUG: WLSIMG_CACHEDIR=" + wlsImgCacheDir);
46+
logger.info("DEBUG: imagetool=" + imagetool);
47+
}
48+
49+
protected static void setup() throws Exception {
50+
51+
// unzip the weblogic-image-tool/imagetool/target/imagetool-${VERSION}-SNAPSHOT.zip
52+
ExecCommand.exec("/bin/rm -rf " + getImagetoolHome());
53+
ExecCommand.exec("/bin/unzip " + getTargetDir() + FS + imagetoolZipfile);
54+
logger.info("running script " + getImagetoolHome() + FS + "bin" + FS + "setup.sh" );
55+
ExecResult result = ExecCommand.exec("source " + getImagetoolHome() + FS + "bin" + FS + "setup.sh");
56+
logger.info("DEBUG: running setup.sh ..." );
57+
logger.info(result.stderr());
58+
59+
//result = ExecCommand.exec(imagetool);
60+
//logger.info("DEBUG: running imagetool");
61+
//logger.info(result.stdout());
62+
}
63+
64+
protected static String getProjectRoot() {
65+
return projectRoot;
66+
}
67+
68+
protected static String getTargetDir() {
69+
return getProjectRoot() + FS + "target";
70+
}
71+
72+
protected static String getImagetoolHome() {
73+
return getProjectRoot() + FS + "imagetool-" + VERSION + "-SNAPSHOT";
74+
}
75+
76+
protected void verifyResult(ExecResult result, String matchString) throws Exception {
77+
if(result.exitValue() != 0 || !result.stdout().contains(matchString)) {
78+
throw new Exception("verifying test result failed.");
79+
}
80+
}
81+
82+
protected void logTestBegin(String testMethodName) throws Exception {
83+
logger.info("=======================================");
84+
logger.info("BEGIN test " + testMethodName + " ...");
85+
}
86+
87+
protected void logTestEnd(String testMethodName) throws Exception {
88+
logger.info("SUCCESS - " + testMethodName);
89+
logger.info("=======================================");
90+
}
91+
92+
protected ExecResult listItemsInCache() throws Exception {
93+
String command = imagetool + " cache listItems";
94+
logger.info("executing command: " + command);
95+
ExecResult result = ExecCommand.exec(command);
96+
logger.info(result.stdout());
97+
return result;
98+
}
99+
100+
protected ExecResult addInstallerToCache(String type, String version, String path) throws Exception {
101+
String command = imagetool + " cache addInstaller --type " + type + " --version " + version +
102+
" --path " + path;
103+
logger.info("executing command: " + command);
104+
ExecResult result = ExecCommand.exec(command);
105+
logger.info(result.stdout());
106+
return result;
107+
}
108+
109+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.oracle.weblogic.imagetool.integration;
2+
3+
import com.oracle.weblogic.imagetool.integration.utils.ExecCommand;
4+
import com.oracle.weblogic.imagetool.integration.utils.ExecResult;
5+
import org.junit.AfterClass;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
import org.junit.FixMethodOrder;
9+
import org.junit.runners.MethodSorters;
10+
11+
12+
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
13+
public class ITImagetool extends BaseTest {
14+
15+
16+
17+
@BeforeClass
18+
public static void staticPrepare() throws Exception {
19+
logger.info("prepare for image tool test ...");
20+
initialize();
21+
setup();
22+
}
23+
24+
@AfterClass
25+
public static void staticUnprepare() throws Exception {
26+
logger.info("cleaning up after the test ...");
27+
}
28+
29+
@Test
30+
public void test1CacheListItems() throws Exception {
31+
String testMethodName = new Object() {}.getClass().getEnclosingMethod().getName();
32+
logTestBegin(testMethodName);
33+
34+
ExecResult result = listItemsInCache();
35+
36+
// verify the test result
37+
String expectedString = "cache.dir=" + wlsImgCacheDir;
38+
verifyResult(result, expectedString);
39+
40+
logTestEnd(testMethodName);
41+
}
42+
43+
@Test
44+
public void test2CacheAddInstallerJDK() throws Exception {
45+
String testMethodName = new Object() {}.getClass().getEnclosingMethod().getName();
46+
logTestBegin(testMethodName);
47+
48+
String jdkPath = getProjectRoot() + FS + ".." + FS + "caches" + FS + "jdk-8u212-linux-x64.tar.gz";
49+
ExecResult result = addInstallerToCache("jdk", "8u212", jdkPath);
50+
logger.info(result.stderr());
51+
52+
result = listItemsInCache();
53+
String expectedString = "jdk_8u212=" + jdkPath;
54+
verifyResult(result, expectedString);
55+
56+
logTestEnd(testMethodName);
57+
}
58+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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 com.oracle.weblogic.imagetool.integration.utils;
6+
7+
import com.google.common.io.ByteStreams;
8+
import java.io.*;
9+
import java.util.stream.Collectors;
10+
11+
/** Class for executing shell commands from java */
12+
public class ExecCommand {
13+
14+
public static ExecResult exec(String command) throws Exception {
15+
return exec(command, false);
16+
}
17+
18+
public static ExecResult exec(String command, boolean isRedirectToOut) throws Exception {
19+
Process p = Runtime.getRuntime().exec(new String[] {"/bin/sh", "-c", command});
20+
21+
InputStreamWrapper in = new SimpleInputStreamWrapper(p.getInputStream());
22+
Thread out = null;
23+
24+
try {
25+
if (isRedirectToOut) {
26+
InputStream i = in.getInputStream();
27+
@SuppressWarnings("resource")
28+
CopyingOutputStream copyOut = new CopyingOutputStream(System.out);
29+
// this makes sense because CopyingOutputStream is an InputStreamWrapper
30+
in = copyOut;
31+
out =
32+
new Thread(
33+
new Runnable() {
34+
public void run() {
35+
try {
36+
ByteStreams.copy(i, copyOut);
37+
} catch (IOException ex) {
38+
ex.printStackTrace();
39+
}
40+
}
41+
});
42+
out.start();
43+
}
44+
45+
p.waitFor();
46+
return new ExecResult(p.exitValue(), read(in.getInputStream()), read(p.getErrorStream()));
47+
} finally {
48+
if (out != null) {
49+
out.join();
50+
}
51+
p.destroy();
52+
}
53+
}
54+
55+
private static String read(InputStream is) throws Exception {
56+
return new BufferedReader(new InputStreamReader(is)).lines().collect(Collectors.joining("\n"));
57+
}
58+
59+
private interface InputStreamWrapper {
60+
InputStream getInputStream();
61+
}
62+
63+
private static class SimpleInputStreamWrapper implements InputStreamWrapper {
64+
final InputStream in;
65+
66+
SimpleInputStreamWrapper(InputStream in) {
67+
this.in = in;
68+
}
69+
70+
@Override
71+
public InputStream getInputStream() {
72+
return in;
73+
}
74+
}
75+
76+
private static class CopyingOutputStream extends OutputStream implements InputStreamWrapper {
77+
final OutputStream out;
78+
final ByteArrayOutputStream copy = new ByteArrayOutputStream();
79+
80+
CopyingOutputStream(OutputStream out) {
81+
this.out = out;
82+
}
83+
84+
@Override
85+
public void write(int b) throws IOException {
86+
out.write(b);
87+
copy.write(b);
88+
}
89+
90+
@Override
91+
public InputStream getInputStream() {
92+
return new ByteArrayInputStream(copy.toByteArray());
93+
}
94+
}
95+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 com.oracle.weblogic.imagetool.integration.utils;
6+
7+
/**
8+
* Class that holds the results of using java to exec a command (i.e. exit value, stdout and stderr)
9+
*/
10+
public class ExecResult {
11+
private int exitValue;
12+
private String stdout;
13+
private String stderr;
14+
15+
public ExecResult(int exitValue, String stdout, String stderr) throws Exception {
16+
this.exitValue = exitValue;
17+
this.stdout = stdout;
18+
this.stderr = stderr;
19+
}
20+
21+
public int exitValue() {
22+
return this.exitValue;
23+
}
24+
25+
public String stdout() {
26+
return this.stdout;
27+
}
28+
29+
public String stderr() {
30+
return this.stderr;
31+
}
32+
}

pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@
7171
<artifactId>junit</artifactId>
7272
<version>4.12</version>
7373
</dependency>
74+
<dependency>
75+
<groupId>com.google.guava</groupId>
76+
<artifactId>guava</artifactId>
77+
<version>19.0</version>
78+
</dependency>
7479
</dependencies>
7580
</dependencyManagement>
7681

@@ -211,4 +216,4 @@
211216
</pluginManagement>
212217
</build>
213218

214-
</project>
219+
</project>

0 commit comments

Comments
 (0)