20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 package jdk.test.lib.containers.docker;
25
26 import java.io.File;
27 import java.io.FileWriter;
28 import java.io.IOException;
29 import java.nio.file.Files;
30 import java.nio.file.FileVisitResult;
31 import java.nio.file.Path;
32 import java.nio.file.Paths;
33 import java.nio.file.SimpleFileVisitor;
34 import java.nio.file.StandardCopyOption;
35 import java.nio.file.attribute.BasicFileAttributes;
36 import java.util.Arrays;
37 import java.util.ArrayList;
38 import java.util.Collections;
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 =
163 /**
164 * Build a docker image based on given docker file and docker build directory.
165 *
166 * @param imageName name of the image to be created, including version tag
167 * @param dockerfile path to the Dockerfile to be used for building the docker
168 * image. The specified dockerfile will be copied to the docker build
169 * directory as 'Dockerfile'
170 * @param buildDir build directory; it should already contain all the content
171 * needed to build the docker image.
172 * @throws Exception
173 */
174 public static void
175 buildDockerImage(String imageName, Path dockerfile, Path buildDir) throws Exception {
176
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
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
264 /**
265 * Execute a specified command in a process, report diagnostic info.
266 *
267 * @param command to be executed
268 * @return The output from the process
269 * @throws Exception
270 */
271 public static OutputAnalyzer execute(String... command) throws Exception {
272
273 ProcessBuilder pb = new ProcessBuilder(command);
274 System.out.println("[COMMAND]\n" + Utils.getCommandLine(pb));
275
276 long started = System.currentTimeMillis();
277 Process p = pb.start();
278 long pid = p.pid();
279 OutputAnalyzer output = new OutputAnalyzer(p);
280
281 String stdoutLogFile = String.format("docker-stdout-%d.log", pid);
282 System.out.println("[ELAPSED: " + (System.currentTimeMillis() - started) + " ms]");
283 System.out.println("[STDERR]\n" + output.getStderr());
284 System.out.println("[STDOUT]\n" +
285 trimLines(output.getStdout(),MAX_LINES_TO_COPY_FOR_CHILD_STDOUT));
286 System.out.printf("Child process STDOUT is trimmed to %d lines \n",
287 MAX_LINES_TO_COPY_FOR_CHILD_STDOUT);
288 writeOutputToFile(output.getStdout(), stdoutLogFile);
289 System.out.println("Full child process STDOUT was saved to " + stdoutLogFile);
290
291 return output;
292 }
293
|
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 package jdk.test.lib.containers.docker;
25
26 import java.io.File;
27 import java.io.FileWriter;
28 import java.io.IOException;
29 import java.nio.file.Files;
30 import java.nio.file.FileVisitResult;
31 import java.nio.file.Path;
32 import java.nio.file.Paths;
33 import java.nio.file.SimpleFileVisitor;
34 import java.nio.file.StandardCopyOption;
35 import java.nio.file.attribute.BasicFileAttributes;
36 import java.util.Arrays;
37 import java.util.ArrayList;
38 import java.util.Collections;
39 import java.util.List;
40 import java.util.Map;
41 import jdk.test.lib.Utils;
42 import jdk.test.lib.process.OutputAnalyzer;
43 import jdk.test.lib.process.ProcessTools;
44 import jtreg.SkippedException;
45
46
47 public class DockerTestUtils {
48 private static final String FS = File.separator;
49 private static boolean isDockerEngineAvailable = false;
50 private static boolean wasDockerEngineChecked = false;
51
52 // Specifies how many lines to copy from child STDOUT to main test output.
53 // Having too many lines in the main test output will result
54 // in JT harness trimming the output, and can lead to loss of useful
55 // diagnostic information.
56 private static final int MAX_LINES_TO_COPY_FOR_CHILD_STDOUT = 100;
57
58 // Use this property to specify docker location on your system.
59 // E.g.: "/usr/local/bin/docker".
60 private static final String DOCKER_COMMAND =
164 /**
165 * Build a docker image based on given docker file and docker build directory.
166 *
167 * @param imageName name of the image to be created, including version tag
168 * @param dockerfile path to the Dockerfile to be used for building the docker
169 * image. The specified dockerfile will be copied to the docker build
170 * directory as 'Dockerfile'
171 * @param buildDir build directory; it should already contain all the content
172 * needed to build the docker image.
173 * @throws Exception
174 */
175 public static void
176 buildDockerImage(String imageName, Path dockerfile, Path buildDir) throws Exception {
177
178 generateDockerFile(buildDir.resolve("Dockerfile"),
179 DockerfileConfig.getBaseImageName(),
180 DockerfileConfig.getBaseImageVersion());
181 try {
182 // Build the docker
183 execute(DOCKER_COMMAND, "build", "--no-cache", "--tag", imageName, buildDir.toString())
184 .shouldHaveExitValue(0);
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
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
264 /**
265 * Execute a specified command in a process, report diagnostic info.
266 *
267 * @param command to be executed
268 * @return The output from the process
269 * @throws Exception
270 */
271 public static OutputAnalyzer execute(String... command) throws Exception {
272
273 ProcessBuilder pb = new ProcessBuilder(command);
274 // Add /usr/sbin to PATH so as to allow for podman compatibility
275 // which relies on iptables being on the PATH
276 Map<String, String> env = pb.environment();
277 env.put("PATH", System.getenv("PATH") + ":/usr/sbin");
278 System.out.println("[COMMAND]\n" + Utils.getCommandLine(pb));
279
280 long started = System.currentTimeMillis();
281 Process p = pb.start();
282 long pid = p.pid();
283 OutputAnalyzer output = new OutputAnalyzer(p);
284
285 String stdoutLogFile = String.format("docker-stdout-%d.log", pid);
286 System.out.println("[ELAPSED: " + (System.currentTimeMillis() - started) + " ms]");
287 System.out.println("[STDERR]\n" + output.getStderr());
288 System.out.println("[STDOUT]\n" +
289 trimLines(output.getStdout(),MAX_LINES_TO_COPY_FOR_CHILD_STDOUT));
290 System.out.printf("Child process STDOUT is trimmed to %d lines \n",
291 MAX_LINES_TO_COPY_FOR_CHILD_STDOUT);
292 writeOutputToFile(output.getStdout(), stdoutLogFile);
293 System.out.println("Full child process STDOUT was saved to " + stdoutLogFile);
294
295 return output;
296 }
297
|