@Test public void connect () throws IOException { String hostname = randomUUID().toString(); int port = new Random().nextInt(65534) + 1; SSHCredentials sshCredentials = Mockito.spy(SSHCredentials.class); SSHClient sshClient = Mockito.mock(SSHClient.class); String username = randomUUID().toString(); Collection<AuthMethod> authMethodCollection = Collections.emptyList(); doReturn(username).when(sshCredentials).getUsername(); doReturn(authMethodCollection).when(sshCredentials).getAuthMethods(); try { chaosSSHClient.connect(); fail("Expected NPE"); } catch (NullPointerException ignored) { } try { chaosSSHClient.withEndpoint(hostname, null).connect(); fail("Expected NPE"); } catch (NullPointerException ignored) { } try { chaosSSHClient.withEndpoint(hostname, port).connect(); fail("Expected NPE"); } catch (NullPointerException ignored) { } try { doReturn(sshClient).when(chaosSSHClient).buildNewSSHClient(); doNothing().when(sshClient).connect(hostname, port); doNothing().when(sshClient).auth(username, authMethodCollection); chaosSSHClient.withSSHCredentials(sshCredentials).connect(); verify(sshClient, times(1)).connect(hostname, port); verify(sshClient, times(1)).auth(username, authMethodCollection); verify(sshClient, never()).close(); } catch (NullPointerException e) { fail("Should not have an NPE now"); } reset(sshClient); doThrow(new IOException()).when(sshClient).connect(hostname, port); try { chaosSSHClient.connect(); fail("Expected a thrown ExecutionException to produce a ChaosException"); } catch (ChaosException e) { assertEquals(e.getCause().getClass(), ExecutionException.class); } reset(sshClient); doNothing().when(sshClient).connect(hostname, port); doThrow(new TransportException(Strings.EMPTY)).when(sshClient).auth(username, authMethodCollection); try { chaosSSHClient.connect(); fail("Expected a thrown TransportException to produce a ChaosException"); } catch (ChaosException e) { assertEquals(e.getCause().getClass(), TransportException.class); } verify(chaosSSHClient, times(1)).close(); reset(sshClient); doNothing().when(sshClient).connect(hostname, port); doThrow(new UserAuthException(Strings.EMPTY)).when(sshClient).auth(username, authMethodCollection); try { chaosSSHClient.connect(); fail("Expected a thrown UserAuthException to produce a ChaosException"); } catch (ChaosException e) { assertEquals(e.getCause().getClass(), UserAuthException.class); } verify(chaosSSHClient, times(2)).close(); }
@Override public ShellClient connect (int connectionTimeout) throws IOException { boolean opened = false; Objects.requireNonNull(hostname); Objects.requireNonNull(port); Objects.requireNonNull(sshCredentials); log.debug("Creating connection to {}:{} with {} second timeout", hostname, port, connectionTimeout); sshClient = buildNewSSHClient(); sshClient.addHostKeyVerifier(new PromiscuousVerifier()); sshClient.setConnectTimeout(connectionTimeout); Future<Void> connection = Executors.newSingleThreadExecutor().submit(() -> { try { sshClient.connect(hostname, port); return null; } catch (IOException e) { throw new ChaosException(SHELL_CLIENT_CONNECT_FAILURE, e); } }); try { connection.get(connectionTimeout, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new ChaosException(SHELL_CLIENT_CONNECT_FAILURE, e); } catch (ExecutionException | TimeoutException e) { throw new ChaosException(SHELL_CLIENT_CONNECT_FAILURE, e); } try { log.debug("Connection made, sending authentication"); sshClient.auth(sshCredentials.getUsername(), sshCredentials.getAuthMethods()); opened = true; } catch (UserAuthException | TransportException e) { throw new ChaosException(SHELL_CLIENT_CONNECT_FAILURE, e); } finally { if (!opened) { close(); } } return this; }
ChaosSSHClient implements SSHClientWrapper { @Override public ShellClient connect (int connectionTimeout) throws IOException { boolean opened = false; Objects.requireNonNull(hostname); Objects.requireNonNull(port); Objects.requireNonNull(sshCredentials); log.debug("Creating connection to {}:{} with {} second timeout", hostname, port, connectionTimeout); sshClient = buildNewSSHClient(); sshClient.addHostKeyVerifier(new PromiscuousVerifier()); sshClient.setConnectTimeout(connectionTimeout); Future<Void> connection = Executors.newSingleThreadExecutor().submit(() -> { try { sshClient.connect(hostname, port); return null; } catch (IOException e) { throw new ChaosException(SHELL_CLIENT_CONNECT_FAILURE, e); } }); try { connection.get(connectionTimeout, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new ChaosException(SHELL_CLIENT_CONNECT_FAILURE, e); } catch (ExecutionException | TimeoutException e) { throw new ChaosException(SHELL_CLIENT_CONNECT_FAILURE, e); } try { log.debug("Connection made, sending authentication"); sshClient.auth(sshCredentials.getUsername(), sshCredentials.getAuthMethods()); opened = true; } catch (UserAuthException | TransportException e) { throw new ChaosException(SHELL_CLIENT_CONNECT_FAILURE, e); } finally { if (!opened) { close(); } } return this; } }
ChaosSSHClient implements SSHClientWrapper { @Override public ShellClient connect (int connectionTimeout) throws IOException { boolean opened = false; Objects.requireNonNull(hostname); Objects.requireNonNull(port); Objects.requireNonNull(sshCredentials); log.debug("Creating connection to {}:{} with {} second timeout", hostname, port, connectionTimeout); sshClient = buildNewSSHClient(); sshClient.addHostKeyVerifier(new PromiscuousVerifier()); sshClient.setConnectTimeout(connectionTimeout); Future<Void> connection = Executors.newSingleThreadExecutor().submit(() -> { try { sshClient.connect(hostname, port); return null; } catch (IOException e) { throw new ChaosException(SHELL_CLIENT_CONNECT_FAILURE, e); } }); try { connection.get(connectionTimeout, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new ChaosException(SHELL_CLIENT_CONNECT_FAILURE, e); } catch (ExecutionException | TimeoutException e) { throw new ChaosException(SHELL_CLIENT_CONNECT_FAILURE, e); } try { log.debug("Connection made, sending authentication"); sshClient.auth(sshCredentials.getUsername(), sshCredentials.getAuthMethods()); opened = true; } catch (UserAuthException | TransportException e) { throw new ChaosException(SHELL_CLIENT_CONNECT_FAILURE, e); } finally { if (!opened) { close(); } } return this; }  }
ChaosSSHClient implements SSHClientWrapper { @Override public ShellClient connect (int connectionTimeout) throws IOException { boolean opened = false; Objects.requireNonNull(hostname); Objects.requireNonNull(port); Objects.requireNonNull(sshCredentials); log.debug("Creating connection to {}:{} with {} second timeout", hostname, port, connectionTimeout); sshClient = buildNewSSHClient(); sshClient.addHostKeyVerifier(new PromiscuousVerifier()); sshClient.setConnectTimeout(connectionTimeout); Future<Void> connection = Executors.newSingleThreadExecutor().submit(() -> { try { sshClient.connect(hostname, port); return null; } catch (IOException e) { throw new ChaosException(SHELL_CLIENT_CONNECT_FAILURE, e); } }); try { connection.get(connectionTimeout, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new ChaosException(SHELL_CLIENT_CONNECT_FAILURE, e); } catch (ExecutionException | TimeoutException e) { throw new ChaosException(SHELL_CLIENT_CONNECT_FAILURE, e); } try { log.debug("Connection made, sending authentication"); sshClient.auth(sshCredentials.getUsername(), sshCredentials.getAuthMethods()); opened = true; } catch (UserAuthException | TransportException e) { throw new ChaosException(SHELL_CLIENT_CONNECT_FAILURE, e); } finally { if (!opened) { close(); } } return this; }  @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 ShellClient connect (int connectionTimeout) throws IOException { boolean opened = false; Objects.requireNonNull(hostname); Objects.requireNonNull(port); Objects.requireNonNull(sshCredentials); log.debug("Creating connection to {}:{} with {} second timeout", hostname, port, connectionTimeout); sshClient = buildNewSSHClient(); sshClient.addHostKeyVerifier(new PromiscuousVerifier()); sshClient.setConnectTimeout(connectionTimeout); Future<Void> connection = Executors.newSingleThreadExecutor().submit(() -> { try { sshClient.connect(hostname, port); return null; } catch (IOException e) { throw new ChaosException(SHELL_CLIENT_CONNECT_FAILURE, e); } }); try { connection.get(connectionTimeout, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new ChaosException(SHELL_CLIENT_CONNECT_FAILURE, e); } catch (ExecutionException | TimeoutException e) { throw new ChaosException(SHELL_CLIENT_CONNECT_FAILURE, e); } try { log.debug("Connection made, sending authentication"); sshClient.auth(sshCredentials.getUsername(), sshCredentials.getAuthMethods()); opened = true; } catch (UserAuthException | TransportException e) { throw new ChaosException(SHELL_CLIENT_CONNECT_FAILURE, e); } finally { if (!opened) { close(); } } return this; }  @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);  }