@Test(groups="Integration") @SuppressWarnings("resource") public void testWhichKnife() throws IOException, InterruptedException { // requires that knife is installed on the path of login shells Process p = Runtime.getRuntime().exec(new String[] { "bash", "-l", "-c", "which knife" }); ByteArrayOutputStream out = new ByteArrayOutputStream(); new StreamGobbler(p.getInputStream(), out, log).start(); new StreamGobbler(p.getErrorStream(), out, log).start(); log.info("bash -l -c 'which knife' gives exit code: "+p.waitFor()); Time.sleep(Duration.millis(1000)); log.info("output:\n"+out); Assert.assertEquals(p.exitValue(), 0); }
@Test(groups="Integration") @SuppressWarnings("resource") public void testWhichKnife() throws IOException, InterruptedException { // requires that knife is installed on the path of login shells Process p = Runtime.getRuntime().exec(new String[] { "bash", "-l", "-c", "which knife" }); ByteArrayOutputStream out = new ByteArrayOutputStream(); new StreamGobbler(p.getInputStream(), out, log).start(); new StreamGobbler(p.getErrorStream(), out, log).start(); log.info("bash -l -c 'which knife' gives exit code: "+p.waitFor()); Time.sleep(Duration.millis(1000)); log.info("output:\n"+out); Assert.assertEquals(p.exitValue(), 0); }
@Test public void testGobbleStream() throws Exception { byte[] bytes = new byte[] {'a','b','c'}; InputStream stream = new ByteArrayInputStream(bytes); ByteArrayOutputStream out = new ByteArrayOutputStream(); StreamGobbler gobbler = new StreamGobbler(stream, out, null); gobbler.start(); try { gobbler.join(10*1000); assertFalse(gobbler.isAlive()); assertEquals(new String(out.toByteArray()), "abc" + NL); } finally { gobbler.close(); gobbler.interrupt(); } }
PipedInputStream insO = new PipedInputStream(); OutputStream outO = new PipedOutputStream(insO); PipedInputStream insE = new PipedInputStream(); OutputStream outE = new PipedOutputStream(insE); StreamGobbler sgsO = new StreamGobbler(insO, null, LOG); sgsO.setLogPrefix("[curl @ "+address+":stdout] ").start(); StreamGobbler sgsE = new StreamGobbler(insE, null, LOG); sgsE.setLogPrefix("[curl @ "+address+":stdout] ").start(); Map<String, ?> sshProps = MutableMap.<String, Object>builder().putAll(props).put("out", outO).put("err", outE).build(); int result = execScript(sshProps, "copying remote resource "+url+" to server", ImmutableList.of(
@Test public void testGobbleMultiLineBlockingStream() throws Exception { PipedOutputStream pipedOutputStream = new PipedOutputStream(); PipedInputStream stream = new PipedInputStream(pipedOutputStream); ByteArrayOutputStream out = new ByteArrayOutputStream(); StreamGobbler gobbler = new StreamGobbler(stream, out, null); gobbler.start(); try { pipedOutputStream.write("line1\n".getBytes()); pipedOutputStream.flush(); assertEqualsEventually(out, "line1" + NL); pipedOutputStream.write("line2\n".getBytes()); pipedOutputStream.flush(); assertEqualsEventually(out, "line1" + NL + "line2" + NL); pipedOutputStream.write("line".getBytes()); pipedOutputStream.write("3\n".getBytes()); pipedOutputStream.flush(); assertEqualsEventually(out, "line1" + NL + "line2" + NL + "line3" + NL); pipedOutputStream.close(); gobbler.join(10*1000); assertFalse(gobbler.isAlive()); assertEquals(new String(out.toByteArray()), "line1" + NL + "line2" + NL + "line3" + NL); } finally { gobbler.close(); gobbler.interrupt(); } }