@Override public int exec(ShellTool tool, Map<String,?> flags, List<String> cmds, Map<String,?> env) { return tool.execCommands(flags, cmds, env); }}); }
@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; }
@Override public void run() { int exitStatus = tool.execCommands(ImmutableMap.<String,Object>of(), ImmutableList.of("ps aux| grep \"sleep 63\" | grep -v grep")); assertEquals(exitStatus, 0); }});
@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 testExecCapturesStderr() throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream err = new ByteArrayOutputStream(); String nonExistantCmd = "acmdthatdoesnotexist"; tool.execCommands(ImmutableMap.of("out", out, "err", err), ImmutableList.of(nonExistantCmd)); String errMsg = new String(err.toByteArray()); assertTrue(errMsg.contains(nonExistantCmd+": command not found\n"), "errMsg="+errMsg+"; out="+out+"; err="+err); }
@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); }
@Override @Test(groups = {"Integration"}) public void testExecReturningNonZeroExitCode() throws Exception { int exitcode = tool.execCommands(MutableMap.<String,Object>of(), ImmutableList.of("exit 123")); assertEquals(exitcode, 123); }
@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 testExecReturningNonZeroExitCode() throws Exception { int exitcode = tool.execCommands(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 testExecReturningZeroExitCode() throws Exception { int exitcode = tool.execCommands(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); }
protected String execCommands(ConfigBag config, List<String> cmds, Map<String,?> env) { ByteArrayOutputStream out = new ByteArrayOutputStream(); MutableMap<String,Object> flags = MutableMap.<String,Object>of("out", out); if (config!=null) flags.add(config.getAllConfig()); tool.execCommands(flags, cmds, env); return new String(out.toByteArray()); }
@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); }
}}); tool.execCommands(ImmutableMap.<String,Object>of(), ImmutableList.of("ps aux| grep \"sleep 63\" | grep -v grep | awk '{print($2)}' | xargs kill"));
@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 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 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); } }