protected String execCommands(List<String> cmds, Map<String,?> env) { return execCommands(null, cmds, env); }
protected String execScript(List<String> cmds) { return execScript(cmds, ImmutableMap.<String,Object>of()); }
@Override protected SshTool newTool(Map<String,?> flags) { return (SshTool) super.newTool(flags); }
@Test(groups = {"Integration"}) public void testScriptHeader() { final ShellTool localtool = newTool(); connect(localtool); String out = execScript(MutableMap.of("scriptHeader", "#!/bin/bash -e\necho hello world\n"), localtool, Arrays.asList("echo goodbye world"), null); assertTrue(out.contains("goodbye world"), "no goodbye in output: "+out); assertTrue(out.contains("hello world"), "no hello in output: "+out); }
@Test(groups = {"Integration", "WIP"}) public void testExecBigConcurrentCommandWithStaggeredStart() throws Exception { // This test is to vary the concurrency of concurrent actions runExecBigConcurrentCommand(50, 100L); }
protected ShellTool newTool(Map<String,?> flags) { ShellTool t = newUnregisteredTool(flags); tools.add(t); return t; }
@Override @BeforeMethod(alwaysRun=true) public void setUp() throws Exception { super.setUp(); remoteFilePath = "/tmp/ssh-test-remote-"+Identifiers.makeRandomId(8); filesCreated.add(remoteFilePath); }
@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 testExecBigConcurrentCommand() throws Exception { runExecBigConcurrentCommand(10, 0L); }
@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); }
protected String execScript(String... cmds) { return execScript(tool, Arrays.asList(cmds)); }
protected String execCommands(String... cmds) { return execCommands(Arrays.asList(cmds)); }
protected ShellTool newTool() { return newTool(MutableMap.<String,Object>of()); }
@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); }
@Test(groups = {"Integration"}) public void testExecConsecutiveCommands() throws Exception { String out = execScript("echo run1"); String out2 = execScript("echo run2"); assertTrue(out.contains("run1"), "out="+out); assertTrue(out2.contains("run2"), "out="+out); }
protected String execCommands(List<String> cmds) { return execCommands(cmds, ImmutableMap.<String,Object>of()); }
@Test(groups = {"WIP", "Integration"}) public void testExecScriptBigConcurrentCommand() throws Exception { ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool()); List<ListenableFuture<?>> futures = new ArrayList<ListenableFuture<?>>(); try { for (int i = 0; i < 10; i++) { final ShellTool localtool = newTool(); connect(localtool); futures.add(executor.submit(new Runnable() { @Override public void run() { String bigstring = Strings.repeat("abcdefghij", 1000); // 10KB String out = execScript(localtool, ImmutableList.of("export MYPROP="+bigstring, "echo val is $MYPROP")); assertTrue(out.contains("val is "+bigstring), "outSize="+out.length()+"; out="+out); }})); } Futures.allAsList(futures).get(); } finally { executor.shutdownNow(); } }
@Test(groups = {"Integration"}) public void testExecScriptChainOfCommands() throws Exception { String out = execScript("export MYPROP=abc", "echo val is $MYPROP"); assertTrue(out.contains("val is abc"), "out="+out); }
@Test(groups = {"Integration"}) public void testExecChainOfCommands() throws Exception { String out = execCommands("MYPROP=abc", "echo val is $MYPROP"); assertEquals(out, "val is abc\n"); }