Skip to content

Commit

Permalink
Tests: Support running release tests outside of Docker container. #108
Browse files Browse the repository at this point in the history
  • Loading branch information
smarkwal committed Jan 10, 2022
1 parent 503b0ce commit 198c68c
Show file tree
Hide file tree
Showing 10 changed files with 527 additions and 228 deletions.
237 changes: 98 additions & 139 deletions src/releaseTest/java/org/jarhc/test/release/JarHcTest.java

Large diffs are not rendered by default.

56 changes: 9 additions & 47 deletions src/releaseTest/java/org/jarhc/test/release/ReleaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.assertj.core.api.Assertions;
import org.jarhc.test.release.utils.JavaContainer;
import org.jarhc.test.release.utils.JavaImage;

abstract class ReleaseTest {

Expand All @@ -41,8 +39,8 @@ abstract class ReleaseTest {

private void findProjectFiles() {

// get current working directory
projectDir = new File(".").getAbsoluteFile();
// TODO: check if current directory is project dir

// read JarHC version from VERSION file (if it exists)
String versionFilePath = "build/resources/main/VERSION";
Expand All @@ -51,14 +49,14 @@ private void findProjectFiles() {

}

String getDependencies(String configuration) {
protected String getDependencies(String configuration) {
File file = getProjectFile("build/configurations.properties");
try {
Properties properties = new Properties();
properties.load(new FileInputStream(file));
return properties.getProperty(configuration, "");
} catch (IOException e) {
throw new IllegalArgumentException(configuration);
throw new AssertionError("Unexpected I/O error.", e);
}
}

Expand All @@ -67,7 +65,7 @@ String getDependencies(String configuration) {
*
* @return JarHC version, for example "1.4" or "1.5-SNAPSHOT".
*/
String getJarHcVersion() {
protected String getJarHcVersion() {
return jarHcVersion;
}

Expand All @@ -77,7 +75,7 @@ String getJarHcVersion() {
* @param path Directory path, relative to project root.
* @return Project directory.
*/
File getProjectDirectory(String path) {
protected File getProjectDirectory(String path) {
File directory = new File(projectDir, path);
Assertions.assertThat(directory).isDirectory();
return directory;
Expand All @@ -89,7 +87,7 @@ File getProjectDirectory(String path) {
* @param path File path, relative to project root.
* @return Project file.
*/
File getProjectFile(String path) {
protected File getProjectFile(String path) {
File file = new File(projectDir, path);
Assertions.assertThat(file).isFile().canRead();
return file;
Expand All @@ -101,7 +99,7 @@ File getProjectFile(String path) {
* @param path File path, relative to project root.
* @return Content of text file.
*/
String readProjectFile(String path) {
protected String readProjectFile(String path) {
File file = getProjectFile(path);
try {
return FileUtils.readFileToString(file, StandardCharsets.UTF_8);
Expand All @@ -110,7 +108,7 @@ String readProjectFile(String path) {
}
}

void writeProjectFile(String path, String text) {
protected void writeProjectFile(String path, String text) {
File file = getProjectFile(path);
try {
FileUtils.writeStringToFile(file, text, StandardCharsets.UTF_8);
Expand All @@ -125,7 +123,7 @@ void writeProjectFile(String path, String text) {
* @param path Resource path, relative to classpath root.
* @return Content of text resource.
*/
String readResource(String path) {
protected String readResource(String path) {
try (InputStream stream = this.getClass().getClassLoader().getResourceAsStream(path)) {
if (stream == null) {
String message = String.format("Resource not found: %s", path);
Expand All @@ -137,42 +135,6 @@ String readResource(String path) {
}
}

JavaContainer createJavaContainer(JavaImage javaImage, File reportsDir, File dataDir) {

// create a new container with the given Java image
JavaContainer container = new JavaContainer(javaImage);

// map JarHC JAR file into container
String jarFilePath = String.format("build/libs/jarhc-%s.jar", jarHcVersion);
File jarFile = getProjectFile(jarFilePath);
container.withFileSystemBind(jarFile.getAbsolutePath(), "/jarhc/jarhc.jar");

// map JarHC fat/uber JAR file into container
jarFilePath = String.format("build/libs/jarhc-%s-with-deps.jar", jarHcVersion);
jarFile = getProjectFile(jarFilePath);
container.withFileSystemBind(jarFile.getAbsolutePath(), "/jarhc/jarhc-with-deps.jar");

// make sure that image-specific report directory exists
File reportDir = new File(reportsDir, javaImage.getPath());
createDirectory(reportDir);

// set path to JarHC reports directory
container.withFileSystemBind(reportsDir.getAbsolutePath(), "/jarhc/reports");

// set path to JarHC data directory
container.withFileSystemBind(dataDir.getAbsolutePath(), "/jarhc/data");
container.withEnv("JARHC_DATA", "/jarhc/data");

// set working directory so that JAR is easily accessible
container.withWorkingDirectory("/jarhc");

// override default container command
// (otherwise, a JShell may be started and consume valuable memory)
container.withCommand("sleep", "1h");

return container;
}

protected void createDirectory(File directory) {
if (!directory.exists()) {
boolean created = directory.mkdirs();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright 2022 Stephan Markwalder
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jarhc.test.release.utils;

import static org.junit.jupiter.api.Assumptions.assumeTrue;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public abstract class AbstractTestRunner {

protected final File workDir;
protected final File dataDir;
protected final Map<String, File> files = new HashMap<>();

public AbstractTestRunner(File workDir, File dataDir) {
this.workDir = workDir;
this.dataDir = dataDir;
}

public abstract String getName();

public abstract String getJavaVendor();

protected abstract String getJavaVersion();

public void installFile(File file, String path) {
files.put(path, file);
}

public String getOutputPath(String resourceName) {
return "reports/" + resourceName;
}

public File getOutputFile(String resourceName) {
return new File(workDir, getOutputPath(resourceName));
}

public String findResourcePath(String resourceName) {

// try to find report in runner-specific test resources
String reportPath = "reports/" + getJavaVendor() + "/" + getJavaVersion() + "/" + resourceName;
if (resourceExists(reportPath)) {
return reportPath;
}

// try to find report in Java version-specific test resources
reportPath = "reports/all/" + getJavaVersion() + "/" + resourceName;
if (resourceExists(reportPath)) {
return reportPath;
}

// try to find report in generic test resources
reportPath = "reports/all/all/" + resourceName;
if (resourceExists(reportPath)) {
return reportPath;
}

assumeTrue(resourceExists(reportPath), "Test resource found: " + reportPath);
return reportPath;
}

private boolean resourceExists(String reportPath) {
return AbstractTestRunner.class.getClassLoader().getResource(reportPath) != null;
}

public abstract TestResult execute(Command command);

}
33 changes: 21 additions & 12 deletions src/releaseTest/java/org/jarhc/test/release/utils/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,34 @@ public class Command {
private final List<String> jarHcArguments = new ArrayList<>();

public static Command java(String... arguments) {
Command builder = new Command();
builder.javaArguments.addAll(Arrays.asList(arguments));
return builder;
Command command = new Command();
command.javaArguments.addAll(Arrays.asList(arguments));
return command;
}

public static Command jarHc(String... arguments) {
Command builder = new Command();
builder.jarHcArguments.add("-jar");
builder.jarHcArguments.add("jarhc-with-deps.jar");
builder.jarHcArguments.addAll(Arrays.asList(arguments));
return builder;
Command command = new Command();
command.jarHcArguments.add("-jar");
command.jarHcArguments.add("jarhc-with-deps.jar");
command.jarHcArguments.addAll(Arrays.asList(arguments));
return command;
}

private Command() {
}

public Command addJavaArguments(String... arguments) {
@SuppressWarnings("UnusedReturnValue")
public void addJavaArguments(String... arguments) {
javaArguments.addAll(Arrays.asList(arguments));
return this;
}

public Command addJarHcArguments(String... arguments) {
@SuppressWarnings("UnusedReturnValue")
public void addJarHcArguments(String... arguments) {
jarHcArguments.addAll(Arrays.asList(arguments));
return this;
}

public boolean isJarHcCommand() {
return !jarHcArguments.isEmpty();
}

public String[] build() {
Expand All @@ -60,4 +64,9 @@ public String[] build() {
return command.toArray(new String[0]);
}

@Override
public String toString() {
return String.join(" ", build());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright 2022 Stephan Markwalder
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jarhc.test.release.utils;

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.testcontainers.containers.Container;

public class DockerTestRunner extends AbstractTestRunner {

protected final JavaImage javaImage;

public DockerTestRunner(JavaImage javaImage, File workDir, File dataDir) {
super(workDir, dataDir);
this.javaImage = javaImage;
}

@Override
public String getName() {
return javaImage.getImageName();
}

@Override
public String getJavaVendor() {
return javaImage.getVendor() + "-" + javaImage.getProduct();
}

@Override
protected String getJavaVersion() {
return javaImage.getVersion();
}

@Override
public TestResult execute(Command command) {

JavaContainer container = createJavaContainer();

Container.ExecResult result;
try {
container.start();
result = container.exec(command);
} finally {
container.stop();
}

return new TestResult(result);
}

protected JavaContainer createJavaContainer() {

prepareWorkDir();

// create a new container with the given Java image
JavaContainer container = new JavaContainer(javaImage);

// map JarHC work directory into container
container.withFileSystemBind(workDir.getAbsolutePath(), "/jarhc");

// map installed files into container
for (String path : files.keySet()) {
File file = files.get(path);
container.withFileSystemBind(file.getAbsolutePath(), "/jarhc/" + path);
}

// map JarHC data directory into container
container.withFileSystemBind(dataDir.getAbsolutePath(), "/jarhc/data");

// set path to JarHC data directory
container.withEnv("JARHC_DATA", "/jarhc/data");

// set working directory
container.withWorkingDirectory("/jarhc");

// override default container command
// (otherwise, a JShell may be started and consume valuable memory)
container.withCommand("sleep", "1h");

return container;
}

private void prepareWorkDir() {
try {
if (!workDir.exists()) {
FileUtils.forceMkdir(workDir);
}
File reportsDir = new File(workDir, "reports");
if (!reportsDir.exists()) {
FileUtils.forceMkdir(reportsDir);
}
if (!dataDir.exists()) {
FileUtils.forceMkdir(dataDir);
}
} catch (IOException e) {
throw new AssertionError("Unexpected I/O error.", e);
}
}

}
Loading

0 comments on commit 198c68c

Please sign in to comment.