Skip to content

Commit

Permalink
Merge pull request #60 from jamesneale/feature/get-output-from-dc-exec
Browse files Browse the repository at this point in the history
Return output from docker-compose exec so it can actually be used.
  • Loading branch information
hpryce committed May 12, 2016
2 parents 7b4206a + 26e1ce3 commit e482cbf
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ public void rm() throws IOException, InterruptedException {
}

@Override
public void exec(DockerComposeExecOption dockerComposeExecOption, String containerName,
public String exec(DockerComposeExecOption dockerComposeExecOption, String containerName,
DockerComposeExecArgument dockerComposeExecArgument) throws IOException, InterruptedException {
verifyDockerComposeVersionAtLeast(Version.valueOf("1.7.0"));
String[] fullArgs = constructFullDockerComposeExecArguments(dockerComposeExecOption, containerName, dockerComposeExecArgument);
executeDockerComposeCommand(throwingOnError(), fullArgs);
return executeDockerComposeCommand(throwingOnError(), fullArgs);
}

//Current docker-compose version output format: docker-compose version 1.7.0rc1, build 1ad8866
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public interface DockerCompose {
void down() throws IOException, InterruptedException;
void kill() throws IOException, InterruptedException;
void rm() throws IOException, InterruptedException;
void exec(DockerComposeExecOption dockerComposeExecOption, String containerName, DockerComposeExecArgument dockerComposeExecArgument) throws IOException, InterruptedException;
String exec(DockerComposeExecOption dockerComposeExecOption, String containerName, DockerComposeExecArgument dockerComposeExecArgument) throws IOException, InterruptedException;
ContainerNames ps() throws IOException, InterruptedException;
Container container(String containerName);
boolean writeLogs(String container, OutputStream output) throws IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public void rm() throws IOException, InterruptedException {
}

@Override
public void exec(DockerComposeExecOption dockerComposeExecOption, String containerName,
public String exec(DockerComposeExecOption dockerComposeExecOption, String containerName,
DockerComposeExecArgument dockerComposeExecArgument) throws IOException, InterruptedException {
dockerCompose.exec(dockerComposeExecOption, containerName, dockerComposeExecArgument);
return dockerCompose.exec(dockerComposeExecOption, containerName, dockerComposeExecArgument);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import org.junit.Test;
import org.junit.rules.ExpectedException;


public class DockerComposeTest {

@Rule
Expand Down Expand Up @@ -142,4 +141,32 @@ public void docker_compose_exec_fails_if_docker_compose_version_is_prior_1_7() t
compose.exec(options("-d"), "container_1", arguments("ls"));
}

@Test
public void docker_compose_exec_returns_the_output_from_the_executed_process() throws Exception {
String lsString = "-rw-r--r-- 1 user 1318458867 11326 Mar 9 17:47 LICENSE\n"
+ "-rw-r--r-- 1 user 1318458867 12570 May 12 14:51 README.md";

String versionString = "docker-compose version 1.7.0rc1, build 1ad8866";

DockerComposeExecutable processExecutor = mock(DockerComposeExecutable.class);

addProcessToExecutor(processExecutor, processWithOutput(versionString), "-v");
addProcessToExecutor(processExecutor, processWithOutput(lsString), "exec", "container_1", "ls", "-l");

DockerCompose processCompose = new DefaultDockerCompose(processExecutor, dockerMachine);

assertThat(processCompose.exec(options(), "container_1", arguments("ls", "-l")), is(lsString));
}

private void addProcessToExecutor(DockerComposeExecutable dockerComposeExecutable, Process process, String... commands) throws Exception {
when(dockerComposeExecutable.execute(commands)).thenReturn(process);
}

private Process processWithOutput(String output) {
Process mockedProcess = mock(Process.class);
when(mockedProcess.getInputStream()).thenReturn(toInputStream(output));
when(mockedProcess.exitValue()).thenReturn(0);
return mockedProcess;
}

}

0 comments on commit e482cbf

Please sign in to comment.