diff --git a/solr/core/src/java/org/apache/solr/cli/RunExampleTool.java b/solr/core/src/java/org/apache/solr/cli/RunExampleTool.java index 66828271c060..3b5f5195f056 100644 --- a/solr/core/src/java/org/apache/solr/cli/RunExampleTool.java +++ b/solr/core/src/java/org/apache/solr/cli/RunExampleTool.java @@ -872,6 +872,11 @@ void runCloudExample(CloudExampleParams params) throws Exception { Locale.ROOT, "Please enter the port for node%d [%d]: ", (n + 1), cloudPorts[n]); int port = promptForPort(readInput, n + 1, promptMsg, cloudPorts[n]); while (!isPortAvailable(port)) { + if (usingScriptInputs) { + // a retry would consume the next scripted answer as the port + throw new IllegalStateException( + "Port " + port + " from --script-inputs is already being used by another process."); + } port = promptForPort( readInput, diff --git a/solr/core/src/test/org/apache/solr/cli/TestSolrCLIRunExample.java b/solr/core/src/test/org/apache/solr/cli/TestSolrCLIRunExample.java index b9957e993cc1..b2ef966034f7 100644 --- a/solr/core/src/test/org/apache/solr/cli/TestSolrCLIRunExample.java +++ b/solr/core/src/test/org/apache/solr/cli/TestSolrCLIRunExample.java @@ -570,7 +570,8 @@ public void testSolrCloudExampleWithScriptInputs() throws Exception { RunExampleTool tool = new RunExampleTool(executor, System.in, runtime); try { - runTool(tool, toolArgs); + int status = runTool(tool, toolArgs); + assertEquals("tool output: " + runtime.getOutput(), 0, status); } catch (Exception e) { System.err.println( "RunExampleTool failed due to: " @@ -635,6 +636,32 @@ public void testSolrCloudExampleWithScriptInputs() throws Exception { executor.execute(CommandLine.parse("bin/solr stop -p " + bindPort)); } + /** A scripted port that is already in use fails fast instead of consuming later answers. */ + @Test + public void testScriptInputsPortInUse() throws Exception { + Path solrHomeDir = ExternalPaths.SERVER_HOME; + if (!Files.isDirectory(solrHomeDir)) + fail(solrHomeDir + " not found and is required to run this test!"); + + try (ServerSocket busy = new ServerSocket(0)) { + String[] toolArgs = + new String[] { + "--example", + "cloud", + "--server-dir", + solrHomeDir.getParent().toString(), + "--example-dir", + createTempDir().toString(), + "--script-inputs", + "1," + busy.getLocalPort() + ",\"coll\",2,2,_default" + }; + RunExampleTool tool = + new RunExampleTool( + new RunExampleExecutor(), System.in, new CLITestHelper.TestingRuntime(true)); + assertEquals(1, runTool(tool, toolArgs)); + } + } + @Test public void testFailExecuteScript() throws Exception { Path solrHomeDir = ExternalPaths.SERVER_HOME;