A_G
A_G

Reputation: 2370

Connection refused with Elasticsearch test container even after adding wait

I'm trying to move the application to write tests using Elasticsearch container. The container does come up and checking its elasticsearchContainer.isRunning() status returns true but while making a search request Connection Refused exception is thrown. From the Spring boot, ElasticSearch and TestContainers integration tests. Connection refused I added a wait but the same issue persists.

    private static final DockerImageName ELASTICSEARCH_IMAGE =
          DockerImageName.parse("docker.elastic.co/elasticsearch/elasticsearch").withTag("7.11.2");
    elasticsearchContainer = new ElasticsearchContainer(ELASTICSEARCH_IMAGE)
                  .withEnv("foo", "bar");
            elasticsearchContainer.addExposedPorts(9200, 9300);
            //elasticsearchContainer.withStartupTimeout(Duration.of(5, ChronoUnit.MINUTES));
            elasticsearchContainer.setWaitStrategy(
                  Wait.forHttp("/")
                        .forPort(9200)
                        .forStatusCode(200)
                        .withStartupTimeout(Duration.ofSeconds(300)));
            elasticsearchContainer.start();
            assert elasticsearchContainer.isRunning() == true;
            System.out.println("ES Get mapped port" + elasticsearchContainer.getMappedPort(9200)); // outputs a random 5 digit number
            try {
                SearchResponse response = getClient(elasticsearchContainer).search(new SearchRequest(), RequestOptions.DEFAULT); // ERRORS OUT HERE
                System.out.println(response);
            } catch (IOException e) {
                System.out.println("ERROR WHILE getting data " + e.getMessage());
            }

The container starts up fine and no error in the logs either:

CONTAINER ID   IMAGE                                                  COMMAND                  CREATED          STATUS          PORTS                                              NAMES
48b1cc29fea8   docker.elastic.co/elasticsearch/elasticsearch:7.11.2   "/bin/tini -- /usr/l…"   24 seconds ago   Up 23 seconds   0.0.0.0:64492->9200/tcp, 0.0.0.0:64493->9300/tcp   vigorous_banzai
d64503cd1a71   testcontainers/ryuk:0.3.3                              "/app"                   25 seconds ago   Up 24 seconds   0.0.0.0:64490->8080/tcp                            testcontainers-ryuk-bc01909a-5fcc-424e-8346-a7560c31c989

Any suggestions on how to make the connection work?

Upvotes: 1

Views: 3158

Answers (1)

A_G
A_G

Reputation: 2370

Realized that I was using hardcoded ports for creating the rest client. Changed to and it worked

    private static RestHighLevelClient getRestHighLevelClient(ElasticsearchContainer container) {
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
                                           new UsernamePasswordCredentials(ELASTICSEARCH_USERNAME, ELASTICSEARCH_PASSWORD));
        RestClientBuilder restClientBuilder = RestClient.builder(HttpHost.create(container.getHttpHostAddress()))
              .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder
                    .setDefaultCredentialsProvider(credentialsProvider)
        // Try to prevent SocketTimeoutException when fetching larger batch size
        restClientBuilder.setRequestConfigCallback(
              requestConfigBuilder -> requestConfigBuilder.setSocketTimeout(2 * 60 * 1000));

        return new RestHighLevelClient(restClientBuilder);
    }

Upvotes: 1

Related Questions