Internal: Remove Environment.resolveConfig
This method has multiple modes of resolving config files by first looking in the config directory, then on the classpath, and finally by prefixing with "config/" on the classpath. Most of the places taking advantage of this were tests, so they did not have to setup a real home dir with config. The only place that was really relying on it was the code which loads names.txt to randomly choose a node name. This change fixes test to setup fake home dirs with their config files. It also makes the logic for finding names.txt explicit: look in config dir, and if it doesn't exist, load /config/names.txt from the classpath.
This commit is contained in:
parent
dcf3f4679f
commit
be638fb6ef
|
@ -496,7 +496,6 @@ public class ElasticsearchException extends RuntimeException implements ToXConte
|
|||
org.elasticsearch.http.HttpException.class,
|
||||
org.elasticsearch.index.shard.IndexShardNotRecoveringException.class,
|
||||
org.elasticsearch.indices.IndexPrimaryShardNotAllocatedException.class,
|
||||
org.elasticsearch.env.FailedToResolveConfigException.class,
|
||||
org.elasticsearch.action.UnavailableShardsException.class,
|
||||
org.elasticsearch.transport.ActionNotFoundTransportException.class,
|
||||
org.elasticsearch.index.shard.TranslogRecoveryPerformer.BatchOperationException.class,
|
||||
|
|
|
@ -24,10 +24,13 @@ import org.elasticsearch.common.io.FileSystemUtils;
|
|||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
/**
|
||||
|
@ -35,23 +38,20 @@ import java.util.concurrent.ThreadLocalRandom;
|
|||
*/
|
||||
public abstract class Names {
|
||||
|
||||
public static String randomNodeName(URL nodeNames) {
|
||||
public static String randomNodeName(InputStream namesFile) {
|
||||
try {
|
||||
int numberOfNames = 0;
|
||||
try (BufferedReader reader = FileSystemUtils.newBufferedReader(nodeNames, Charsets.UTF_8)) {
|
||||
while (reader.readLine() != null) {
|
||||
numberOfNames++;
|
||||
List<String> names = new ArrayList<>();
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(namesFile, Charsets.UTF_8))) {
|
||||
String name = reader.readLine();
|
||||
while (name != null) {
|
||||
names.add(name);
|
||||
name = reader.readLine();
|
||||
}
|
||||
}
|
||||
try (BufferedReader reader = FileSystemUtils.newBufferedReader(nodeNames, Charsets.UTF_8)) {
|
||||
int number = ((ThreadLocalRandom.current().nextInt(numberOfNames)) % numberOfNames);
|
||||
for (int i = 0; i < number; i++) {
|
||||
reader.readLine();
|
||||
}
|
||||
return reader.readLine();
|
||||
}
|
||||
int index = ((ThreadLocalRandom.current().nextInt(names.size())) % names.size());
|
||||
return names.get(index);
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
throw new RuntimeException("Could not read node names list", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -21,17 +21,19 @@ package org.elasticsearch.common.logging.log4j;
|
|||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
|
||||
import org.apache.log4j.PropertyConfigurator;
|
||||
import org.elasticsearch.ElasticsearchException;
|
||||
import org.elasticsearch.common.collect.MapBuilder;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.settings.SettingsException;
|
||||
import org.elasticsearch.env.Environment;
|
||||
import org.elasticsearch.env.FailedToResolveConfigException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.nio.file.*;
|
||||
import java.nio.file.FileVisitOption;
|
||||
import java.nio.file.FileVisitResult;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.SimpleFileVisitor;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
|
@ -143,8 +145,8 @@ public class LogConfigurator {
|
|||
|
||||
public static void loadConfig(Path file, Settings.Builder settingsBuilder) {
|
||||
try {
|
||||
settingsBuilder.loadFromUrl(file.toUri().toURL());
|
||||
} catch (FailedToResolveConfigException | NoClassDefFoundError | MalformedURLException e) {
|
||||
settingsBuilder.loadFromPath(file);
|
||||
} catch (SettingsException | NoClassDefFoundError e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1079,18 +1079,6 @@ public final class Settings implements ToXContent {
|
|||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads settings from a url that represents them using the
|
||||
* {@link SettingsLoaderFactory#loaderFromSource(String)}.
|
||||
*/
|
||||
public Builder loadFromUrl(URL url) throws SettingsException {
|
||||
try {
|
||||
return loadFromStream(url.toExternalForm(), url.openStream());
|
||||
} catch (IOException e) {
|
||||
throw new SettingsException("Failed to open stream for url [" + url.toExternalForm() + "]", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads settings from a url that represents them using the
|
||||
* {@link SettingsLoaderFactory#loaderFromSource(String)}.
|
||||
|
|
|
@ -307,29 +307,4 @@ public class Environment {
|
|||
public FileStore getFileStore(Path path) throws IOException {
|
||||
return ESFileStore.getMatchingFileStore(path, fileStores);
|
||||
}
|
||||
|
||||
public URL resolveConfig(String path) throws FailedToResolveConfigException {
|
||||
// first, try it as a path in the config directory
|
||||
Path f = configFile.resolve(path);
|
||||
if (Files.exists(f)) {
|
||||
try {
|
||||
return f.toUri().toURL();
|
||||
} catch (MalformedURLException e) {
|
||||
throw new FailedToResolveConfigException("Failed to resolve path [" + f + "]", e);
|
||||
}
|
||||
}
|
||||
// try and load it from the classpath directly
|
||||
URL resource = settings.getClassLoader().getResource(path);
|
||||
if (resource != null) {
|
||||
return resource;
|
||||
}
|
||||
// try and load it from the classpath with config/ prefix
|
||||
if (!path.startsWith("config/")) {
|
||||
resource = settings.getClassLoader().getResource("config/" + path);
|
||||
if (resource != null) {
|
||||
return resource;
|
||||
}
|
||||
}
|
||||
throw new FailedToResolveConfigException("Failed to resolve config path [" + path + "], tried config path [" + f + "] and classpath");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,43 +0,0 @@
|
|||
/*
|
||||
* 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.env;
|
||||
|
||||
import org.elasticsearch.ElasticsearchException;
|
||||
import org.elasticsearch.common.io.stream.StreamInput;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class FailedToResolveConfigException extends ElasticsearchException {
|
||||
|
||||
public FailedToResolveConfigException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
public FailedToResolveConfigException(String msg, Throwable cause) {
|
||||
super(msg, cause);
|
||||
}
|
||||
|
||||
public FailedToResolveConfigException(StreamInput in) throws IOException{
|
||||
super(in);
|
||||
}
|
||||
}
|
|
@ -58,7 +58,6 @@ import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
|
|||
import org.apache.lucene.analysis.tr.TurkishAnalyzer;
|
||||
import org.apache.lucene.analysis.util.CharArraySet;
|
||||
import org.apache.lucene.util.Version;
|
||||
|
||||
import org.elasticsearch.common.Strings;
|
||||
import org.elasticsearch.common.collect.MapBuilder;
|
||||
import org.elasticsearch.common.io.FileSystemUtils;
|
||||
|
@ -71,8 +70,14 @@ import org.elasticsearch.index.settings.IndexSettings;
|
|||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.net.URL;
|
||||
import java.util.*;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -226,9 +231,9 @@ public class Analysis {
|
|||
}
|
||||
}
|
||||
|
||||
final URL wordListFile = env.resolveConfig(wordListPath);
|
||||
final Path wordListFile = env.configFile().resolve(wordListPath);
|
||||
|
||||
try (BufferedReader reader = FileSystemUtils.newBufferedReader(wordListFile, Charsets.UTF_8)) {
|
||||
try (BufferedReader reader = FileSystemUtils.newBufferedReader(wordListFile.toUri().toURL(), Charsets.UTF_8)) {
|
||||
return loadWordList(reader, "#");
|
||||
} catch (IOException ioe) {
|
||||
String message = String.format(Locale.ROOT, "IOException while reading %s_path: %s", settingPrefix, ioe.getMessage());
|
||||
|
@ -273,10 +278,10 @@ public class Analysis {
|
|||
return null;
|
||||
}
|
||||
|
||||
final URL fileUrl = env.resolveConfig(filePath);
|
||||
final Path path = env.configFile().resolve(filePath);
|
||||
|
||||
try {
|
||||
return FileSystemUtils.newBufferedReader(fileUrl, Charsets.UTF_8);
|
||||
return FileSystemUtils.newBufferedReader(path.toUri().toURL(), Charsets.UTF_8);
|
||||
} catch (IOException ioe) {
|
||||
String message = String.format(Locale.ROOT, "IOException while reading %s_path: %s", settingPrefix, ioe.getMessage());
|
||||
throw new IllegalArgumentException(message);
|
||||
|
|
|
@ -35,6 +35,8 @@ import org.elasticsearch.index.settings.IndexSettings;
|
|||
import org.xml.sax.InputSource;
|
||||
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
/**
|
||||
* Uses the {@link org.apache.lucene.analysis.compound.HyphenationCompoundWordTokenFilter} to decompound tokens based on hyphenation rules.
|
||||
|
@ -55,10 +57,10 @@ public class HyphenationCompoundWordTokenFilterFactory extends AbstractCompoundW
|
|||
throw new IllegalArgumentException("hyphenation_patterns_path is a required setting.");
|
||||
}
|
||||
|
||||
URL hyphenationPatternsFile = env.resolveConfig(hyphenationPatternsPath);
|
||||
Path hyphenationPatternsFile = env.configFile().resolve(hyphenationPatternsPath);
|
||||
|
||||
try {
|
||||
hyphenationTree = HyphenationCompoundWordTokenFilter.getHyphenationTree(new InputSource(hyphenationPatternsFile.toExternalForm()));
|
||||
hyphenationTree = HyphenationCompoundWordTokenFilter.getHyphenationTree(new InputSource(Files.newInputStream(hyphenationPatternsFile)));
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("Exception while reading hyphenation_patterns_path: " + e.getMessage());
|
||||
}
|
||||
|
|
|
@ -28,9 +28,13 @@ import org.elasticsearch.common.Strings;
|
|||
import org.elasticsearch.common.cli.Terminal;
|
||||
import org.elasticsearch.common.collect.Tuple;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.settings.SettingsException;
|
||||
import org.elasticsearch.env.Environment;
|
||||
import org.elasticsearch.env.FailedToResolveConfigException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -92,23 +96,23 @@ public class InternalSettingsPreparer {
|
|||
// if its default, then load it, but also load form env
|
||||
if (Strings.hasText(System.getProperty("es.default.config"))) {
|
||||
loadFromEnv = true;
|
||||
settingsBuilder.loadFromUrl(environment.resolveConfig(System.getProperty("es.default.config")));
|
||||
settingsBuilder.loadFromPath(environment.configFile().resolve(System.getProperty("es.default.config")));
|
||||
}
|
||||
// if explicit, just load it and don't load from env
|
||||
if (Strings.hasText(System.getProperty("es.config"))) {
|
||||
loadFromEnv = false;
|
||||
settingsBuilder.loadFromUrl(environment.resolveConfig(System.getProperty("es.config")));
|
||||
settingsBuilder.loadFromPath(environment.configFile().resolve(System.getProperty("es.config")));
|
||||
}
|
||||
if (Strings.hasText(System.getProperty("elasticsearch.config"))) {
|
||||
loadFromEnv = false;
|
||||
settingsBuilder.loadFromUrl(environment.resolveConfig(System.getProperty("elasticsearch.config")));
|
||||
settingsBuilder.loadFromPath(environment.configFile().resolve(System.getProperty("elasticsearch.config")));
|
||||
}
|
||||
}
|
||||
if (loadFromEnv) {
|
||||
for (String allowedSuffix : ALLOWED_SUFFIXES) {
|
||||
try {
|
||||
settingsBuilder.loadFromUrl(environment.resolveConfig("elasticsearch" + allowedSuffix));
|
||||
} catch (FailedToResolveConfigException e) {
|
||||
settingsBuilder.loadFromPath(environment.configFile().resolve("elasticsearch" + allowedSuffix));
|
||||
} catch (SettingsException e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
@ -154,16 +158,22 @@ public class InternalSettingsPreparer {
|
|||
// all settings placeholders have been resolved. resolve the value for the name setting by checking for name,
|
||||
// then looking for node.name, and finally generate one if needed
|
||||
if (settings.get("name") == null) {
|
||||
final String name = settings.get("node.name");
|
||||
String name = settings.get("node.name");
|
||||
if (name == null || name.isEmpty()) {
|
||||
settings = settingsBuilder().put(settings)
|
||||
.put("name", Names.randomNodeName(environment.resolveConfig("names.txt")))
|
||||
.build();
|
||||
} else {
|
||||
settings = settingsBuilder().put(settings)
|
||||
.put("name", name)
|
||||
.build();
|
||||
InputStream input;
|
||||
Path namesPath = environment.configFile().resolve("names.txt");
|
||||
if (Files.exists(namesPath)) {
|
||||
try {
|
||||
input = Files.newInputStream(namesPath);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Failed to load custom names.txt from " + namesPath, e);
|
||||
}
|
||||
} else {
|
||||
input = InternalSettingsPreparer.class.getResourceAsStream("/config/names.txt");
|
||||
}
|
||||
name = Names.randomNodeName(input);
|
||||
}
|
||||
settings = settingsBuilder().put(settings).put("name", name).build();
|
||||
}
|
||||
|
||||
environment = new Environment(settings);
|
||||
|
|
|
@ -51,28 +51,6 @@ public class EnvironmentTests extends ESTestCase {
|
|||
return new Environment(build);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResolveJaredResource() throws IOException {
|
||||
Environment environment = newEnvironment();
|
||||
URL url = environment.resolveConfig("META-INF/MANIFEST.MF"); // this works because there is one jar having this file in the classpath
|
||||
assertNotNull(url);
|
||||
try (BufferedReader reader = FileSystemUtils.newBufferedReader(url, Charsets.UTF_8)) {
|
||||
String string = Streams.copyToString(reader);
|
||||
assertTrue(string, string.contains("Manifest-Version"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResolveFileResource() throws IOException {
|
||||
Environment environment = newEnvironment();
|
||||
URL url = environment.resolveConfig("org/elasticsearch/common/cli/tool.help");
|
||||
assertNotNull(url);
|
||||
try (BufferedReader reader = FileSystemUtils.newBufferedReader(url, Charsets.UTF_8)) {
|
||||
String string = Streams.copyToString(reader);
|
||||
assertEquals(string, "tool help");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRepositoryResolution() throws IOException {
|
||||
Environment environment = newEnvironment();
|
||||
|
|
|
@ -22,7 +22,7 @@ package org.elasticsearch.index.analysis;
|
|||
import org.apache.lucene.analysis.Tokenizer;
|
||||
import org.apache.lucene.analysis.core.WhitespaceTokenizer;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.env.FailedToResolveConfigException;
|
||||
import org.elasticsearch.common.settings.SettingsException;
|
||||
import org.elasticsearch.test.ESTokenStreamTestCase;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
@ -72,7 +72,7 @@ public class KeepFilterFactoryTests extends ESTokenStreamTestCase {
|
|||
AnalysisTestsHelper.createAnalysisServiceFromSettings(settings);
|
||||
fail("expected an exception due to non existent keep_words_path");
|
||||
} catch (Throwable e) {
|
||||
assertThat(e.getCause(), instanceOf(FailedToResolveConfigException.class));
|
||||
assertThat(e.getCause(), instanceOf(IllegalArgumentException.class));
|
||||
}
|
||||
|
||||
settings = Settings.settingsBuilder().put(settings)
|
||||
|
|
|
@ -22,6 +22,7 @@ package org.elasticsearch.index.analysis.commongrams;
|
|||
import org.apache.lucene.analysis.Analyzer;
|
||||
import org.apache.lucene.analysis.Tokenizer;
|
||||
import org.apache.lucene.analysis.core.WhitespaceTokenizer;
|
||||
import org.elasticsearch.common.io.FileSystemUtils;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.index.analysis.AnalysisService;
|
||||
import org.elasticsearch.index.analysis.AnalysisTestsHelper;
|
||||
|
@ -31,7 +32,10 @@ import org.junit.Assert;
|
|||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.StringReader;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.hamcrest.Matchers.instanceOf;
|
||||
public class CommonGramsTokenFilterFactoryTests extends ESTokenStreamTestCase {
|
||||
|
@ -136,7 +140,7 @@ public class CommonGramsTokenFilterFactoryTests extends ESTokenStreamTestCase {
|
|||
public void testCommonGramsAnalysis() throws IOException {
|
||||
Settings settings = Settings.settingsBuilder()
|
||||
.loadFromClasspath("org/elasticsearch/index/analysis/commongrams/commongrams.json")
|
||||
.put("path.home", createTempDir().toString())
|
||||
.put("path.home", createHome())
|
||||
.build();
|
||||
{
|
||||
AnalysisService analysisService = AnalysisTestsHelper.createAnalysisServiceFromSettings(settings);
|
||||
|
@ -219,8 +223,8 @@ public class CommonGramsTokenFilterFactoryTests extends ESTokenStreamTestCase {
|
|||
@Test
|
||||
public void testQueryModeCommonGramsAnalysis() throws IOException {
|
||||
Settings settings = Settings.settingsBuilder()
|
||||
.loadFromClasspath("org/elasticsearch/index/analysis/commongrams/commongrams_query_mode.json")
|
||||
.put("path.home", createTempDir().toString())
|
||||
.loadFromClasspath("org/elasticsearch/index/analysis/commongrams/commongrams_query_mode.json")
|
||||
.put("path.home", createHome())
|
||||
.build();
|
||||
{
|
||||
AnalysisService analysisService = AnalysisTestsHelper.createAnalysisServiceFromSettings(settings);
|
||||
|
@ -238,4 +242,13 @@ public class CommonGramsTokenFilterFactoryTests extends ESTokenStreamTestCase {
|
|||
}
|
||||
}
|
||||
|
||||
private Path createHome() throws IOException {
|
||||
InputStream words = getClass().getResourceAsStream("common_words.txt");
|
||||
Path home = createTempDir();
|
||||
Path config = home.resolve("config");
|
||||
Files.createDirectory(config);
|
||||
Files.copy(words, config.resolve("common_words.txt"));
|
||||
return home;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@
|
|||
},
|
||||
"common_grams_file":{
|
||||
"type":"common_grams",
|
||||
"common_words_path":"org/elasticsearch/index/analysis/commongrams/common_words.txt"
|
||||
"common_words_path":"common_words.txt"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,7 +23,7 @@
|
|||
"common_grams_file":{
|
||||
"type":"common_grams",
|
||||
"query_mode" : true,
|
||||
"common_words_path":"org/elasticsearch/index/analysis/commongrams/common_words.txt"
|
||||
"common_words_path":"common_words.txt"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -46,6 +46,9 @@ import org.hamcrest.MatcherAssert;
|
|||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.elasticsearch.common.settings.Settings.settingsBuilder;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
@ -59,9 +62,17 @@ public class SynonymsAnalysisTest extends ESTestCase {
|
|||
|
||||
@Test
|
||||
public void testSynonymsAnalysis() throws IOException {
|
||||
InputStream synonyms = getClass().getResourceAsStream("synonyms.txt");
|
||||
InputStream synonymsWordnet = getClass().getResourceAsStream("synonyms_wordnet.txt");
|
||||
Path home = createTempDir();
|
||||
Path config = home.resolve("config");
|
||||
Files.createDirectory(config);
|
||||
Files.copy(synonyms, config.resolve("synonyms.txt"));
|
||||
Files.copy(synonymsWordnet, config.resolve("synonyms_wordnet.txt"));
|
||||
|
||||
Settings settings = settingsBuilder().
|
||||
loadFromClasspath("org/elasticsearch/index/analysis/synonyms/synonyms.json")
|
||||
.put("path.home", createTempDir().toString())
|
||||
.put("path.home", home)
|
||||
.put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).build();
|
||||
|
||||
Index index = new Index("test");
|
||||
|
|
|
@ -41,7 +41,7 @@
|
|||
},
|
||||
"synonym_file":{
|
||||
"type":"synonym",
|
||||
"synonyms_path":"org/elasticsearch/index/analysis/synonyms/synonyms.txt"
|
||||
"synonyms_path":"synonyms.txt"
|
||||
},
|
||||
"synonymWordnet":{
|
||||
"type":"synonym",
|
||||
|
@ -55,7 +55,7 @@
|
|||
"synonymWordnet_file":{
|
||||
"type":"synonym",
|
||||
"format":"wordnet",
|
||||
"synonyms_path":"org/elasticsearch/index/analysis/synonyms/synonyms_wordnet.txt"
|
||||
"synonyms_path":"synonyms_wordnet.txt"
|
||||
},
|
||||
"synonymWithTokenizerSettings":{
|
||||
"type":"synonym",
|
||||
|
|
|
@ -29,8 +29,11 @@ import org.junit.After;
|
|||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
@ -73,11 +76,21 @@ public class InternalSettingsPreparerTests extends ESTestCase {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testAlternateConfigFileSuffixes() {
|
||||
public void testAlternateConfigFileSuffixes() throws Exception {
|
||||
InputStream yaml = getClass().getResourceAsStream("/config/elasticsearch.yaml");
|
||||
InputStream json = getClass().getResourceAsStream("/config/elasticsearch.json");
|
||||
InputStream properties = getClass().getResourceAsStream("/config/elasticsearch.properties");
|
||||
Path home = createTempDir();
|
||||
Path config = home.resolve("config");
|
||||
Files.createDirectory(config);
|
||||
Files.copy(yaml, config.resolve("elasticsearch.yaml"));
|
||||
Files.copy(json, config.resolve("elasticsearch.json"));
|
||||
Files.copy(properties, config.resolve("elasticsearch.properties"));
|
||||
|
||||
// test that we can read config files with .yaml, .json, and .properties suffixes
|
||||
Tuple<Settings, Environment> tuple = InternalSettingsPreparer.prepareSettings(settingsBuilder()
|
||||
.put("config.ignore_system_properties", true)
|
||||
.put("path.home", createTempDir().toString())
|
||||
.put("path.home", home)
|
||||
.build(), true);
|
||||
|
||||
assertThat(tuple.v1().get("yaml.config.exists"), equalTo("true"));
|
||||
|
|
|
@ -28,7 +28,6 @@ import org.elasticsearch.common.inject.assistedinject.Assisted;
|
|||
import org.elasticsearch.common.io.Streams;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.env.Environment;
|
||||
import org.elasticsearch.env.FailedToResolveConfigException;
|
||||
import org.elasticsearch.index.Index;
|
||||
import org.elasticsearch.index.settings.IndexSettings;
|
||||
|
||||
|
@ -61,7 +60,7 @@ public class IcuCollationTokenFilterFactory extends AbstractTokenFilterFactory {
|
|||
Exception failureToResolve = null;
|
||||
try {
|
||||
rules = Streams.copyToString(Files.newBufferedReader(environment.configFile().resolve(rules), Charset.forName("UTF-8")));
|
||||
} catch (FailedToResolveConfigException | IOException | SecurityException e) {
|
||||
} catch (IOException | SecurityException e) {
|
||||
failureToResolve = e;
|
||||
}
|
||||
try {
|
||||
|
|
|
@ -42,8 +42,11 @@ import org.elasticsearch.test.ESTestCase;
|
|||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
|
@ -190,9 +193,17 @@ public class KuromojiAnalysisTests extends ESTestCase {
|
|||
}
|
||||
|
||||
|
||||
public AnalysisService createAnalysisService() {
|
||||
public AnalysisService createAnalysisService() throws IOException {
|
||||
InputStream empty_dict = getClass().getResourceAsStream("empty_user_dict.txt");
|
||||
InputStream dict = getClass().getResourceAsStream("user_dict.txt");
|
||||
Path home = createTempDir();
|
||||
Path config = home.resolve("config");
|
||||
Files.createDirectory(config);
|
||||
Files.copy(empty_dict, config.resolve("empty_user_dict.txt"));
|
||||
Files.copy(dict, config.resolve("user_dict.txt"));
|
||||
|
||||
Settings settings = Settings.settingsBuilder()
|
||||
.put("path.home", createTempDir())
|
||||
.put("path.home", home)
|
||||
.loadFromClasspath("org/elasticsearch/index/analysis/kuromoji_analysis.json")
|
||||
.put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT)
|
||||
.build();
|
||||
|
@ -258,7 +269,7 @@ public class KuromojiAnalysisTests extends ESTestCase {
|
|||
|
||||
// fix #59
|
||||
@Test
|
||||
public void testKuromojiEmptyUserDict() {
|
||||
public void testKuromojiEmptyUserDict() throws IOException {
|
||||
AnalysisService analysisService = createAnalysisService();
|
||||
TokenizerFactory tokenizerFactory = analysisService.tokenizer("kuromoji_empty_user_dict");
|
||||
assertThat(tokenizerFactory, instanceOf(KuromojiTokenizerFactory.class));
|
||||
|
|
|
@ -43,11 +43,11 @@
|
|||
},
|
||||
"kuromoji_empty_user_dict" : {
|
||||
"type":"kuromoji_tokenizer",
|
||||
"user_dictionary":"org/elasticsearch/index/analysis/empty_user_dict.txt"
|
||||
"user_dictionary":"empty_user_dict.txt"
|
||||
},
|
||||
"kuromoji_user_dict" : {
|
||||
"type":"kuromoji_tokenizer",
|
||||
"user_dictionary":"org/elasticsearch/index/analysis/user_dict.txt"
|
||||
"user_dictionary":"user_dict.txt"
|
||||
}
|
||||
},
|
||||
"analyzer" : {
|
||||
|
|
|
@ -20,9 +20,9 @@
|
|||
package org.elasticsearch.cloud.aws;
|
||||
|
||||
import org.elasticsearch.common.Strings;
|
||||
import org.elasticsearch.common.io.PathUtils;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.env.Environment;
|
||||
import org.elasticsearch.env.FailedToResolveConfigException;
|
||||
import org.elasticsearch.common.settings.SettingsException;
|
||||
import org.elasticsearch.plugin.cloud.aws.CloudAwsPlugin;
|
||||
import org.elasticsearch.test.ESIntegTestCase;
|
||||
import org.elasticsearch.test.ESIntegTestCase.ThirdParty;
|
||||
|
@ -80,16 +80,14 @@ public abstract class AbstractAwsTest extends ESIntegTestCase {
|
|||
.put("cloud.aws.test.write_failures", 0.1)
|
||||
.put("cloud.aws.test.read_failures", 0.1);
|
||||
|
||||
Environment environment = new Environment(settings.build());
|
||||
|
||||
// if explicit, just load it and don't load from env
|
||||
try {
|
||||
if (Strings.hasText(System.getProperty("tests.config"))) {
|
||||
settings.loadFromUrl(environment.resolveConfig(System.getProperty("tests.config")));
|
||||
settings.loadFromPath(PathUtils.get(System.getProperty("tests.config")));
|
||||
} else {
|
||||
throw new IllegalStateException("to run integration tests, you need to set -Dtest.thirdparty=true and -Dtests.config=/path/to/elasticsearch.yml");
|
||||
}
|
||||
} catch (FailedToResolveConfigException exception) {
|
||||
} catch (SettingsException exception) {
|
||||
throw new IllegalStateException("your test configuration file is incorrect: " + System.getProperty("tests.config"), exception);
|
||||
}
|
||||
return settings.build();
|
||||
|
|
|
@ -20,9 +20,9 @@
|
|||
package org.elasticsearch.cloud.azure;
|
||||
|
||||
import org.elasticsearch.common.Strings;
|
||||
import org.elasticsearch.common.io.PathUtils;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.env.Environment;
|
||||
import org.elasticsearch.env.FailedToResolveConfigException;
|
||||
import org.elasticsearch.common.settings.SettingsException;
|
||||
import org.elasticsearch.plugin.cloud.azure.CloudAzurePlugin;
|
||||
import org.elasticsearch.test.ESIntegTestCase;
|
||||
import org.elasticsearch.test.ESIntegTestCase.ThirdParty;
|
||||
|
@ -48,16 +48,15 @@ public abstract class AbstractAzureTest extends ESIntegTestCase {
|
|||
protected Settings readSettingsFromFile() {
|
||||
Settings.Builder settings = Settings.builder();
|
||||
settings.put("path.home", createTempDir());
|
||||
Environment environment = new Environment(settings.build());
|
||||
|
||||
// if explicit, just load it and don't load from env
|
||||
try {
|
||||
if (Strings.hasText(System.getProperty("tests.config"))) {
|
||||
settings.loadFromUrl(environment.resolveConfig(System.getProperty("tests.config")));
|
||||
settings.loadFromPath(PathUtils.get((System.getProperty("tests.config"))));
|
||||
} else {
|
||||
throw new IllegalStateException("to run integration tests, you need to set -Dtests.thirdparty=true and -Dtests.config=/path/to/elasticsearch.yml");
|
||||
}
|
||||
} catch (FailedToResolveConfigException exception) {
|
||||
} catch (SettingsException exception) {
|
||||
throw new IllegalStateException("your test configuration file is incorrect: " + System.getProperty("tests.config"), exception);
|
||||
}
|
||||
return settings.build();
|
||||
|
|
Loading…
Reference in New Issue