You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by js...@apache.org on 2017/10/19 23:10:46 UTC

[geode] 02/02: GEODE-3830: Add more logging for GfshRule

This is an automated email from the ASF dual-hosted git repository.

jstewart pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/geode.git

commit ff6bfd91fede96d8f00631874920cc541c191809
Author: Jared Stewart <js...@pivotal.io>
AuthorDate: Tue Oct 17 14:48:08 2017 -0700

    GEODE-3830: Add more logging for GfshRule
    
    This closes #946.
---
 .../geode/test/junit/rules/gfsh/GfshExecution.java | 66 ++++++++++++++++++++++
 .../geode/test/junit/rules/gfsh/GfshRule.java      | 21 ++-----
 .../geode/test/junit/rules/gfsh/GfshScript.java    | 45 +++++++++------
 3 files changed, 100 insertions(+), 32 deletions(-)

diff --git a/geode-junit/src/main/java/org/apache/geode/test/junit/rules/gfsh/GfshExecution.java b/geode-junit/src/main/java/org/apache/geode/test/junit/rules/gfsh/GfshExecution.java
index 3c1e130..d7260e9 100644
--- a/geode-junit/src/main/java/org/apache/geode/test/junit/rules/gfsh/GfshExecution.java
+++ b/geode-junit/src/main/java/org/apache/geode/test/junit/rules/gfsh/GfshExecution.java
@@ -14,7 +14,21 @@
  */
 package org.apache.geode.test.junit.rules.gfsh;
 
+import java.io.BufferedReader;
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.Arrays;
+import java.util.List;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import com.google.common.base.Charsets;
+import org.apache.commons.lang.exception.ExceptionUtils;
 
 import org.apache.geode.test.junit.rules.gfsh.internal.ProcessLogger;
 
@@ -44,4 +58,56 @@ public class GfshExecution {
   public Process getProcess() {
     return this.process;
   }
+
+  public List<File> getServerDirs() {
+    File[] potentialMemberDirectories = workingDir.listFiles(File::isDirectory);
+
+    Predicate<File> isServerDir = (File directory) -> Arrays.stream(directory.list())
+        .anyMatch(filename -> filename.endsWith("server.pid"));
+
+    return Arrays.stream(potentialMemberDirectories).filter(isServerDir)
+        .collect(Collectors.toList());
+  }
+
+  public List<File> getLocatorDirs() {
+    File[] potentialMemberDirectories = workingDir.listFiles(File::isDirectory);
+
+    Predicate<File> isLocatorDir = (File directory) -> Arrays.stream(directory.list())
+        .anyMatch(filename -> filename.endsWith("locator.pid"));
+
+    return Arrays.stream(potentialMemberDirectories).filter(isLocatorDir)
+        .collect(Collectors.toList());
+  }
+
+  public void printLogFiles() {
+    System.out
+        .println("Printing contents of all log files found in " + workingDir.getAbsolutePath());
+    List<File> logFiles = findLogFiles();
+
+    for (File logFile : logFiles) {
+      System.out.println("Contents of " + logFile.getAbsolutePath());
+      try (BufferedReader br =
+          new BufferedReader(new InputStreamReader(new FileInputStream(logFile), Charsets.UTF_8))) {
+        String line;
+        while ((line = br.readLine()) != null) {
+          System.out.println(line);
+        }
+      } catch (IOException ignored) {
+        System.out.println("Unable to print log due to: " + ExceptionUtils.getStackTrace(ignored));
+      }
+    }
+  }
+
+  private List<File> findLogFiles() {
+    List<File> servers = getServerDirs();
+    List<File> locators = getLocatorDirs();
+
+    return Stream.concat(servers.stream(), locators.stream()).flatMap(this::findLogFiles)
+        .collect(Collectors.toList());
+  }
+
+  private Stream<File> findLogFiles(File memberDir) {
+    return Arrays.stream(memberDir.listFiles()).filter(File::isFile)
+        .filter(file -> file.getName().toLowerCase().endsWith(".log"));
+  }
 }
diff --git a/geode-junit/src/main/java/org/apache/geode/test/junit/rules/gfsh/GfshRule.java b/geode-junit/src/main/java/org/apache/geode/test/junit/rules/gfsh/GfshRule.java
index 68baaf5..e2ccf0c 100644
--- a/geode-junit/src/main/java/org/apache/geode/test/junit/rules/gfsh/GfshRule.java
+++ b/geode-junit/src/main/java/org/apache/geode/test/junit/rules/gfsh/GfshRule.java
@@ -22,11 +22,9 @@ import java.io.IOException;
 import java.io.UncheckedIOException;
 import java.nio.file.Path;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.TimeUnit;
-import java.util.function.Predicate;
 import java.util.stream.Collectors;
 
 import org.junit.rules.ExternalResource;
@@ -62,8 +60,7 @@ public class GfshRule extends ExternalResource {
    */
   @Override
   protected void after() {
-    gfshExecutions.stream().map(GfshExecution::getWorkingDir).collect(Collectors.toList())
-        .forEach(this::stopMembersQuietly);
+    gfshExecutions.stream().collect(Collectors.toList()).forEach(this::stopMembersQuietly);
 
     gfshExecutions.stream().map(GfshExecution::getProcess).map(Process::destroyForcibly)
         .forEach((Process process) -> {
@@ -94,7 +91,7 @@ public class GfshRule extends ExternalResource {
       Process process = toProcessBuilder(gfshScript, gfsh, workingDir).start();
       gfshExecution = new GfshExecution(process, workingDir);
       gfshExecutions.add(gfshExecution);
-      gfshScript.awaitIfNecessary(process);
+      gfshScript.awaitIfNecessary(gfshExecution);
     } catch (IOException e) {
       throw new UncheckedIOException(e);
     }
@@ -127,17 +124,9 @@ public class GfshRule extends ExternalResource {
     return processBuilder;
   }
 
-  private void stopMembersQuietly(File parentDirectory) {
-    File[] potentialMemberDirectories = parentDirectory.listFiles(File::isDirectory);
-
-    Predicate<File> isServerDir = (File directory) -> Arrays.stream(directory.list())
-        .anyMatch(filename -> filename.endsWith("server.pid"));
-
-    Predicate<File> isLocatorDir = (File directory) -> Arrays.stream(directory.list())
-        .anyMatch(filename -> filename.endsWith("locator.pid"));
-
-    Arrays.stream(potentialMemberDirectories).filter(isServerDir).forEach(this::stopServerInDir);
-    Arrays.stream(potentialMemberDirectories).filter(isLocatorDir).forEach(this::stopLocatorInDir);
+  private void stopMembersQuietly(GfshExecution gfshExecution) {
+    gfshExecution.getServerDirs().forEach(this::stopServerInDir);
+    gfshExecution.getLocatorDirs().forEach(this::stopLocatorInDir);
   }
 
   private void stopServerInDir(File dir) {
diff --git a/geode-junit/src/main/java/org/apache/geode/test/junit/rules/gfsh/GfshScript.java b/geode-junit/src/main/java/org/apache/geode/test/junit/rules/gfsh/GfshScript.java
index c89bddd..afd34d6 100644
--- a/geode-junit/src/main/java/org/apache/geode/test/junit/rules/gfsh/GfshScript.java
+++ b/geode-junit/src/main/java/org/apache/geode/test/junit/rules/gfsh/GfshScript.java
@@ -18,6 +18,7 @@ import static org.assertj.core.api.Assertions.assertThat;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Random;
 import java.util.concurrent.TimeUnit;
 
 public class GfshScript {
@@ -28,10 +29,11 @@ public class GfshScript {
   private boolean awaitQuietly = false;
   private int expectedExitValue = 0;
   private List<String> extendedClasspath = new ArrayList<>();
+  private Random random = new Random();
 
   public GfshScript(String... commands) {
     this.commands = commands;
-    this.name = defaultName(commands);
+    this.name = defaultName();
   }
 
   /**
@@ -103,32 +105,44 @@ public class GfshScript {
     return gfshRule.execute(this);
   }
 
-  protected void awaitIfNecessary(Process process) {
+  protected void awaitIfNecessary(GfshExecution gfshExecution) {
     if (shouldAwaitQuietly()) {
-      awaitQuietly(process);
+      awaitQuietly(gfshExecution);
     } else if (shouldAwaitLoudly()) {
-      awaitLoudly(process);
+      awaitLoudly(gfshExecution);
+    }
+
+    try {
+      assertThat(gfshExecution.getProcess().exitValue()).isEqualTo(expectedExitValue);
+    } catch (AssertionError e) {
+      gfshExecution.printLogFiles();
+      throw e;
     }
 
-    assertThat(process.exitValue()).isEqualTo(expectedExitValue);
   }
 
-  private void awaitQuietly(Process process) {
+  private void awaitQuietly(GfshExecution gfshExecution) {
     try {
-      process.waitFor(timeout, timeoutTimeUnit);
+      gfshExecution.getProcess().waitFor(timeout, timeoutTimeUnit);
     } catch (InterruptedException ignore) {
       // ignore since we are waiting *quietly*
     }
   }
 
-  private void awaitLoudly(Process process) {
+  private void awaitLoudly(GfshExecution gfshExecution) {
     boolean exited;
     try {
-      exited = process.waitFor(timeout, timeoutTimeUnit);
+      exited = gfshExecution.getProcess().waitFor(timeout, timeoutTimeUnit);
     } catch (InterruptedException e) {
       throw new RuntimeException(e);
     }
-    assertThat(exited).isTrue();
+
+    try {
+      assertThat(exited).isTrue();
+    } catch (AssertionError e) {
+      gfshExecution.printLogFiles();
+      throw e;
+    }
   }
 
   private boolean shouldAwait() {
@@ -151,11 +165,10 @@ public class GfshScript {
     return name;
   }
 
-  private String defaultName(String... commands) {
-    try {
-      return commands[0].substring(0, commands[0].indexOf("-")).trim();
-    } catch (Exception handled) {
-      return commands[0];
-    }
+  private String defaultName() {
+    return Long.toHexString(random.nextLong());
   }
+
+
+
 }

-- 
To stop receiving notification emails like this one, please contact
"commits@geode.apache.org" <co...@geode.apache.org>.