Skip to content

ElasticsearchContainer is not waiting for container being ready #2982

@ghost

Description

In /modules/elasticsearch/src/main/java/org/testcontainers/elasticsearch/ElasticsearchContainer.java#L55 the ElasticsearchContainer is waiting for either of two status codes:

.forStatusCodeMatching(response -> response == HTTP_OK || response == HTTP_UNAUTHORIZED)

But ES returning UNAUTHORIZED means that it is not 100% ready yet. Due to this, a simple test relying on the default ElasticsearchContainer without any changes and trying to use its API fails in about 10%-20% of invocations with errors like

org.elasticsearch.ElasticsearchStatusException: Elasticsearch exception [type=security_exception, reason=missing authentication token for REST request 

or if one adds default ES user credentials with

  org.elasticsearch.ElasticsearchStatusException: Elasticsearch exception [type=security_exception, reason=failed to authenticate user [elastic]]

The current workaround is (Scala code):

setWaitStrategy(
  getWaitStrategy.asInstanceOf[HttpWaitStrategy] // Get the current HttpWaitStrategy
    .forStatusCodeMatching(null) // Clear its StatusCodeMatching predicate.
    .forStatusCode(HTTP_OK)) // Set a single HTTP_OK StatusCode

This fixes the flaky test and I believe this should be the default in the ElasticsearchContainer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions