org.testcontainers.containers.wait.strategy.WaitStrategy Java Examples

The following examples show how to use org.testcontainers.containers.wait.strategy.WaitStrategy. 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: EtcdContainer.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
private WaitStrategy waitStrategy() {
    return new AbstractWaitStrategy() {
        @Override
        protected void waitUntilReady() {
            final DockerClient client = DockerClientFactory.instance().client();
            final WaitingConsumer waitingConsumer = new WaitingConsumer();

            LogUtils.followOutput(client, waitStrategyTarget.getContainerId(), waitingConsumer);

            try {
                waitingConsumer.waitUntil(
                    f -> f.getUtf8String().contains("etcdserver: published"),
                    startupTimeout.getSeconds(),
                    TimeUnit.SECONDS,
                    1
                );
            } catch (TimeoutException e) {
                throw new ContainerLaunchException("Timed out");
            }
        }
    };
}
 
Example #2
Source File: DockerComposeContainer.java    From testcontainers-java with MIT License 6 votes vote down vote up
public SELF withExposedService(String serviceName, int servicePort, @NonNull WaitStrategy waitStrategy) {

        String serviceInstanceName = getServiceInstanceName(serviceName);

        /*
         * For every service/port pair that needs to be exposed, we register a target on an 'ambassador container'.
         *
         * The ambassador container's role is to link (within the Docker network) to one of the
         * compose services, and proxy TCP network I/O out to a port that the ambassador container
         * exposes.
         *
         * This avoids the need for the docker compose file to explicitly expose ports on all the
         * services.
         *
         * {@link GenericContainer} should ensure that the ambassador container is on the same network
         * as the rest of the compose environment.
         */

        // Ambassador container will be started together after docker compose has started
        int ambassadorPort = nextAmbassadorPort.getAndIncrement();
        ambassadorPortMappings.computeIfAbsent(serviceInstanceName, __ -> new ConcurrentHashMap<>()).put(servicePort, ambassadorPort);
        ambassadorContainer.withTarget(ambassadorPort, serviceInstanceName, servicePort);
        ambassadorContainer.addLink(new FutureContainer(this.project + "_" + serviceInstanceName), serviceInstanceName);
        addWaitStrategy(serviceInstanceName, waitStrategy);
        return self();
    }
 
Example #3
Source File: OrientDBContainer.java    From testcontainers-java with MIT License 6 votes vote down vote up
public OrientDBContainer(@NonNull String dockerImageName) {
    super(dockerImageName);

    serverPassword = DEFAULT_SERVER_PASSWORD;
    databaseName = DEFAULT_DATABASE_NAME;

    WaitStrategy waitForHttp = new HttpWaitStrategy()
        .forPort(DEFAULT_HTTP_PORT)
        .forStatusCodeMatching(response -> response == HTTP_OK);

    waitStrategy = new WaitAllStrategy()
        .withStrategy(Wait.forListeningPort())
        .withStrategy(waitForHttp)
        .withStartupTimeout(Duration.ofMinutes(2));

    addExposedPorts(DEFAULT_BINARY_PORT, DEFAULT_HTTP_PORT);
}
 
Example #4
Source File: Neo4jContainer.java    From testcontainers-java with MIT License 6 votes vote down vote up
/**
 * Creates a Testcontainer using a specific docker image.
 *
 * @param dockerImageName The docker image to use.
 */
public Neo4jContainer(String dockerImageName) {
    super(dockerImageName);

    WaitStrategy waitForBolt = new LogMessageWaitStrategy()
        .withRegEx(String.format(".*Bolt enabled on 0\\.0\\.0\\.0:%d\\.\n", DEFAULT_BOLT_PORT));
    WaitStrategy waitForHttp = new HttpWaitStrategy()
        .forPort(DEFAULT_HTTP_PORT)
        .forStatusCodeMatching(response -> response == HTTP_OK);

    this.waitStrategy = new WaitAllStrategy()
        .withStrategy(waitForBolt)
        .withStrategy(waitForHttp)
        .withStartupTimeout(Duration.ofMinutes(2));

    addExposedPorts(DEFAULT_BOLT_PORT, DEFAULT_HTTP_PORT, DEFAULT_HTTPS_PORT);
}
 
Example #5
Source File: SyndesisS2iAssemblyContainer.java    From syndesis with Apache License 2.0 6 votes vote down vote up
public SyndesisS2iAssemblyContainer(String integrationName, Path projectDir, String imageTag) {
    super(new ImageFromDockerfile(integrationName + "-s2i", true)
        .withFileFromPath(SRC_DIR, projectDir)
        .withDockerfileFromBuilder(builder -> builder.from(String.format("syndesis/syndesis-s2i:%s", imageTag))
            .withStatement(new MultiArgsStatement("ADD", SRC_DIR, SRC_DIR))
            .user("0")
            .run("chown", "-R", "1000", SRC_DIR)
            .user("1000")
            .cmd(S2I_ASSEMBLE_SCRIPT)
            .build()));

    final WaitStrategy onLogDone = new LogMessageWaitStrategy()
        .withRegEx(".*\\.\\.\\. done.*\\s")
        .withStartupTimeout(SyndesisTestEnvironment.getContainerStartupTimeout());

    setWaitStrategy(onLogDone);
}
 
Example #6
Source File: DockerComposeWaitStrategyTest.java    From testcontainers-java with MIT License 6 votes vote down vote up
@Test
public void testWaitingForNonexistentServices() {
    String existentServiceName = "db_1";
    String nonexistentServiceName1 = "some_nonexistent_service_1";
    String nonexistentServiceName2 = "some_nonexistent_service_2";
    WaitStrategy someWaitStrategy = Mockito.mock(WaitStrategy.class);

    environment
        .waitingFor(existentServiceName, someWaitStrategy)
        .waitingFor(nonexistentServiceName1, someWaitStrategy)
        .waitingFor(nonexistentServiceName2, someWaitStrategy);

    Throwable thrownWhenRequestedToWaitForNonexistentService =
        catchThrowable(environment::start);

    assertThat(thrownWhenRequestedToWaitForNonexistentService)
        .isInstanceOf(IllegalStateException.class)
        .hasMessageContaining(
            nonexistentServiceName1,
            nonexistentServiceName2)
        .hasMessageNotContaining(existentServiceName);
}
 
Example #7
Source File: EmbeddedPrometheusBootstrapConfiguration.java    From kayenta with Apache License 2.0 6 votes vote down vote up
@Bean(name = "prometheus", destroyMethod = "stop")
public GenericContainer prometheus(
    ConfigurableEnvironment environment, WaitStrategy prometheusWaitStrategy) {

  GenericContainer container =
      new GenericContainer("prom/prometheus:v2.10.0")
          .withLogConsumer(containerLogsConsumer(log))
          .withExposedPorts(PORT)
          .withCopyFileToContainer(
              MountableFile.forClasspathResource("/external/prometheus/prometheus.yml"),
              "/etc/prometheus/prometheus.yml")
          .waitingFor(prometheusWaitStrategy)
          .withStartupTimeout(Duration.ofSeconds(30));
  container.start();
  Map<String, Object> env = registerEnvironment(environment, container.getMappedPort(PORT));
  log.info("Started Prometheus server. Connection details: {}", env);
  return container;
}
 
Example #8
Source File: EmbeddedGraphiteBootstrapConfiguration.java    From kayenta with Apache License 2.0 6 votes vote down vote up
@Bean(name = "graphite", destroyMethod = "stop")
public GenericContainer graphite(
    ConfigurableEnvironment environment, WaitStrategy graphiteWaitStrategy) {

  GenericContainer container =
      new GenericContainer("graphiteapp/graphite-statsd:1.1.5-12")
          .withLogConsumer(containerLogsConsumer(log))
          .withExposedPorts(PICKLE_RECEIVER_PORT)
          .waitingFor(graphiteWaitStrategy)
          .withClasspathResourceMapping(
              "/external/graphite/storage-schemas.conf",
              "/opt/graphite/conf/storage-schemas.conf",
              BindMode.READ_ONLY)
          .withStartupTimeout(Duration.ofSeconds(30));
  container.start();

  Map<String, Object> map = registerEnvironment(environment, container);
  log.info("Started Graphite server. Connection details: {}", map);
  return container;
}
 
Example #9
Source File: DockerRabbitMQ.java    From james-project with Apache License 2.0 5 votes vote down vote up
private WaitStrategy waitStrategy() {
    return new WaitAllStrategy()
        .withStrategy(Wait.forHttp("").forPort(DEFAULT_RABBITMQ_ADMIN_PORT)
            .withRateLimiter(RateLimiters.TWENTIES_PER_SECOND)
            .withStartupTimeout(TEN_MINUTES_TIMEOUT))
        .withStrategy(new RabbitMQWaitStrategy(this, TEN_MINUTES_TIMEOUT))
        .withStartupTimeout(TEN_MINUTES_TIMEOUT);
}
 
Example #10
Source File: AbstractWaitStrategyTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
protected GenericContainer startContainerWithCommand(String shellCommand, WaitStrategy waitStrategy, Integer... ports) {
    // apply WaitStrategy to container
    return new GenericContainer(IMAGE_NAME)
            .withExposedPorts(ports)
            .withCommand("sh", "-c", shellCommand)
            .waitingFor(waitStrategy.withStartupTimeout(Duration.ofMillis(WAIT_TIMEOUT_MILLIS)));
}
 
Example #11
Source File: BrowserWebDriverContainer.java    From testcontainers-java with MIT License 5 votes vote down vote up
/**
 */
public BrowserWebDriverContainer() {
    final WaitStrategy logWaitStrategy = new LogMessageWaitStrategy()
            .withRegEx(".*(RemoteWebDriver instances should connect to|Selenium Server is up and running).*\n")
            .withStartupTimeout(Duration.of(15, SECONDS));

    this.waitStrategy = new WaitAllStrategy()
            .withStrategy(logWaitStrategy)
            .withStrategy(new HostPortWaitStrategy())
            .withStartupTimeout(Duration.of(15, SECONDS));

    this.withRecordingFileFactory(new DefaultRecordingFileFactory());
}
 
Example #12
Source File: CassandraWaitStrategy.java    From james-project with Apache License 2.0 4 votes vote down vote up
@Override
public WaitStrategy withStartupTimeout(Duration startupTimeout) {
    return new CassandraWaitStrategy(cassandraContainer, startupTimeout);
}
 
Example #13
Source File: RabbitMQWaitStrategy.java    From james-project with Apache License 2.0 4 votes vote down vote up
@Override
public WaitStrategy withStartupTimeout(Duration startupTimeout) {
    return new RabbitMQWaitStrategy(rabbitMQ, startupTimeout);
}
 
Example #14
Source File: DockerContainer.java    From james-project with Apache License 2.0 4 votes vote down vote up
public DockerContainer waitingFor(WaitStrategy waitStrategy) {
    container.waitingFor(waitStrategy);
    return this;
}
 
Example #15
Source File: SpamAssassinWaitStrategy.java    From james-project with Apache License 2.0 4 votes vote down vote up
@Override
public WaitStrategy withStartupTimeout(Duration startupTimeout) {
    return new SpamAssassinWaitStrategy(spamAssassinContainer, startupTimeout);
}
 
Example #16
Source File: DockerComposeContainer.java    From testcontainers-java with MIT License 4 votes vote down vote up
private void addWaitStrategy(String serviceInstanceName, @NonNull WaitStrategy waitStrategy) {
    final WaitAllStrategy waitAllStrategy = waitStrategyMap.computeIfAbsent(serviceInstanceName, __ ->
        new WaitAllStrategy(WaitAllStrategy.Mode.WITH_MAXIMUM_OUTER_TIMEOUT).withStartupTimeout(Duration.ofMinutes(30)));
    waitAllStrategy.withStrategy(waitStrategy);
}
 
Example #17
Source File: GenericContainer.java    From testcontainers-java with MIT License 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public SELF waitingFor(@NonNull org.testcontainers.containers.wait.strategy.WaitStrategy waitStrategy) {
    this.waitStrategy = waitStrategy;
    return self();
}
 
Example #18
Source File: DockerComposeContainer.java    From testcontainers-java with MIT License 4 votes vote down vote up
public DockerComposeContainer withExposedService(String serviceName, int instance, int servicePort, WaitStrategy waitStrategy) {
    return withExposedService(serviceName + "_" + instance, servicePort, waitStrategy);
}
 
Example #19
Source File: GenericContainer.java    From testcontainers-java with MIT License 4 votes vote down vote up
@Override
public void setWaitStrategy(org.testcontainers.containers.wait.strategy.WaitStrategy waitStrategy) {
    this.waitStrategy = waitStrategy;
}
 
Example #20
Source File: ApplicationContainer.java    From microshed-testing with Apache License 2.0 4 votes vote down vote up
@Override
public ApplicationContainer waitingFor(WaitStrategy waitStrategy) {
    waitStrategySet = true;
    return super.waitingFor(waitStrategy);
}
 
Example #21
Source File: IntegrationRuntime.java    From syndesis with Apache License 2.0 4 votes vote down vote up
public WaitStrategy getReadinessProbe() {
    return readinessProbe;
}
 
Example #22
Source File: IntegrationRuntime.java    From syndesis with Apache License 2.0 4 votes vote down vote up
private IntegrationRuntime(String id, String command, WaitStrategy readinessProbe, ProjectBuilderSupplier projectBuilderSupplier) {
    this.id = id;
    this.command = command;
    this.readinessProbe = readinessProbe;
    this.projectBuilderSupplier = projectBuilderSupplier;
}
 
Example #23
Source File: EmbeddedPrometheusBootstrapConfiguration.java    From kayenta with Apache License 2.0 4 votes vote down vote up
@Bean(name = "prometheusWaitStrategy")
public WaitStrategy prometheusWaitStrategy() {
  return new HttpWaitStrategy().forPath("/status").forPort(PORT).forStatusCode(200);
}
 
Example #24
Source File: EmbeddedGraphiteBootstrapConfiguration.java    From kayenta with Apache License 2.0 4 votes vote down vote up
@Bean(name = "graphiteWaitStrategy")
public WaitStrategy graphiteWaitStrategy() {
  return new HostPortWaitStrategy();
}
 
Example #25
Source File: ContainerUtils.java    From Mahuta with Apache License 2.0 4 votes vote down vote up
public ContainerDefinition(String image, Map<String, String> envVars, WaitStrategy waitStrategy, Integer... exposedPorts) {
    this.image = image;
    this.exposedPorts = exposedPorts;
    this.envVars = envVars;
    this.waitStrategy = waitStrategy;
}
 
Example #26
Source File: HollowTestcontainersConfigurationTest2.java    From microshed-testing with Apache License 2.0 4 votes vote down vote up
@Override
public WaitStrategy withStartupTimeout(Duration startupTimeout) {
    return this;
}
 
Example #27
Source File: HollowTestcontainersConfigurationTest.java    From microshed-testing with Apache License 2.0 4 votes vote down vote up
@Override
public WaitStrategy withStartupTimeout(Duration startupTimeout) {
    return this;
}
 
Example #28
Source File: ApplicationContainer.java    From microshed-testing with Apache License 2.0 4 votes vote down vote up
@Override
public void setWaitStrategy(WaitStrategy waitStrategy) {
    waitStrategySet = true;
    super.setWaitStrategy(waitStrategy);
}
 
Example #29
Source File: GenericContainer.java    From testcontainers-java with MIT License 3 votes vote down vote up
/**
 * Wait until the container has started. The default implementation simply
 * waits for a port to start listening; other implementations are available
 * as implementations of {@link org.testcontainers.containers.wait.strategy.WaitStrategy}
 *
 * @see #waitingFor(org.testcontainers.containers.wait.strategy.WaitStrategy)
 */
protected void waitUntilContainerStarted() {
    org.testcontainers.containers.wait.strategy.WaitStrategy waitStrategy = getWaitStrategy();
    if (waitStrategy != null) {
        waitStrategy.waitUntilReady(this);
    }
}
 
Example #30
Source File: GenericContainer.java    From testcontainers-java with MIT License 2 votes vote down vote up
/**
 * The {@link WaitStrategy} to use to determine if the container is ready.
 * Defaults to {@link Wait#defaultWaitStrategy()}.
 *
 * @return the {@link WaitStrategy} to use
 */
protected org.testcontainers.containers.wait.strategy.WaitStrategy getWaitStrategy() {
    return waitStrategy;
}