Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle error output of docker commands #188

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
}
Expand All @@ -70,15 +70,17 @@ private ProcessResult run(String... commands) throws IOException, InterruptedExc
Process process = executable.execute(commands);

ExecutorService exec = newSingleThreadExecutor();
Future<String> outputProcessing = exec
.submit(() -> processOutputFrom(process));
Future<String[]> 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) {
Expand All @@ -87,7 +89,17 @@ private String processOutputFrom(Process process) {
.collect(joining(System.lineSeparator()));
}

private static String waitForResultFrom(Future<String> 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<String[]> outputProcessing) {
try {
return outputProcessing.get(HOURS_TO_WAIT_FOR_STD_OUT_TO_CLOSE, TimeUnit.HOURS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,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.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -97,7 +97,7 @@ public Process execute(String... commands) throws IOException {

return dockerConfiguration().configuredDockerComposeProcess()
.command(args)
.redirectErrorStream(true)
.redirectErrorStream(false)
.start();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public Process execute(String... commands) throws IOException {

return dockerConfiguration().configuredDockerComposeProcess()
.command(args)
.redirectErrorStream(true)
.redirectErrorStream(false)
.start();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -31,4 +39,8 @@ public int exitCode() {
public String output() {
return output;
}

public String error() {
return error;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down