Not able connect with Rest High Level Client in ESIntegTestCase of ElasticSearch 7.0.0

Hello,

I have written some integration tests for my project which uses ElasticSearch as database.

I was using Rest High Level Client with ElasticSearch version 6.6.0. But recently I upgraded the version to 7.0.0. Now, my Integration tests are failing to connect with ElasticSearch on Rest High Level Client. Earlier with 6.6.0, I was using org.elasticsearch.common.network.NetworkModule.HTTP_ENABLED setting to enable HTTP. But now it's deprecated and somehow the HTTP is not getting enabled. It seems nodes created by ESIntegTestCase do not have HTTP enabled by default.

Error

java.net.ConnectException: Timeout connecting to [/0.0.0.0:80]

pom.xml

<dependency>
	<groupId>org.elasticsearch</groupId>
	<artifactId>elasticsearch</artifactId>
	<version>7.0.0</version>
</dependency>
<dependency>
	<groupId>org.elasticsearch.client</groupId>
	<artifactId>elasticsearch-rest-high-level-client</artifactId>
	<version>7.0.0</version>
</dependency>
<dependency>
	<groupId>org.elasticsearch.plugin</groupId>
	<artifactId>transport-netty4-client</artifactId>
	<version>7.0.0</version>
	<scope>test</scope>
</dependency>
<dependency>
	<groupId>org.elasticsearch.test</groupId>
	<artifactId>framework</artifactId>
	<version>7.0.0</version>
	<scope>test</scope>
</dependency>

Test class

package com.rp;

import com.carrotsearch.randomizedtesting.RandomizedRunner;
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.test.ESIntegTestCase.ClusterScope;
import org.elasticsearch.transport.Netty4Plugin;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import static com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope.Scope.NONE;

@ThreadLeakScope(NONE)
@ClusterScope(numDataNodes = 1)
@RunWith(RandomizedRunner.class)
public class CommonUtilTests extends ESIntegTestCase {

    private RestHighLevelClient esClient;

    @Override
    protected Settings nodeSettings(int nodeOrdinal) {
        return Settings.builder()
                .put(super.nodeSettings(nodeOrdinal))
                .build();
    }

    @Override
    protected Collection<Class<? extends Plugin>> nodePlugins() {
        final Collection<Class<? extends Plugin>> plugins = new ArrayList<>();
        plugins.add(Netty4Plugin.class);
        return plugins;
    }

    @Before
    public void initialize() {
        esClient = new RestHighLevelClient(RestClient.builder(getRestClient().getNodes().get(0)));
    }

    @Test
    public void test_createIndex() throws IOException {
        Boolean isCreated = CommonUtil.createIndex(esClient, "my-index");
        assertTrue(isCreated);
        assertTrue(indexExists("my-index"));
    }
}

How can I get RestHighLevelClient in ESIntegTestCase?

ESIntegTestCase is meant for internal whitebox testing of Elasticsearch nodes and clustering. For users testing external behavior with Elasticsearch, we recommend standing up a real elasticsearch node and/or multi node cluster. This will allow you to then use the RestHighLevelClient, just as your normal application would.

Yes. But I wanted to run some unit tests with in memory or embedded ES. In previous version 6.6.0 I was able to use testing framework to start embedded cluster and run my unit tests.

I had to enable the HTTP using HTTP_ENABLED flag.

Now is there any way to get the same thing working with this version? Because I cannot start separate cluster for these tests.

You can not run elasticsearch embedded. Read this blog post.

Note that to run integration tests (not unit tests) you would probably prefer running that in something close to a production environment, like a real elasticsearch server instance. I shared some ideas about integration testing in this thread: In memory testing with RestHighLevelClient

Also this sample project shows how to use Elasticsearch Test Classes:

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.

I have confirm the solution posted here works for High Level Rest Client Integration Tests with single node embedded in 7.6.