Skip to content

Commit 260a929

Browse files
committed
[TEST] inject a random index to TestClusterService in BaseQueryTestCase#init
Some of our next queries to refactor rely on some state taken from the cluster state. That is why we need to mock cluster service and inject an index to it, the index that we simulate the execution of the queries against. The best would be to have multiple indices actually, but that would make our setup a lot more complicated, especially given that IndexQueryParseService is still per index. We might be able to improve that in the future though, for now this is as good as it gets.
1 parent 3ebf7a4 commit 260a929

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

core/src/test/java/org/elasticsearch/index/query/BaseQueryTestCase.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.elasticsearch.Version;
2424
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
2525
import org.elasticsearch.cluster.ClusterService;
26+
import org.elasticsearch.cluster.ClusterState;
2627
import org.elasticsearch.cluster.metadata.IndexMetaData;
2728
import org.elasticsearch.cluster.metadata.MetaData;
2829
import org.elasticsearch.common.ParseFieldMatcher;
@@ -62,6 +63,7 @@
6263
import org.elasticsearch.test.ESTestCase;
6364
import org.elasticsearch.test.TestSearchContext;
6465
import org.elasticsearch.test.VersionUtils;
66+
import org.elasticsearch.test.cluster.TestClusterService;
6567
import org.elasticsearch.threadpool.ThreadPool;
6668
import org.elasticsearch.threadpool.ThreadPoolModule;
6769
import org.joda.time.DateTime;
@@ -95,6 +97,10 @@ public abstract class BaseQueryTestCase<QB extends AbstractQueryBuilder<QB>> ext
9597
private static IndexQueryParserService queryParserService;
9698
private static Index index;
9799

100+
protected static Index getIndex() {
101+
return index;
102+
}
103+
98104
private static String[] currentTypes;
99105

100106
protected static String[] getCurrentTypes() {
@@ -109,30 +115,33 @@ protected static String[] getCurrentTypes() {
109115
*/
110116
@BeforeClass
111117
public static void init() throws IOException {
118+
Version version = VersionUtils.randomVersionBetween(random(), Version.V_1_0_0, Version.CURRENT);
112119
Settings settings = Settings.settingsBuilder()
113120
.put("name", BaseQueryTestCase.class.toString())
114121
.put("path.home", createTempDir())
115-
.put(IndexMetaData.SETTING_VERSION_CREATED, VersionUtils.randomVersionBetween(random(),
116-
Version.V_1_0_0, Version.CURRENT))
117122
.build();
118-
119-
index = new Index("test");
123+
Settings indexSettings = Settings.settingsBuilder()
124+
.put(IndexMetaData.SETTING_VERSION_CREATED, version).build();
125+
index = new Index(randomAsciiOfLengthBetween(1, 10));
126+
final TestClusterService clusterService = new TestClusterService();
127+
clusterService.setState(new ClusterState.Builder(clusterService.state()).metaData(new MetaData.Builder().put(
128+
new IndexMetaData.Builder(index.name()).settings(indexSettings).numberOfShards(1).numberOfReplicas(0))));
120129
injector = new ModulesBuilder().add(
121130
new EnvironmentModule(new Environment(settings)),
122131
new SettingsModule(settings),
123132
new ThreadPoolModule(new ThreadPool(settings)),
124133
new IndicesQueriesModule(),
125134
new ScriptModule(settings),
126-
new IndexSettingsModule(index, settings),
127-
new IndexCacheModule(settings),
128-
new AnalysisModule(settings, new IndicesAnalysisService(settings)),
129-
new SimilarityModule(settings),
135+
new IndexSettingsModule(index, indexSettings),
136+
new IndexCacheModule(indexSettings),
137+
new AnalysisModule(indexSettings, new IndicesAnalysisService(indexSettings)),
138+
new SimilarityModule(indexSettings),
130139
new IndexNameModule(index),
131140
new AbstractModule() {
132141
@Override
133142
protected void configure() {
134143
Multibinder.newSetBinder(binder(), ScoreFunctionParser.class);
135-
bind(ClusterService.class).toProvider(Providers.of((ClusterService) null));
144+
bind(ClusterService.class).toProvider(Providers.of(clusterService));
136145
bind(CircuitBreakerService.class).to(NoneCircuitBreakerService.class);
137146
bind(NamedWriteableRegistry.class).asEagerSingleton();
138147
}

0 commit comments

Comments
 (0)