Skip to content

Commit

Permalink
Merge pull request #95 from palantir/feature/docker-compose-run-support
Browse files Browse the repository at this point in the history
Add support for calling docker-compose run
  • Loading branch information
iamdanfox authored Aug 8, 2016
2 parents 55f6117 + 34d64fe commit 89c4ec5
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import com.palantir.docker.compose.execution.DockerComposeExecArgument;
import com.palantir.docker.compose.execution.DockerComposeExecOption;
import com.palantir.docker.compose.execution.DockerComposeExecutable;
import com.palantir.docker.compose.execution.DockerComposeRunArgument;
import com.palantir.docker.compose.execution.DockerComposeRunOption;
import com.palantir.docker.compose.execution.DockerExecutable;
import com.palantir.docker.compose.execution.RetryingDockerCompose;
import com.palantir.docker.compose.logging.DoNothingLogCollector;
Expand Down Expand Up @@ -165,6 +167,11 @@ public String exec(DockerComposeExecOption options, String containerName,
return dockerCompose().exec(options, containerName, arguments);
}

public String run(DockerComposeRunOption options, String containerName,
DockerComposeRunArgument arguments) throws IOException, InterruptedException {
return dockerCompose().run(options, containerName, arguments);
}

public static ImmutableDockerComposeRule.Builder builder() {
return ImmutableDockerComposeRule.builder();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import com.palantir.docker.compose.execution.DockerCompose;
import com.palantir.docker.compose.execution.DockerComposeExecArgument;
import com.palantir.docker.compose.execution.DockerComposeExecOption;
import com.palantir.docker.compose.execution.DockerComposeRunArgument;
import com.palantir.docker.compose.execution.DockerComposeRunOption;
import java.io.IOException;
import org.junit.rules.ExternalResource;

Expand Down Expand Up @@ -101,6 +103,11 @@ public String exec(DockerComposeExecOption options, String containerName, Docker
return rule.exec(options, containerName, arguments);
}

public String run(DockerComposeRunOption options, String containerName, DockerComposeRunArgument arguments)
throws IOException, InterruptedException {
return rule.run(options, containerName, arguments);
}

public DockerPort hostNetworkedPort(int port) {
return rule.hostNetworkedPort(port);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ public String exec(DockerComposeExecOption dockerComposeExecOption, String conta
return command.execute(Command.throwingOnError(), fullArgs);
}

@Override
public String run(DockerComposeRunOption dockerComposeRunOption, String containerName,
DockerComposeRunArgument dockerComposeRunArgument) throws IOException, InterruptedException {
String[] fullArgs = constructFullDockerComposeRunArguments(dockerComposeRunOption, containerName, dockerComposeRunArgument);
return command.execute(Command.throwingOnError(), fullArgs);
}

private void verifyDockerComposeVersionAtLeast(Version targetVersion, String message) throws IOException, InterruptedException {
validState(version().greaterThanOrEqualTo(targetVersion), message);
}
Expand All @@ -115,13 +122,23 @@ private Version version() throws IOException, InterruptedException {
private String[] constructFullDockerComposeExecArguments(DockerComposeExecOption dockerComposeExecOption,
String containerName, DockerComposeExecArgument dockerComposeExecArgument) {
ImmutableList<String> fullArgs = new ImmutableList.Builder<String>().add("exec")
.addAll(dockerComposeExecOption.asList())
.addAll(dockerComposeExecOption.options())
.add(containerName)
.addAll(dockerComposeExecArgument.asList())
.addAll(dockerComposeExecArgument.arguments())
.build();
return fullArgs.toArray(new String[fullArgs.size()]);
}

private String[] constructFullDockerComposeRunArguments(DockerComposeRunOption dockerComposeRunOption,
String containerName, DockerComposeRunArgument dockerComposeRunArgument) {
ImmutableList<String> fullArgs = new ImmutableList.Builder<String>().add("run")
.addAll(dockerComposeRunOption.options())
.add(containerName)
.addAll(dockerComposeRunArgument.arguments())
.build();
return fullArgs.toArray(new String[fullArgs.size()]);
}

@Override
public ContainerNames ps() throws IOException, InterruptedException {
String psOutput = command.execute(Command.throwingOnError(), "ps");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ public String exec(DockerComposeExecOption dockerComposeExecOption, String conta
return dockerCompose.exec(dockerComposeExecOption, containerName, dockerComposeExecArgument);
}

@Override
public String run(DockerComposeRunOption dockerComposeRunOption, String containerName,
DockerComposeRunArgument dockerComposeRunArgument) throws IOException, InterruptedException {
return dockerCompose.run(dockerComposeRunOption, containerName, dockerComposeRunArgument);
}

@Override
public ContainerNames ps() throws IOException, InterruptedException {
return dockerCompose.ps();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public interface DockerCompose {
void start(Container container) throws IOException, InterruptedException;
void stop(Container container) throws IOException, InterruptedException;
String exec(DockerComposeExecOption dockerComposeExecOption, String containerName, DockerComposeExecArgument dockerComposeExecArgument) throws IOException, InterruptedException;
String run(DockerComposeRunOption dockerComposeRunOption, String containerName, DockerComposeRunArgument dockerComposeRunArgument) 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 @@ -17,20 +17,14 @@

import java.util.Arrays;
import java.util.List;

import org.immutables.value.Value;

@Value.Immutable
public abstract class DockerComposeExecArgument {

@Value.Parameter
protected abstract String[] arguments();
public abstract List<String> arguments();

public static DockerComposeExecArgument arguments(String... arguments) {
return ImmutableDockerComposeExecArgument.of(arguments);
}

public List<String> asList() {
return Arrays.asList(arguments());
return ImmutableDockerComposeExecArgument.of(Arrays.asList(arguments));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,10 @@

@Value.Immutable
public abstract class DockerComposeExecOption {

@Value.Parameter
protected abstract String[] options();
public abstract List<String> options();

public static DockerComposeExecOption options(String... options) {
return ImmutableDockerComposeExecOption.of(options);
}

public List<String> asList() {
return Arrays.asList(options());
return ImmutableDockerComposeExecOption.of(Arrays.asList(options));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2016 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.palantir.docker.compose.execution;

import java.util.Arrays;
import java.util.List;
import org.immutables.value.Value;

@Value.Immutable
public abstract class DockerComposeRunArgument {
@Value.Parameter
public abstract List<String> arguments();

public static DockerComposeRunArgument arguments(String... arguments) {
return ImmutableDockerComposeRunArgument.of(Arrays.asList(arguments));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2016 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.palantir.docker.compose.execution;

import java.util.Arrays;
import java.util.List;
import org.immutables.value.Value;

@Value.Immutable
public abstract class DockerComposeRunOption {
@Value.Parameter
public abstract List<String> options();

public static DockerComposeRunOption options(String... options) {
return ImmutableDockerComposeRunOption.of(Arrays.asList(options));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import com.palantir.docker.compose.connection.State;
import com.palantir.docker.compose.connection.waiting.HealthCheck;
import com.palantir.docker.compose.connection.waiting.SuccessOrFailure;
import com.palantir.docker.compose.execution.DockerComposeRunArgument;
import com.palantir.docker.compose.execution.DockerComposeRunOption;
import java.io.IOException;
import java.util.List;
import java.util.function.Consumer;
Expand Down Expand Up @@ -145,4 +147,8 @@ public void exec_returns_output() throws Exception {
assertThat(docker.exec(options(), CONTAINERS.get(0), arguments("echo", "hello")), is("hello"));
}

@Test
public void run_returns_output() throws Exception {
assertThat(docker.run(DockerComposeRunOption.options("--entrypoint", "echo"), CONTAINERS.get(0), DockerComposeRunArgument.arguments("hello")), is("hello"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,17 @@ public void fail_if_docker_compose_version_is_prior_1_7_on_docker_compose_exec()
compose.exec(options("-d"), "container_1", arguments("ls"));
}

@Test
public void pass_concatenated_arguments_to_executor_on_docker_compose_run()
throws IOException, InterruptedException {
compose.run(DockerComposeRunOption.options("-d"), "container_1", DockerComposeRunArgument.arguments("ls"));
verify(executor, times(1)).execute("run", "-d", "container_1", "ls");
}

@Test
public void return_the_output_from_the_executed_process_on_docker_compose_exec() 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";
+ "-rw-r--r-- 1 user 1318458867 12570 May 12 14:51 README.md";

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

Expand All @@ -198,6 +205,20 @@ public void return_the_output_from_the_executed_process_on_docker_compose_exec()
assertThat(processCompose.exec(options(), "container_1", arguments("ls", "-l")), is(lsString));
}

@Test
public void return_the_output_from_the_executed_process_on_docker_compose_run() 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";

DockerComposeExecutable processExecutor = mock(DockerComposeExecutable.class);

addProcessToExecutor(processExecutor, processWithOutput(lsString), "run", "-it", "container_1", "ls", "-l");

DockerCompose processCompose = new DefaultDockerCompose(processExecutor, dockerMachine);

assertThat(processCompose.run(DockerComposeRunOption.options("-it"), "container_1", DockerComposeRunArgument.arguments("ls", "-l")), is(lsString));
}

private void addProcessToExecutor(DockerComposeExecutable dockerComposeExecutable, Process process, String... commands) throws Exception {
when(dockerComposeExecutable.execute(commands)).thenReturn(process);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,11 @@ public void calls_exec_on_the_underlying_docker_compose_and_not_invoke_retryer()
verifyRetryerWasNotUsed();
verify(dockerCompose).exec(options("-d"), CONTAINER_NAME, arguments("ls"));
}

@Test
public void calls_run_on_the_underlying_docker_compose_and_not_invoke_retryer() throws IOException, InterruptedException {
retryingDockerCompose.run(DockerComposeRunOption.options("-d"), CONTAINER_NAME, DockerComposeRunArgument.arguments("ls"));
verifyRetryerWasNotUsed();
verify(dockerCompose).run(DockerComposeRunOption.options("-d"), CONTAINER_NAME, DockerComposeRunArgument.arguments("ls"));
}
}

0 comments on commit 89c4ec5

Please sign in to comment.