< prev index next >
test/lib/jdk/test/lib/containers/docker/DockerTestUtils.java
Print this page
@@ -137,10 +137,33 @@
*/
public static void
buildJdkDockerImage(String imageName, String dockerfile, String buildDirName)
throws Exception {
+ buildJdkDockerImage(imageName, dockerfile, buildDirName, "");
+ }
+
+ /**
+ * Build a docker image that contains JDK under test.
+ * The jdk will be placed under the "/jdk/" folder inside the docker file system.
+ *
+ * @param imageName name of the image to be created, including version tag
+ * @param dockerfile name of the dockerfile residing in the test source;
+ * we check for a platform specific dockerfile as well
+ * and use this one in case it exists
+ * @param buildDirName name of the docker build/staging directory, which will
+ * be created in the jtreg's scratch folder
+ * @param additionalDockerFileContent
+ * additional docker file content to be appended to the
+ * template
+ * @throws Exception
+ */
+ public static void
+ buildJdkDockerImage(String imageName, String dockerfile,
+ String buildDirName, String additionalDockerFileContent)
+ throws Exception {
+
Path buildDir = Paths.get(".", buildDirName);
if (Files.exists(buildDir)) {
throw new RuntimeException("The docker build directory already exists: " + buildDir);
}
@@ -150,11 +173,12 @@
Files.createDirectories(jdkDstDir);
// Copy JDK-under-test tree to the docker build directory.
// This step is required for building a docker image.
Files.walkFileTree(jdkSrcDir, new CopyFileVisitor(jdkSrcDir, jdkDstDir));
- buildDockerImage(imageName, Paths.get(Utils.TEST_SRC, dockerfile), buildDir);
+ buildDockerImage(imageName, Paths.get(Utils.TEST_SRC, dockerfile),
+ buildDir, additionalDockerFileContent);
}
/**
* Build a docker image based on given docker file and docker build directory.
@@ -163,28 +187,50 @@
* @param dockerfile path to the Dockerfile to be used for building the docker
* image. The specified dockerfile will be copied to the docker build
* directory as 'Dockerfile'
* @param buildDir build directory; it should already contain all the content
* needed to build the docker image.
+ * @param additionalDockerfileContent additional docker file content, to be added
+ * after the basic template
* @throws Exception
*/
public static void
- buildDockerImage(String imageName, Path dockerfile, Path buildDir) throws Exception {
+ buildDockerImage(String imageName, Path dockerfile,
+ Path buildDir, String additionalDockerfileContent) throws Exception {
generateDockerFile(buildDir.resolve("Dockerfile"),
DockerfileConfig.getBaseImageName(),
- DockerfileConfig.getBaseImageVersion());
+ DockerfileConfig.getBaseImageVersion(),
+ additionalDockerfileContent);
try {
// Build the docker
execute(Container.ENGINE_COMMAND, "build", "--no-cache", "--tag", imageName, buildDir.toString())
- .shouldHaveExitValue(0);
+ .shouldHaveExitValue(0)
+ .shouldContain("Successfully built");
} catch (Exception e) {
// If docker image building fails there is a good chance it happens due to environment and/or
// configuration other than product failure. Throw jtreg skipped exception in such case
// instead of failing the test.
throw new SkippedException("Building docker image failed. Details: \n" + e.getMessage());
}
+
+ }
+
+ /**
+ * Build a docker image based on given docker file and docker build directory.
+ *
+ * @param imageName name of the image to be created, including version tag
+ * @param dockerfile path to the Dockerfile to be used for building the docker
+ * image. The specified dockerfile will be copied to the docker build
+ * directory as 'Dockerfile'
+ * @param buildDir build directory; it should already contain all the content
+ * needed to build the docker image.
+ * @throws Exception
+ */
+ public static void
+ buildDockerImage(String imageName, Path dockerfile, Path buildDir) throws Exception {
+ buildDockerImage(imageName, dockerfile, buildDir, "");
}
/**
* Build the docker command to run java inside a container
@@ -285,10 +331,55 @@
return output;
}
+ /**
+ * Check if a container is running
+ *
+ * @param name container name
+ * @return True if container is running
+ * @throws Exception
+ */
+ public static boolean isContainerRunning(String name) throws Exception {
+ OutputAnalyzer out =
+ DockerTestUtils.execute(Container.ENGINE_COMMAND,
+ "ps", "--no-trunc",
+ "--filter", "name=" + name);
+ return out.getStdout().contains(name);
+ }
+
+
+ /**
+ * Wait for container to start
+ *
+ * @param name container name
+ * @param initialDelay initial delay (milliseconds) before the first check
+ * @param checkDelay delay between checks (milliseconds)
+ * @param count how many times to check before giving up
+ * @throws Exception will throw an exception if a specified container did not start
+ */
+ public static void waitForContainerToStart(String name, int initialDelay,
+ int checkDelay, int count) throws Exception {
+ boolean started = false;
+
+ try { Thread.sleep(initialDelay); } catch (InterruptedException e) {}
+
+ for(int i=0; i < count; i++) {
+ try { Thread.sleep(checkDelay); } catch (InterruptedException e) {}
+ if (isContainerRunning(name)) {
+ started = true;
+ break;
+ }
+ System.out.println("The main container has not started yet, count = " + i);
+ }
+ if (!started) {
+ throw new RuntimeException("Main container did not start");
+ }
+ }
+
+
private static void writeOutputToFile(String output, String fileName) throws Exception {
try (FileWriter fw = new FileWriter(fileName)) {
fw.write(output, 0, output.length());
}
}
@@ -303,21 +394,23 @@
return String.join("\n", l.subList(0, nrOfLines));
}
private static void generateDockerFile(Path dockerfile, String baseImage,
- String baseImageVersion) throws Exception {
+ String baseImageVersion,
+ String additionalDockerfileContent) throws Exception {
String template =
"FROM %s:%s\n" +
"COPY /jdk /jdk\n" +
"ENV JAVA_HOME=/jdk\n" +
- "CMD [\"/bin/bash\"]\n";
+ "CMD [\"/bin/bash\"]\n" +
+ additionalDockerfileContent;
+
String dockerFileStr = String.format(template, baseImage, baseImageVersion);
Files.writeString(dockerfile, dockerFileStr);
}
-
private static class CopyFileVisitor extends SimpleFileVisitor<Path> {
private final Path src;
private final Path dst;
public CopyFileVisitor(Path src, Path dst) {
< prev index next >