fix many test bugs by minimizing URI handling

This commit is contained in:
Robert Muir 2015-04-16 21:24:07 -04:00
parent 84b20c0e0e
commit 65367f5efa
15 changed files with 100 additions and 73 deletions

View File

@ -55,7 +55,7 @@ public class NamingConventionTests extends ElasticsearchTestCase {
String[] packages = {"org.elasticsearch", "org.apache.lucene"};
for (final String packageName : packages) {
final String path = "/" + packageName.replace('.', '/');
final Path startPath = PathUtils.get(NamingConventionTests.class.getResource(path).toURI());
final Path startPath = getResourcePath(path);
final Set<Path> ignore = Sets.newHashSet(PathUtils.get("/org/elasticsearch/stresstest"), PathUtils.get("/org/elasticsearch/benchmark/stress"));
Files.walkFileTree(startPath, new FileVisitor<Path>() {
private Path pkgPrefix = PathUtils.get(path).getParent();

View File

@ -19,7 +19,6 @@
package org.elasticsearch.bwcompat;
import com.carrotsearch.randomizedtesting.LifecycleScope;
import com.google.common.util.concurrent.ListenableFuture;
import org.apache.lucene.index.IndexWriter;
@ -31,7 +30,6 @@ import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.io.FileSystemUtils;
import org.elasticsearch.common.io.PathUtils;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
@ -52,22 +50,19 @@ import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.elasticsearch.test.hamcrest.ElasticsearchAssertions;
import org.elasticsearch.test.index.merge.NoMergePolicyProvider;
import org.elasticsearch.test.junit.annotations.TestLogging;
import org.elasticsearch.test.rest.client.http.HttpRequestBuilder;
import org.hamcrest.Matchers;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.nio.file.DirectoryStream;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;
@ -83,15 +78,14 @@ public class OldIndexBackwardsCompatibilityTests extends ElasticsearchIntegratio
// TODO: test for proper exception on unsupported indexes (maybe via separate test?)
// We have a 0.20.6.zip etc for this.
static List<String> indexes;
List<String> indexes;
static Path singleDataPath;
static Path[] multiDataPath;
@BeforeClass
public static void initIndexesList() throws Exception {
@Before
public void initIndexesList() throws Exception {
indexes = new ArrayList<>();
URL dirUrl = OldIndexBackwardsCompatibilityTests.class.getResource(".");
Path dir = PathUtils.get(dirUrl.toURI());
Path dir = getResourcePath(".");
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "index-*.zip")) {
for (Path path : stream) {
indexes.add(path.getFileName().toString());
@ -102,7 +96,6 @@ public class OldIndexBackwardsCompatibilityTests extends ElasticsearchIntegratio
@AfterClass
public static void tearDownStatics() {
indexes = null;
singleDataPath = null;
multiDataPath = null;
}
@ -160,7 +153,7 @@ public class OldIndexBackwardsCompatibilityTests extends ElasticsearchIntegratio
String indexName = indexFile.replace(".zip", "").toLowerCase(Locale.ROOT);
// decompress the index
Path backwardsIndex = PathUtils.get(getClass().getResource(indexFile).toURI());
Path backwardsIndex = getResourcePath(indexFile);
try (InputStream stream = Files.newInputStream(backwardsIndex)) {
TestUtil.unzip(stream, unzipDir);
}

View File

@ -93,9 +93,9 @@ public class RestoreBackwardsCompatTests extends AbstractSnapshotTests {
}
}
public static List<String> repoVersions() throws Exception {
private List<String> repoVersions() throws Exception {
List<String> repoVersions = newArrayList();
Path repoFiles = PathUtils.get(RestoreBackwardsCompatTests.class.getResource(".").toURI());
Path repoFiles = getResourcePath(".");
try (DirectoryStream<Path> stream = Files.newDirectoryStream(repoFiles, "repo-*.zip")) {
for (Path entry : stream) {
String fileName = entry.getFileName().toString();

View File

@ -25,7 +25,6 @@ import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.io.PathUtils;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.node.Node;
@ -33,7 +32,6 @@ import org.elasticsearch.search.SearchHit;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import java.nio.file.Path;
import java.nio.file.Paths;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse;
@ -51,7 +49,7 @@ public class RoutingBackwardCompatibilityUponUpgradeTests extends ElasticsearchI
}
private void test(String name, Class<? extends HashFunction> expectedHashFunction, boolean expectedUseType) throws Exception {
Path zippedIndexDir = PathUtils.get(getClass().getResource("/org/elasticsearch/cluster/routing/" + name + ".zip").toURI());
Path zippedIndexDir = getResourcePath("/org/elasticsearch/cluster/routing/" + name + ".zip");
Settings baseSettings = prepareBackwardsDataDir(zippedIndexDir);
internalCluster().startNode(ImmutableSettings.builder()
.put(baseSettings)

View File

@ -55,7 +55,7 @@ public class FileSystemUtilsTests extends ElasticsearchTestCase {
// We first copy sources test files from src/test/resources
// Because after when the test runs, src files are moved to their destination
final Path path = PathUtils.get(FileSystemUtilsTests.class.getResource("/org/elasticsearch/common/io/copyappend").toURI());
final Path path = getResourcePath("/org/elasticsearch/common/io/copyappend");
FileSystemUtils.copyDirectoryRecursively(path, src);
}

View File

@ -52,7 +52,7 @@ public class Log4jESLoggerTests extends ElasticsearchTestCase {
super.setUp();
this.testLevel = Log4jESLoggerFactory.getLogger("test").getLevel();
LogConfigurator.reset();
Path configDir = resolveConfigDir();
Path configDir = getResourcePath("config");
// Need to set custom path.conf so we can use a custom logging.yml file for the test
Settings settings = ImmutableSettings.builder()
.put("path.conf", configDir.toAbsolutePath())
@ -128,11 +128,6 @@ public class Log4jESLoggerTests extends ElasticsearchTestCase {
}
private static Path resolveConfigDir() throws Exception {
URL url = Log4jESLoggerTests.class.getResource("config");
return PathUtils.get(url.toURI());
}
private static class TestAppender extends AppenderSkeleton {
private List<LoggingEvent> events = new ArrayList<>();

View File

@ -56,7 +56,7 @@ public class LoggingConfigurationTests extends ElasticsearchTestCase {
public void testResolveMultipleConfigs() throws Exception {
String level = Log4jESLoggerFactory.getLogger("test").getLevel();
try {
Path configDir = resolveConfigDir();
Path configDir = getResourcePath("config");
Settings settings = ImmutableSettings.builder()
.put("path.conf", configDir.toAbsolutePath())
.build();
@ -144,11 +144,6 @@ public class LoggingConfigurationTests extends ElasticsearchTestCase {
assertThat(logSettings.get("yml"), Matchers.nullValue());
}
private static Path resolveConfigDir() throws Exception {
URL url = LoggingConfigurationTests.class.getResource("config");
return PathUtils.get(url.toURI());
}
private static String loggingConfiguration(String suffix) {
return "logging." + randomAsciiOfLength(randomIntBetween(0, 10)) + "." + suffix;
}

View File

@ -30,6 +30,7 @@ import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.nodesinfo.plugin.dummy1.TestPlugin;
import org.elasticsearch.nodesinfo.plugin.dummy2.TestNoVersionPlugin;
import org.elasticsearch.plugins.PluginTestCase;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.elasticsearch.test.ElasticsearchIntegrationTest.ClusterScope;
import org.elasticsearch.test.hamcrest.ElasticsearchAssertions;
@ -51,7 +52,7 @@ import static org.hamcrest.Matchers.*;
*
*/
@ClusterScope(scope= Scope.TEST, numDataNodes =0)
public class SimpleNodesInfoTests extends ElasticsearchIntegrationTest {
public class SimpleNodesInfoTests extends PluginTestCase {
static final class Fields {
static final String SITE_PLUGIN = "dummy";
@ -154,29 +155,11 @@ public class SimpleNodesInfoTests extends ElasticsearchIntegrationTest {
Lists.newArrayList(PluginInfo.VERSION_NOT_AVAILABLE));
}
public static String startNodeWithPlugins(int nodeId, String ... pluginClassNames) throws URISyntaxException {
public String startNodeWithPlugins(int nodeId, String ... pluginClassNames) throws URISyntaxException {
return startNodeWithPlugins(ImmutableSettings.EMPTY, "/org/elasticsearch/nodesinfo/node" + Integer.toString(nodeId) + "/", pluginClassNames);
}
public static String startNodeWithPlugins(Settings nodeSettings, String pluginDir, String ... pluginClassNames) throws URISyntaxException {
URL resource = SimpleNodesInfoTests.class.getResource(pluginDir);
ImmutableSettings.Builder settings = settingsBuilder();
settings.put(nodeSettings);
if (resource != null) {
settings.put("path.plugins", PathUtils.get(resource.toURI()).toAbsolutePath());
}
if (pluginClassNames.length > 0) {
settings.putArray("plugin.types", pluginClassNames);
}
String nodeName = internalCluster().startNode(settings);
// We wait for a Green status
client().admin().cluster().health(clusterHealthRequest().waitForGreenStatus()).actionGet();
return internalCluster().getInstance(ClusterService.class, nodeName).state().nodes().localNodeId();
}
}

View File

@ -36,7 +36,7 @@ import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilde
*
*/
@ClusterScope(scope= ElasticsearchIntegrationTest.Scope.TEST, numDataNodes=0, transportClientRatio = 0)
public class PluginLuceneCheckerTests extends ElasticsearchIntegrationTest {
public class PluginLuceneCheckerTests extends PluginTestCase {
/**
* We check that no Lucene version checking is done
@ -44,7 +44,7 @@ public class PluginLuceneCheckerTests extends ElasticsearchIntegrationTest {
*/
@Test
public void testDisableLuceneVersionCheckingPlugin() throws URISyntaxException {
String serverNodeId = SimpleNodesInfoTests.startNodeWithPlugins(
String serverNodeId = startNodeWithPlugins(
settingsBuilder().put(PluginsService.PLUGINS_CHECK_LUCENE_KEY, false)
.put(PluginsService.ES_PLUGIN_PROPERTIES_FILE_KEY, "es-plugin-test.properties")
.put(PluginsService.LOAD_PLUGIN_FROM_CLASSPATH, true).build(),
@ -69,7 +69,7 @@ public class PluginLuceneCheckerTests extends ElasticsearchIntegrationTest {
*/
@Test
public void testEnableLuceneVersionCheckingPlugin() throws URISyntaxException {
String serverNodeId = SimpleNodesInfoTests.startNodeWithPlugins(
String serverNodeId = startNodeWithPlugins(
settingsBuilder().put(PluginsService.PLUGINS_CHECK_LUCENE_KEY, true)
.put(PluginsService.ES_PLUGIN_PROPERTIES_FILE_KEY, "es-plugin-test.properties")
.put(PluginsService.LOAD_PLUGIN_FROM_CLASSPATH, true).build(),

View File

@ -41,7 +41,7 @@ import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.instanceOf;
@ClusterScope(scope= ElasticsearchIntegrationTest.Scope.TEST, numDataNodes=0, numClientNodes = 1, transportClientRatio = 0)
public class PluginServiceTests extends ElasticsearchIntegrationTest {
public class PluginServiceTests extends PluginTestCase {
@Test
public void testPluginLoadingFromClassName() throws URISyntaxException {
@ -53,7 +53,7 @@ public class PluginServiceTests extends ElasticsearchIntegrationTest {
.put("plugin.types", InSettingsPlugin.class.getName())
.build();
SimpleNodesInfoTests.startNodeWithPlugins(settings, "/org/elasticsearch/plugins/loading/");
startNodeWithPlugins(settings, "/org/elasticsearch/plugins/loading/");
Plugin plugin = getPlugin("in-settings-plugin");
assertNotNull("InSettingsPlugin (defined below in this class) must be loaded", plugin);

View File

@ -0,0 +1,59 @@
/*
* 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.plugins;
import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.junit.Ignore;
import java.net.URISyntaxException;
import java.net.URL;
import static org.elasticsearch.client.Requests.clusterHealthRequest;
import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder;
/**
* Base class that lets you start a node with plugins.
*/
@Ignore
public abstract class PluginTestCase extends ElasticsearchIntegrationTest {
public String startNodeWithPlugins(Settings nodeSettings, String pluginDir, String ... pluginClassNames) throws URISyntaxException {
URL resource = getClass().getResource(pluginDir);
ImmutableSettings.Builder settings = settingsBuilder();
settings.put(nodeSettings);
if (resource != null) {
settings.put("path.plugins", getResourcePath(pluginDir).toAbsolutePath());
}
if (pluginClassNames.length > 0) {
settings.putArray("plugin.types", pluginClassNames);
}
String nodeName = internalCluster().startNode(settings);
// We wait for a Green status
client().admin().cluster().health(clusterHealthRequest().waitForGreenStatus()).actionGet();
return internalCluster().getInstance(ClusterService.class, nodeName).state().nodes().localNodeId();
}
}

View File

@ -49,16 +49,12 @@ public class SitePluginTests extends ElasticsearchIntegrationTest {
@Override
protected Settings nodeSettings(int nodeOrdinal) {
try {
Path pluginDir = Paths.get(SitePluginTests.class.getResource("/org/elasticsearch/plugins").toURI());
return settingsBuilder()
.put(super.nodeSettings(nodeOrdinal))
.put("path.plugins", pluginDir.toAbsolutePath())
.put("force.http.enabled", true)
.build();
} catch (URISyntaxException ex) {
throw new RuntimeException(ex);
}
Path pluginDir = getResourcePath("/org/elasticsearch/plugins");
return settingsBuilder()
.put(super.nodeSettings(nodeOrdinal))
.put("path.plugins", pluginDir.toAbsolutePath())
.put("force.http.enabled", true)
.build();
}
public HttpRequestBuilder httpClient() {

View File

@ -56,7 +56,9 @@ import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
@ -69,7 +71,6 @@ import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAllS
/**
* Base testcase for randomized unit testing with Elasticsearch
*/
@LuceneTestCase.SuppressFileSystems("*")
public abstract class ElasticsearchTestCase extends ESTestCase {
private static Thread.UncaughtExceptionHandler defaultHandler;
@ -173,8 +174,11 @@ public abstract class ElasticsearchTestCase extends ESTestCase {
* non-standard characters.
*/
public Path getResourcePath(String relativePath) {
URI uri = URI.create(getClass().getResource(relativePath).toString());
return PathUtils.get(uri);
try {
return getDataPath(relativePath);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@After

View File

@ -26,6 +26,7 @@ import org.elasticsearch.common.io.PathUtils;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystems;
@ -107,7 +108,11 @@ public final class FileUtils {
}
}
return PathUtils.get(URI.create(resource.toString()));
try {
return PathUtils.get(resource.toURI());
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
private static URL findResource(String path, String optionalFileSuffix) {

View File

@ -22,7 +22,6 @@ package org.elasticsearch.tribe;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.io.PathUtils;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.node.Node;
@ -87,7 +86,7 @@ public class TribeUnitTests extends ElasticsearchTestCase {
@Test
public void testThatTribeClientsIgnoreGlobalConfig() throws Exception {
Path pathConf = PathUtils.get(TribeUnitTests.class.getResource("elasticsearch.yml").toURI()).getParent();
Path pathConf = getResourcePath("elasticsearch.yml").getParent();
Settings settings = ImmutableSettings.builder().put("config.ignore_system_properties", true).put("path.conf", pathConf).build();
assertTribeNodeSuccesfullyCreated(settings);
}