From 38d88b2e2cec10a4d6acec7e7101f59fe39c106d Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Tue, 9 Sep 2014 21:48:42 +0200 Subject: [PATCH] [TEST] Added basic test for InternalTestCluster reproducibility --- pom.xml | 2 + .../test/test/InternalTestClusterTests.java | 122 ++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 src/test/java/org/elasticsearch/test/test/InternalTestClusterTests.java diff --git a/pom.xml b/pom.xml index 99d3852a292..031aa969d57 100644 --- a/pom.xml +++ b/pom.xml @@ -651,6 +651,8 @@ org/elasticsearch/test/rest/test/**/* + + org/elasticsearch/test/test/**/* true diff --git a/src/test/java/org/elasticsearch/test/test/InternalTestClusterTests.java b/src/test/java/org/elasticsearch/test/test/InternalTestClusterTests.java new file mode 100644 index 00000000000..6db77a0d4ec --- /dev/null +++ b/src/test/java/org/elasticsearch/test/test/InternalTestClusterTests.java @@ -0,0 +1,122 @@ +/* + * Licensed to Elasticsearch 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.test; + +import com.google.common.collect.ImmutableSet; +import org.apache.lucene.util.IOUtils; +import org.elasticsearch.client.Client; +import org.elasticsearch.cluster.ClusterName; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.test.ElasticsearchTestCase; +import org.elasticsearch.test.InternalTestCluster; +import org.elasticsearch.test.SettingsSource; + +import java.io.IOException; +import java.util.Iterator; +import java.util.Map; +import java.util.Random; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasEntry; + +/** + * Basic test that ensure that the internal cluster reproduces the same + * configuration given the same seed / input. + */ +public class InternalTestClusterTests extends ElasticsearchTestCase { + + public void testInitializiationIsConsistent() { + long clusterSeed = randomLong(); + int minNumDataNodes = randomIntBetween(0, 9); + int maxNumDataNodes = randomIntBetween(minNumDataNodes, 10); + String clusterName = randomRealisticUnicodeOfCodepointLengthBetween(1, 10); + SettingsSource settingsSource = SettingsSource.EMPTY; + int numClientNodes = randomIntBetween(0, 10); + boolean enableRandomBenchNodes = randomBoolean(); + int jvmOrdinal = randomIntBetween(0, 10); + String nodePrefix = randomRealisticUnicodeOfCodepointLengthBetween(1, 10); + + InternalTestCluster cluster0 = new InternalTestCluster(clusterSeed, minNumDataNodes, maxNumDataNodes, clusterName, settingsSource, numClientNodes, enableRandomBenchNodes, jvmOrdinal, nodePrefix); + InternalTestCluster cluster1 = new InternalTestCluster(clusterSeed, minNumDataNodes, maxNumDataNodes, clusterName, settingsSource, numClientNodes, enableRandomBenchNodes, jvmOrdinal, nodePrefix); + assertClusters(cluster0, cluster1, true); + + } + + public static void assertClusters(InternalTestCluster cluster0, InternalTestCluster cluster1, boolean assertClusterName) { + Settings defaultSettings0 = cluster0.getDefaultSettings(); + Settings defaultSettings1 = cluster1.getDefaultSettings(); + assertSettings(defaultSettings0, defaultSettings1, assertClusterName); + assertThat(cluster0.numDataNodes(), equalTo(cluster1.numDataNodes())); + assertThat(cluster0.numBenchNodes(), equalTo(cluster1.numBenchNodes())); + if (assertClusterName) { + assertThat(cluster0.getClusterName(), equalTo(cluster1.getClusterName())); + } + } + + public static void assertSettings(Settings left, Settings right, boolean compareClusterName) { + ImmutableSet> entries0 = left.getAsMap().entrySet(); + Map entries1 = right.getAsMap(); + assertThat(entries0.size(), equalTo(entries1.size())); + for (Map.Entry entry : entries0) { + if(entry.getKey().equals(ClusterName.SETTING) && compareClusterName == false) { + continue; + } + assertThat(entries1, hasEntry(entry.getKey(), entry.getValue())); + } + } + + public void testBeforeTest() throws IOException { + long clusterSeed = randomLong(); + int minNumDataNodes = randomIntBetween(0, 3); + int maxNumDataNodes = randomIntBetween(minNumDataNodes, 4); + String clusterName = this.getClass().getName() + ":" + this.getTestName(); + SettingsSource settingsSource = SettingsSource.EMPTY; + int numClientNodes = randomIntBetween(0, 2); + boolean enableRandomBenchNodes = randomBoolean(); + int jvmOrdinal = randomIntBetween(0, 10); + String nodePrefix = "foobar"; + + InternalTestCluster cluster0 = new InternalTestCluster(clusterSeed, minNumDataNodes, maxNumDataNodes, clusterName+"1", settingsSource, numClientNodes, enableRandomBenchNodes, jvmOrdinal, nodePrefix); + InternalTestCluster cluster1 = new InternalTestCluster(clusterSeed, minNumDataNodes, maxNumDataNodes, clusterName+"2", settingsSource, numClientNodes, enableRandomBenchNodes, jvmOrdinal, nodePrefix); + + assertClusters(cluster0, cluster1, false); + long seed = randomLong(); + try { + { + Random random = new Random(seed); + cluster0.beforeTest(random, random.nextDouble()); + } + { + Random random = new Random(seed); + cluster1.beforeTest(random, random.nextDouble()); + } + assertArrayEquals(cluster0.getNodeNames(), cluster1.getNodeNames()); + Iterator iterator1 = cluster1.iterator(); + for (Client client : cluster0) { + assertTrue(iterator1.hasNext()); + Client other = iterator1.next(); + assertSettings(client.settings(), other.settings(), false); + } + assertArrayEquals(cluster0.getNodeNames(), cluster1.getNodeNames()); + + } finally { + IOUtils.close(cluster0, cluster1); + } + } +}