StackAdvisorRunner { public void runScript(String script, StackAdvisorCommandType saCommandType, File actionDirectory) throws StackAdvisorException { LOG.info(String.format("Script=%s, actionDirectory=%s, command=%s", script, actionDirectory, saCommandType)); String outputFile = actionDirectory + File.separator + "stackadvisor.out"; String errorFile = actionDirectory + File.separator + "stackadvisor.err"; ProcessBuilder builder = prepareShellCommand(script, saCommandType, actionDirectory, outputFile, errorFile); try { Process process = builder.start(); try { LOG.info("Stack-advisor output={}, error={}", outputFile, errorFile); int exitCode = process.waitFor(); String outMessage; String errMessage = null; try { outMessage = FileUtils.readFileToString(new File(outputFile)).trim(); errMessage = FileUtils.readFileToString(new File(errorFile)).trim(); LOG.info("Stack advisor output files"); LOG.info(" advisor script stdout: {}", outMessage); LOG.info(" advisor script stderr: {}", errMessage); } catch (IOException io) { LOG.error("Error in reading script log files", io); } if (exitCode > 0) { String errorMessage; if (errMessage != null) { int index = errMessage.lastIndexOf("\n"); if (index > 0 && index == (errMessage.length() - 1)) { index = errMessage.lastIndexOf("\n", index - 1); } if (index > -1) { errMessage = errMessage.substring(index + 1).trim(); } errorMessage = "Stack Advisor reported an error: " + errMessage; } else { errorMessage = "Error occurred during stack advisor execution"; } errorMessage += "\nStdOut file: " + outputFile + "\n"; errorMessage += "\nStdErr file: " + errorFile; switch (exitCode) { case 1: throw new StackAdvisorRequestException(errorMessage); case 2: throw new StackAdvisorException(errorMessage); } } } finally { process.destroy(); } } catch (StackAdvisorException ex) { throw ex; } catch (Exception ioe) { String message = "Error executing stack advisor: "; LOG.error(message, ioe); throw new StackAdvisorException(message + ioe.getMessage()); } }  void runScript(String script, StackAdvisorCommandType saCommandType, File actionDirectory);  }
@Test(expected = StackAdvisorException.class) public void testRunScript_processStartThrowsException_returnFalse() throws Exception { String script = "echo"; StackAdvisorCommandType saCommandType = StackAdvisorCommandType.RECOMMEND_COMPONENT_LAYOUT; File actionDirectory = temp.newFolder("actionDir"); ProcessBuilder processBuilder = createNiceMock(ProcessBuilder.class); StackAdvisorRunner saRunner = new StackAdvisorRunner(); stub(PowerMock.method(StackAdvisorRunner.class, "prepareShellCommand")) .toReturn(processBuilder); expect(processBuilder.start()).andThrow(new IOException()); replay(processBuilder); saRunner.runScript(script, saCommandType, actionDirectory); }
@Test(expected = StackAdvisorRequestException.class) public void testRunScript_processExitCode1_returnFalse() throws Exception { String script = "echo"; StackAdvisorCommandType saCommandType = StackAdvisorCommandType.RECOMMEND_COMPONENT_LAYOUT; File actionDirectory = temp.newFolder("actionDir"); ProcessBuilder processBuilder = createNiceMock(ProcessBuilder.class); Process process = createNiceMock(Process.class); StackAdvisorRunner saRunner = new StackAdvisorRunner(); stub(PowerMock.method(StackAdvisorRunner.class, "prepareShellCommand")) .toReturn(processBuilder); expect(processBuilder.start()).andReturn(process); expect(process.waitFor()).andReturn(1); replay(processBuilder, process); saRunner.runScript(script, saCommandType, actionDirectory); }
@Test(expected = StackAdvisorException.class) public void testRunScript_processExitCode2_returnFalse() throws Exception { String script = "echo"; StackAdvisorCommandType saCommandType = StackAdvisorCommandType.RECOMMEND_COMPONENT_LAYOUT; File actionDirectory = temp.newFolder("actionDir"); ProcessBuilder processBuilder = createNiceMock(ProcessBuilder.class); Process process = createNiceMock(Process.class); StackAdvisorRunner saRunner = new StackAdvisorRunner(); stub(PowerMock.method(StackAdvisorRunner.class, "prepareShellCommand")) .toReturn(processBuilder); expect(processBuilder.start()).andReturn(process); expect(process.waitFor()).andReturn(2); replay(processBuilder, process); saRunner.runScript(script, saCommandType, actionDirectory); }
@Test public void testRunScript_processExitCodeZero_returnTrue() throws Exception { String script = "echo"; StackAdvisorCommandType saCommandType = StackAdvisorCommandType.RECOMMEND_COMPONENT_LAYOUT; File actionDirectory = temp.newFolder("actionDir"); ProcessBuilder processBuilder = createNiceMock(ProcessBuilder.class); Process process = createNiceMock(Process.class); StackAdvisorRunner saRunner = new StackAdvisorRunner(); stub(PowerMock.method(StackAdvisorRunner.class, "prepareShellCommand")) .toReturn(processBuilder); expect(processBuilder.start()).andReturn(process); expect(process.waitFor()).andReturn(0); replay(processBuilder, process); try { saRunner.runScript(script, saCommandType, actionDirectory); } catch (StackAdvisorException ex) { fail("Should not fail with StackAdvisorException"); } }