@Test public void testExecuteCommand() { ArrayList<String> stdOut = new ArrayList(); ArrayList<String> stdErr = new ArrayList(); try { sensor.executeCommand(Arrays.asList("invalid-command", "bar"), stdOut, stdErr); fail("Invalid/unknown command executed"); } catch (Exception e) { assertTrue(e instanceof IOException); } try { stdOut.clear(); stdErr.clear(); ArrayList<String> command = new ArrayList<>(); if (System.getProperty("os.name").toLowerCase().contains("windows")) { command.add(new File(getClass().getResource("/scripts/echo.cmd").getFile()).getAbsolutePath()); } else { String path = new File(getClass().getResource("/scripts/echo.sh").getFile()).getAbsolutePath(); command.add(path); setShellRights(path); } command.add("foo"); assertEquals(0, sensor.executeCommand(command, stdOut, stdErr)); assertEquals(1, stdOut.size()); assertEquals("foo", stdOut.get(0)); assertEquals(0, stdErr.size()); stdOut.clear(); stdErr.clear(); assertEquals(0, sensor.executeCommand(Arrays.asList("java", "-version"), stdOut, stdErr)); assertEquals(0, stdOut.size()); assertNotEquals(0, stdErr.size()); } catch (Exception e) { fail("Command not executed: " + e.getMessage()); } }
protected int executeCommand(List<String> command, List<String> stdOut, List<String> errOut) throws InterruptedException, IOException { assert stdOut != null; assert errOut != null; LOGGER.debug("Executing command: {}", command); try { ProcessBuilder pb = new ProcessBuilder(command); LOGGER.debug("Work directory: {}", fileSystem.baseDir()); pb.directory(fileSystem.baseDir()); Process p = pb.start(); LineInputReader stdOutputReader = new LineInputReader(p.getInputStream()); stdOutputReader.start(); while (!stdOutputReader.isReady()) { Thread.sleep(100); } LineInputReader errOutputReader = new LineInputReader(p.getErrorStream()); errOutputReader.start(); while (!errOutputReader.isReady()) { Thread.sleep(100); } int status = p.waitFor(); stdOut.addAll(stdOutputReader.getOutput()); errOut.addAll(errOutputReader.getOutput()); return status; } catch (InterruptedException|IOException e) { LOGGER.error("Error executing command: {}", e.getMessage()); LOGGER.debug("Stack trace:", e); throw e; } }
AbstractAnsibleSensor implements Sensor { protected int executeCommand(List<String> command, List<String> stdOut, List<String> errOut) throws InterruptedException, IOException { assert stdOut != null; assert errOut != null; LOGGER.debug("Executing command: {}", command); try { ProcessBuilder pb = new ProcessBuilder(command); LOGGER.debug("Work directory: {}", fileSystem.baseDir()); pb.directory(fileSystem.baseDir()); Process p = pb.start(); LineInputReader stdOutputReader = new LineInputReader(p.getInputStream()); stdOutputReader.start(); while (!stdOutputReader.isReady()) { Thread.sleep(100); } LineInputReader errOutputReader = new LineInputReader(p.getErrorStream()); errOutputReader.start(); while (!errOutputReader.isReady()) { Thread.sleep(100); } int status = p.waitFor(); stdOut.addAll(stdOutputReader.getOutput()); errOut.addAll(errOutputReader.getOutput()); return status; } catch (InterruptedException|IOException e) { LOGGER.error("Error executing command: {}", e.getMessage()); LOGGER.debug("Stack trace:", e); throw e; } } }
AbstractAnsibleSensor implements Sensor { protected int executeCommand(List<String> command, List<String> stdOut, List<String> errOut) throws InterruptedException, IOException { assert stdOut != null; assert errOut != null; LOGGER.debug("Executing command: {}", command); try { ProcessBuilder pb = new ProcessBuilder(command); LOGGER.debug("Work directory: {}", fileSystem.baseDir()); pb.directory(fileSystem.baseDir()); Process p = pb.start(); LineInputReader stdOutputReader = new LineInputReader(p.getInputStream()); stdOutputReader.start(); while (!stdOutputReader.isReady()) { Thread.sleep(100); } LineInputReader errOutputReader = new LineInputReader(p.getErrorStream()); errOutputReader.start(); while (!errOutputReader.isReady()) { Thread.sleep(100); } int status = p.waitFor(); stdOut.addAll(stdOutputReader.getOutput()); errOut.addAll(errOutputReader.getOutput()); return status; } catch (InterruptedException|IOException e) { LOGGER.error("Error executing command: {}", e.getMessage()); LOGGER.debug("Stack trace:", e); throw e; } } protected  AbstractAnsibleSensor(FileSystem fileSystem); }
AbstractAnsibleSensor implements Sensor { protected int executeCommand(List<String> command, List<String> stdOut, List<String> errOut) throws InterruptedException, IOException { assert stdOut != null; assert errOut != null; LOGGER.debug("Executing command: {}", command); try { ProcessBuilder pb = new ProcessBuilder(command); LOGGER.debug("Work directory: {}", fileSystem.baseDir()); pb.directory(fileSystem.baseDir()); Process p = pb.start(); LineInputReader stdOutputReader = new LineInputReader(p.getInputStream()); stdOutputReader.start(); while (!stdOutputReader.isReady()) { Thread.sleep(100); } LineInputReader errOutputReader = new LineInputReader(p.getErrorStream()); errOutputReader.start(); while (!errOutputReader.isReady()) { Thread.sleep(100); } int status = p.waitFor(); stdOut.addAll(stdOutputReader.getOutput()); errOut.addAll(errOutputReader.getOutput()); return status; } catch (InterruptedException|IOException e) { LOGGER.error("Error executing command: {}", e.getMessage()); LOGGER.debug("Stack trace:", e); throw e; } } protected  AbstractAnsibleSensor(FileSystem fileSystem);  }
AbstractAnsibleSensor implements Sensor { protected int executeCommand(List<String> command, List<String> stdOut, List<String> errOut) throws InterruptedException, IOException { assert stdOut != null; assert errOut != null; LOGGER.debug("Executing command: {}", command); try { ProcessBuilder pb = new ProcessBuilder(command); LOGGER.debug("Work directory: {}", fileSystem.baseDir()); pb.directory(fileSystem.baseDir()); Process p = pb.start(); LineInputReader stdOutputReader = new LineInputReader(p.getInputStream()); stdOutputReader.start(); while (!stdOutputReader.isReady()) { Thread.sleep(100); } LineInputReader errOutputReader = new LineInputReader(p.getErrorStream()); errOutputReader.start(); while (!errOutputReader.isReady()) { Thread.sleep(100); } int status = p.waitFor(); stdOut.addAll(stdOutputReader.getOutput()); errOut.addAll(errOutputReader.getOutput()); return status; } catch (InterruptedException|IOException e) { LOGGER.error("Error executing command: {}", e.getMessage()); LOGGER.debug("Stack trace:", e); throw e; } } protected  AbstractAnsibleSensor(FileSystem fileSystem);   }