You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by cp...@apache.org on 2017/04/04 15:33:09 UTC

[27/36] lucene-solr:jira/solr-6203: SOLR-9745: fix solr.cmd to print errors from invoked script

SOLR-9745: fix solr.cmd to print errors from invoked script


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/65b4530f
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/65b4530f
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/65b4530f

Branch: refs/heads/jira/solr-6203
Commit: 65b4530fb39842418eae8d2acb7c463182039083
Parents: 52632cf
Author: Mikhail Khludnev <mk...@apache.org>
Authored: Mon Apr 3 23:45:54 2017 +0300
Committer: Mikhail Khludnev <mk...@apache.org>
Committed: Mon Apr 3 23:53:54 2017 +0300

----------------------------------------------------------------------
 solr/CHANGES.txt                                |  2 ++
 .../src/java/org/apache/solr/util/SolrCLI.java  | 18 +++++++---
 .../apache/solr/util/TestSolrCLIRunExample.java | 38 ++++++++++++++++++++
 3 files changed, 53 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/65b4530f/solr/CHANGES.txt
----------------------------------------------------------------------
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 6fe4cc0..c1a7503 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -190,6 +190,8 @@ Other Changes
 
 * SOLR-8906: Make transient core cache pluggable (Erick Erickson)
 
+* SOLR-9745: print errors from solr.cmd (Gopikannan Venugopalsamy via Mikhail Khludnev)
+
 ==================  6.5.1 ==================
 
 Bug Fixes

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/65b4530f/solr/core/src/java/org/apache/solr/util/SolrCLI.java
----------------------------------------------------------------------
diff --git a/solr/core/src/java/org/apache/solr/util/SolrCLI.java b/solr/core/src/java/org/apache/solr/util/SolrCLI.java
index 6a85422..da7e63e 100644
--- a/solr/core/src/java/org/apache/solr/util/SolrCLI.java
+++ b/solr/core/src/java/org/apache/solr/util/SolrCLI.java
@@ -62,6 +62,7 @@ import org.apache.commons.cli.Options;
 import org.apache.commons.cli.ParseException;
 import org.apache.commons.exec.DefaultExecuteResultHandler;
 import org.apache.commons.exec.DefaultExecutor;
+import org.apache.commons.exec.ExecuteException;
 import org.apache.commons.exec.Executor;
 import org.apache.commons.exec.OS;
 import org.apache.commons.exec.environment.EnvironmentUtils;
@@ -2928,18 +2929,25 @@ public class SolrCLI {
             }
           }
         }
-        executor.execute(org.apache.commons.exec.CommandLine.parse(startCmd), startEnv, new DefaultExecuteResultHandler());
+        DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler();
+        executor.execute(org.apache.commons.exec.CommandLine.parse(startCmd), startEnv, handler);
 
-        // brief wait before proceeding on Windows
+        // wait for execution.
         try {
-          Thread.sleep(3000);
+          handler.waitFor();
         } catch (InterruptedException ie) {
           // safe to ignore ...
           Thread.interrupted();
         }
-
+        if (handler.getExitValue() != 0) {
+          throw new Exception("Failed to start Solr using command: "+startCmd+" Exception : "+handler.getException());
+        }
       } else {
-        code = executor.execute(org.apache.commons.exec.CommandLine.parse(startCmd));
+        try {
+          code = executor.execute(org.apache.commons.exec.CommandLine.parse(startCmd));
+        } catch(ExecuteException e){
+          throw new Exception("Failed to start Solr using command: "+startCmd+" Exception : "+ e);
+        }
       }
       if (code != 0)
         throw new Exception("Failed to start Solr using command: "+startCmd);

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/65b4530f/solr/core/src/test/org/apache/solr/util/TestSolrCLIRunExample.java
----------------------------------------------------------------------
diff --git a/solr/core/src/test/org/apache/solr/util/TestSolrCLIRunExample.java b/solr/core/src/test/org/apache/solr/util/TestSolrCLIRunExample.java
index 7980560..02d91b0 100644
--- a/solr/core/src/test/org/apache/solr/util/TestSolrCLIRunExample.java
+++ b/solr/core/src/test/org/apache/solr/util/TestSolrCLIRunExample.java
@@ -482,4 +482,42 @@ public class TestSolrCLIRunExample extends SolrTestCaseJ4 {
     // stop the test instance
     executor.execute(org.apache.commons.exec.CommandLine.parse("bin/solr stop -p "+bindPort));
   }
+  
+  @Test
+  public void testFailExecuteScript() throws Exception {
+    File solrHomeDir = new File(ExternalPaths.SERVER_HOME);
+    if (!solrHomeDir.isDirectory())
+      fail(solrHomeDir.getAbsolutePath()+" not found and is required to run this test!");
+   
+    Path tmpDir = createTempDir();
+    File solrExampleDir = tmpDir.toFile();
+    File solrServerDir = solrHomeDir.getParentFile();
+
+    // need a port to start the example server on
+    int bindPort = -1;
+    try (ServerSocket socket = new ServerSocket(0)) {
+      bindPort = socket.getLocalPort();
+    }
+
+    File toExecute = new File(tmpDir.toString(), "failExecuteScript");
+    assertTrue("Should have been able to create file '" + toExecute.getAbsolutePath() + "' ", toExecute.createNewFile());
+    
+    String[] toolArgs = new String[] {
+        "-e", "techproducts",
+        "-serverDir", solrServerDir.getAbsolutePath(),
+        "-exampleDir", solrExampleDir.getAbsolutePath(),
+        "-p", String.valueOf(bindPort),
+        "-script", toExecute.getAbsolutePath().toString()
+    };
+
+    // capture tool output to stdout
+    ByteArrayOutputStream baos = new ByteArrayOutputStream();
+    PrintStream stdoutSim = new PrintStream(baos, true, StandardCharsets.UTF_8.name());
+
+    DefaultExecutor executor = new DefaultExecutor();
+
+    SolrCLI.RunExampleTool tool = new SolrCLI.RunExampleTool(executor, System.in, stdoutSim);
+    int code = tool.runTool(SolrCLI.processCommandLineArgs(SolrCLI.joinCommonAndToolOptions(tool.getOptions()), toolArgs));
+    assertTrue("Execution should have failed with return code 1", code == 1);
+  }
 }