From 7c5656f77d173a263de27176ed3f77945753470f Mon Sep 17 00:00:00 2001 From: Nicolas Marsal Date: Tue, 28 Mar 2017 14:20:50 +0200 Subject: [PATCH 1/2] no redirection of error output to standard output --- .../docker/compose/execution/DockerComposeExecutable.java | 4 ++-- .../palantir/docker/compose/execution/DockerExecutable.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/execution/DockerComposeExecutable.java b/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/execution/DockerComposeExecutable.java index 9c2daed21..1c328cc11 100644 --- a/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/execution/DockerComposeExecutable.java +++ b/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/execution/DockerComposeExecutable.java @@ -59,7 +59,7 @@ public Process execute(String... commands) throws IOException { .add(defaultDockerComposePath()) .add(commands) .build(); - return new ProcessBuilder(args).redirectErrorStream(true).start(); + return new ProcessBuilder(args).redirectErrorStream(false).start(); } }, log::trace); @@ -97,7 +97,7 @@ public Process execute(String... commands) throws IOException { return dockerConfiguration().configuredDockerComposeProcess() .command(args) - .redirectErrorStream(true) + .redirectErrorStream(false) .start(); } diff --git a/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/execution/DockerExecutable.java b/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/execution/DockerExecutable.java index 1ff5cf01e..e53542f7a 100644 --- a/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/execution/DockerExecutable.java +++ b/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/execution/DockerExecutable.java @@ -59,7 +59,7 @@ public Process execute(String... commands) throws IOException { return dockerConfiguration().configuredDockerComposeProcess() .command(args) - .redirectErrorStream(true) + .redirectErrorStream(false) .start(); } From 86cb4beccc6648301d9069d08c0c6f6098186a5e Mon Sep 17 00:00:00 2001 From: Nicolas Marsal Date: Thu, 30 Mar 2017 13:40:27 +0200 Subject: [PATCH 2/2] Update ErrorHandler interface to manage error output. Update Command to handle this new ErrorHandler --- .../docker/compose/execution/Command.java | 28 +++++++++++++------ .../execution/DefaultDockerCompose.java | 4 +-- .../compose/execution/ErrorHandler.java | 2 +- .../compose/execution/ProcessResult.java | 12 ++++++++ .../compose/execution/CommandShould.java | 2 +- 5 files changed, 36 insertions(+), 12 deletions(-) diff --git a/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/execution/Command.java b/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/execution/Command.java index 815e5b9e6..0a07584a2 100644 --- a/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/execution/Command.java +++ b/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/execution/Command.java @@ -47,16 +47,16 @@ public String execute(ErrorHandler errorHandler, String... commands) throws IOEx ProcessResult result = run(commands); if (result.exitCode() != 0) { - errorHandler.handle(result.exitCode(), result.output(), executable.commandName(), commands); + errorHandler.handle(result.exitCode(), result.output(), result.error(), executable.commandName(), commands); } return result.output(); } public static ErrorHandler throwingOnError() { - return (exitCode, output, commandName, commands) -> { + return (exitCode, output, error, commandName, commands) -> { String message = - constructNonZeroExitErrorMessage(exitCode, commandName, commands) + "\nThe output was:\n" + output; + constructNonZeroExitErrorMessage(exitCode, commandName, commands) + "\nThe output was:\n" + output + "\nThe error output was:\n" + error; throw new DockerExecutionException(message); }; } @@ -70,15 +70,17 @@ private ProcessResult run(String... commands) throws IOException, InterruptedExc Process process = executable.execute(commands); ExecutorService exec = newSingleThreadExecutor(); - Future outputProcessing = exec - .submit(() -> processOutputFrom(process)); + Future outputProcessing = exec + .submit(() -> new String[]{processOutputFrom(process), processErrorFrom(process)}); - String output = waitForResultFrom(outputProcessing); + String[] outputs = waitForResultFrom(outputProcessing); + String output = outputs[0]; + String error = outputs[1]; process.waitFor(MINUTES_TO_WAIT_AFTER_STD_OUT_CLOSES, TimeUnit.MINUTES); exec.shutdown(); - return new ProcessResult(process.exitValue(), output); + return new ProcessResult(process.exitValue(), output, error); } private String processOutputFrom(Process process) { @@ -87,7 +89,17 @@ private String processOutputFrom(Process process) { .collect(joining(System.lineSeparator())); } - private static String waitForResultFrom(Future outputProcessing) { + private String processErrorFrom(Process process) { + if (process.getErrorStream() != null) { + return asReader(process.getErrorStream()).lines() + .peek(logConsumer) + .collect(joining(System.lineSeparator())); + } else { + return null; + } + } + + private static String[] waitForResultFrom(Future outputProcessing) { try { return outputProcessing.get(HOURS_TO_WAIT_FOR_STD_OUT_TO_CLOSE, TimeUnit.HOURS); } catch (InterruptedException | ExecutionException | TimeoutException e) { diff --git a/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/execution/DefaultDockerCompose.java b/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/execution/DefaultDockerCompose.java index ab8c636cc..5966c1c84 100644 --- a/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/execution/DefaultDockerCompose.java +++ b/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/execution/DefaultDockerCompose.java @@ -213,9 +213,9 @@ public Ports ports(String service) throws IOException, InterruptedException { } private static ErrorHandler swallowingDownCommandDoesNotExist() { - return (exitCode, output, commandName, commands) -> { + return (exitCode, output, error, commandName, commands) -> { if (downCommandWasPresent(output)) { - Command.throwingOnError().handle(exitCode, output, commandName, commands); + Command.throwingOnError().handle(exitCode, output, error, commandName, commands); } log.warn("It looks like `docker-compose down` didn't work."); diff --git a/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/execution/ErrorHandler.java b/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/execution/ErrorHandler.java index 185f09b53..244052503 100644 --- a/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/execution/ErrorHandler.java +++ b/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/execution/ErrorHandler.java @@ -17,5 +17,5 @@ @FunctionalInterface public interface ErrorHandler { - void handle(int exitCode, String output, String commandName, String... commands); + void handle(int exitCode, String output, String error, String commandName, String... commands); } diff --git a/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/execution/ProcessResult.java b/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/execution/ProcessResult.java index 98f7fcd7e..0863d00fc 100644 --- a/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/execution/ProcessResult.java +++ b/docker-compose-rule-core/src/main/java/com/palantir/docker/compose/execution/ProcessResult.java @@ -18,10 +18,18 @@ public class ProcessResult { private int exitCode; private final String output; + private final String error; public ProcessResult(int exitCode, String output) { this.exitCode = exitCode; this.output = output; + this.error = null; + } + + public ProcessResult(int exitCode, String output, String error) { + this.exitCode = exitCode; + this.output = output; + this.error = error; } public int exitCode() { @@ -31,4 +39,8 @@ public int exitCode() { public String output() { return output; } + + public String error() { + return error; + } } diff --git a/docker-compose-rule-core/src/test/java/com/palantir/docker/compose/execution/CommandShould.java b/docker-compose-rule-core/src/test/java/com/palantir/docker/compose/execution/CommandShould.java index a4bbd70e0..28cfb1703 100644 --- a/docker-compose-rule-core/src/test/java/com/palantir/docker/compose/execution/CommandShould.java +++ b/docker-compose-rule-core/src/test/java/com/palantir/docker/compose/execution/CommandShould.java @@ -59,7 +59,7 @@ public void setup() throws IOException { givenTheUnderlyingProcessTerminatesWithAnExitCodeOf(expectedExitCode); dockerComposeCommand.execute(errorHandler, "rm", "-f"); - verify(errorHandler).handle(expectedExitCode, "", "docker-compose", "rm", "-f"); + verify(errorHandler).handle(expectedExitCode, "", null, "docker-compose", "rm", "-f"); } @Test public void