public static String execCliCommand(String client, String command) throws IOException, InterruptedException { return docker.exec( DockerComposeExecOption.noOptions(), client, DockerComposeExecArgument.arguments("bash", "-c", command)); }
private static String[] constructFullDockerComposeExecArguments(DockerComposeExecOption dockerComposeExecOption, String containerName, DockerComposeExecArgument dockerComposeExecArgument) { ImmutableList<String> fullArgs = new ImmutableList.Builder<String>().add("exec") .addAll(dockerComposeExecOption.options()) .add(containerName) .addAll(dockerComposeExecArgument.arguments()) .build(); return fullArgs.toArray(new String[fullArgs.size()]); }
private static String[] constructFullDockerComposeExecArguments(DockerComposeExecOption dockerComposeExecOption, String containerName, DockerComposeExecArgument dockerComposeExecArgument) { // The "-T" option here disables pseudo-TTY allocation, which is not useful here since we are not using // terminal features here (e.g. we are not sending ^C to kill the executed process). // Disabling pseudo-TTY allocation means this will work on OS's that don't support TTY (i.e. Windows) ImmutableList<String> fullArgs = new ImmutableList.Builder<String>().add("exec") .add("-T") .addAll(dockerComposeExecOption.options()) .add(containerName) .addAll(dockerComposeExecArgument.arguments()) .build(); return fullArgs.toArray(new String[fullArgs.size()]); }
private String[] constructFullDockerComposeExecArguments(DockerComposeExecOption dockerComposeExecOption, String containerName, DockerComposeExecArgument dockerComposeExecArgument) { ImmutableList<String> fullArgs = new ImmutableList.Builder<String>().add("exec") .addAll(dockerComposeExecOption.options()) .add(containerName) .addAll(dockerComposeExecArgument.arguments()) .build(); return fullArgs.toArray(new String[fullArgs.size()]); }
@Test public void pass_concatenated_arguments_to_executor_on_docker_compose_exec() throws IOException, InterruptedException { when(executedProcess.getInputStream()).thenReturn(toInputStream("docker-compose version 1.7.0rc1, build 1ad8866")); compose.exec(options("-d"), "container_1", arguments("ls")); verify(executor, times(1)).execute("exec", "-T", "-d", "container_1", "ls"); }
@Ignore // This test will not run on Circle CI because it does not currently support docker-compose exec. @Test public void exec_returns_output() throws Exception { assertThat(docker.exec(options(), CONTAINERS.get(0), arguments("echo", "hello")), is("hello")); }
@Test public void fail_if_docker_compose_version_is_prior_1_7_on_docker_compose_exec() throws IOException, InterruptedException { when(executedProcess.getInputStream()).thenReturn(toInputStream("docker-compose version 1.5.6, build 1ad8866")); exception.expect(IllegalStateException.class); exception.expectMessage("You need at least docker-compose 1.7 to run docker-compose exec"); compose.exec(options("-d"), "container_1", arguments("ls")); }
@Test public void calls_exec_on_the_underlying_docker_compose_and_not_invoke_retryer() throws IOException, InterruptedException { retryingDockerCompose.exec(options("-d"), CONTAINER_NAME, arguments("ls")); verifyRetryerWasNotUsed(); verify(dockerCompose).exec(options("-d"), CONTAINER_NAME, arguments("ls")); }
@Test public void return_the_output_from_the_executed_process_on_docker_compose_exec() throws Exception { String lsString = String.format("-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", "-T", "container_1", "ls", "-l"); DockerCompose processCompose = new DefaultDockerCompose(processExecutor, dockerMachine); assertThat(processCompose.exec(options(), "container_1", arguments("ls", "-l")), is(lsString)); }
/** * This test is not currently enabled in Circle as it does not provide a sufficiently recent version of docker-compose. * * @see <a href="https://github.com/palantir/docker-compose-rule/issues/156">Issue #156</a> */ @Test public void testStateChanges_withHealthCheck() throws IOException, InterruptedException { assumeThat("docker version", Docker.version(), new GreaterOrEqual<>(Version.forIntegers(1, 12, 0))); assumeThat("docker-compose version", DockerCompose.version(), new GreaterOrEqual<>(Version.forIntegers(1, 10, 0))); DockerCompose dockerCompose = new DefaultDockerCompose( DockerComposeFiles.from("src/test/resources/native-healthcheck.yaml"), dockerMachine, ProjectName.random()); // The withHealthcheck service's healthcheck checks every 100ms whether the file "healthy" exists Container container = new Container("withHealthcheck", docker, dockerCompose); assertEquals(State.DOWN, container.state()); container.up(); assertEquals(State.UNHEALTHY, container.state()); dockerCompose.exec(noOptions(), "withHealthcheck", arguments("touch", "healthy")); wait.until(container::state, equalTo(State.HEALTHY)); dockerCompose.exec(noOptions(), "withHealthcheck", arguments("rm", "healthy")); wait.until(container::state, equalTo(State.UNHEALTHY)); container.kill(); assertEquals(State.DOWN, container.state()); } }
rule.dockerCompose().exec(noOptions(), "dummy", arguments("touch", "healthy")); await().until(container::state, Matchers.equalTo(State.HEALTHY)); getUninterruptibly(beforeFuture, 1, TimeUnit.SECONDS);