/** * Create an Elasticsearch Container by passing the full docker image name * @param dockerImageName Full docker image name, like: docker.elastic.co/elasticsearch/elasticsearch:6.4.1 */ public ElasticsearchContainer(String dockerImageName) { super(dockerImageName); logger().info("Starting an elasticsearch container using [{}]", dockerImageName); withNetworkAliases("elasticsearch-" + Base58.randomString(6)); withEnv("discovery.type", "single-node"); addExposedPorts(ELASTICSEARCH_DEFAULT_PORT, ELASTICSEARCH_DEFAULT_TCP_PORT); setWaitStrategy(new HttpWaitStrategy() .forPort(ELASTICSEARCH_DEFAULT_PORT) .forStatusCodeMatching(response -> response == HTTP_OK || response == HTTP_UNAUTHORIZED) .withStartupTimeout(Duration.ofMinutes(2))); }
@NotNull private HttpWaitStrategy createNodeWaitStrategy() { return new HttpWaitStrategy() .forPath("/pools/default/") .withBasicCredentials(clusterUsername, clusterPassword) .forStatusCode(HTTP_OK) .forResponsePredicate(response -> { try { return Optional.of(MAPPER.readTree(response)) .map(n -> n.at("/nodes/0/status")) .map(JsonNode::asText) .map("healthy"::equals) .orElse(false); } catch (IOException e) { logger().error("Unable to parse response {}", response); return false; } }); }
@Override protected void waitUntilReady() { delegateStrategy.waitUntilReady(this.waitStrategyTarget); } }
/** * Convenience method to return a WaitStrategy for an HTTP endpoint. * * @param path the path to check * @return the WaitStrategy * @see HttpWaitStrategy */ public static HttpWaitStrategy forHttp(String path) { return new HttpWaitStrategy() .forPath(path); }
/** * 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)); }
public static GenericContainer<?> createTestContainer(final int testPort) { return new GenericContainer<>("localstack/localstack:latest") .withEnv("DEBUG", "1") .waitingFor(new HttpWaitStrategy().forStatusCode(200).forPort(testPort)); }
public PulsarContainer(String pulsarVersion) { super(TestcontainersConfiguration.getInstance().getPulsarImage() + ":" + pulsarVersion); withExposedPorts(BROKER_PORT, BROKER_HTTP_PORT); withCommand("/pulsar/bin/pulsar", "standalone", "--no-functions-worker", "-nss"); waitingFor(Wait.forHttp(METRICS_ENDPOINT).forStatusCode(200).forPort(BROKER_HTTP_PORT)); }
public DockerSwiftContainer() { this.swiftContainer = new GenericContainer<>(SWIFT_DOCKER_IMAGE); this.swiftContainer .withExposedPorts(KEYSTONE_ADMIN_PORT) .withExposedPorts(SWIFT_PORT) .withLogConsumer(DockerSwiftContainer::displayDockerLog) .waitingFor( new WaitAllStrategy() .withStrategy( forHttp("/v3") .forPort(KEYSTONE_ADMIN_PORT) .forStatusCode(200) .withRateLimiter(RateLimiters.TWENTIES_PER_SECOND) ).withStrategy( forHttp("/info") .forPort(SWIFT_PORT) .forStatusCode(200) .withRateLimiter(RateLimiters.TWENTIES_PER_SECOND) ) ); }
public InfluxDBContainer(final String version) { super(IMAGE_NAME + ":" + version); waitStrategy = new WaitAllStrategy() .withStrategy(Wait.forHttp("/ping").withBasicCredentials(username, password).forStatusCode(204)) .withStrategy(Wait.forListeningPort()); }
/** * Expects that the WaitStrategy returns successfully after receiving an HTTP 401 response from the container. * This 401 response is checked with with many status codes using {@link HttpWaitStrategy#forStatusCode(int)} * and a lambda using {@link HttpWaitStrategy#forStatusCodeMatching(Predicate)} */ @Test public void testWaitUntilReadyWithManyStatusCodesAndLambda() { waitUntilReadyAndSucceed(startContainerWithCommand(createShellCommand("401 UNAUTHORIZED", GOOD_RESPONSE_BODY), createHttpWaitStrategy(ready) .forStatusCode(300) .forStatusCode(500) .forStatusCodeMatching(it -> it == 401) )); }
/** * Waits for the given status code. * * @param statusCode the expected status code * @return this */ public HttpWaitStrategy forStatusCode(int statusCode) { delegateStrategy.forStatusCode(statusCode); return this; }
.waitingFor(Wait.forHttp(path).forPort(giteaPort) .withStartupTimeout(Duration.of(120, ChronoUnit.SECONDS))); Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(log);
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); }
/** * Expects the WaitStrategy probing the right port. */ @Test public void testWaitUntilReadyWithSpecificPort() { waitUntilReadyAndSucceed(startContainerWithCommand( createShellCommand("200 OK", GOOD_RESPONSE_BODY, 9090), createHttpWaitStrategy(ready) .forPort(9090), 7070, 8080, 9090 )); }
/** Starts a docker container optionally. For example, skips if Docker is unavailable. */ @SuppressWarnings("rawtypes") @BeforeClass public static void startContainer() { try { container = new GenericContainer(JAEGER_IMAGE) .withExposedPorts(JAEGER_HTTP_PORT, JAEGER_HTTP_PORT_THRIFT) .waitingFor(new HttpWaitStrategy()); container.start(); } catch (RuntimeException e) { throw new AssumptionViolatedException("could not start docker container", e); } }
/** * Expects that the WaitStrategy returns successfully after receiving an HTTP 401 response from the container. * This 401 response is checked with a lambda using {@link HttpWaitStrategy#forStatusCodeMatching(Predicate)} */ @Test public void testWaitUntilReadyWithUnauthorizedWithLambda() { waitUntilReadyAndSucceed(startContainerWithCommand(createShellCommand("401 UNAUTHORIZED", GOOD_RESPONSE_BODY), createHttpWaitStrategy(ready) .forStatusCodeMatching(it -> it >= 200 && it < 300 || it == 401) )); }
@Test public void testWaitingFails() { final DockerComposeContainer environment = new DockerComposeContainer(new File("src/test/resources/compose-test.yml")) .withExposedService("redis_1", REDIS_PORT, Wait.forHttp("/test").withStartupTimeout(Duration.ofSeconds(10))); VisibleAssertions.assertThrows("waiting on an invalid http path times out", RuntimeException.class, () -> environment.starting(Description.createTestDescription(Object.class, "name"))); }
/** * Authenticate with HTTP Basic Authorization credentials. * * @param username the username * @param password the password * @return this */ public HttpWaitStrategy withBasicCredentials(String username, String password) { delegateStrategy.withBasicCredentials(username, password); return this; }
/** * Waits for the response to pass the given predicate * @param responsePredicate The predicate to test the response against * @return this */ public HttpWaitStrategy forResponsePredicate(Predicate<String> responsePredicate) { delegateStrategy.forResponsePredicate(responsePredicate); return this; }
public CouchbaseContainer(String containerName) { super(containerName); withNetwork(Network.SHARED); withNetworkAliases("couchbase-" + Base58.randomString(6)); setWaitStrategy(new HttpWaitStrategy().forPath("/ui/index.html")); }