@Override public int exec(ShellTool ssh, Map<String, ?> flags, List<String> cmds, Map<String, ?> env) { return ssh.execScript(flags, cmds, env); }}); }
protected String execScript(Map<String, ?> props, ShellTool tool, List<String> cmds, Map<String,?> env) { Map<String, Object> props2 = new LinkedHashMap<String, Object>(props); ByteArrayOutputStream out = new ByteArrayOutputStream(); props2.put("out", out); int exitcode = tool.execScript(props2, cmds, env); String outstr = new String(out.toByteArray()); assertEquals(exitcode, 0, outstr); return outstr; }
@Test(groups = {"Integration"}) public void testExecScriptAbortsOnCommandFailure() throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); int exitcode = tool.execScript(ImmutableMap.of("out", out), ImmutableList.of("export MYPROP=myval", "acmdthatdoesnotexist", "echo val is $MYPROP")); String outstr = new String(out.toByteArray()); assertFalse(outstr.contains("val is myval"), "out="+out); assertNotEquals(exitcode, 0); }
@Test(groups = {"Integration"}) @Deprecated // tests deprecated code public void testExecScriptCapturesStderr() throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream err = new ByteArrayOutputStream(); String nonExistantCmd = "acmdthatdoesnotexist"; tool.execScript(ImmutableMap.of("out", out, "err", err), ImmutableList.of(nonExistantCmd)); assertTrue(new String(err.toByteArray()).contains(nonExistantCmd+": command not found"), "out="+out+"; err="+err); }
@Test(groups = {"Integration"}) public void testExecScriptReturningNonZeroExitCode() throws Exception { int exitcode = tool.execScript(MutableMap.<String,Object>of(), ImmutableList.of("exit 123")); assertEquals(exitcode, 123); }
@Test(groups = {"Integration"}) public void testExecScriptReturningZeroExitCode() throws Exception { int exitcode = tool.execScript(MutableMap.<String,Object>of(), ImmutableList.of("date")); assertEquals(exitcode, 0); }
@Test(groups = {"Integration"}) public void testStdErr() { final ShellTool localtool = newTool(); connect(localtool); 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); assertFalse(out.toString().contains("hello err"), "hello found where it shouldn't have been, in stdout: "+out); assertTrue(err.toString().contains("hello err"), "no hello in stderr: "+err); assertEquals(0, exitcode); }
@Test(groups = {"Integration"}) public void testRunAsRoot() { final ShellTool localtool = newTool(); connect(localtool); Map<String,Object> props = new LinkedHashMap<String, Object>(); ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream err = new ByteArrayOutputStream(); props.put("out", out); props.put("err", err); props.put(SshTool.PROP_RUN_AS_ROOT.getName(), true); int exitcode = localtool.execScript(props, Arrays.asList("whoami"), null); assertTrue(out.toString().contains("root"), "not running as root; whoami is: "+out+" (err is '"+err+"')"); assertEquals(0, exitcode); }
@Test(groups = {"Integration"}) public void testExecScriptEchosDontExecuteWhenToldNoExtraOutput() throws Exception { final ShellTool localtool = newTool(); connect(localtool); Map<String,Object> props = new LinkedHashMap<String, Object>(); ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream err = new ByteArrayOutputStream(); props.put("out", out); props.put("err", err); props.put(SshTool.PROP_NO_EXTRA_OUTPUT.getName(), true); int exitcode = localtool.execScript(props, Arrays.asList("echo hello world"), null); assertFalse(out.toString().contains("Executed"), "Executed should not have displayed: "+out); assertEquals(out.toString().trim(), "hello world"); assertEquals(0, exitcode); }
@Override public void run() { try { Stopwatch stopwatch = Stopwatch.createStarted(); int exitStatus = tool.execScript( ImmutableMap.of(SshjTool.PROP_EXEC_ASYNC.getName(), true, SshjTool.PROP_EXEC_TIMEOUT.getName(), Duration.millis(1)), ImmutableList.of("sleep 63"), ImmutableMap.<String,String>of()); assertEquals(exitStatus, 143 /* 128 + Signal number (SIGTERM) */); long seconds = stopwatch.elapsed(TimeUnit.SECONDS); assertTrue(seconds < 30, "exec took "+seconds+" seconds"); } catch (Throwable t) { error.set(t); } }});
@Test(groups = {"Integration"}) public void testAsyncExecReturnsExitCode() throws Exception { boolean origFeatureEnablement = BrooklynFeatureEnablement.enable(BrooklynFeatureEnablement.FEATURE_SSH_ASYNC_EXEC); try { int exitCode = tool.execScript( ImmutableMap.of(SshjTool.PROP_EXEC_ASYNC.getName(), true), ImmutableList.of("exit 123"), ImmutableMap.<String,String>of()); assertEquals(exitCode, 123); } finally { BrooklynFeatureEnablement.setEnablement(BrooklynFeatureEnablement.FEATURE_SSH_ASYNC_EXEC, origFeatureEnablement); } }
@Test(groups = {"Integration"}) public void testAllocatePty() { final ShellTool localtool = newTool(MutableMap.of("host", "localhost", SshTool.PROP_ALLOCATE_PTY.getName(), true)); connect(localtool); 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); assertTrue(out.toString().contains("hello err"), "no hello in output: "+out+" (err is '"+err+"')"); assertFalse(err.toString().contains("hello err"), "hello found in stderr: "+err); assertEquals(0, exitcode); }
@Test(groups = {"Integration"}) public void testRunAsRootWithAuthSudo() { final ShellTool localtool = newTool(); connect(localtool); Map<String,Object> props = new LinkedHashMap<String, Object>(); ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream err = new ByteArrayOutputStream(); props.put("out", out); props.put("err", err); props.put(SshTool.PROP_RUN_AS_ROOT.getName(), true); props.put(SshTool.PROP_AUTH_SUDO.getName(), true); props.put(SshTool.PROP_PASSWORD.getName(), "dummypa55w0rd"); int exitcode = localtool.execScript(props, Arrays.asList("whoami"), null); assertTrue(out.toString().contains("root"), "not running as root; whoami is: "+out+" (err is '"+err+"')"); assertEquals(0, exitcode); }
int exitCode = tool.execScript( ImmutableMap.of( "out", out,
@Test(groups = {"Integration"}) public void testAsyncExecTimesOut() throws Exception { Stopwatch stopwatch = Stopwatch.createStarted(); boolean origFeatureEnablement = BrooklynFeatureEnablement.enable(BrooklynFeatureEnablement.FEATURE_SSH_ASYNC_EXEC); try { tool.execScript( ImmutableMap.of(SshjTool.PROP_EXEC_ASYNC.getName(), true, SshjTool.PROP_EXEC_TIMEOUT.getName(), Duration.millis(1)), ImmutableList.of("sleep 60"), ImmutableMap.<String,String>of()); fail(); } catch (Exception e) { TimeoutException te = Exceptions.getFirstThrowableOfType(e, TimeoutException.class); if (te == null) throw e; } finally { BrooklynFeatureEnablement.setEnablement(BrooklynFeatureEnablement.FEATURE_SSH_ASYNC_EXEC, origFeatureEnablement); } long seconds = stopwatch.elapsed(TimeUnit.SECONDS); assertTrue(seconds < 30, "exec took "+seconds+" seconds"); }
@Test(groups = {"Integration"}) public void testSshKeyWithPassphrase() throws Exception { final SshTool localtool = newTool(ImmutableMap.<String,Object>builder() .put(SshTool.PROP_HOST.getName(), "localhost") .put(SshTool.PROP_PRIVATE_KEY_FILE.getName(), SSH_KEY_WITH_PASSPHRASE) .put(SshTool.PROP_PRIVATE_KEY_PASSPHRASE.getName(), SSH_PASSPHRASE) .build()); localtool.connect(); assertEquals(tool.execScript(MutableMap.<String,Object>of(), ImmutableList.of("date")), 0); // Also needs the negative test to prove that we're really using an ssh-key with a passphrase try { final SshTool localtool2 = newTool(ImmutableMap.<String,Object>builder() .put(SshTool.PROP_HOST.getName(), "localhost") .put(SshTool.PROP_PRIVATE_KEY_FILE.getName(), SSH_KEY_WITH_PASSPHRASE) .build()); localtool2.connect(); fail(); } catch (Exception e) { SshException se = Exceptions.getFirstThrowableOfType(e, SshException.class); if (se == null) throw e; } }