@Override protected SshTool newTool(Map<String,?> flags) { return (SshTool) super.newTool(flags); }
protected ShellTool newTool() { return newTool(MutableMap.<String,Object>of()); }
@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); }
@BeforeMethod(alwaysRun=true) public void setUp() throws Exception { localFilePath = "/tmp/ssh-test-local-"+Identifiers.makeRandomId(8); filesCreated = new ArrayList<String>(); filesCreated.add(localFilePath); tool = newTool(); connect(tool); }
@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); }
@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 = {"WIP", "Integration"}) public void testExecScriptBigConcurrentSleepyCommand() throws Exception { ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool()); List<ListenableFuture<?>> futures = new ArrayList<ListenableFuture<?>>(); try { long starttime = System.currentTimeMillis(); 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("sleep 2", "export MYPROP="+bigstring, "echo val is $MYPROP")); assertTrue(out.contains("val is "+bigstring), "out="+out); }})); } Futures.allAsList(futures).get(); long runtime = System.currentTimeMillis() - starttime; long OVERHEAD = 20*1000; assertTrue(runtime < 2000+OVERHEAD, "runtime="+runtime); } finally { executor.shutdownNow(); } }
@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(); } }