39 import java.util.List;
40 import jdk.test.lib.Utils;
41 import jdk.test.lib.process.OutputAnalyzer;
42 import jdk.test.lib.process.ProcessTools;
43 import jtreg.SkippedException;
44
45
46 public class DockerTestUtils {
47 private static final String FS = File.separator;
48 private static boolean isDockerEngineAvailable = false;
49 private static boolean wasDockerEngineChecked = false;
50
51 // Specifies how many lines to copy from child STDOUT to main test output.
52 // Having too many lines in the main test output will result
53 // in JT harness trimming the output, and can lead to loss of useful
54 // diagnostic information.
55 private static final int MAX_LINES_TO_COPY_FOR_CHILD_STDOUT = 100;
56
57 // Use this property to specify docker location on your system.
58 // E.g.: "/usr/local/bin/docker".
59 private static final String DOCKER_COMMAND =
60 System.getProperty("jdk.test.docker.command", "docker");
61
62 // Set this property to true to retain image after test. By default
63 // images are removed after test execution completes.
64 // Retaining the image can be useful for diagnostics and image inspection.
65 // E.g.: start image interactively: docker run -it <IMAGE_NAME>.
66 public static final boolean RETAIN_IMAGE_AFTER_TEST =
67 Boolean.getBoolean("jdk.test.docker.retain.image");
68
69 // Path to a JDK under test.
70 // This may be useful when developing tests on non-Linux platforms.
71 public static final String JDK_UNDER_TEST =
72 System.getProperty("jdk.test.docker.jdk", Utils.TEST_JDK);
73
74
75 /**
76 * Optimized check of whether the docker engine is available in a given
77 * environment. Checks only once, then remembers the result in a singleton.
78 *
79 * @return true if docker engine is available
177 generateDockerFile(buildDir.resolve("Dockerfile"),
178 DockerfileConfig.getBaseImageName(),
179 DockerfileConfig.getBaseImageVersion());
180 try {
181 // Build the docker
182 execute(DOCKER_COMMAND, "build", "--no-cache", "--tag", imageName, buildDir.toString())
183 .shouldHaveExitValue(0)
184 .shouldContain("Successfully built");
185 } catch (Exception e) {
186 // If docker image building fails there is a good chance it happens due to environment and/or
187 // configuration other than product failure. Throw jtreg skipped exception in such case
188 // instead of failing the test.
189 throw new SkippedException("Building docker image failed. Details: \n" + e.getMessage());
190 }
191 }
192
193
194 /**
195 * Build the docker command to run java inside a container
196 *
197 * @param DockerRunOptions optins for running docker
198 *
199 * @return command
200 * @throws Exception
201 */
202 public static List<String> buildJavaCommand(DockerRunOptions opts) throws Exception {
203 List<String> cmd = new ArrayList<>();
204
205 cmd.add(DOCKER_COMMAND);
206 cmd.add("run");
207 if (opts.tty)
208 cmd.add("--tty=true");
209 if (opts.removeContainerAfterUse)
210 cmd.add("--rm");
211
212 cmd.addAll(opts.dockerOpts);
213 cmd.add(opts.imageNameAndTag);
214 cmd.add(opts.command);
215
216 cmd.addAll(opts.javaOpts);
217 if (opts.appendTestJavaOptions) {
218 Collections.addAll(cmd, Utils.getTestJavaOpts());
219 }
220
221 cmd.add(opts.classToRun);
222 cmd.addAll(opts.classParams);
223
224 return cmd;
225 }
226
227 /**
228 * Run Java inside the docker image with specified parameters and options.
229 *
230 * @param DockerRunOptions optins for running docker
231 *
232 * @return output of the run command
233 * @throws Exception
234 */
235 public static OutputAnalyzer dockerRunJava(DockerRunOptions opts) throws Exception {
236 return execute(buildJavaCommand(opts));
237 }
238
239
240 /**
241 * Remove docker image
242 *
243 * @param DockerRunOptions optins for running docker
244 * @throws Exception
245 */
246 public static void removeDockerImage(String imageNameAndTag) throws Exception {
247 execute(DOCKER_COMMAND, "rmi", "--force", imageNameAndTag);
248 }
249
250
251
252 /**
253 * Convenience method - express command as sequence of strings
254 *
255 * @param command to execute
256 * @return The output from the process
257 * @throws Exception
258 */
259 public static OutputAnalyzer execute(List<String> command) throws Exception {
260 return execute(command.toArray(new String[command.size()]));
261 }
262
263
|
39 import java.util.List;
40 import jdk.test.lib.Utils;
41 import jdk.test.lib.process.OutputAnalyzer;
42 import jdk.test.lib.process.ProcessTools;
43 import jtreg.SkippedException;
44
45
46 public class DockerTestUtils {
47 private static final String FS = File.separator;
48 private static boolean isDockerEngineAvailable = false;
49 private static boolean wasDockerEngineChecked = false;
50
51 // Specifies how many lines to copy from child STDOUT to main test output.
52 // Having too many lines in the main test output will result
53 // in JT harness trimming the output, and can lead to loss of useful
54 // diagnostic information.
55 private static final int MAX_LINES_TO_COPY_FOR_CHILD_STDOUT = 100;
56
57 // Use this property to specify docker location on your system.
58 // E.g.: "/usr/local/bin/docker".
59 public static final String DOCKER_COMMAND =
60 System.getProperty("jdk.test.docker.command", "docker");
61
62 // Set this property to true to retain image after test. By default
63 // images are removed after test execution completes.
64 // Retaining the image can be useful for diagnostics and image inspection.
65 // E.g.: start image interactively: docker run -it <IMAGE_NAME>.
66 public static final boolean RETAIN_IMAGE_AFTER_TEST =
67 Boolean.getBoolean("jdk.test.docker.retain.image");
68
69 // Path to a JDK under test.
70 // This may be useful when developing tests on non-Linux platforms.
71 public static final String JDK_UNDER_TEST =
72 System.getProperty("jdk.test.docker.jdk", Utils.TEST_JDK);
73
74
75 /**
76 * Optimized check of whether the docker engine is available in a given
77 * environment. Checks only once, then remembers the result in a singleton.
78 *
79 * @return true if docker engine is available
177 generateDockerFile(buildDir.resolve("Dockerfile"),
178 DockerfileConfig.getBaseImageName(),
179 DockerfileConfig.getBaseImageVersion());
180 try {
181 // Build the docker
182 execute(DOCKER_COMMAND, "build", "--no-cache", "--tag", imageName, buildDir.toString())
183 .shouldHaveExitValue(0)
184 .shouldContain("Successfully built");
185 } catch (Exception e) {
186 // If docker image building fails there is a good chance it happens due to environment and/or
187 // configuration other than product failure. Throw jtreg skipped exception in such case
188 // instead of failing the test.
189 throw new SkippedException("Building docker image failed. Details: \n" + e.getMessage());
190 }
191 }
192
193
194 /**
195 * Build the docker command to run java inside a container
196 *
197 * @param DockerRunOptions options for running docker
198 *
199 * @return command
200 * @throws Exception
201 */
202 public static List<String> buildJavaCommand(DockerRunOptions opts) throws Exception {
203 List<String> cmd = new ArrayList<>();
204
205 cmd.add(DOCKER_COMMAND);
206 cmd.add("run");
207 if (opts.tty)
208 cmd.add("--tty=true");
209 if (opts.removeContainerAfterUse)
210 cmd.add("--rm");
211
212 cmd.addAll(opts.dockerOpts);
213 cmd.add(opts.imageNameAndTag);
214 cmd.add(opts.command);
215
216 cmd.addAll(opts.javaOpts);
217 if (opts.appendTestJavaOptions) {
218 Collections.addAll(cmd, Utils.getTestJavaOpts());
219 }
220
221 cmd.add(opts.classToRun);
222 cmd.addAll(opts.classParams);
223
224 return cmd;
225 }
226
227 /**
228 * Run Java inside a docker container with specified parameters and options.
229 *
230 * @param DockerRunOptions options for running docker
231 *
232 * @return output of the run command
233 * @throws Exception
234 */
235 public static OutputAnalyzer dockerRunJava(DockerRunOptions opts) throws Exception {
236 return execute(buildJavaCommand(opts));
237 }
238
239
240 /**
241 * Remove docker image
242 *
243 * @param DockerRunOptions options for running docker
244 * @throws Exception
245 */
246 public static void removeDockerImage(String imageNameAndTag) throws Exception {
247 execute(DOCKER_COMMAND, "rmi", "--force", imageNameAndTag);
248 }
249
250
251
252 /**
253 * Convenience method - express command as sequence of strings
254 *
255 * @param command to execute
256 * @return The output from the process
257 * @throws Exception
258 */
259 public static OutputAnalyzer execute(List<String> command) throws Exception {
260 return execute(command.toArray(new String[command.size()]));
261 }
262
263
|