protected SshException propagate(Exception e, String message) throws SshException { Exceptions.propagateIfFatal(e); throw new SshException("(" + toString() + ") " + message + ": " + e.getMessage(), e); }
@Test(groups = {"Integration"}) public void testConnectWithInvalidUserThrowsException() throws Exception { final ShellTool localtool = newTool(ImmutableMap.of("user", "wronguser", "host", "localhost", "privateKeyFile", SSH_DEFAULT_KEYFILE)); tools.add(localtool); try { connect(localtool); fail(); } catch (SshException e) { if (!e.toString().contains("failed to connect")) throw e; } }
@Override @Test(enabled=false, groups = {"Integration"}) public void testConnectWithInvalidUserThrowsException() throws Exception { final SshTool localtool = newTool(ImmutableMap.of("user", "wronguser", "host", "localhost", "privateKeyFile", "~/.ssh/id_rsa")); tools.add(localtool); try { localtool.connect(); int result = localtool.execScript(ImmutableMap.<String,Object>of(), ImmutableList.of("date")); fail("exitCode="+result+", but expected exception"); } catch (SshException e) { if (!e.toString().contains("failed to connect")) throw e; } }
throw new SshException("exit code 255 from CLI ssh; probably failed to connect");
@Test(groups = {"Integration"}) public void testGivesUpAfterMaxRetries() throws Exception { final AtomicInteger callCount = new AtomicInteger(); final SshTool localtool = new SshjTool(ImmutableMap.of("sshTries", 3, "host", "localhost", "privateKeyFile", "~/.ssh/id_rsa")) { @Override protected SshAction<Session> newSessionAction() { callCount.incrementAndGet(); throw new RuntimeException("Simulating ssh execution failure"); } }; tools.add(localtool); try { localtool.execScript(ImmutableMap.<String,Object>of(), ImmutableList.of("true")); fail(); } catch (SshException e) { if (!e.toString().contains("out of retries")) throw e; assertEquals(callCount.get(), 3); } }
@Test(groups = {"Integration"}) public void testFlags() throws Exception { final SshTool localtool = newTool(ImmutableMap.of("sshFlags", "-vvv -tt", "host", "localhost")); tools.add(localtool); try { localtool.connect(); Map<String,Object> props = new LinkedHashMap<String, Object>(); ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream err = new ByteArrayOutputStream(); props.put("out", out); props.put("err", err); int exitcode = localtool.execScript(props, Arrays.asList("echo hello err > /dev/stderr"), null); Assert.assertEquals(0, exitcode, "exitCode="+exitcode+", but expected 0"); log.debug("OUT from ssh -vvv command is: "+out); log.debug("ERR from ssh -vvv command is: "+err); assertFalse(err.toString().contains("hello err"), "hello found where it shouldn't have been, in stderr (should have been tty merged to stdout): "+err); assertTrue(out.toString().contains("hello err"), "no hello in stdout: "+err); // look for word 'ssh' to confirm we got verbose output assertTrue(err.toString().toLowerCase().contains("ssh"), "no mention of ssh in stderr: "+err); } catch (SshException e) { if (!e.toString().contains("failed to connect")) throw e; } }