com.palantir.docker.compose.connection.Container Java Examples

The following examples show how to use com.palantir.docker.compose.connection.Container. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RedisSinkTaskReconnectIT.java    From kafka-connect-redis with Apache License 2.0 6 votes vote down vote up
@Test
public void initialConnectionIssues(
    @DockerContainer(container = "redis") Container container,
    @Port(container = "redis", internalPort = 6379) InetSocketAddress address) throws ExecutionException, InterruptedException, IOException {
  log.info("address = {}", address);
  final String topic = "putWrite";
  SinkTaskContext context = mock(SinkTaskContext.class);
  when(context.assignment()).thenReturn(ImmutableSet.of());
  this.task.initialize(context);
  container.stop();

  ExecutorService service = Executors.newSingleThreadExecutor();
  Future<?> future = service.submit(() -> task.start(
        ImmutableMap.of(RedisSinkConnectorConfig.HOSTS_CONFIG, String.format("%s:%s", address.getHostString(), address.getPort())
      )
  ));
  container.start();
  Time.SYSTEM.sleep(2000);
  future.get();
}
 
Example #2
Source File: RedisSinkTaskReconnectIT.java    From kafka-connect-redis with Apache License 2.0 6 votes vote down vote up
@Test
public void serverReset(
    @DockerContainer(container = "redis") Container container,
    @Port(container = "redis", internalPort = 6379) InetSocketAddress address) throws ExecutionException, InterruptedException, IOException {
  log.info("address = {}", address);
  final String topic = "putWrite";
  SinkTaskContext context = mock(SinkTaskContext.class);
  when(context.assignment()).thenReturn(ImmutableSet.of());
  this.task.initialize(context);
  this.task.start(
      ImmutableMap.of(RedisSinkConnectorConfig.HOSTS_CONFIG, String.format("%s:%s", address.getHostString(), address.getPort()))
  );

  sendAndVerifyRecords(task, topic, 0);
  container.stop();

  assertThrows(RetriableException.class, () -> {
    sendAndVerifyRecords(task, topic, 100);
  });
  container.start();
  sendAndVerifyRecords(task, topic, 100);
}
 
Example #3
Source File: ClusterHealthCheck.java    From docker-compose-rule with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a check that the native "healthcheck" status of the docker containers is not unhealthy.
 *
 * <p>Does not wait for DOWN or PAUSED containers, or containers with no healthcheck defined.
 */
static ClusterHealthCheck nativeHealthChecks() {
    return cluster -> {
        Set<String> unhealthyContainers = new LinkedHashSet<>();
        try {
            for (Container container : cluster.allContainers()) {
                State state = container.state();
                if (state == State.UNHEALTHY) {
                    unhealthyContainers.add(container.getContainerName());
                }
            }
            if (!unhealthyContainers.isEmpty()) {
                return SuccessOrFailure.failure(
                        "The following containers are not healthy: " + unhealthyContainers.stream().collect(joining(", ")));
            }
            return SuccessOrFailure.success();
        } catch (IOException e) {
            return SuccessOrFailure.fromException(e);
        }
    };
}
 
Example #4
Source File: DockerComposeRuleIntegrationTest.java    From docker-compose-rule with Apache License 2.0 6 votes vote down vote up
@Test
public void can_kill_and_start_containers() {
    forEachContainer(containerName -> {
        try {
            Container container = docker.containers().container(containerName);

            container.kill();
            assertThat(container.state(), is(State.DOWN));

            container.start();
            assertThat(container.state(), is(State.HEALTHY));
        } catch (IOException | InterruptedException e) {
            propagate(e);
        }
    });
}
 
Example #5
Source File: DockerComposeRuleIntegrationTest.java    From docker-compose-rule with Apache License 2.0 6 votes vote down vote up
@Test
public void can_stop_and_start_containers() {
    forEachContainer(containerName -> {
        try {
            Container container = docker.containers().container(containerName);

            container.stop();
            assertThat(container.state(), is(State.DOWN));

            container.start();
            assertThat(container.state(), is(State.HEALTHY));
        } catch (IOException | InterruptedException e) {
            propagate(e);
        }
    });
}
 
Example #6
Source File: DockerComposeRuleIntegrationTest.java    From docker-compose-rule with Apache License 2.0 5 votes vote down vote up
@Test
public void kill_can_be_run_on_killed_container() {
    forEachContainer(containerName -> {
        try {
            Container container = docker.containers().container(containerName);

            container.kill();
            assertThat(container.state(), is(State.DOWN));

            container.kill();
        } catch (IOException | InterruptedException e) {
            propagate(e);
        }
    });
}
 
Example #7
Source File: DockerComposeManagerNativeHealthcheckIntegrationTest.java    From docker-compose-rule with Apache License 2.0 5 votes vote down vote up
/**
 * 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 dockerComposeManagerWaitsUntilHealthcheckPasses()
        throws ExecutionException, IOException, InterruptedException, TimeoutException {
    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)));

    docker = new DockerComposeManager.Builder()
            .file("src/test/resources/native-healthcheck.yaml")
            .build();
    Future<?> beforeFuture = pool.submit(() -> {
        docker.before();
        return null;
    });

    Container container = docker.containers().container("withHealthcheck");
    await().until(container::state, Matchers.equalTo(State.UNHEALTHY));

    // The "withHealthCheck" container should not initially pass its healthcheck
    try {
        getUninterruptibly(beforeFuture, 1, TimeUnit.SECONDS);
        fail("Expected before() to wait");
    } catch (TimeoutException e) {
        // Expected
    }

    // Touching the "healthy" file in the "withHealthCheck" container should make its healthcheck pass
    docker.dockerCompose().exec(noOptions(), "withHealthcheck", arguments("touch", "healthy"));
    await().until(container::state, Matchers.equalTo(State.HEALTHY));
    getUninterruptibly(beforeFuture, 1, TimeUnit.SECONDS);
}
 
Example #8
Source File: DockerComposeManagerUpContainerIntegrationTest.java    From docker-compose-rule with Apache License 2.0 5 votes vote down vote up
@Test
public void test_docker_compose_manager_up_container() throws IOException, InterruptedException {
    Container container = dockerComposeManager.containers().container(SERVICE_NAME);

    container.up();

    assertThat(container.state(), is(State.HEALTHY));
}
 
Example #9
Source File: DockerComposeManagerUpContainerIntegrationTest.java    From docker-compose-rule with Apache License 2.0 5 votes vote down vote up
@Test
public void test_docker_compose_manager_up_container_with_healthcheck() throws IOException, InterruptedException {
    Container container = dockerComposeManager.containers().container(SERVICE_NAME);

    container.up();

    // to prove that we can use healthcheck manually after starting a single container
    new ClusterWait(serviceHealthCheck(SERVICE_NAME, toHaveAllPortsOpen()), Duration.standardSeconds(5))
            .waitUntilReady(dockerComposeManager.containers());

    assertThat(container.state(), is(State.HEALTHY));
}
 
Example #10
Source File: DockerComposeManagerShould.java    From docker-compose-rule with Apache License 2.0 5 votes vote down vote up
@Test
public void pass_wait_for_service_when_check_is_true() throws IOException, InterruptedException {
    AtomicInteger timesCheckCalled = new AtomicInteger(0);
    withComposeExecutableReturningContainerFor("db");
    HealthCheck<Container> checkCalledOnce = container -> SuccessOrFailure.fromBoolean(timesCheckCalled.incrementAndGet() == 1, "not called once yet");
    defaultBuilder().waitingForService("db", checkCalledOnce).build().before();
    assertThat(timesCheckCalled.get(), is(1));
}
 
Example #11
Source File: DockerComposeManagerShould.java    From docker-compose-rule with Apache License 2.0 5 votes vote down vote up
@Test
public void wait_for_multiple_services_on_wait() throws IOException, InterruptedException {
    Container db1 = withComposeExecutableReturningContainerFor("db1");
    Container db2 = withComposeExecutableReturningContainerFor("db2");
    List<Container> containers = ImmutableList.of(db1, db2);

    when(healthCheck.isHealthy(containers)).thenReturn(SuccessOrFailure.success());

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

    verify(healthCheck).isHealthy(containers);
}
 
Example #12
Source File: DockerComposeRuleIntegrationTest.java    From docker-compose-rule with Apache License 2.0 5 votes vote down vote up
@Test
public void start_can_be_run_on_running_container() {
    forEachContainer(containerName -> {
        try {
            Container container = docker.containers().container(containerName);

            container.start();
            assertThat(container.state(), is(State.HEALTHY));
        } catch (IOException | InterruptedException e) {
            propagate(e);
        }
    });
}
 
Example #13
Source File: DockerComposeRuleIntegrationTest.java    From docker-compose-rule with Apache License 2.0 5 votes vote down vote up
private static HealthCheck<List<Container>> toAllHaveAllPortsOpen() {
    return containers -> {
        boolean healthy = containers.stream()
                .map(Container::areAllPortsOpen)
                .allMatch(SuccessOrFailure::succeeded);

        return SuccessOrFailure.fromBoolean(healthy, "");
    };
}
 
Example #14
Source File: DockerComposeRuleIntegrationTest.java    From docker-compose-rule with Apache License 2.0 5 votes vote down vote up
@Test
public void stop_can_be_run_on_stopped_container() {
    forEachContainer(containerName -> {
        try {
            Container container = docker.containers().container(containerName);

            container.stop();
            assertThat(container.state(), is(State.DOWN));

            container.stop();
        } catch (IOException | InterruptedException e) {
            propagate(e);
        }
    });
}
 
Example #15
Source File: HttpStatusCodeHealthCheck.java    From nifi-minifi with Apache License 2.0 5 votes vote down vote up
@Override
public SuccessOrFailure isHealthy(Container target) {
    try {
        int responseCode = openConnection(urlFunction.apply(target)).getResponseCode();
        if (responseCode == expected) {
            return SuccessOrFailure.success();
        } else {
            return SuccessOrFailure.failure("Expected Status code " + expected + " got " + responseCode);
        }
    } catch (IOException e) {
        return SuccessOrFailure.failure("Expected Status code " + expected + " got IOException " + e.getMessage());
    }
}
 
Example #16
Source File: DockerComposeManagerShould.java    From docker-compose-rule with Apache License 2.0 5 votes vote down vote up
@Test
public void pass_wait_for_service_when_check_is_true_after_being_false() throws IOException, InterruptedException {
    AtomicInteger timesCheckCalled = new AtomicInteger(0);
    withComposeExecutableReturningContainerFor("db");
    HealthCheck<Container> checkCalledTwice = container -> SuccessOrFailure.fromBoolean(timesCheckCalled.incrementAndGet() == 2, "not called twice yet");
    defaultBuilder().waitingForService("db", checkCalledTwice).build().before();
    assertThat(timesCheckCalled.get(), is(2));
}
 
Example #17
Source File: HttpsStatusCodeHealthCheck.java    From nifi-minifi with Apache License 2.0 5 votes vote down vote up
@Override
public SuccessOrFailure isHealthy(List<Container> target) {

    return new HttpStatusCodeHealthCheck(urlFunction, expected) {
        @Override
        protected HttpURLConnection openConnection(String url) throws IOException {
            DockerPort dockerPort = proxyExtractor.apply(target).port(3128);
            return getHttpURLConnection(url, sslSocketFactorySupplier.get(), dockerPort.getIp(), dockerPort.getExternalPort());
        }
    }.isHealthy(serverExtractor.apply(target));
}
 
Example #18
Source File: HttpsStatusCodeHealthCheck.java    From nifi-minifi with Apache License 2.0 5 votes vote down vote up
public HttpsStatusCodeHealthCheck(Function<Container, String> urlFunction, Function<List<Container>, Container> proxyExtractor,
                                  Function<List<Container>, Container> serverExtractor, Supplier<SSLSocketFactory> sslSocketFactorySupplier, int expected) {
    this.urlFunction = urlFunction;
    this.proxyExtractor = proxyExtractor;
    this.serverExtractor = serverExtractor;
    this.sslSocketFactorySupplier = sslSocketFactorySupplier;
    this.expected = expected;
}
 
Example #19
Source File: LogUtil.java    From nifi-minifi with Apache License 2.0 5 votes vote down vote up
public static void verifyLogEntries(String expectedJsonFilename, Container container) throws Exception {
    List<ExpectedLogEntry> expectedLogEntries;
    try (InputStream inputStream = LogUtil.class.getClassLoader().getResourceAsStream(expectedJsonFilename)) {
        List<Map<String, Object>> expected = new ObjectMapper().readValue(inputStream, List.class);
        expectedLogEntries = expected.stream().map(map -> new ExpectedLogEntry(Pattern.compile((String)map.get("pattern")), (int) map.getOrDefault("occurrences", 1))).collect(Collectors.toList());
    }
    DockerPort dockerPort = container.port(8000);
    logger.info("Connecting to external port {} for docker internal port of {}", new Object[]{dockerPort.getExternalPort(), dockerPort.getInternalPort()});
    URL url = new URL("http://" + dockerPort.getIp() + ":" + dockerPort.getExternalPort());
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    try (InputStream inputStream = urlConnection.getInputStream();
         InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
         BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
        String line;
        for (ExpectedLogEntry expectedLogEntry : expectedLogEntries) {
            boolean satisfied = false;
            int occurrences = 0;
            while ((line = bufferedReader.readLine()) != null) {
                if (expectedLogEntry.pattern.matcher(line).find()) {
                    logger.info("Found expected: " + line);
                    if (++occurrences >= expectedLogEntry.numOccurrences) {
                        logger.info("Found target " + occurrences + " times");
                        satisfied = true;
                        break;
                    }
                }
            }
            if (!satisfied) {
                fail("End of log reached without " + expectedLogEntry.numOccurrences + " match(es) of " + expectedLogEntry.pattern);
            }
        }
    } finally {
        urlConnection.disconnect();
    }
}
 
Example #20
Source File: RecordingCluster.java    From docker-compose-rule with Apache License 2.0 4 votes vote down vote up
@Override
public Container container(String name) {
    recordedContainerNames.add(name);
    return delegate.container(name);
}
 
Example #21
Source File: RecordingCluster.java    From docker-compose-rule with Apache License 2.0 4 votes vote down vote up
@Override
public List<Container> containers(List<String> containerNames) {
    recordedContainerNames.addAll(containerNames);
    return delegate.containers(containerNames);
}
 
Example #22
Source File: RecordingCluster.java    From docker-compose-rule with Apache License 2.0 4 votes vote down vote up
@Override
public Set<Container> allContainers() throws IOException, InterruptedException {
    Set<Container> containers = delegate.allContainers();
    containers.forEach(container -> recordedContainerNames.add(container.getContainerName()));
    return containers;
}
 
Example #23
Source File: ClusterHealthCheck.java    From docker-compose-rule with Apache License 2.0 4 votes vote down vote up
static ClusterHealthCheck serviceHealthCheck(List<String> containerNames, HealthCheck<List<Container>> delegate) {
    return transformingHealthCheck(cluster -> cluster.containers(containerNames), delegate);
}
 
Example #24
Source File: ClusterHealthCheck.java    From docker-compose-rule with Apache License 2.0 4 votes vote down vote up
static ClusterHealthCheck serviceHealthCheck(String containerName, HealthCheck<Container> containerCheck) {
    return transformingHealthCheck(cluster -> cluster.container(containerName), containerCheck);
}
 
Example #25
Source File: HealthChecks.java    From docker-compose-rule with Apache License 2.0 4 votes vote down vote up
public static HealthCheck<Container> toRespondOverHttp(int internalPort, Function<DockerPort, String> urlFunction) {
    return container -> container.portIsListeningOnHttp(internalPort, urlFunction);
}
 
Example #26
Source File: HealthChecks.java    From docker-compose-rule with Apache License 2.0 4 votes vote down vote up
public static HealthCheck<Container> toRespond2xxOverHttp(int internalPort, Function<DockerPort, String> urlFunction) {
    return container -> container.portIsListeningOnHttpAndCheckStatus2xx(internalPort, urlFunction);
}
 
Example #27
Source File: HealthChecks.java    From docker-compose-rule with Apache License 2.0 4 votes vote down vote up
public static HealthCheck<Container> toHaveAllPortsOpen() {
    return Container::areAllPortsOpen;
}
 
Example #28
Source File: MsSqlSettingsExtension.java    From kafka-connect-cdc-mssql with Apache License 2.0 4 votes vote down vote up
@Override
protected Object handleResolve(ParameterContext parameterContext, ExtensionContext extensionContext, Annotation annotation, DockerComposeRule docker) throws ParameterResolutionException {
  Container container = docker.containers().container(MsSqlTestConstants.CONTAINER_NAME);
  DockerPort dockerPort = container.port(MsSqlTestConstants.PORT);
  return MsSqlTestConstants.settings(dockerPort.getIp(), dockerPort.getExternalPort());
}
 
Example #29
Source File: DockerComposeManagerShould.java    From docker-compose-rule with Apache License 2.0 4 votes vote down vote up
public Container withComposeExecutableReturningContainerFor(String containerName) {
    return new Container(containerName, mockDocker, dockerCompose);
}
 
Example #30
Source File: DelegatingDockerCompose.java    From docker-compose-rule with Apache License 2.0 4 votes vote down vote up
@Override
public Optional<String> id(Container container) throws IOException, InterruptedException {
    return dockerCompose.id(container);
}