SOLR-6987: SSL support for MiniSolrCloudCluster

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1653402 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gregory Chanan 2015-01-20 23:06:46 +00:00
parent 7844cf5cca
commit e7045badaa
2 changed files with 129 additions and 3 deletions

View File

@ -0,0 +1,90 @@
package org.apache.solr.cloud;
/*
* 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.
*/
import java.io.File;
import java.util.List;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.client.solrj.embedded.JettySolrRunner;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.request.CoreAdminRequest;
import org.apache.solr.common.params.CoreAdminParams.CoreAdminAction;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* Tests SSL (if test framework selects it) with MiniSolrCloudCluster.
* {@link TestMiniSolrCloudCluster} does not inherit from {@link SolrTestCaseJ4}
* so does not support SSL.
*/
public class TestMiniSolrCloudClusterSSL extends SolrTestCaseJ4 {
private static MiniSolrCloudCluster miniCluster;
private static final int NUM_SERVERS = 5;
@BeforeClass
public static void startup() throws Exception {
String testHome = SolrTestCaseJ4.TEST_HOME();
miniCluster = new MiniSolrCloudCluster(NUM_SERVERS, null, createTempDir().toFile(), new File(testHome, "solr-no-core.xml"),
null, null, sslConfig);
}
@AfterClass
public static void shutdown() throws Exception {
if (miniCluster != null) {
miniCluster.shutdown();
}
miniCluster = null;
}
@Test
public void testMiniSolrCloudClusterSSL() throws Exception {
// test send request to each server
sendRequestToEachServer();
// shut down a server
JettySolrRunner stoppedServer = miniCluster.stopJettySolrRunner(0);
assertTrue(stoppedServer.isStopped());
assertEquals(NUM_SERVERS - 1, miniCluster.getJettySolrRunners().size());
// create a new server
JettySolrRunner startedServer = miniCluster.startJettySolrRunner(null, null, null, sslConfig);
assertTrue(startedServer.isRunning());
assertEquals(NUM_SERVERS, miniCluster.getJettySolrRunners().size());
// test send request to each server
sendRequestToEachServer();
}
private void sendRequestToEachServer() throws Exception {
List<JettySolrRunner> jettys = miniCluster.getJettySolrRunners();
for (JettySolrRunner jetty : jettys) {
HttpSolrClient client = new HttpSolrClient(jetty.getBaseUrl().toString());
try {
CoreAdminRequest req = new CoreAdminRequest();
req.setAction( CoreAdminAction.STATUS );
client.request(req);
} finally {
client.shutdown();
}
}
}
}

View File

@ -27,6 +27,7 @@ import java.util.SortedMap;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.embedded.JettySolrRunner;
import org.apache.solr.client.solrj.embedded.SSLConfig;
import org.apache.solr.client.solrj.impl.CloudSolrClient;
import org.apache.solr.client.solrj.request.QueryRequest;
import org.apache.solr.common.cloud.SolrZkClient;
@ -60,6 +61,23 @@ public class MiniSolrCloudCluster {
public MiniSolrCloudCluster(int numServers, String hostContext, File baseDir, File solrXml,
SortedMap<ServletHolder, String> extraServlets,
SortedMap<Class, String> extraRequestFilters) throws Exception {
this(numServers, hostContext, baseDir, solrXml, extraServlets, extraRequestFilters, null);
}
/**
* "Mini" SolrCloud cluster to be used for testing
* @param numServers number of Solr servers to start
* @param hostContext context path of Solr servers used by Jetty
* @param baseDir base directory that the mini cluster should be run from
* @param solrXml solr.xml file to be uploaded to ZooKeeper
* @param extraServlets Extra servlets to be started by Jetty
* @param extraRequestFilters extra filters to be started by Jetty
* @param sslConfig SSL configuration
*/
public MiniSolrCloudCluster(int numServers, String hostContext, File baseDir, File solrXml,
SortedMap<ServletHolder, String> extraServlets,
SortedMap<Class, String> extraRequestFilters,
SSLConfig sslConfig) throws Exception {
testDir = baseDir;
String zkDir = testDir.getAbsolutePath() + File.separator
@ -78,7 +96,11 @@ public class MiniSolrCloudCluster {
jettys = new LinkedList<JettySolrRunner>();
for (int i = 0; i < numServers; ++i) {
startJettySolrRunner(hostContext, extraServlets, extraRequestFilters);
if (sslConfig == null) {
startJettySolrRunner(hostContext, extraServlets, extraRequestFilters);
} else {
startJettySolrRunner(hostContext, extraServlets, extraRequestFilters, sslConfig);
}
}
solrClient = buildSolrClient();
@ -108,9 +130,23 @@ public class MiniSolrCloudCluster {
public JettySolrRunner startJettySolrRunner(String hostContext,
SortedMap<ServletHolder, String> extraServlets,
SortedMap<Class, String> extraRequestFilters) throws Exception {
return startJettySolrRunner(hostContext, extraServlets, extraRequestFilters, null);
}
/**
* Start a new Solr instance
* @param hostContext context path of Solr servers used by Jetty
* @param extraServlets Extra servlets to be started by Jetty
* @param extraRequestFilters extra filters to be started by Jetty
* @param sslConfig SSL configuration
* @return new Solr instance
*/
public JettySolrRunner startJettySolrRunner(String hostContext,
SortedMap<ServletHolder, String> extraServlets,
SortedMap<Class, String> extraRequestFilters, SSLConfig sslConfig) throws Exception {
String context = getHostContextSuitableForServletContext(hostContext);
JettySolrRunner jetty = new JettySolrRunner(testDir.getAbsolutePath(), context, 0, null, null,
true, extraServlets, null, extraRequestFilters);
JettySolrRunner jetty = new JettySolrRunner(testDir.getAbsolutePath(), context,
0, null, null, true, extraServlets, sslConfig, extraRequestFilters);
jetty.start();
jettys.add(jetty);
return jetty;