8224165: [TESTBUG] Docker tests produce excessive output
authormseledtsov
Thu, 23 May 2019 11:37:24 -0700
changeset 55012 fb0cfce19262
parent 55011 2218f9d57d2f
child 55013 8dae495a59e7
8224165: [TESTBUG] Docker tests produce excessive output Summary: Trimmed child process output, saving child stdout to file Reviewed-by: dholmes, lmesnik
test/lib/jdk/test/lib/containers/docker/DockerTestUtils.java
--- a/test/lib/jdk/test/lib/containers/docker/DockerTestUtils.java	Thu May 23 10:05:02 2019 -0700
+++ b/test/lib/jdk/test/lib/containers/docker/DockerTestUtils.java	Thu May 23 11:37:24 2019 -0700
@@ -24,6 +24,7 @@
 package jdk.test.lib.containers.docker;
 
 import java.io.File;
+import java.io.FileWriter;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.FileVisitResult;
@@ -32,6 +33,7 @@
 import java.nio.file.SimpleFileVisitor;
 import java.nio.file.StandardCopyOption;
 import java.nio.file.attribute.BasicFileAttributes;
+import java.util.Arrays;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
@@ -46,6 +48,12 @@
     private static boolean isDockerEngineAvailable = false;
     private static boolean wasDockerEngineChecked = false;
 
+    // Specifies how many lines to copy from child STDOUT to main test output.
+    // Having too many lines in the main test output will result
+    // in JT harness trimming the output, and can lead to loss of useful
+    // diagnostic information.
+    private static final int MAX_LINES_TO_COPY_FOR_CHILD_STDOUT = 100;
+
     // Use this property to specify docker location on your system.
     // E.g.: "/usr/local/bin/docker".
     private static final String DOCKER_COMMAND =
@@ -266,16 +274,41 @@
         System.out.println("[COMMAND]\n" + Utils.getCommandLine(pb));
 
         long started = System.currentTimeMillis();
-        OutputAnalyzer output = new OutputAnalyzer(pb.start());
+        Process p = pb.start();
+        long pid = p.pid();
+        OutputAnalyzer output = new OutputAnalyzer(p);
 
+        String stdoutLogFile = String.format("docker-stdout-%d.log", pid);
         System.out.println("[ELAPSED: " + (System.currentTimeMillis() - started) + " ms]");
         System.out.println("[STDERR]\n" + output.getStderr());
-        System.out.println("[STDOUT]\n" + output.getStdout());
+        System.out.println("[STDOUT]\n" +
+                           trimLines(output.getStdout(),MAX_LINES_TO_COPY_FOR_CHILD_STDOUT));
+        System.out.printf("Child process STDOUT is trimmed to %d lines \n",
+                           MAX_LINES_TO_COPY_FOR_CHILD_STDOUT);
+        writeOutputToFile(output.getStdout(), stdoutLogFile);
+        System.out.println("Full child process STDOUT was saved to " + stdoutLogFile);
 
         return output;
     }
 
 
+    private static void writeOutputToFile(String output, String fileName) throws Exception {
+        try (FileWriter fw = new FileWriter(fileName)) {
+            fw.write(output, 0, output.length());
+        }
+    }
+
+
+    private static String trimLines(String buffer, int nrOfLines) {
+        List<String> l = Arrays.asList(buffer.split("\\R"));
+        if (l.size() < nrOfLines) {
+            return buffer;
+        }
+
+        return String.join("\n", l.subList(0, nrOfLines));
+    }
+
+
     private static void generateDockerFile(Path dockerfile, String baseImage,
                                            String baseImageVersion) throws Exception {
         String template =