@Test(timeout = 30000L) public void runResource () throws Exception { Resource resourceFile = Mockito.mock(Resource.class, RETURNS_DEEP_STUBS); String SCRIPT_NAME = randomUUID().toString(); String output = randomUUID().toString(); doReturn(SCRIPT_NAME).when(resourceFile).getFilename(); SSHClient sshClient = Mockito.mock(SSHClient.class, RETURNS_DEEP_STUBS); Session session = sshClient.startSession(); SCPFileTransfer scpFileTransfer = sshClient.newSCPFileTransfer(); String command = "/tmp/" + SCRIPT_NAME; doReturn(output).when(chaosSSHClient).runCommandInShell(session, command); doReturn(sshClient).when(chaosSSHClient).getSshClient(); assertEquals(output, chaosSSHClient.runResource(resourceFile)); }
@Override public String runResource (Resource resource) { try { log.info("Transferring resource {}", resource); getSshClient().newSCPFileTransfer().upload(new JarResourceFile(resource, true), TEMP_DIRECTORY); } catch (IOException e) { throw new ChaosException(SSH_CLIENT_TRANSFER_ERROR, e); } String shellCommand = runningDirectory + resource.getFilename(); moveFileIfNeeded(resource, shellCommand); try (Session session = getSshClient().startSession()) { log.debug("About to run {}", shellCommand); return runCommandInShell(session, shellCommand); } catch (IOException e) { throw new ChaosException(SSH_CLIENT_COMMAND_ERROR, e); } }
ChaosSSHClient implements SSHClientWrapper { @Override public String runResource (Resource resource) { try { log.info("Transferring resource {}", resource); getSshClient().newSCPFileTransfer().upload(new JarResourceFile(resource, true), TEMP_DIRECTORY); } catch (IOException e) { throw new ChaosException(SSH_CLIENT_TRANSFER_ERROR, e); } String shellCommand = runningDirectory + resource.getFilename(); moveFileIfNeeded(resource, shellCommand); try (Session session = getSshClient().startSession()) { log.debug("About to run {}", shellCommand); return runCommandInShell(session, shellCommand); } catch (IOException e) { throw new ChaosException(SSH_CLIENT_COMMAND_ERROR, e); } } }
ChaosSSHClient implements SSHClientWrapper { @Override public String runResource (Resource resource) { try { log.info("Transferring resource {}", resource); getSshClient().newSCPFileTransfer().upload(new JarResourceFile(resource, true), TEMP_DIRECTORY); } catch (IOException e) { throw new ChaosException(SSH_CLIENT_TRANSFER_ERROR, e); } String shellCommand = runningDirectory + resource.getFilename(); moveFileIfNeeded(resource, shellCommand); try (Session session = getSshClient().startSession()) { log.debug("About to run {}", shellCommand); return runCommandInShell(session, shellCommand); } catch (IOException e) { throw new ChaosException(SSH_CLIENT_COMMAND_ERROR, e); } }  }
ChaosSSHClient implements SSHClientWrapper { @Override public String runResource (Resource resource) { try { log.info("Transferring resource {}", resource); getSshClient().newSCPFileTransfer().upload(new JarResourceFile(resource, true), TEMP_DIRECTORY); } catch (IOException e) { throw new ChaosException(SSH_CLIENT_TRANSFER_ERROR, e); } String shellCommand = runningDirectory + resource.getFilename(); moveFileIfNeeded(resource, shellCommand); try (Session session = getSshClient().startSession()) { log.debug("About to run {}", shellCommand); return runCommandInShell(session, shellCommand); } 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 String runResource (Resource resource) { try { log.info("Transferring resource {}", resource); getSshClient().newSCPFileTransfer().upload(new JarResourceFile(resource, true), TEMP_DIRECTORY); } catch (IOException e) { throw new ChaosException(SSH_CLIENT_TRANSFER_ERROR, e); } String shellCommand = runningDirectory + resource.getFilename(); moveFileIfNeeded(resource, shellCommand); try (Session session = getSshClient().startSession()) { log.debug("About to run {}", shellCommand); return runCommandInShell(session, shellCommand); } 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);  }