From 0800ccd3c269888d06a06974f47e216fb4fdf615 Mon Sep 17 00:00:00 2001 From: "Chris M. Hostetter" Date: Mon, 3 Jun 2013 17:58:01 +0000 Subject: [PATCH] SOLR-4853: Fixed SolrJettyTestBase so it may be reused by end users git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1489081 13f79535-47bb-0310-9956-ffa450edef68 --- solr/CHANGES.txt | 2 + .../solrj/impl/BasicHttpSolrServerTest.java | 16 ++++++ .../org/apache/solr/SolrJettyTestBase.java | 39 ++++++++++---- .../org/apache/solr/util/ExternalPaths.java | 53 ++++++++++++++----- 4 files changed, 87 insertions(+), 23 deletions(-) diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index 3303a7a05ef..55f794f88ff 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -223,6 +223,8 @@ Bug Fixes * SOLR-4858: SolrCore reloading was broken when the UpdateLog was enabled. (Hossman, Anshum Gupta, Alexey Serba, Mark Miller, yonik) +* SOLR-4853: Fixed SolrJettyTestBase so it may be reused by end users + (hossman) Other Changes ---------------------- diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrServerTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrServerTest.java index 9f2af65ea5f..7ff8ed9a301 100644 --- a/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrServerTest.java +++ b/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrServerTest.java @@ -495,6 +495,22 @@ public class BasicHttpSolrServerTest extends SolrJettyTestBase { client.getConnectionManager().shutdown(); } + /** + * A trivial test that verifies the example keystore used for SSL testing can be + * found using the base class. this helps future-proof against hte possibility of + * something moving/breaking thekeystore path in a way that results in the SSL + * randomization logic being forced to silently never use SSL. (We can't enforce + * this type of check in the base class because then it would not be usable by client + * code depending on the test framework + */ + public void testExampleKeystorePath() { + assertNotNull("Example keystore is null, meaning that something has changed in the " + + "structure of the example configs and/or ExternalPaths.java - " + + "SSL randomization is broken", + getExampleKeystoreFile()); + } + + private int findUnusedPort() { for (int port = 0; port < 65535; port++) { Socket s = new Socket(); diff --git a/solr/test-framework/src/java/org/apache/solr/SolrJettyTestBase.java b/solr/test-framework/src/java/org/apache/solr/SolrJettyTestBase.java index 3d619c46da9..feae82a1d26 100755 --- a/solr/test-framework/src/java/org/apache/solr/SolrJettyTestBase.java +++ b/solr/test-framework/src/java/org/apache/solr/SolrJettyTestBase.java @@ -44,18 +44,32 @@ abstract public class SolrJettyTestBase extends SolrTestCaseJ4 public String getSolrHome() { return ExternalPaths.EXAMPLE_HOME; } private static boolean manageSslProps = true; - private static final File TEST_KEYSTORE = new File(ExternalPaths.SOURCE_HOME, - "example/etc/solrtest.keystore"); + private static File TEST_KEYSTORE; private static final Map SSL_PROPS = new HashMap(); static { + TEST_KEYSTORE = (null == ExternalPaths.SOURCE_HOME) + ? null : new File(ExternalPaths.SOURCE_HOME, "example/etc/solrtest.keystore"); + String keystorePath = null == TEST_KEYSTORE ? null : TEST_KEYSTORE.getAbsolutePath(); + SSL_PROPS.put("tests.jettySsl","false"); SSL_PROPS.put("tests.jettySsl.clientAuth","false"); - SSL_PROPS.put("javax.net.ssl.keyStore", TEST_KEYSTORE.getAbsolutePath()); + SSL_PROPS.put("javax.net.ssl.keyStore", keystorePath); SSL_PROPS.put("javax.net.ssl.keyStorePassword","secret"); - SSL_PROPS.put("javax.net.ssl.trustStore", TEST_KEYSTORE.getAbsolutePath()); + SSL_PROPS.put("javax.net.ssl.trustStore", keystorePath); SSL_PROPS.put("javax.net.ssl.trustStorePassword","secret"); } + /** + * Returns the File object for the example keystore used when this baseclass randomly + * uses SSL. May be null ifthis test does not appear to be running as part of the + * standard solr distribution and does not have access to the example configs. + * + * @lucene.internal + */ + protected static File getExampleKeystoreFile() { + return TEST_KEYSTORE; + } + @BeforeClass public static void beforeSolrJettyTestBase() throws Exception { @@ -63,20 +77,27 @@ abstract public class SolrJettyTestBase extends SolrTestCaseJ4 final boolean trySsl = random().nextBoolean(); final boolean trySslClientAuth = random().nextBoolean(); + // only randomize SSL if we are a solr test with access to the example keystore + if (null == getExampleKeystoreFile()) { + log.info("Solr's example keystore not defined (not a solr test?) skipping SSL randomization"); + manageSslProps = false; + return; + } + + assertTrue("test keystore does not exist, randomized ssl testing broken: " + + getExampleKeystoreFile().getAbsolutePath(), + getExampleKeystoreFile().exists() ); + // only randomize SSL if none of the SSL_PROPS are already set final Map sysprops = System.getProperties(); for (String prop : SSL_PROPS.keySet()) { if (sysprops.containsKey(prop)) { log.info("System property explicitly set, so skipping randomized ssl properties: " + prop); manageSslProps = false; - break; + return; } } - assertTrue("test keystore does not exist, can't be used for randomized " + - "ssl testing: " + TEST_KEYSTORE.getAbsolutePath(), - TEST_KEYSTORE.exists() ); - if (manageSslProps) { log.info("Randomized ssl ({}) and clientAuth ({})", trySsl, trySslClientAuth); for (String prop : SSL_PROPS.keySet()) { diff --git a/solr/test-framework/src/java/org/apache/solr/util/ExternalPaths.java b/solr/test-framework/src/java/org/apache/solr/util/ExternalPaths.java index 79258ea6553..f12e3a404a7 100644 --- a/solr/test-framework/src/java/org/apache/solr/util/ExternalPaths.java +++ b/solr/test-framework/src/java/org/apache/solr/util/ExternalPaths.java @@ -25,30 +25,55 @@ import java.io.File; * @lucene.internal */ public class ExternalPaths { + + /** + *

+ * The main directory path for the solr source being built if it can be determined. If it + * can not be determined -- possily because the current context is a client code base + * using hte test frameowrk -- then this variable will be null. + *

+ *

+ * Note that all other static paths available in this class are derived from the source + * home, and if it is null, those paths will just be relative to 'null' and may not be + * meaningful. + */ public static final String SOURCE_HOME = determineSourceHome(); + /* @see #SOURCE_HOME */ public static String WEBAPP_HOME = new File(SOURCE_HOME, "webapp/web").getAbsolutePath(); + /* @see #SOURCE_HOME */ public static String EXAMPLE_HOME = new File(SOURCE_HOME, "example/solr").getAbsolutePath(); + /* @see #SOURCE_HOME */ public static String EXAMPLE_MULTICORE_HOME = new File(SOURCE_HOME, "example/multicore").getAbsolutePath(); + /* @see #SOURCE_HOME */ public static String EXAMPLE_SCHEMA=EXAMPLE_HOME+"/collection1/conf/schema.xml"; + /* @see #SOURCE_HOME */ public static String EXAMPLE_CONFIG=EXAMPLE_HOME+"/collection1/conf/solrconfig.xml"; + /** + * Ugly, ugly hack to determine the example home without depending on the CWD + * this is needed for example/multicore tests which reside outside the classpath. + * if the source home can't be determined, this method returns null. + */ static String determineSourceHome() { - // ugly, ugly hack to determine the example home without depending on the CWD - // this is needed for example/multicore tests which reside outside the classpath - File file; try { - file = new File("solr/conf"); - if (!file.exists()) { - file = new File(Thread.currentThread().getContextClassLoader().getResource("solr/conf").toURI()); + File file; + try { + file = new File("solr/conf"); + if (!file.exists()) { + file = new File(Thread.currentThread().getContextClassLoader().getResource("solr/conf").toURI()); + } + } catch (Exception e) { + // If there is no "solr/conf" in the classpath, fall back to searching from the current directory. + file = new File("."); } - } catch (Exception e) { - // If there is no "solr/conf" in the classpath, fall back to searching from the current directory. - file = new File("."); + File base = file.getAbsoluteFile(); + while (!(new File(base, "solr/CHANGES.txt").exists()) && null != base) { + base = base.getParentFile(); + } + return (null == base) ? null : new File(base, "solr/").getAbsolutePath(); + } catch (RuntimeException e) { + // all bets are off + return null; } - File base = file.getAbsoluteFile(); - while (!new File(base, "solr/CHANGES.txt").exists()) { - base = base.getParentFile(); - } - return new File(base, "solr/").getAbsolutePath(); } }