Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a test for Elasticsearch custom plugin?

Tags:

I create custom Elasticsearch plugin. Now I want to write a test for this plugin. My expectations were - that I could run embedded Elasticsearch instance, set up it properly and then do some testing (index some documents, then query for it)

Problem is that I couldn't set up my plugin properly

Custom plugin code is parsing JSON query and set up some objects for later usage:

public class CustomQueryParserPlugin extends AbstractPlugin {     public static final String PLUGIN_NAME = "custom_query";     private final Settings settings;      @Inject     public CustomQueryParserPlugin (Settings settings) {         this.settings = settings;     }      @Override     public String name() {         return PLUGIN_NAME;     }      @Override     public String description() {         return "custom plugin";     }      public void onModule(IndicesQueriesModule module) {         module.addQuery(new CustomQueryParser(settings));     } } 

Test code:

public class CustomParserPluginTest extends ElasticsearchSingleNodeTest {      private static Node newNode() {         final Settings settings = ImmutableSettings.builder()                 .put(ClusterName.SETTING, nodeName())                 .put("node.name", nodeName())                 .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1)                 .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)                 .put(EsExecutors.PROCESSORS, 1) // limit the number of threads created                 .put("http.enabled", false)                 .put("plugin.types", CustomParserPlugin.class.getName())                 .put("path.plugins", pathToPlugin)                 .put("index.store.type", "ram")                 .put("config.ignore_system_properties", true) // make sure we get what we set :)                 .put("gateway.type", "none").build();         Node build = NodeBuilder.nodeBuilder().local(true).data(true).settings(                 settings).build();         build.start();         assertThat(DiscoveryNode.localNode(build.settings()), is(true));         return build;     }       @Test     public void jsonParsing() throws URISyntaxException {         final Client client = newNode().client();         final SearchResponse test = client.prepareSearch("test-index").setSource(addQuery()).execute().actionGet();     }      private String addQuery() {          return "{"match_all":{"boost":1.2}}"     } 

I've try multiple values for pathToPlugin - but nothing seems to works well, because JSON query always give me an exception:

QueryParsingException[[test-index] No query registered for [custom_query]]; 

All documentation I could find was about installing plugins and testing them on some local Elasticsearch installation.

What I am doing wrong here? Is there any documentation or examples of tests like that?

UPD. Here is a repo with extracted code of CustomQueryParserPlugin - https://github.com/MysterionRise/es-custom-parser

May be in initialize section in test I need to create in memory index?

like image 597
Mysterion Avatar asked Jan 29 '15 17:01

Mysterion


People also ask

What is Elasticsearch testing?

Elasticsearch provides a jar file, which can be added to any java IDE and can be used to test the code which is related to Elasticsearch. A range of tests can be performed by using the framework provided by Elasticsearch. In this chapter, we will discuss these tests in detail − Unit testing. Integration testing.

Which is a plugin type in Elasticsearch?

Plugins are a way to enhance the core Elasticsearch functionality in a custom manner. They range from adding custom mapping types, custom analyzers, native scripts, custom discovery and more. Plugins contain JAR files, but may also contain scripts and config files, and must be installed on every node in the cluster.

Where are the Elasticsearch plugins installed?

It is located in the $ES_HOME/bin directory by default but it may be in a different location depending on which Elasticsearch package you installed: Directory layout of . tar.


1 Answers

To write tests for you plugin you can use Elasticsearch Cluster Runner. For reference check how MinHash Plugin wrote test.

UPDATE:

I've changed CustomParserPluginTest class to use Elasticsearch Cluster Runner:

import static org.codelibs.elasticsearch.runner.ElasticsearchClusterRunner.newConfigs; import java.util.Map; import junit.framework.TestCase; import org.codelibs.elasticsearch.runner.ElasticsearchClusterRunner; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.client.Client; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.ImmutableSettings.Builder; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.index.get.GetField; import org.junit.Assert; import org.elasticsearch.action.search.SearchResponse; import static org.hamcrest.core.Is.is;  public class CustomParserPluginTest extends TestCase {      private ElasticsearchClusterRunner runner;      @Override     protected void setUp() throws Exception {         // create runner instance         runner = new ElasticsearchClusterRunner();         // create ES nodes         runner.onBuild(new ElasticsearchClusterRunner.Builder() {             @Override             public void build(final int number, final Builder settingsBuilder) {             }         }).build(newConfigs().ramIndexStore().numOfNode(1));          // wait for yellow status         runner.ensureYellow();     }      @Override     protected void tearDown() throws Exception {         // close runner         runner.close();         // delete all files         runner.clean();     }      public void test_jsonParsing() throws Exception {         final String index = "test_index";          runner.createIndex(index, ImmutableSettings.builder().build());         runner.ensureYellow(index);          final SearchResponse test = runner.client().prepareSearch(index).setSource(addQuery()).execute().actionGet();     }      private String addQuery() {         return "{\"match_all\":{\"boost\":1.2}}";     } } 

I've created es-plugin.properties(pluginrootdirectory\src\main\resources) file with following content which will force elasticsearch instance to load plugin:

plugin=CustomQueryParserPlugin 

When you will run the this test you will see in the output that the newly created insance of elasticsearch loaded the plugin.

[2015-04-29 19:22:10,783][INFO ][org.elasticsearch.node ] [Node 1] version[1.5 .0], pid[34360], build[5448160/2015-03-23T14:30:58Z] [2015-04-29 19:22:10,784][INFO ][org.elasticsearch.node ] [Node 1] initializin g ... [2015-04-29 19:22:10,795][INFO ][org.elasticsearch.plugins] [Node 1] loaded [custom_query], sites [] [2015-04-29 19:22:13,342][INFO ][org.elasticsearch.node ] [Node 1] initialized

[2015-04-29 19:22:13,342][INFO ][org.elasticsearch.node ] [Node 1] starting .. .

Hope this helps.

like image 117
Rob Avatar answered Nov 16 '22 21:11

Rob