diff --git a/src/test/java/org/elasticsearch/test/AbstractIntegrationTest.java b/src/test/java/org/elasticsearch/test/AbstractIntegrationTest.java index 23eeda62cf9..10e614a0481 100644 --- a/src/test/java/org/elasticsearch/test/AbstractIntegrationTest.java +++ b/src/test/java/org/elasticsearch/test/AbstractIntegrationTest.java @@ -20,8 +20,6 @@ package org.elasticsearch.test; import com.carrotsearch.randomizedtesting.SeedUtils; import com.google.common.base.Joiner; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableMap.Builder; import com.google.common.collect.Iterators; import org.apache.lucene.util.AbstractRandomizedTest.IntegrationTests; import org.apache.lucene.util.IOUtils; @@ -595,12 +593,24 @@ public abstract class AbstractIntegrationTest extends ElasticSearchTestCase { protected TestCluster buildTestCluster(Scope scope) { long currentClusterSeed = randomLong(); - Builder ordinalMap = ImmutableMap.builder(); int numNodes = getNumNodes(); - for (int i = 0; i < numNodes; i++) { - ordinalMap.put(i, nodeSettings(i)); + NodeSettingsSource nodeSettingsSource; + if (numNodes > 0) { + NodeSettingsSource.Immutable.Builder nodesSettings = NodeSettingsSource.Immutable.builder(); + for (int i = 0; i < numNodes; i++) { + nodesSettings.set(i, nodeSettings(i)); + } + nodeSettingsSource = nodesSettings.build(); + } else { + nodeSettingsSource = new NodeSettingsSource() { + @Override + public Settings settings(int nodeOrdinal) { + return nodeSettings(nodeOrdinal); + } + }; } - return new TestCluster(currentClusterSeed, getNumNodes(), TestCluster.clusterName(scope.name(), ElasticSearchTestCase.CHILD_VM_ID, currentClusterSeed), ordinalMap.build()); + + return new TestCluster(currentClusterSeed, numNodes, TestCluster.clusterName(scope.name(), ElasticSearchTestCase.CHILD_VM_ID, currentClusterSeed), nodeSettingsSource); } @Retention(RetentionPolicy.RUNTIME) diff --git a/src/test/java/org/elasticsearch/test/NodeSettingsSource.java b/src/test/java/org/elasticsearch/test/NodeSettingsSource.java new file mode 100644 index 00000000000..b16c55dc873 --- /dev/null +++ b/src/test/java/org/elasticsearch/test/NodeSettingsSource.java @@ -0,0 +1,77 @@ +/* + * Licensed to ElasticSearch and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. ElasticSearch licenses this + * file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.test; + +import com.google.common.collect.ImmutableMap; +import org.elasticsearch.common.settings.Settings; + +import java.util.Map; + +public abstract class NodeSettingsSource { + + public static final NodeSettingsSource EMPTY = new NodeSettingsSource() { + @Override + public Settings settings(int nodeOrdinal) { + return null; + } + }; + + /** + * @return the settings for the node represented by the given ordinal, or {@code null} if there are not settings defined (in which + * case a random settings will be generated for the node) + */ + public abstract Settings settings(int nodeOrdinal); + + public static class Immutable extends NodeSettingsSource { + + private final Map settingsPerNode; + + private Immutable(Map settingsPerNode) { + this.settingsPerNode = settingsPerNode; + } + + public static Builder builder() { + return new Builder(); + } + + @Override + public Settings settings(int nodeOrdinal) { + return settingsPerNode.get(nodeOrdinal); + } + + public static class Builder { + + private final ImmutableMap.Builder settingsPerNode = ImmutableMap.builder(); + + private Builder() { + } + + public Builder set(int ordinal, Settings settings) { + settingsPerNode.put(ordinal, settings); + return this; + } + + public Immutable build() { + return new Immutable(settingsPerNode.build()); + } + + } + } +} diff --git a/src/test/java/org/elasticsearch/test/TestCluster.java b/src/test/java/org/elasticsearch/test/TestCluster.java index 1d772bc79a0..8b1afb93d35 100644 --- a/src/test/java/org/elasticsearch/test/TestCluster.java +++ b/src/test/java/org/elasticsearch/test/TestCluster.java @@ -94,14 +94,13 @@ public class TestCluster implements Closeable, Iterable { private double transportClientRatio = 0.0; - private final Map perNodeSettingsMap; - private static final Map EMPTY = Collections.emptyMap(); + private final NodeSettingsSource nodeSettingsSource; public TestCluster(long clusterSeed, String clusterName) { - this(clusterSeed, -1, clusterName, EMPTY); + this(clusterSeed, -1, clusterName, NodeSettingsSource.EMPTY); } - public TestCluster(long clusterSeed, int numNodes, String clusterName, Map perNodeSettings) { + public TestCluster(long clusterSeed, int numNodes, String clusterName, NodeSettingsSource nodeSettingsSource) { this.clusterName = clusterName; Random random = new Random(clusterSeed); numSharedNodes = numNodes == -1 ? 2 + random.nextInt(4) : numNodes; // at least 2 nodes if randomized @@ -129,12 +128,12 @@ public class TestCluster implements Closeable, Iterable { // default to non gateway .put("gateway.type", "none") .build(); - this.perNodeSettingsMap = perNodeSettings; + this.nodeSettingsSource = nodeSettingsSource; } private Settings getSettings(int nodeOrdinal, Settings others) { Builder builder = ImmutableSettings.settingsBuilder().put(defaultSettings); - Settings settings = perNodeSettingsMap.get(nodeOrdinal); + Settings settings = nodeSettingsSource.settings(nodeOrdinal); if (settings != null) { builder.put(settings); }