Skip to content

Commit

Permalink
Merge pull request #62 from palantir/feature/fix-container-log-output…
Browse files Browse the repository at this point in the history
…-for-docker-compose-1-7

Feature/fix container log output for docker compose 1 7
  • Loading branch information
joelea committed May 16, 2016
2 parents f5c37c1 + c1ca809 commit ea6bb10
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

public class DefaultDockerCompose implements DockerCompose {

public static final Version VERSION_1_7_0 = Version.valueOf("1.7.0");
private static final Duration COMMAND_TIMEOUT = standardMinutes(2);
private static final Logger log = LoggerFactory.getLogger(DefaultDockerCompose.class);

Expand Down Expand Up @@ -88,16 +89,18 @@ public void rm() throws IOException, InterruptedException {
@Override
public String exec(DockerComposeExecOption dockerComposeExecOption, String containerName,
DockerComposeExecArgument dockerComposeExecArgument) throws IOException, InterruptedException {
verifyDockerComposeVersionAtLeast(Version.valueOf("1.7.0"));
verifyDockerComposeVersionAtLeast(VERSION_1_7_0, "You need at least docker-compose 1.7 to run docker-compose exec");
String[] fullArgs = constructFullDockerComposeExecArguments(dockerComposeExecOption, containerName, dockerComposeExecArgument);
return executeDockerComposeCommand(throwingOnError(), fullArgs);
}

//Current docker-compose version output format: docker-compose version 1.7.0rc1, build 1ad8866
private void verifyDockerComposeVersionAtLeast(Version targetVersion) throws IOException, InterruptedException {
private void verifyDockerComposeVersionAtLeast(Version targetVersion, String message) throws IOException, InterruptedException {
validState(version().greaterThanOrEqualTo(targetVersion), message);
}

private Version version() throws IOException, InterruptedException {
String versionOutput = executeDockerComposeCommand(throwingOnError(), "-v");
Version version = DockerComposeVersion.parseFromDockerComposeVersion(versionOutput);
validState(version.compareTo(targetVersion) >= 0, "You need at least docker-compose 1.7 to run docker-compose exec");
return DockerComposeVersion.parseFromDockerComposeVersion(versionOutput);
}

private String[] constructFullDockerComposeExecArguments(DockerComposeExecOption dockerComposeExecOption,
Expand Down Expand Up @@ -127,16 +130,24 @@ public Container container(String containerName) {
*/
@Override
public boolean writeLogs(String container, OutputStream output) throws IOException {
Process executedProcess = rawExecutable.execute("logs", "--no-color", container);
IOUtils.copy(executedProcess.getInputStream(), output);
try {
Process executedProcess = followLogs(container);
IOUtils.copy(executedProcess.getInputStream(), output);
executedProcess.waitFor(COMMAND_TIMEOUT.getMillis(), MILLISECONDS);
} catch (InterruptedException e) {
return false;
}
return true;
}

private Process followLogs(String container) throws IOException, InterruptedException {
if (version().greaterThanOrEqualTo(VERSION_1_7_0)) {
return rawExecutable.execute("logs", "--no-color", "--follow", container);
}

return rawExecutable.execute("logs", "--no-color", container);
}

@Override
public Ports ports(String service) throws IOException, InterruptedException {
String psOutput = executeDockerComposeCommand(throwingOnError(), "ps", service);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,26 @@ public void ps_parses_and_returns_container_names() throws IOException, Interrup

@Test
public void logs_calls_docker_compose_with_no_colour_flag() throws IOException, InterruptedException {
when(executedProcess.getInputStream()).thenReturn(toInputStream("logs"));
when(executedProcess.getInputStream()).thenReturn(
toInputStream("docker-compose version 1.5.6, build 1ad8866"),
toInputStream("logs"));
ByteArrayOutputStream output = new ByteArrayOutputStream();
compose.writeLogs("db", output);
verify(executor).execute("logs", "--no-color", "db");
assertThat(new String(output.toByteArray(), StandardCharsets.UTF_8), is("logs"));
}

@Test
public void logs_calls_docker_compose_with_the_follow_flag_when_the_version_is_at_least_1_7_0() throws IOException, InterruptedException {
when(executedProcess.getInputStream()).thenReturn(
toInputStream("docker-compose version 1.7.0, build 1ad8866"),
toInputStream("logs"));
ByteArrayOutputStream output = new ByteArrayOutputStream();
compose.writeLogs("db", output);
verify(executor).execute("logs", "--no-color", "--follow", "db");
assertThat(new String(output.toByteArray(), StandardCharsets.UTF_8), is("logs"));
}

@Test
public void when_kill_exits_with_a_non_zero_exit_code_an_exception_is_thrown() throws IOException, InterruptedException {
when(executedProcess.exitValue()).thenReturn(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public void return_equals_for_the_same_version_strings() throws Exception {

@Test
public void remove_non_digits_when_passing_version_string() {
assertEquals(Version.valueOf("1.7.0"), DockerComposeVersion.parseFromDockerComposeVersion("docker-compose version 1.7.0rc1, build 1ad8866"));
assertEquals(
DockerComposeVersion.parseFromDockerComposeVersion("docker-compose version 1.7.0rc1, build 1ad8866"),
Version.valueOf("1.7.0"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package com.palantir.docker.compose.logging;

import static com.palantir.docker.compose.connection.waiting.HealthChecks.toHaveAllPortsOpen;
import static com.palantir.docker.compose.matchers.IOMatchers.file;
import static com.palantir.docker.compose.matchers.IOMatchers.fileWithConents;
import static com.palantir.docker.compose.matchers.IOMatchers.matchingPattern;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
Expand Down Expand Up @@ -53,8 +53,10 @@ public void logs_can_be_saved_to_a_directory() throws IOException, InterruptedEx
} finally {
loggingComposition.after();
}
assertThat(new File(logFolder.getRoot(), "db.log"), is(file(matchingPattern(".*Attaching to \\w+_db_1.*"))));
assertThat(new File(logFolder.getRoot(), "db2.log"), is(file(matchingPattern(".*Attaching to \\w+_db2_1.*"))));
assertThat(new File(logFolder.getRoot(), "db.log"), is(fileWithConents(matchingPattern(
".*Attaching to \\w+_db_1.*server started.*"))));
assertThat(new File(logFolder.getRoot(), "db2.log"), is(fileWithConents(matchingPattern(
".*Attaching to \\w+_db2_1.*server started.*"))));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected boolean matchesSafely() {
}

public static Matcher<File> fileContainingString(String contents) {
return file(containsString(contents));
return fileWithConents(containsString(contents));
}

public static Matcher<String> matchingPattern(String patternStr) {
Expand All @@ -94,7 +94,7 @@ protected boolean matchesSafely(String text, Description mismatchDescription) {
Pattern pattern = Pattern.compile(patternStr, Pattern.DOTALL);
boolean matches = pattern.matcher(text).matches();
if (!matches) {
mismatchDescription.appendText("not matching " + patternStr);
mismatchDescription.appendText(text);
}
return matches;
}
Expand All @@ -106,7 +106,7 @@ public void describeTo(Description description) {
};
}

public static Matcher<File> file(Matcher<String> contentsMatcher) {
public static Matcher<File> fileWithConents(Matcher<String> contentsMatcher) {
return new FeatureMatcher<File, String>(contentsMatcher, "file contents", "file contents") {

@Override
Expand Down

0 comments on commit ea6bb10

Please sign in to comment.