Start elasticsearch within gradle build for integr

2019-08-09 06:22发布

问题:

Is there a way to start elasticsearch within a gradle build before running integration tests and afterwards stop elasticsearch?

My approach so far is the following, but this blocks the further execution of the gradle build.

task runES(type: JavaExec) {
    main = 'org.elasticsearch.bootstrap.Elasticsearch'
    classpath = sourceSets.main.runtimeClasspath
    systemProperties = ["es.path.home":"$buildDir/elastichome",
                        "es.path.data":"$buildDir/elastichome/data"]
}

回答1:

For my purpose i have decided to start elasticsearch within my integration test in java code.

I've tried out ElasticsearchIntegrationTest but that didn't worked with spring, because it didn't harmony with SpringJUnit4ClassRunner.

I've found it easier to start elasticsearch in the before method:

My test class testing some 'dummy' productive code (indexing a document):

import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;

import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.ImmutableSettings.Builder;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.indices.IndexAlreadyExistsException;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class MyIntegrationTest {

    private Node node;
    private Client client;

    @Before
    public void before() {
        createElasticsearchClient();
        createIndex();
    }

    @After
    public void after() {
        this.client.close();
        this.node.close();
    }

    @Test
    public void testSomething() throws Exception {
        // do something with elasticsearch
        final String json = "{\"mytype\":\"bla\"}";
        final String type = "mytype";
        final String id = index(json, type);
        assertThat(id, notNullValue());
    }

    /**
     * some productive code
     */
    private String index(final String json, final String type) {
        // create Client
        final Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "mycluster").build();
        final TransportClient tc = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress(
                "localhost", 9300));

        // index a document
        final IndexResponse response = tc.prepareIndex("myindex", type).setSource(json).execute().actionGet();
        return response.getId();
    }

    private void createElasticsearchClient() {
        final NodeBuilder nodeBuilder = NodeBuilder.nodeBuilder();
        final Builder settingsBuilder = nodeBuilder.settings();
        settingsBuilder.put("network.publish_host", "localhost");
        settingsBuilder.put("network.bind_host", "localhost");
        final Settings settings = settingsBuilder.build();
        this.node = nodeBuilder.clusterName("mycluster").local(false).data(true).settings(settings).node();
        this.client = this.node.client();
    }

    private void createIndex() {
        try {
            this.client.admin().indices().prepareCreate("myindex").execute().actionGet();
        } catch (final IndexAlreadyExistsException e) {
            // index already exists => we ignore this exception
        }
    }
}

It is also very important to use elasticsearch version 1.3.3 or higher. See Issue 5401.