Skip to content

Commit

Permalink
DockerCompositionTest -> DockerComposeRuleTest
Browse files Browse the repository at this point in the history
  • Loading branch information
iamdanfox committed Apr 29, 2016
1 parent 69ca023 commit 4916c57
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 39 deletions.
24 changes: 15 additions & 9 deletions src/main/java/com/palantir/docker/compose/DockerComposeRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import com.palantir.docker.compose.configuration.DockerComposeFiles;
import com.palantir.docker.compose.configuration.ProjectName;
import com.palantir.docker.compose.connection.ContainerAccessor;
import com.palantir.docker.compose.connection.Cluster;
import com.palantir.docker.compose.connection.ContainerCache;
import com.palantir.docker.compose.connection.DockerMachine;
import com.palantir.docker.compose.connection.waiting.ClusterWait;
Expand Down Expand Up @@ -43,7 +43,7 @@ public abstract class DockerComposeRule extends ExternalResource {
protected abstract List<ClusterWait> clusterWaits();

@Value.Default
protected DockerMachine machine() {
public DockerMachine machine() {
return DockerMachine.localMachine().build();
}

Expand All @@ -62,21 +62,21 @@ public DockerComposeExecutable executable() {
}

@Value.Default
protected int retryAttempts() {
return DEFAULT_RETRY_ATTEMPTS;
}

@Value.Default
protected DockerCompose dockerCompose() {
public DockerCompose dockerCompose() {
DockerCompose dockerCompose = new DefaultDockerCompose(executable(), machine());
return new RetryingDockerCompose(retryAttempts(), dockerCompose);
}

@Value.Default
public ContainerAccessor containers() {
public Cluster containers() {
return new ContainerCache(dockerCompose());
}

@Value.Default
protected int retryAttempts() {
return DEFAULT_RETRY_ATTEMPTS;
}

@Value.Default
protected LogCollector logCollector() {
return new DoNothingLogCollector();
Expand Down Expand Up @@ -120,6 +120,12 @@ public static ImmutableDockerComposeRule.Builder builder() {

public abstract static class Builder {

public abstract ImmutableDockerComposeRule.Builder files(DockerComposeFiles files);

public ImmutableDockerComposeRule.Builder file(String dockerComposeYmlFile) {
return files(DockerComposeFiles.from(dockerComposeYmlFile));
}

public abstract ImmutableDockerComposeRule.Builder logCollector(LogCollector logCollector);

public ImmutableDockerComposeRule.Builder saveLogsTo(String path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.palantir.docker.compose;

import com.google.common.collect.ImmutableList;
import com.palantir.docker.compose.ImmutableDockerComposeRule.Builder;
import com.palantir.docker.compose.configuration.DockerComposeFiles;
import com.palantir.docker.compose.configuration.ProjectName;
Expand All @@ -38,7 +37,7 @@ public DockerCompositionBuilder waitingForService(String serviceName, SingleServ
return this;
}

public DockerCompositionBuilder waitingForServices(ImmutableList<String> services, MultiServiceHealthCheck check) {
public DockerCompositionBuilder waitingForServices(List<String> services, MultiServiceHealthCheck check) {
builder.waitingForServices(services, check);
return this;
}
Expand Down Expand Up @@ -87,5 +86,4 @@ public DockerComposition build() {
DockerComposeRule rule = builder.build();
return new DockerComposition(rule);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

package com.palantir.docker.compose.connection;

public interface ContainerAccessor {
public interface Cluster {

Container container(String name);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.util.HashMap;
import java.util.Map;

public class ContainerCache implements ContainerAccessor {
public class ContainerCache implements Cluster {

private final Map<String, Container> containers = new HashMap<>();
private final DockerCompose dockerCompose;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

package com.palantir.docker.compose.connection.waiting;

import com.palantir.docker.compose.connection.ContainerAccessor;
import com.palantir.docker.compose.connection.Cluster;

@FunctionalInterface
public interface ClusterWait {

void waitUntilReady(ContainerAccessor containers);
void waitUntilReady(Cluster cluster);

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

import static java.util.stream.Collectors.toList;

import com.palantir.docker.compose.connection.Cluster;
import com.palantir.docker.compose.connection.Container;
import com.palantir.docker.compose.connection.ContainerAccessor;
import java.util.List;
import org.immutables.value.Value;
import org.joda.time.Duration;
Expand All @@ -29,7 +29,7 @@ public static MultiServiceWait of(List<String> serviceNames, MultiServiceHealthC
}

@Override
public void waitUntilReady(ContainerAccessor containers) {
public void waitUntilReady(Cluster containers) {
List<Container> containersToWaitFor = containerNames().stream()
.map(containers::container)
.collect(toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

package com.palantir.docker.compose.connection.waiting;

import com.palantir.docker.compose.connection.Cluster;
import com.palantir.docker.compose.connection.Container;
import com.palantir.docker.compose.connection.ContainerAccessor;
import org.immutables.value.Value;
import org.joda.time.Duration;

Expand All @@ -26,7 +26,7 @@ public static SingleServiceWait of(String serviceName, SingleServiceHealthCheck
}

@Override
public void waitUntilReady(ContainerAccessor containers) {
public void waitUntilReady(Cluster containers) {
Container container = containers.container(containerName());
ServiceWait serviceWait = new ServiceWait(container, healthCheck(), timeout());
serviceWait.waitTillServiceIsUp();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
*/
package com.palantir.docker.compose.execution;

import com.palantir.docker.compose.connection.Cluster;
import com.palantir.docker.compose.connection.Container;
import com.palantir.docker.compose.connection.ContainerAccessor;
import com.palantir.docker.compose.connection.ContainerNames;
import com.palantir.docker.compose.connection.Ports;
import java.io.IOException;
import java.io.OutputStream;

public interface DockerCompose extends ContainerAccessor {
public interface DockerCompose extends Cluster {
void build() throws IOException, InterruptedException;
void up() throws IOException, InterruptedException;
void down() throws IOException, InterruptedException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;

public class DockerCompositionTest {
public class DockerComposeRuleTest {

private static final String IP = "127.0.0.1";

Expand All @@ -66,18 +66,18 @@ public class DockerCompositionTest {
private final MockDockerEnvironment env = new MockDockerEnvironment(dockerCompose);
private DockerComposeFiles mockFiles = mock(DockerComposeFiles.class);
private DockerMachine machine = mock(DockerMachine.class);
private final DockerCompositionBuilder dockerComposition = DockerComposition.of(dockerCompose).files(mockFiles).machine(machine);
private final DockerComposeRule rule = DockerComposeRule.builder().dockerCompose(dockerCompose).files(mockFiles).machine(machine).build();

@Test
public void docker_compose_build_and_up_is_called_before_tests_are_run() throws IOException, InterruptedException {
dockerComposition.build().before();
rule.before();
verify(dockerCompose).build();
verify(dockerCompose).up();
}

@Test
public void docker_compose_kill_and_rm_are_called_after_tests_are_run() throws IOException, InterruptedException {
dockerComposition.build().after();
rule.after();
verify(dockerCompose).kill();
verify(dockerCompose).rm();
}
Expand All @@ -87,7 +87,7 @@ public void docker_compose_wait_for_service_passes_when_check_is_true() throws I
AtomicInteger timesCheckCalled = new AtomicInteger(0);
withComposeExecutableReturningContainerFor("db");
SingleServiceHealthCheck checkCalledOnce = (container) -> SuccessOrFailure.fromBoolean(timesCheckCalled.incrementAndGet() == 1, "not called once yet");
dockerComposition.waitingForService("db", checkCalledOnce).build().before();
DockerComposeRule.builder().from(rule).waitingForService("db", checkCalledOnce).build().before();
assertThat(timesCheckCalled.get(), is(1));
}

Expand All @@ -100,7 +100,8 @@ public void docker_compose_wait_for_service_waits_multiple_services() throws IOE
MultiServiceHealthCheck healthCheck = mock(MultiServiceHealthCheck.class);
when(healthCheck.areServicesUp(containers)).thenReturn(SuccessOrFailure.success());

dockerComposition.waitingForServices(ImmutableList.of("db1", "db2"), healthCheck).build().before();

DockerComposeRule.builder().from(rule).waitingForServices(ImmutableList.of("db1", "db2"), healthCheck).build().before();

verify(healthCheck).areServicesUp(containers);
}
Expand All @@ -110,7 +111,7 @@ public void docker_compose_wait_for_service_passes_when_check_is_true_after_bein
AtomicInteger timesCheckCalled = new AtomicInteger(0);
withComposeExecutableReturningContainerFor("db");
SingleServiceHealthCheck checkCalledTwice = (container) -> SuccessOrFailure.fromBoolean(timesCheckCalled.incrementAndGet() == 2, "not called twice yet");
dockerComposition.waitingForService("db", checkCalledTwice).build().before();
DockerComposeRule.builder().from(rule).waitingForService("db", checkCalledTwice).build().before();
assertThat(timesCheckCalled.get(), is(2));
}

Expand All @@ -121,15 +122,15 @@ public void throws_if_a_wait_for_service_check_remains_false_till_the_timeout()
exception.expect(IllegalStateException.class);
exception.expectMessage("Container 'db' failed to pass startup check:\noops");

dockerComposition.waitingForService("db", (container) -> SuccessOrFailure.failure("oops"), millis(200)).build().before();
DockerComposeRule.builder().from(rule).waitingForService("db", (container) -> SuccessOrFailure.failure("oops"), millis(200)).build().before();
}

@Test
public void port_for_container_can_be_retrieved_by_external_mapping() throws IOException, InterruptedException {
DockerPort expectedPort = env.port("db", IP, 5433, 5432);
withComposeExecutableReturningContainerFor("db");

DockerPort actualPort = dockerComposition.build().portOnContainerWithExternalMapping("db", 5433);
DockerPort actualPort = rule.containers().container("db").portMappedExternallyTo(5433);

assertThat(actualPort, is(expectedPort));
}
Expand All @@ -139,7 +140,7 @@ public void port_for_container_can_be_retrieved_by_internal_mapping() throws IOE
DockerPort expectedPort = env.port("db", IP, 5433, 5432);
withComposeExecutableReturningContainerFor("db");

DockerPort actualPort = dockerComposition.build().portOnContainerWithInternalMapping("db", 5432);
DockerPort actualPort = rule.containers().container("db").portMappedInternallyTo(5432);

assertThat(actualPort, is(expectedPort));
}
Expand All @@ -148,10 +149,9 @@ public void port_for_container_can_be_retrieved_by_internal_mapping() throws IOE
public void when_two_external_ports_on_a_container_are_requested_docker_compose_ps_is_only_executed_once() throws Exception {
env.ports("db", IP, 5432, 8080);
withComposeExecutableReturningContainerFor("db");
DockerComposition composition = dockerComposition.build();

composition.portOnContainerWithInternalMapping("db", 5432);
composition.portOnContainerWithInternalMapping("db", 8080);
rule.containers().container("db").portMappedInternallyTo(5432);
rule.containers().container("db").portMappedInternallyTo(8080);

verify(dockerCompose, times(1)).ports("db");
}
Expand All @@ -166,14 +166,14 @@ public void waiting_for_service_that_does_not_exist_results_in_an_illegal_state_
exception.expect(IllegalStateException.class);
exception.expectMessage(nonExistentContainer);

dockerComposition.waitingForService(nonExistentContainer, toHaveAllPortsOpen()).build().before();
DockerComposeRule.builder().from(rule).waitingForService(nonExistentContainer, toHaveAllPortsOpen()).build().before();
}

@SuppressWarnings("unchecked")
@Test
public void logs_can_be_saved_to_a_directory_while_containers_are_running() throws IOException, InterruptedException {
File logLocation = logFolder.newFolder();
DockerComposition loggingComposition = dockerComposition.saveLogsTo(logLocation.getAbsolutePath()).build();
DockerComposeRule loggingComposition = DockerComposeRule.builder().from(rule).saveLogsTo(logLocation.getAbsolutePath()).build();
when(dockerCompose.ps()).thenReturn(new ContainerNames("db"));
CountDownLatch latch = new CountDownLatch(1);
when(dockerCompose.writeLogs(eq("db"), any(OutputStream.class))).thenAnswer((args) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.palantir.docker.compose.connection.Cluster;
import com.palantir.docker.compose.connection.Container;
import com.palantir.docker.compose.connection.ContainerAccessor;
import org.junit.Before;
import org.junit.Test;

public class SingleServiceWaitTest {

private ContainerAccessor containerAccessor = mock(ContainerAccessor.class);
private Cluster containerAccessor = mock(Cluster.class);
private SingleServiceHealthCheck healthCheck = mock(SingleServiceHealthCheck.class);
private Container someContainer = mock(Container.class);
private SingleServiceWait wait = SingleServiceWait.of("somecontainer", healthCheck, DEFAULT_TIMEOUT);
Expand Down

0 comments on commit 4916c57

Please sign in to comment.