You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by an...@apache.org on 2016/04/21 23:49:27 UTC

[6/7] lucene-solr:branch_5x: SOLR-8758: Add SolrCloudTestCase base class

SOLR-8758: Add SolrCloudTestCase base class


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/27e284bb
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/27e284bb
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/27e284bb

Branch: refs/heads/branch_5x
Commit: 27e284bb7bcd6536b3c017d76e675f24397cce9c
Parents: 3c6ef10
Author: Alan Woodward <ro...@apache.org>
Authored: Mon Feb 29 18:16:36 2016 +0000
Committer: anshum <an...@apache.org>
Committed: Thu Apr 21 14:24:25 2016 -0700

----------------------------------------------------------------------
 solr/CHANGES.txt                                |   7 +
 .../apache/solr/cloud/SolrCloudTestCase.java    | 163 +++++++++++++++++++
 2 files changed, 170 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/27e284bb/solr/CHANGES.txt
----------------------------------------------------------------------
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 07c81fc..6f9d428 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -106,6 +106,13 @@ Bug Fixes
 
 * SOLR-8375: ReplicaAssigner rejects valid nodes (Kelvin Tan, noble)
 
+
+Other Changes
+----------------------
+
+* SOLR-8758: Add a new SolrCloudTestCase class, using MiniSolrCloudCluster (Alan
+  Woodward)
+
 ======================= 5.5.0 =======================
 
 Consult the LUCENE_CHANGES.txt file for additional, low level, changes in this release

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/27e284bb/solr/test-framework/src/java/org/apache/solr/cloud/SolrCloudTestCase.java
----------------------------------------------------------------------
diff --git a/solr/test-framework/src/java/org/apache/solr/cloud/SolrCloudTestCase.java b/solr/test-framework/src/java/org/apache/solr/cloud/SolrCloudTestCase.java
new file mode 100644
index 0000000..615365e
--- /dev/null
+++ b/solr/test-framework/src/java/org/apache/solr/cloud/SolrCloudTestCase.java
@@ -0,0 +1,163 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF 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.apache.solr.cloud;
+
+import java.io.IOException;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.solr.SolrTestCaseJ4;
+import org.apache.solr.client.solrj.embedded.JettyConfig;
+import org.apache.solr.client.solrj.impl.CloudSolrClient;
+import org.junit.AfterClass;
+import org.junit.Before;
+
+/**
+ * Base class for SolrCloud tests
+ *
+ * Derived tests should call {@link #configureCluster(int)} in a {@code BeforeClass}
+ * static method.  This configures and starts a {@link MiniSolrCloudCluster}, available
+ * via the {@code cluster} variable.  Cluster shutdown is handled automatically.
+ *
+ * <pre>
+ *   <code>
+ *   {@literal @}BeforeClass
+ *   public static void setupCluster() {
+ *     configureCluster(NUM_NODES)
+ *        .addConfig("configname", pathToConfig)
+ *        .configure();
+ *   }
+ *   </code>
+ * </pre>
+ */
+public class SolrCloudTestCase extends SolrTestCaseJ4 {
+
+  private static class Config {
+    final String name;
+    final Path path;
+
+    private Config(String name, Path path) {
+      this.name = name;
+      this.path = path;
+    }
+  }
+
+  /**
+   * Builder class for a MiniSolrCloudCluster
+   */
+  protected static class Builder {
+
+    private final int nodeCount;
+    private final Path baseDir;
+    private String solrxml = MiniSolrCloudCluster.DEFAULT_CLOUD_SOLR_XML;
+    private JettyConfig jettyConfig = buildJettyConfig("/solr");
+
+    private List<Config> configs = new ArrayList<>();
+
+    /**
+     * Create a builder
+     * @param nodeCount the number of nodes in the cluster
+     * @param baseDir   a base directory for the cluster
+     */
+    public Builder(int nodeCount, Path baseDir) {
+      this.nodeCount = nodeCount;
+      this.baseDir = baseDir;
+    }
+
+    /**
+     * Use a {@link JettyConfig} to configure the cluster's jetty servers
+     */
+    public Builder withJettyConfig(JettyConfig jettyConfig) {
+      this.jettyConfig = jettyConfig;
+      return this;
+    }
+
+    /**
+     * Use the provided string as solr.xml content
+     */
+    public Builder withSolrXml(String solrXml) {
+      this.solrxml = solrXml;
+      return this;
+    }
+
+    /**
+     * Read solr.xml from the provided path
+     */
+    public Builder withSolrXml(Path solrXml) {
+      try {
+        this.solrxml = new String(Files.readAllBytes(solrXml), Charset.defaultCharset());
+      } catch (IOException e) {
+        throw new RuntimeException(e);
+      }
+      return this;
+    }
+
+    /**
+     * Upload a collection config before tests start
+     * @param configName the config name
+     * @param configPath the path to the config files
+     */
+    public Builder addConfig(String configName, Path configPath) {
+      this.configs.add(new Config(configName, configPath));
+      return this;
+    }
+
+    /**
+     * Configure and run the {@link MiniSolrCloudCluster}
+     * @throws Exception if an error occurs on startup
+     */
+    public void configure() throws Exception {
+      cluster = new MiniSolrCloudCluster(nodeCount, baseDir, solrxml, jettyConfig);
+      CloudSolrClient client = cluster.getSolrClient();
+      for (Config config : configs) {
+        client.uploadConfig(config.path, config.name);
+      }
+    }
+
+  }
+
+  /** The cluster */
+  protected static MiniSolrCloudCluster cluster;
+
+  /**
+   * Call this to configure a cluster of n nodes.
+   *
+   * NB you must call {@link Builder#configure()} to start the cluster
+   *
+   * @param nodeCount the number of nodes
+   */
+  protected static Builder configureCluster(int nodeCount) {
+    return new Builder(nodeCount, createTempDir());
+  }
+
+  @AfterClass
+  public static void shutdownCluster() throws Exception {
+    if (cluster != null)
+      cluster.shutdown();
+  }
+
+  @Before
+  public void checkClusterConfiguration() {
+    if (cluster == null)
+      throw new RuntimeException("MiniSolrCloudCluster not configured - have you called configureCluster().configure()?");
+  }
+
+}