Skip to content

Commit

Permalink
Dump container logs on shell command failure (elastic#52316)
Browse files Browse the repository at this point in the history
The Docker tests framework captures container logs when Elasticsearch
fails to start. However, it doesn't do this if a later shell command
fails. Amend the DockerShell wrapper to capture the container logs if
a command fails when it should succeed.
  • Loading branch information
pugnascotia authored Feb 19, 2020
1 parent c665cf0 commit 5d74c1f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
30 changes: 30 additions & 0 deletions qa/os/src/test/java/org/elasticsearch/packaging/util/Docker.java
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,36 @@ protected String[] getScriptCommand(String script) {

return super.getScriptCommand("docker exec --user elasticsearch:root --tty " + containerId + " " + script);
}

/**
* Overrides {@link Shell#run(String)} to attempt to collect Docker container
* logs when a command fails to execute successfully.
* @param script the command to run
* @return the command's output
*/
@Override
public Result run(String script) {
try {
return super.run(script);
} catch (ShellException e) {
try {
final Shell.Result dockerLogs = getContainerLogs();
logger.error(
"Command [{}] failed.\n\nContainer stdout: [{}]\n\nContainer stderr: [{}]",
script,
dockerLogs.stdout,
dockerLogs.stderr
);
} catch (ShellException shellException) {
logger.error(
"Command [{}] failed.\n\nTried to dump container logs but that failed too: [{}]",
script,
shellException.getMessage()
);
}
throw e;
}
}
}

/**
Expand Down
15 changes: 14 additions & 1 deletion qa/os/src/test/java/org/elasticsearch/packaging/util/Shell.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private Result runScript(String[] command) {
logger.warn("Running command with env: " + env);
Result result = runScriptIgnoreExitCode(command);
if (result.isSuccess() == false) {
throw new RuntimeException("Command was not successful: [" + String.join(" ", command) + "]\n result: " + result.toString());
throw new ShellException("Command was not successful: [" + String.join(" ", command) + "]\n result: " + result.toString());
}
return result;
}
Expand Down Expand Up @@ -266,4 +266,17 @@ public String toString() {
}
}

/**
* An exception to model failures to run a shell command. This exists so that calling code can differentiate between
* shell / command errors, and other runtime errors.
*/
public static class ShellException extends RuntimeException {
public ShellException(String message) {
super(message);
}

public ShellException(String message, Throwable cause) {
super(message, cause);
}
}
}

0 comments on commit 5d74c1f

Please sign in to comment.