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.
In /modules/elasticsearch/src/main/java/org/testcontainers/elasticsearch/ElasticsearchContainer.java#L55 the ElasticsearchContainer is waiting for either of two status codes:
But ES returning UNAUTHORIZED means that it is not 100% ready yet. Due to this, a simple test relying on the default
ElasticsearchContainerwithout any changes and trying to use its API fails in about 10%-20% of invocations with errors likeor if one adds default ES user credentials with
The current workaround is (Scala code):
This fixes the flaky test and I believe this should be the default in the
ElasticsearchContainer.