@Test public void runCommand () throws Exception { String shellCommand = randomUUID().toString(); String output = randomUUID().toString(); SSHClient sshClient = Mockito.mock(SSHClient.class, RETURNS_DEEP_STUBS); Session session = sshClient.startSession(); Session.Command command = session.exec(shellCommand); doReturn(new ByteArrayInputStream(output.getBytes())).when(command).getInputStream(); doReturn(sshClient).when(chaosSSHClient).getSshClient(); assertEquals(output, chaosSSHClient.runCommand(shellCommand).getStdOut()); }
@Override public ShellOutput runCommand (String command) { log.info("About to run command {}", command); try (Session session = getSshClient().startSession()) { session.allocateDefaultPTY(); try (Session.Command exec = session.exec(command)) { exec.join(); int exitCode = exec.getExitStatus(); String output = IOUtils.readFully(exec.getInputStream()).toString(); ShellOutput shellOutput = ShellOutput.builder().withExitCode(exitCode).withStdOut(output).build(); if (exitCode > 0) { log.debug("Command execution failed {}", v("failure", shellOutput)); } return shellOutput; } } catch (IOException e) { throw new ChaosException(SSH_CLIENT_COMMAND_ERROR, e); } }
ChaosSSHClient implements SSHClientWrapper { @Override public ShellOutput runCommand (String command) { log.info("About to run command {}", command); try (Session session = getSshClient().startSession()) { session.allocateDefaultPTY(); try (Session.Command exec = session.exec(command)) { exec.join(); int exitCode = exec.getExitStatus(); String output = IOUtils.readFully(exec.getInputStream()).toString(); ShellOutput shellOutput = ShellOutput.builder().withExitCode(exitCode).withStdOut(output).build(); if (exitCode > 0) { log.debug("Command execution failed {}", v("failure", shellOutput)); } return shellOutput; } } catch (IOException e) { throw new ChaosException(SSH_CLIENT_COMMAND_ERROR, e); } } }
ChaosSSHClient implements SSHClientWrapper { @Override public ShellOutput runCommand (String command) { log.info("About to run command {}", command); try (Session session = getSshClient().startSession()) { session.allocateDefaultPTY(); try (Session.Command exec = session.exec(command)) { exec.join(); int exitCode = exec.getExitStatus(); String output = IOUtils.readFully(exec.getInputStream()).toString(); ShellOutput shellOutput = ShellOutput.builder().withExitCode(exitCode).withStdOut(output).build(); if (exitCode > 0) { log.debug("Command execution failed {}", v("failure", shellOutput)); } return shellOutput; } } catch (IOException e) { throw new ChaosException(SSH_CLIENT_COMMAND_ERROR, e); } }  }
ChaosSSHClient implements SSHClientWrapper { @Override public ShellOutput runCommand (String command) { log.info("About to run command {}", command); try (Session session = getSshClient().startSession()) { session.allocateDefaultPTY(); try (Session.Command exec = session.exec(command)) { exec.join(); int exitCode = exec.getExitStatus(); String output = IOUtils.readFully(exec.getInputStream()).toString(); ShellOutput shellOutput = ShellOutput.builder().withExitCode(exitCode).withStdOut(output).build(); if (exitCode > 0) { log.debug("Command execution failed {}", v("failure", shellOutput)); } return shellOutput; } } catch (IOException e) { throw new ChaosException(SSH_CLIENT_COMMAND_ERROR, e); } }  @Override ShellClient connect(int connectionTimeout); @Override void close(); @Override SSHClientWrapper withSSHCredentials(SSHCredentials sshCredentials); @Override SSHClientWrapper withEndpoint(String hostname); @Override SSHClientWrapper withEndpoint(String hostname, Integer portNumber); @Override SSHClientWrapper withRunningDirectory(String runningDirectory); @Override String runResource(Resource resource); @Override ShellOutput runCommand(String command); }
ChaosSSHClient implements SSHClientWrapper { @Override public ShellOutput runCommand (String command) { log.info("About to run command {}", command); try (Session session = getSshClient().startSession()) { session.allocateDefaultPTY(); try (Session.Command exec = session.exec(command)) { exec.join(); int exitCode = exec.getExitStatus(); String output = IOUtils.readFully(exec.getInputStream()).toString(); ShellOutput shellOutput = ShellOutput.builder().withExitCode(exitCode).withStdOut(output).build(); if (exitCode > 0) { log.debug("Command execution failed {}", v("failure", shellOutput)); } return shellOutput; } } catch (IOException e) { throw new ChaosException(SSH_CLIENT_COMMAND_ERROR, e); } }  @Override ShellClient connect(int connectionTimeout); @Override void close(); @Override SSHClientWrapper withSSHCredentials(SSHCredentials sshCredentials); @Override SSHClientWrapper withEndpoint(String hostname); @Override SSHClientWrapper withEndpoint(String hostname, Integer portNumber); @Override SSHClientWrapper withRunningDirectory(String runningDirectory); @Override String runResource(Resource resource); @Override ShellOutput runCommand(String command);  }