Apache Solr open-source search software
SOLR-9745: fix solr.cmd to print errors from invoked script
Mikhail Khludnev5ee18c8ab528
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex e5708e748f5..ceda297a4cc 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -198,6 +198,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 Fixesdiff --git a/solr/core/src/java/org/apache/solr/util/SolrCLI.java b/solr/core/src/java/org/apache/solr/util/SolrCLI.javaindex 6a85422185d..da7e63ee506 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);diff --git a/solr/core/src/test/org/apache/solr/util/TestSolrCLIRunExample.java b/solr/core/src/test/org/apache/solr/util/TestSolrCLIRunExample.javaindex 7980560f899..02d91b03503 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 instanceexecutor.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);+ }}