From 290e4c35461cc63575369d1555fa3fb0ca7ca012 Mon Sep 17 00:00:00 2001 From: David Roberts Date: Thu, 2 Nov 2017 15:44:25 +0000 Subject: [PATCH 1/3] Forbid the single argument Environment constructor in production code Only tests should use the single argument Environment constructor. Production code (beyond initial Bootstrap) should always use the same Environment object that Node.getEnvironment() returns. This Environment is also available via dependency injection. --- buildSrc/src/main/resources/forbidden/es-core-signatures.txt | 3 +++ core/src/main/java/org/elasticsearch/env/Environment.java | 1 + 2 files changed, 4 insertions(+) diff --git a/buildSrc/src/main/resources/forbidden/es-core-signatures.txt b/buildSrc/src/main/resources/forbidden/es-core-signatures.txt index 6507f05be5cd3..0fc565141e6dc 100644 --- a/buildSrc/src/main/resources/forbidden/es-core-signatures.txt +++ b/buildSrc/src/main/resources/forbidden/es-core-signatures.txt @@ -74,6 +74,9 @@ java.util.concurrent.Future#cancel(boolean) org.elasticsearch.common.io.PathUtils#get(java.lang.String, java.lang.String[]) org.elasticsearch.common.io.PathUtils#get(java.net.URI) +@defaultMessage The single argument constructor of Environment is only for tests, always use the Node's Environment in production code +org.elasticsearch.env.Environment#(org.elasticsearch.common.settings.Settings) + @defaultMessage Constructing a DateTime without a time zone is dangerous org.joda.time.DateTime#() org.joda.time.DateTime#(long) diff --git a/core/src/main/java/org/elasticsearch/env/Environment.java b/core/src/main/java/org/elasticsearch/env/Environment.java index 31a67333a810f..c0197503c530c 100644 --- a/core/src/main/java/org/elasticsearch/env/Environment.java +++ b/core/src/main/java/org/elasticsearch/env/Environment.java @@ -85,6 +85,7 @@ public class Environment { /** Path to the temporary file directory used by the JDK */ private final Path tmpFile = PathUtils.get(System.getProperty("java.io.tmpdir")); + /** Convenience method for tests - do not use in production code */ public Environment(Settings settings) { this(settings, null); } From 3d0163c0c33e854387ed996348733e1e3819416d Mon Sep 17 00:00:00 2001 From: David Roberts Date: Fri, 3 Nov 2017 09:46:28 +0000 Subject: [PATCH 2/3] Revert "Forbid the single argument Environment constructor in production code" This reverts commit d8fdce8f0c908fc43f2a58c849f32f0cc8120aba. --- buildSrc/src/main/resources/forbidden/es-core-signatures.txt | 3 --- core/src/main/java/org/elasticsearch/env/Environment.java | 1 - 2 files changed, 4 deletions(-) diff --git a/buildSrc/src/main/resources/forbidden/es-core-signatures.txt b/buildSrc/src/main/resources/forbidden/es-core-signatures.txt index 0fc565141e6dc..6507f05be5cd3 100644 --- a/buildSrc/src/main/resources/forbidden/es-core-signatures.txt +++ b/buildSrc/src/main/resources/forbidden/es-core-signatures.txt @@ -74,9 +74,6 @@ java.util.concurrent.Future#cancel(boolean) org.elasticsearch.common.io.PathUtils#get(java.lang.String, java.lang.String[]) org.elasticsearch.common.io.PathUtils#get(java.net.URI) -@defaultMessage The single argument constructor of Environment is only for tests, always use the Node's Environment in production code -org.elasticsearch.env.Environment#(org.elasticsearch.common.settings.Settings) - @defaultMessage Constructing a DateTime without a time zone is dangerous org.joda.time.DateTime#() org.joda.time.DateTime#(long) diff --git a/core/src/main/java/org/elasticsearch/env/Environment.java b/core/src/main/java/org/elasticsearch/env/Environment.java index c0197503c530c..31a67333a810f 100644 --- a/core/src/main/java/org/elasticsearch/env/Environment.java +++ b/core/src/main/java/org/elasticsearch/env/Environment.java @@ -85,7 +85,6 @@ public class Environment { /** Path to the temporary file directory used by the JDK */ private final Path tmpFile = PathUtils.get(System.getProperty("java.io.tmpdir")); - /** Convenience method for tests - do not use in production code */ public Environment(Settings settings) { this(settings, null); } From 4f401a1a5f8c2e2fa873e6b377b7389f7935a270 Mon Sep 17 00:00:00 2001 From: David Roberts Date: Fri, 3 Nov 2017 11:00:54 +0000 Subject: [PATCH 3/3] Replace single arg Environment constructor with test framework factory This ensures that only the two argument constructor can ever be used in production code. --- .../org/elasticsearch/env/Environment.java | 4 -- .../indices/TransportAnalyzeActionTests.java | 3 +- .../settings/KeyStoreCommandTestCase.java | 4 +- .../elasticsearch/env/EnvironmentTests.java | 10 ++--- .../env/NodeEnvironmentTests.java | 12 +++--- .../elasticsearch/index/IndexModuleTests.java | 3 +- .../index/analysis/AnalysisRegistryTests.java | 9 +++-- .../index/analysis/AnalysisTests.java | 7 ++-- .../index/shard/NewPathForShardTests.java | 10 ++--- .../indices/analysis/AnalysisModuleTests.java | 19 ++++++---- .../indices/cluster/ClusterStateChanges.java | 3 +- .../plugins/PluginsServiceTests.java | 3 +- .../plugins/InstallPluginCommandTests.java | 5 ++- .../plugins/ListPluginsCommandTests.java | 3 +- .../plugins/RemovePluginCommandTests.java | 5 ++- .../common/CompoundAnalysisTests.java | 3 +- .../repositories/url/URLRepositoryTests.java | 10 +++-- .../analysis/AnalysisPolishFactoryTests.java | 3 +- .../FileBasedUnicastHostsProviderTests.java | 3 +- .../azure/AzureRepositorySettingsTests.java | 5 ++- .../gcs/GoogleCloudStorageServiceTests.java | 9 ++--- .../bootstrap/EvilSecurityTests.java | 5 ++- .../env/NodeEnvironmentEvilTests.java | 6 +-- .../bootstrap/SpawnerNoBootstrapTests.java | 9 +++-- .../elasticsearch/env/TestEnvironment.java | 37 +++++++++++++++++++ .../test/AbstractQueryTestCase.java | 3 +- .../elasticsearch/test/ESIntegTestCase.java | 3 +- .../org/elasticsearch/test/ESTestCase.java | 5 ++- 28 files changed, 129 insertions(+), 72 deletions(-) create mode 100644 test/framework/src/main/java/org/elasticsearch/env/TestEnvironment.java diff --git a/core/src/main/java/org/elasticsearch/env/Environment.java b/core/src/main/java/org/elasticsearch/env/Environment.java index 31a67333a810f..721cdcf9ba6db 100644 --- a/core/src/main/java/org/elasticsearch/env/Environment.java +++ b/core/src/main/java/org/elasticsearch/env/Environment.java @@ -85,10 +85,6 @@ public class Environment { /** Path to the temporary file directory used by the JDK */ private final Path tmpFile = PathUtils.get(System.getProperty("java.io.tmpdir")); - public Environment(Settings settings) { - this(settings, null); - } - public Environment(final Settings settings, final Path configPath) { final Path homeFile; if (PATH_HOME_SETTING.exists(settings)) { diff --git a/core/src/test/java/org/elasticsearch/action/admin/indices/TransportAnalyzeActionTests.java b/core/src/test/java/org/elasticsearch/action/admin/indices/TransportAnalyzeActionTests.java index fc41770b37766..90857da0be089 100644 --- a/core/src/test/java/org/elasticsearch/action/admin/indices/TransportAnalyzeActionTests.java +++ b/core/src/test/java/org/elasticsearch/action/admin/indices/TransportAnalyzeActionTests.java @@ -28,6 +28,7 @@ import org.elasticsearch.common.UUIDs; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; +import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.analysis.AbstractCharFilterFactory; import org.elasticsearch.index.analysis.AbstractTokenFilterFactory; @@ -74,7 +75,7 @@ public void setUp() throws Exception { .put("index.analysis.normalizer.my_normalizer.type", "custom") .putList("index.analysis.normalizer.my_normalizer.filter", "lowercase").build(); IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", indexSettings); - environment = new Environment(settings); + environment = TestEnvironment.newEnvironment(settings); AnalysisPlugin plugin = new AnalysisPlugin() { class MockFactory extends AbstractTokenFilterFactory { MockFactory(IndexSettings indexSettings, Environment env, String name, Settings settings) { diff --git a/core/src/test/java/org/elasticsearch/common/settings/KeyStoreCommandTestCase.java b/core/src/test/java/org/elasticsearch/common/settings/KeyStoreCommandTestCase.java index 3d853404664b1..c1118b3bc6513 100644 --- a/core/src/test/java/org/elasticsearch/common/settings/KeyStoreCommandTestCase.java +++ b/core/src/test/java/org/elasticsearch/common/settings/KeyStoreCommandTestCase.java @@ -22,7 +22,6 @@ import java.io.IOException; import java.io.InputStream; import java.nio.file.FileSystem; -import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; @@ -35,6 +34,7 @@ import org.elasticsearch.cli.CommandTestCase; import org.elasticsearch.common.io.PathUtilsForTesting; import org.elasticsearch.env.Environment; +import org.elasticsearch.env.TestEnvironment; import org.junit.After; import org.junit.Before; @@ -70,7 +70,7 @@ public static Environment setupEnv(boolean posix, List fileSystems) PathUtilsForTesting.installMock(fs); // restored by restoreFileSystem in ESTestCase Path home = fs.getPath("/", "test-home"); Files.createDirectories(home.resolve("config")); - return new Environment(Settings.builder().put("path.home", home).build()); + return TestEnvironment.newEnvironment(Settings.builder().put("path.home", home).build()); } KeyStoreWrapper createKeystore(String password, String... settings) throws Exception { diff --git a/core/src/test/java/org/elasticsearch/env/EnvironmentTests.java b/core/src/test/java/org/elasticsearch/env/EnvironmentTests.java index 70df7d33f291c..6ddf6b3ba73b1 100644 --- a/core/src/test/java/org/elasticsearch/env/EnvironmentTests.java +++ b/core/src/test/java/org/elasticsearch/env/EnvironmentTests.java @@ -43,7 +43,7 @@ public Environment newEnvironment(Settings settings) throws IOException { .put(settings) .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toAbsolutePath()) .putList(Environment.PATH_DATA_SETTING.getKey(), tmpPaths()).build(); - return new Environment(build); + return new Environment(build, null); } public void testRepositoryResolution() throws IOException { @@ -76,21 +76,21 @@ public void testRepositoryResolution() throws IOException { public void testPathDataWhenNotSet() { final Path pathHome = createTempDir().toAbsolutePath(); final Settings settings = Settings.builder().put("path.home", pathHome).build(); - final Environment environment = new Environment(settings); + final Environment environment = new Environment(settings, null); assertThat(environment.dataFiles(), equalTo(new Path[]{pathHome.resolve("data")})); } public void testPathDataNotSetInEnvironmentIfNotSet() { final Settings settings = Settings.builder().put("path.home", createTempDir().toAbsolutePath()).build(); assertFalse(Environment.PATH_DATA_SETTING.exists(settings)); - final Environment environment = new Environment(settings); + final Environment environment = new Environment(settings, null); assertFalse(Environment.PATH_DATA_SETTING.exists(environment.settings())); } public void testPathLogsWhenNotSet() { final Path pathHome = createTempDir().toAbsolutePath(); final Settings settings = Settings.builder().put("path.home", pathHome).build(); - final Environment environment = new Environment(settings); + final Environment environment = new Environment(settings, null); assertThat(environment.logsFile(), equalTo(pathHome.resolve("logs"))); } @@ -111,7 +111,7 @@ public void testConfigPath() { public void testConfigPathWhenNotSet() { final Path pathHome = createTempDir().toAbsolutePath(); final Settings settings = Settings.builder().put("path.home", pathHome).build(); - final Environment environment = new Environment(settings); + final Environment environment = new Environment(settings, null); assertThat(environment.configFile(), equalTo(pathHome.resolve("config"))); } diff --git a/core/src/test/java/org/elasticsearch/env/NodeEnvironmentTests.java b/core/src/test/java/org/elasticsearch/env/NodeEnvironmentTests.java index 615a75dda025a..90161e5faaf9f 100644 --- a/core/src/test/java/org/elasticsearch/env/NodeEnvironmentTests.java +++ b/core/src/test/java/org/elasticsearch/env/NodeEnvironmentTests.java @@ -80,12 +80,12 @@ public void testNodeLockSingleEnvironment() throws IOException { // Reuse the same location and attempt to lock again IllegalStateException ex = - expectThrows(IllegalStateException.class, () -> new NodeEnvironment(settings, new Environment(settings))); + expectThrows(IllegalStateException.class, () -> new NodeEnvironment(settings, TestEnvironment.newEnvironment(settings))); assertThat(ex.getMessage(), containsString("failed to obtain node lock")); // Close the environment that holds the lock and make sure we can get the lock after release env.close(); - env = new NodeEnvironment(settings, new Environment(settings)); + env = new NodeEnvironment(settings, TestEnvironment.newEnvironment(settings)); assertThat(env.nodeDataPaths(), arrayWithSize(dataPaths.size())); for (int i = 0; i < dataPaths.size(); i++) { @@ -120,7 +120,7 @@ public void testNodeLockMultipleEnvironment() throws IOException { final Settings settings = buildEnvSettings(Settings.builder().put("node.max_local_storage_nodes", 2).build()); final NodeEnvironment first = newNodeEnvironment(settings); List dataPaths = Environment.PATH_DATA_SETTING.get(settings); - NodeEnvironment second = new NodeEnvironment(settings, new Environment(settings)); + NodeEnvironment second = new NodeEnvironment(settings, TestEnvironment.newEnvironment(settings)); assertEquals(first.nodeDataPaths().length, dataPaths.size()); assertEquals(second.nodeDataPaths().length, dataPaths.size()); for (int i = 0; i < dataPaths.size(); i++) { @@ -477,7 +477,7 @@ public NodeEnvironment newNodeEnvironment() throws IOException { @Override public NodeEnvironment newNodeEnvironment(Settings settings) throws IOException { Settings build = buildEnvSettings(settings); - return new NodeEnvironment(build, new Environment(build)); + return new NodeEnvironment(build, TestEnvironment.newEnvironment(build)); } public Settings buildEnvSettings(Settings settings) { @@ -492,7 +492,7 @@ public NodeEnvironment newNodeEnvironment(String[] dataPaths, Settings settings) .put(settings) .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toAbsolutePath().toString()) .putList(Environment.PATH_DATA_SETTING.getKey(), dataPaths).build(); - return new NodeEnvironment(build, new Environment(build)); + return new NodeEnvironment(build, TestEnvironment.newEnvironment(build)); } public NodeEnvironment newNodeEnvironment(String[] dataPaths, String sharedDataPath, Settings settings) throws IOException { @@ -501,6 +501,6 @@ public NodeEnvironment newNodeEnvironment(String[] dataPaths, String sharedDataP .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toAbsolutePath().toString()) .put(Environment.PATH_SHARED_DATA_SETTING.getKey(), sharedDataPath) .putList(Environment.PATH_DATA_SETTING.getKey(), dataPaths).build(); - return new NodeEnvironment(build, new Environment(build)); + return new NodeEnvironment(build, TestEnvironment.newEnvironment(build)); } } diff --git a/core/src/test/java/org/elasticsearch/index/IndexModuleTests.java b/core/src/test/java/org/elasticsearch/index/IndexModuleTests.java index 609ed02eb2e89..f1037d67ff4aa 100644 --- a/core/src/test/java/org/elasticsearch/index/IndexModuleTests.java +++ b/core/src/test/java/org/elasticsearch/index/IndexModuleTests.java @@ -42,6 +42,7 @@ import org.elasticsearch.env.Environment; import org.elasticsearch.env.NodeEnvironment; import org.elasticsearch.env.ShardLock; +import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.index.analysis.AnalysisRegistry; import org.elasticsearch.index.cache.query.DisabledQueryCache; import org.elasticsearch.index.cache.query.IndexQueryCache; @@ -118,7 +119,7 @@ public void setUp() throws Exception { indicesQueryCache = new IndicesQueryCache(settings); indexSettings = IndexSettingsModule.newIndexSettings("foo", settings); index = indexSettings.getIndex(); - environment = new Environment(settings); + environment = TestEnvironment.newEnvironment(settings); emptyAnalysisRegistry = new AnalysisRegistry(environment, emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptyMap()); threadPool = new TestThreadPool("test"); diff --git a/core/src/test/java/org/elasticsearch/index/analysis/AnalysisRegistryTests.java b/core/src/test/java/org/elasticsearch/index/analysis/AnalysisRegistryTests.java index d93533ffc80d3..9c0f2b3c7a550 100644 --- a/core/src/test/java/org/elasticsearch/index/analysis/AnalysisRegistryTests.java +++ b/core/src/test/java/org/elasticsearch/index/analysis/AnalysisRegistryTests.java @@ -30,6 +30,7 @@ import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; +import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.indices.analysis.AnalysisModule; import org.elasticsearch.indices.analysis.AnalysisModule.AnalysisProvider; @@ -56,8 +57,8 @@ private static AnalyzerProvider analyzerProvider(final String name) { } private static AnalysisRegistry emptyAnalysisRegistry(Settings settings) { - return new AnalysisRegistry(new Environment(settings), emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptyMap(), - emptyMap(), emptyMap()); + return new AnalysisRegistry(TestEnvironment.newEnvironment(settings), emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptyMap(), + emptyMap(), emptyMap(), emptyMap()); } private static IndexSettings indexSettingsOfCurrentVersion(Settings.Builder settings) { @@ -157,8 +158,8 @@ public Map> getTokenFilters() { return singletonMap("mock", MockFactory::new); } }; - IndexAnalyzers indexAnalyzers = new AnalysisModule(new Environment(settings), singletonList(plugin)).getAnalysisRegistry() - .build(idxSettings); + IndexAnalyzers indexAnalyzers = new AnalysisModule(TestEnvironment.newEnvironment(settings), + singletonList(plugin)).getAnalysisRegistry().build(idxSettings); // This shouldn't contain English stopwords try (NamedAnalyzer custom_analyser = indexAnalyzers.get("custom_analyzer_with_camel_case")) { diff --git a/core/src/test/java/org/elasticsearch/index/analysis/AnalysisTests.java b/core/src/test/java/org/elasticsearch/index/analysis/AnalysisTests.java index e07b4e5b9d435..4ed2202f585ea 100644 --- a/core/src/test/java/org/elasticsearch/index/analysis/AnalysisTests.java +++ b/core/src/test/java/org/elasticsearch/index/analysis/AnalysisTests.java @@ -22,6 +22,7 @@ import org.apache.lucene.analysis.CharArraySet; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; +import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.test.ESTestCase; import java.io.BufferedWriter; @@ -61,7 +62,7 @@ public void testParseNonExistingFile() { Settings nodeSettings = Settings.builder() .put("foo.bar_path", tempDir.resolve("foo.dict")) .put(Environment.PATH_HOME_SETTING.getKey(), tempDir).build(); - Environment env = new Environment(nodeSettings); + Environment env = TestEnvironment.newEnvironment(nodeSettings); IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> Analysis.getWordList(env, nodeSettings, "foo.bar")); assertEquals("IOException while reading foo.bar_path: " + tempDir.resolve("foo.dict").toString(), ex.getMessage()); @@ -80,7 +81,7 @@ public void testParseFalseEncodedFile() throws IOException { writer.write(new byte[]{(byte) 0xff, 0x00, 0x00}); // some invalid UTF-8 writer.write('\n'); } - Environment env = new Environment(nodeSettings); + Environment env = TestEnvironment.newEnvironment(nodeSettings); IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> Analysis.getWordList(env, nodeSettings, "foo.bar")); assertEquals("Unsupported character encoding detected while reading foo.bar_path: " + tempDir.resolve("foo.dict").toString() @@ -101,7 +102,7 @@ public void testParseWordList() throws IOException { writer.write("world"); writer.write('\n'); } - Environment env = new Environment(nodeSettings); + Environment env = TestEnvironment.newEnvironment(nodeSettings); List wordList = Analysis.getWordList(env, nodeSettings, "foo.bar"); assertEquals(Arrays.asList("hello", "world"), wordList); diff --git a/core/src/test/java/org/elasticsearch/index/shard/NewPathForShardTests.java b/core/src/test/java/org/elasticsearch/index/shard/NewPathForShardTests.java index 7e0328641e319..4e6e3036f4c40 100644 --- a/core/src/test/java/org/elasticsearch/index/shard/NewPathForShardTests.java +++ b/core/src/test/java/org/elasticsearch/index/shard/NewPathForShardTests.java @@ -27,6 +27,7 @@ import org.elasticsearch.env.Environment; import org.elasticsearch.env.NodeEnvironment; import org.elasticsearch.env.NodeEnvironment.NodePath; +import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.IndexSettingsModule; @@ -34,7 +35,6 @@ import org.junit.BeforeClass; import java.io.IOException; -import java.math.BigInteger; import java.nio.file.FileStore; import java.nio.file.FileSystem; import java.nio.file.Files; @@ -178,7 +178,7 @@ public void testSelectNewPathForShard() throws Exception { Settings settings = Settings.builder() .put(Environment.PATH_HOME_SETTING.getKey(), path) .putList(Environment.PATH_DATA_SETTING.getKey(), paths).build(); - NodeEnvironment nodeEnv = new NodeEnvironment(settings, new Environment(settings)); + NodeEnvironment nodeEnv = new NodeEnvironment(settings, TestEnvironment.newEnvironment(settings)); // Make sure all our mocking above actually worked: NodePath[] nodePaths = nodeEnv.nodePaths(); @@ -233,7 +233,7 @@ public void testSelectNewPathForShardEvenly() throws Exception { Settings settings = Settings.builder() .put(Environment.PATH_HOME_SETTING.getKey(), path) .putList(Environment.PATH_DATA_SETTING.getKey(), paths).build(); - NodeEnvironment nodeEnv = new NodeEnvironment(settings, new Environment(settings)); + NodeEnvironment nodeEnv = new NodeEnvironment(settings, TestEnvironment.newEnvironment(settings)); // Make sure all our mocking above actually worked: NodePath[] nodePaths = nodeEnv.nodePaths(); @@ -290,7 +290,7 @@ public void testGettingPathWithMostFreeSpace() throws Exception { Settings settings = Settings.builder() .put(Environment.PATH_HOME_SETTING.getKey(), path) .putList(Environment.PATH_DATA_SETTING.getKey(), paths).build(); - NodeEnvironment nodeEnv = new NodeEnvironment(settings, new Environment(settings)); + NodeEnvironment nodeEnv = new NodeEnvironment(settings, TestEnvironment.newEnvironment(settings)); aFileStore.usableSpace = 100000; bFileStore.usableSpace = 1000; @@ -315,7 +315,7 @@ public void testTieBreakWithMostShards() throws Exception { Settings settings = Settings.builder() .put(Environment.PATH_HOME_SETTING.getKey(), path) .putList(Environment.PATH_DATA_SETTING.getKey(), paths).build(); - NodeEnvironment nodeEnv = new NodeEnvironment(settings, new Environment(settings)); + NodeEnvironment nodeEnv = new NodeEnvironment(settings, TestEnvironment.newEnvironment(settings)); // Make sure all our mocking above actually worked: NodePath[] nodePaths = nodeEnv.nodePaths(); diff --git a/core/src/test/java/org/elasticsearch/indices/analysis/AnalysisModuleTests.java b/core/src/test/java/org/elasticsearch/indices/analysis/AnalysisModuleTests.java index 2abb4a5dedb0b..2bc98885f9096 100644 --- a/core/src/test/java/org/elasticsearch/indices/analysis/AnalysisModuleTests.java +++ b/core/src/test/java/org/elasticsearch/indices/analysis/AnalysisModuleTests.java @@ -34,6 +34,7 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.env.Environment; +import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.analysis.Analysis; import org.elasticsearch.index.analysis.AnalysisRegistry; @@ -91,7 +92,7 @@ public IndexAnalyzers getIndexAnalyzers(AnalysisRegistry registry, Settings sett public AnalysisRegistry getNewRegistry(Settings settings) { try { - return new AnalysisModule(new Environment(settings), singletonList(new AnalysisPlugin() { + return new AnalysisModule(TestEnvironment.newEnvironment(settings), singletonList(new AnalysisPlugin() { @Override public Map> getTokenFilters() { return singletonMap("myfilter", MyFilterTokenFilterFactory::new); @@ -162,7 +163,8 @@ public void testVersionedAnalyzers() throws Exception { indexAnalyzers.get("thai").analyzer().getVersion()); assertThat(indexAnalyzers.get("custom7").analyzer(), is(instanceOf(StandardAnalyzer.class))); - assertEquals(org.apache.lucene.util.Version.fromBits(3,6,0), indexAnalyzers.get("custom7").analyzer().getVersion()); + assertEquals(org.apache.lucene.util.Version.fromBits(3,6,0), + indexAnalyzers.get("custom7").analyzer().getVersion()); } private void testSimpleConfiguration(Settings settings) throws IOException { @@ -194,7 +196,7 @@ public void testWordListPath() throws Exception { Settings settings = Settings.builder() .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) .build(); - Environment env = new Environment(settings); + Environment env = TestEnvironment.newEnvironment(settings); String[] words = new String[]{"donau", "dampf", "schiff", "spargel", "creme", "suppe"}; Path wordListFile = generateWordList(words); @@ -241,7 +243,8 @@ public void testPluginPreConfiguredCharFilters() throws IOException { boolean noVersionSupportsMultiTerm = randomBoolean(); boolean luceneVersionSupportsMultiTerm = randomBoolean(); boolean elasticsearchVersionSupportsMultiTerm = randomBoolean(); - AnalysisRegistry registry = new AnalysisModule(new Environment(emptyNodeSettings), singletonList(new AnalysisPlugin() { + AnalysisRegistry registry = new AnalysisModule(TestEnvironment.newEnvironment(emptyNodeSettings), + singletonList(new AnalysisPlugin() { @Override public List getPreConfiguredCharFilters() { return Arrays.asList( @@ -285,7 +288,8 @@ public void testPluginPreConfiguredTokenFilters() throws IOException { boolean noVersionSupportsMultiTerm = randomBoolean(); boolean luceneVersionSupportsMultiTerm = randomBoolean(); boolean elasticsearchVersionSupportsMultiTerm = randomBoolean(); - AnalysisRegistry registry = new AnalysisModule(new Environment(emptyNodeSettings), singletonList(new AnalysisPlugin() { + AnalysisRegistry registry = new AnalysisModule(TestEnvironment.newEnvironment(emptyNodeSettings), + singletonList(new AnalysisPlugin() { @Override public List getPreConfiguredTokenFilters() { return Arrays.asList( @@ -359,7 +363,8 @@ public void reset() throws IOException { read = false; } } - AnalysisRegistry registry = new AnalysisModule(new Environment(emptyNodeSettings), singletonList(new AnalysisPlugin() { + AnalysisRegistry registry = new AnalysisModule(TestEnvironment.newEnvironment(emptyNodeSettings), + singletonList(new AnalysisPlugin() { @Override public List getPreConfiguredTokenizers() { return Arrays.asList( @@ -402,7 +407,7 @@ public void testRegisterHunspellDictionary() throws Exception { .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) .build(); - Environment environment = new Environment(settings); + Environment environment = TestEnvironment.newEnvironment(settings); InputStream aff = getClass().getResourceAsStream("/indices/analyze/conf_dir/hunspell/en_US/en_US.aff"); InputStream dic = getClass().getResourceAsStream("/indices/analyze/conf_dir/hunspell/en_US/en_US.dic"); Dictionary dictionary; diff --git a/core/src/test/java/org/elasticsearch/indices/cluster/ClusterStateChanges.java b/core/src/test/java/org/elasticsearch/indices/cluster/ClusterStateChanges.java index 61bc09fb0f1bb..6e6eaf726a599 100644 --- a/core/src/test/java/org/elasticsearch/indices/cluster/ClusterStateChanges.java +++ b/core/src/test/java/org/elasticsearch/indices/cluster/ClusterStateChanges.java @@ -72,6 +72,7 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.NamedXContentRegistry; import org.elasticsearch.env.Environment; +import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.index.IndexService; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.shard.IndexEventListener; @@ -130,7 +131,7 @@ public ClusterStateChanges(NamedXContentRegistry xContentRegistry, ThreadPool th ActionFilters actionFilters = new ActionFilters(Collections.emptySet()); IndexNameExpressionResolver indexNameExpressionResolver = new IndexNameExpressionResolver(settings); DestructiveOperations destructiveOperations = new DestructiveOperations(settings, clusterSettings); - Environment environment = new Environment(settings); + Environment environment = TestEnvironment.newEnvironment(settings); Transport transport = null; // it's not used // mocks diff --git a/core/src/test/java/org/elasticsearch/plugins/PluginsServiceTests.java b/core/src/test/java/org/elasticsearch/plugins/PluginsServiceTests.java index 2f4644c858935..3bd31097dcae6 100644 --- a/core/src/test/java/org/elasticsearch/plugins/PluginsServiceTests.java +++ b/core/src/test/java/org/elasticsearch/plugins/PluginsServiceTests.java @@ -24,6 +24,7 @@ import org.elasticsearch.Version; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; +import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.index.IndexModule; import org.elasticsearch.test.ESTestCase; @@ -59,7 +60,7 @@ public Settings additionalSettings() { public static class FilterablePlugin extends Plugin implements ScriptPlugin {} static PluginsService newPluginsService(Settings settings, Class... classpathPlugins) { - return new PluginsService(settings, null, null, new Environment(settings).pluginsFile(), Arrays.asList(classpathPlugins)); + return new PluginsService(settings, null, null, TestEnvironment.newEnvironment(settings).pluginsFile(), Arrays.asList(classpathPlugins)); } public void testAdditionalSettings() { diff --git a/distribution/tools/plugin-cli/src/test/java/org/elasticsearch/plugins/InstallPluginCommandTests.java b/distribution/tools/plugin-cli/src/test/java/org/elasticsearch/plugins/InstallPluginCommandTests.java index f5f8cdb32ef5f..8e37b10efc83f 100644 --- a/distribution/tools/plugin-cli/src/test/java/org/elasticsearch/plugins/InstallPluginCommandTests.java +++ b/distribution/tools/plugin-cli/src/test/java/org/elasticsearch/plugins/InstallPluginCommandTests.java @@ -37,6 +37,7 @@ import org.elasticsearch.common.settings.KeyStoreWrapper; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; +import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.PosixPermissionsResetter; import org.junit.After; @@ -176,7 +177,7 @@ static Tuple createEnv(FileSystem fs, Function Settings settings = Settings.builder() .put("path.home", home) .build(); - return Tuple.tuple(home, new Environment(settings)); + return Tuple.tuple(home, TestEnvironment.newEnvironment(settings)); } static Path createPluginDir(Function temp) throws IOException { @@ -236,7 +237,7 @@ MockTerminal installPlugin(String pluginUrl, Path home) throws Exception { } MockTerminal installPlugin(String pluginUrl, Path home, InstallPluginCommand command) throws Exception { - Environment env = new Environment(Settings.builder().put("path.home", home).build()); + Environment env = TestEnvironment.newEnvironment(Settings.builder().put("path.home", home).build()); MockTerminal terminal = new MockTerminal(); command.execute(terminal, pluginUrl, true, env); return terminal; diff --git a/distribution/tools/plugin-cli/src/test/java/org/elasticsearch/plugins/ListPluginsCommandTests.java b/distribution/tools/plugin-cli/src/test/java/org/elasticsearch/plugins/ListPluginsCommandTests.java index 2b98a13f4fd55..9a1f61c0d889c 100644 --- a/distribution/tools/plugin-cli/src/test/java/org/elasticsearch/plugins/ListPluginsCommandTests.java +++ b/distribution/tools/plugin-cli/src/test/java/org/elasticsearch/plugins/ListPluginsCommandTests.java @@ -37,6 +37,7 @@ import org.elasticsearch.cli.UserException; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; +import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.test.ESTestCase; import org.junit.Before; @@ -54,7 +55,7 @@ public void setUp() throws Exception { Settings settings = Settings.builder() .put("path.home", home) .build(); - env = new Environment(settings); + env = TestEnvironment.newEnvironment(settings); } static MockTerminal listPlugins(Path home) throws Exception { diff --git a/distribution/tools/plugin-cli/src/test/java/org/elasticsearch/plugins/RemovePluginCommandTests.java b/distribution/tools/plugin-cli/src/test/java/org/elasticsearch/plugins/RemovePluginCommandTests.java index 3a78da6b28404..6c462d39e5775 100644 --- a/distribution/tools/plugin-cli/src/test/java/org/elasticsearch/plugins/RemovePluginCommandTests.java +++ b/distribution/tools/plugin-cli/src/test/java/org/elasticsearch/plugins/RemovePluginCommandTests.java @@ -26,6 +26,7 @@ import org.elasticsearch.cli.UserException; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; +import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.test.ESTestCase; import org.junit.Before; @@ -73,11 +74,11 @@ public void setUp() throws Exception { Settings settings = Settings.builder() .put("path.home", home) .build(); - env = new Environment(settings); + env = TestEnvironment.newEnvironment(settings); } static MockTerminal removePlugin(String name, Path home, boolean purge) throws Exception { - Environment env = new Environment(Settings.builder().put("path.home", home).build()); + Environment env = TestEnvironment.newEnvironment(Settings.builder().put("path.home", home).build()); MockTerminal terminal = new MockTerminal(); new MockRemovePluginCommand(env).execute(terminal, env, name, purge); return terminal; diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/CompoundAnalysisTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/CompoundAnalysisTests.java index 88d218f907a52..9a7bf5eb91570 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/CompoundAnalysisTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/CompoundAnalysisTests.java @@ -26,6 +26,7 @@ import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; +import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.analysis.IndexAnalyzers; import org.elasticsearch.index.analysis.MyFilterTokenFilterFactory; @@ -87,7 +88,7 @@ private List analyze(Settings settings, String analyzerName, String text private AnalysisModule createAnalysisModule(Settings settings) throws IOException { CommonAnalysisPlugin commonAnalysisPlugin = new CommonAnalysisPlugin(); - return new AnalysisModule(new Environment(settings), Arrays.asList(commonAnalysisPlugin, new AnalysisPlugin() { + return new AnalysisModule(TestEnvironment.newEnvironment(settings), Arrays.asList(commonAnalysisPlugin, new AnalysisPlugin() { @Override public Map> getTokenFilters() { return singletonMap("myfilter", MyFilterTokenFilterFactory::new); diff --git a/modules/repository-url/src/test/java/org/elasticsearch/repositories/url/URLRepositoryTests.java b/modules/repository-url/src/test/java/org/elasticsearch/repositories/url/URLRepositoryTests.java index ea274eeae602a..1af4c1eaba9ad 100644 --- a/modules/repository-url/src/test/java/org/elasticsearch/repositories/url/URLRepositoryTests.java +++ b/modules/repository-url/src/test/java/org/elasticsearch/repositories/url/URLRepositoryTests.java @@ -23,6 +23,7 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.NamedXContentRegistry; import org.elasticsearch.env.Environment; +import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.repositories.RepositoryException; import org.elasticsearch.test.ESTestCase; @@ -40,7 +41,8 @@ public void testWhiteListingRepoURL() throws IOException { .put(URLRepository.REPOSITORIES_URL_SETTING.getKey(), repoPath) .build(); RepositoryMetaData repositoryMetaData = new RepositoryMetaData("url", URLRepository.TYPE, baseSettings); - new URLRepository(repositoryMetaData, new Environment(baseSettings), new NamedXContentRegistry(Collections.emptyList())); + new URLRepository(repositoryMetaData, TestEnvironment.newEnvironment(baseSettings), + new NamedXContentRegistry(Collections.emptyList())); } public void testIfNotWhiteListedMustSetRepoURL() throws IOException { @@ -51,7 +53,8 @@ public void testIfNotWhiteListedMustSetRepoURL() throws IOException { .build(); RepositoryMetaData repositoryMetaData = new RepositoryMetaData("url", URLRepository.TYPE, baseSettings); try { - new URLRepository(repositoryMetaData, new Environment(baseSettings), new NamedXContentRegistry(Collections.emptyList())); + new URLRepository(repositoryMetaData, TestEnvironment.newEnvironment(baseSettings), + new NamedXContentRegistry(Collections.emptyList())); fail("RepositoryException should have been thrown."); } catch (RepositoryException e) { String msg = "[url] file url [" + repoPath @@ -71,7 +74,8 @@ public void testMustBeSupportedProtocol() throws IOException { .build(); RepositoryMetaData repositoryMetaData = new RepositoryMetaData("url", URLRepository.TYPE, baseSettings); try { - new URLRepository(repositoryMetaData, new Environment(baseSettings), new NamedXContentRegistry(Collections.emptyList())); + new URLRepository(repositoryMetaData, TestEnvironment.newEnvironment(baseSettings), + new NamedXContentRegistry(Collections.emptyList())); fail("RepositoryException should have been thrown."); } catch (RepositoryException e) { assertEquals("[url] unsupported url protocol [file] from URL [" + repoPath +"]", e.getMessage()); diff --git a/plugins/analysis-stempel/src/test/java/org/elasticsearch/index/analysis/AnalysisPolishFactoryTests.java b/plugins/analysis-stempel/src/test/java/org/elasticsearch/index/analysis/AnalysisPolishFactoryTests.java index ae78b9c01b3f8..f13d7b01149b5 100644 --- a/plugins/analysis-stempel/src/test/java/org/elasticsearch/index/analysis/AnalysisPolishFactoryTests.java +++ b/plugins/analysis-stempel/src/test/java/org/elasticsearch/index/analysis/AnalysisPolishFactoryTests.java @@ -28,6 +28,7 @@ import org.elasticsearch.common.UUIDs; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; +import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.analysis.pl.PolishStemTokenFilterFactory; import org.elasticsearch.indices.analysis.AnalysisFactoryTestCase; @@ -59,7 +60,7 @@ public void testThreadSafety() throws IOException { .put(IndexMetaData.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()) .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) .build(); - Environment environment = new Environment(settings); + Environment environment = TestEnvironment.newEnvironment(settings); IndexMetaData metaData = IndexMetaData.builder(IndexMetaData.INDEX_UUID_NA_VALUE).settings(settings).build(); IndexSettings indexSettings = new IndexSettings(metaData, Settings.EMPTY); testThreadSafety(new PolishStemTokenFilterFactory(indexSettings, environment, "stempelpolishstem", settings)); diff --git a/plugins/discovery-file/src/test/java/org/elasticsearch/discovery/file/FileBasedUnicastHostsProviderTests.java b/plugins/discovery-file/src/test/java/org/elasticsearch/discovery/file/FileBasedUnicastHostsProviderTests.java index db56f9c2f8341..3ddd15a7b4cf3 100644 --- a/plugins/discovery-file/src/test/java/org/elasticsearch/discovery/file/FileBasedUnicastHostsProviderTests.java +++ b/plugins/discovery-file/src/test/java/org/elasticsearch/discovery/file/FileBasedUnicastHostsProviderTests.java @@ -27,6 +27,7 @@ import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.env.Environment; +import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.indices.breaker.NoneCircuitBreakerService; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.transport.MockTransportService; @@ -126,7 +127,7 @@ public void testUnicastHostsDoesNotExist() throws Exception { final Settings settings = Settings.builder() .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()) .build(); - final Environment environment = new Environment(settings); + final Environment environment = TestEnvironment.newEnvironment(settings); final FileBasedUnicastHostsProvider provider = new FileBasedUnicastHostsProvider(environment, transportService, executorService); final List nodes = provider.buildDynamicNodes(); assertEquals(0, nodes.size()); diff --git a/plugins/repository-azure/src/test/java/org/elasticsearch/repositories/azure/AzureRepositorySettingsTests.java b/plugins/repository-azure/src/test/java/org/elasticsearch/repositories/azure/AzureRepositorySettingsTests.java index 6d609bd08d2c6..75025332889a7 100644 --- a/plugins/repository-azure/src/test/java/org/elasticsearch/repositories/azure/AzureRepositorySettingsTests.java +++ b/plugins/repository-azure/src/test/java/org/elasticsearch/repositories/azure/AzureRepositorySettingsTests.java @@ -27,6 +27,7 @@ import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.xcontent.NamedXContentRegistry; import org.elasticsearch.env.Environment; +import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.test.ESTestCase; import java.io.IOException; @@ -42,8 +43,8 @@ private AzureRepository azureRepository(Settings settings) throws StorageExcepti .putList(Environment.PATH_DATA_SETTING.getKey(), tmpPaths()) .put(settings) .build(); - return new AzureRepository(new RepositoryMetaData("foo", "azure", internalSettings), new Environment(internalSettings), - NamedXContentRegistry.EMPTY, null); + return new AzureRepository(new RepositoryMetaData("foo", "azure", internalSettings), + TestEnvironment.newEnvironment(internalSettings), NamedXContentRegistry.EMPTY, null); } diff --git a/plugins/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageServiceTests.java b/plugins/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageServiceTests.java index 5353f1c28e649..a12cd4fdb5c94 100644 --- a/plugins/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageServiceTests.java +++ b/plugins/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageServiceTests.java @@ -21,19 +21,16 @@ import java.io.IOException; import java.io.InputStream; -import java.nio.file.Files; -import java.nio.file.Path; import java.util.Collections; import java.util.Map; import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; +import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.repositories.gcs.GoogleCloudStorageService.InternalGoogleCloudStorageService; import org.elasticsearch.test.ESTestCase; -import static org.hamcrest.Matchers.containsString; - public class GoogleCloudStorageServiceTests extends ESTestCase { private InputStream getDummyCredentialStream() throws IOException { @@ -41,7 +38,7 @@ private InputStream getDummyCredentialStream() throws IOException { } public void testDefaultCredential() throws Exception { - Environment env = new Environment(Settings.builder().put("path.home", createTempDir()).build()); + Environment env = TestEnvironment.newEnvironment(Settings.builder().put("path.home", createTempDir()).build()); GoogleCredential cred = GoogleCredential.fromStream(getDummyCredentialStream()); InternalGoogleCloudStorageService service = new InternalGoogleCloudStorageService(env, Collections.emptyMap()) { @Override @@ -55,7 +52,7 @@ GoogleCredential getDefaultCredential() throws IOException { public void testClientCredential() throws Exception { GoogleCredential cred = GoogleCredential.fromStream(getDummyCredentialStream()); Map credentials = Collections.singletonMap("clientname", cred); - Environment env = new Environment(Settings.builder().put("path.home", createTempDir()).build()); + Environment env = TestEnvironment.newEnvironment(Settings.builder().put("path.home", createTempDir()).build()); InternalGoogleCloudStorageService service = new InternalGoogleCloudStorageService(env, credentials); assertSame(cred, service.getCredential("clientname")); } diff --git a/qa/evil-tests/src/test/java/org/elasticsearch/bootstrap/EvilSecurityTests.java b/qa/evil-tests/src/test/java/org/elasticsearch/bootstrap/EvilSecurityTests.java index 9ba59f8d49727..aa753f6d4509a 100644 --- a/qa/evil-tests/src/test/java/org/elasticsearch/bootstrap/EvilSecurityTests.java +++ b/qa/evil-tests/src/test/java/org/elasticsearch/bootstrap/EvilSecurityTests.java @@ -24,6 +24,7 @@ import org.elasticsearch.common.io.PathUtils; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; +import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.test.ESTestCase; import java.io.FilePermission; @@ -54,7 +55,7 @@ public void testGeneratedPermissions() throws Exception { Permissions permissions; try { System.setProperty("java.io.tmpdir", fakeTmpDir.toString()); - Environment environment = new Environment(settings); + Environment environment = TestEnvironment.newEnvironment(settings); permissions = Security.createPermissions(environment); } finally { System.setProperty("java.io.tmpdir", realTmpDir); @@ -156,7 +157,7 @@ public void testDuplicateDataPaths() throws IOException { .putList(Environment.PATH_DATA_SETTING.getKey(), data.toString(), duplicate.toString()) .build(); - final Environment environment = new Environment(settings); + final Environment environment = TestEnvironment.newEnvironment(settings); final IllegalStateException e = expectThrows(IllegalStateException.class, () -> Security.createPermissions(environment)); assertThat(e, hasToString(containsString("path [" + duplicate.toRealPath() + "] is duplicated by [" + duplicate + "]"))); } diff --git a/qa/evil-tests/src/test/java/org/elasticsearch/env/NodeEnvironmentEvilTests.java b/qa/evil-tests/src/test/java/org/elasticsearch/env/NodeEnvironmentEvilTests.java index 8192a8c8a29c5..57d4a363cc8c7 100644 --- a/qa/evil-tests/src/test/java/org/elasticsearch/env/NodeEnvironmentEvilTests.java +++ b/qa/evil-tests/src/test/java/org/elasticsearch/env/NodeEnvironmentEvilTests.java @@ -52,7 +52,7 @@ public void testMissingWritePermission() throws IOException { .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toAbsolutePath().toString()) .putList(Environment.PATH_DATA_SETTING.getKey(), tempPaths).build(); IOException ioException = expectThrows(IOException.class, () -> { - new NodeEnvironment(build, new Environment(build)); + new NodeEnvironment(build, TestEnvironment.newEnvironment(build)); }); assertTrue(ioException.getMessage(), ioException.getMessage().startsWith(path.toString())); } @@ -72,7 +72,7 @@ public void testMissingWritePermissionOnIndex() throws IOException { .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toAbsolutePath().toString()) .putList(Environment.PATH_DATA_SETTING.getKey(), tempPaths).build(); IOException ioException = expectThrows(IOException.class, () -> { - new NodeEnvironment(build, new Environment(build)); + new NodeEnvironment(build, TestEnvironment.newEnvironment(build)); }); assertTrue(ioException.getMessage(), ioException.getMessage().startsWith("failed to test writes in data directory")); } @@ -97,7 +97,7 @@ public void testMissingWritePermissionOnShard() throws IOException { .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toAbsolutePath().toString()) .putList(Environment.PATH_DATA_SETTING.getKey(), tempPaths).build(); IOException ioException = expectThrows(IOException.class, () -> { - new NodeEnvironment(build, new Environment(build)); + new NodeEnvironment(build, TestEnvironment.newEnvironment(build)); }); assertTrue(ioException.getMessage(), ioException.getMessage().startsWith("failed to test writes in data directory")); } diff --git a/qa/no-bootstrap-tests/src/test/java/org/elasticsearch/bootstrap/SpawnerNoBootstrapTests.java b/qa/no-bootstrap-tests/src/test/java/org/elasticsearch/bootstrap/SpawnerNoBootstrapTests.java index f4e2f0cb7b0c3..d9d4ab5c3aca9 100644 --- a/qa/no-bootstrap-tests/src/test/java/org/elasticsearch/bootstrap/SpawnerNoBootstrapTests.java +++ b/qa/no-bootstrap-tests/src/test/java/org/elasticsearch/bootstrap/SpawnerNoBootstrapTests.java @@ -24,6 +24,7 @@ import org.elasticsearch.Version; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; +import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.plugins.PluginTestUtil; import org.elasticsearch.plugins.Platforms; @@ -72,7 +73,7 @@ public void testNoControllerSpawn() throws IOException, InterruptedException { settingsBuilder.put(Environment.PATH_HOME_SETTING.getKey(), esHome.toString()); Settings settings = settingsBuilder.build(); - Environment environment = new Environment(settings); + Environment environment = TestEnvironment.newEnvironment(settings); // This plugin will NOT have a controller daemon Path plugin = environment.pluginsFile().resolve("a_plugin"); @@ -108,7 +109,7 @@ public void testControllerSpawn() throws IOException, InterruptedException { settingsBuilder.put(Environment.PATH_HOME_SETTING.getKey(), esHome.toString()); Settings settings = settingsBuilder.build(); - Environment environment = new Environment(settings); + Environment environment = TestEnvironment.newEnvironment(settings); // this plugin will have a controller daemon Path plugin = environment.pluginsFile().resolve("test_plugin"); @@ -169,7 +170,7 @@ public void testControllerSpawnWithIncorrectDescriptor() throws IOException { settingsBuilder.put(Environment.PATH_HOME_SETTING.getKey(), esHome.toString()); Settings settings = settingsBuilder.build(); - Environment environment = new Environment(settings); + Environment environment = TestEnvironment.newEnvironment(settings); Path plugin = environment.pluginsFile().resolve("test_plugin"); Files.createDirectories(plugin); @@ -198,7 +199,7 @@ public void testSpawnerHandlingOfDesktopServicesStoreFiles() throws IOException final Path esHome = createTempDir().resolve("home"); final Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), esHome.toString()).build(); - final Environment environment = new Environment(settings); + final Environment environment = TestEnvironment.newEnvironment(settings); Files.createDirectories(environment.pluginsFile()); diff --git a/test/framework/src/main/java/org/elasticsearch/env/TestEnvironment.java b/test/framework/src/main/java/org/elasticsearch/env/TestEnvironment.java new file mode 100644 index 0000000000000..aa2e03ae22ac1 --- /dev/null +++ b/test/framework/src/main/java/org/elasticsearch/env/TestEnvironment.java @@ -0,0 +1,37 @@ +/* + * 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.common.settings.Settings; + +/** + * Provides a convenience method for tests to construct an Environment when the config path does not matter. + * This is in the test framework to force people who construct an Environment in production code to think + * about what the config path needs to be set to. + */ +public class TestEnvironment { + + private TestEnvironment() { + } + + public static Environment newEnvironment(Settings settings) { + return new Environment(settings, null); + } +} diff --git a/test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java index d56db722def82..43904d1f1f9eb 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java @@ -63,6 +63,7 @@ import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.env.Environment; +import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.analysis.IndexAnalyzers; @@ -1048,7 +1049,7 @@ private static class ServiceHolder implements Closeable { ).flatMap(Function.identity()).collect(toList())); IndexScopedSettings indexScopedSettings = settingsModule.getIndexScopedSettings(); idxSettings = IndexSettingsModule.newIndexSettings(index, indexSettings, indexScopedSettings); - AnalysisModule analysisModule = new AnalysisModule(new Environment(nodeSettings), emptyList()); + AnalysisModule analysisModule = new AnalysisModule(TestEnvironment.newEnvironment(nodeSettings), emptyList()); IndexAnalyzers indexAnalyzers = analysisModule.getAnalysisRegistry().build(idxSettings); scriptService = scriptModule.getScriptService(); similarityService = new SimilarityService(idxSettings, null, Collections.emptyMap()); diff --git a/test/framework/src/main/java/org/elasticsearch/test/ESIntegTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/ESIntegTestCase.java index b0b0ffc9df8ad..4a25e95dbc304 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/ESIntegTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/ESIntegTestCase.java @@ -110,6 +110,7 @@ import org.elasticsearch.discovery.zen.ZenDiscovery; import org.elasticsearch.env.Environment; import org.elasticsearch.env.NodeEnvironment; +import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexModule; import org.elasticsearch.index.IndexService; @@ -1971,7 +1972,7 @@ public Path randomRepoPath() { * Returns path to a random directory that can be used to create a temporary file system repo */ public static Path randomRepoPath(Settings settings) { - Environment environment = new Environment(settings); + Environment environment = TestEnvironment.newEnvironment(settings); Path[] repoFiles = environment.repoFiles(); assert repoFiles.length > 0; Path path; diff --git a/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java index 121471bdb0783..db43b5c9c599a 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java @@ -77,6 +77,7 @@ import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.env.Environment; import org.elasticsearch.env.NodeEnvironment; +import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.analysis.AnalysisRegistry; @@ -811,7 +812,7 @@ public NodeEnvironment newNodeEnvironment(Settings settings) throws IOException .put(settings) .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toAbsolutePath()) .putList(Environment.PATH_DATA_SETTING.getKey(), tmpPaths()).build(); - return new NodeEnvironment(build, new Environment(build)); + return new NodeEnvironment(build, TestEnvironment.newEnvironment(build)); } /** Return consistent index settings for the provided index version. */ @@ -1205,7 +1206,7 @@ public static TestAnalysis createTestAnalysis(Index index, Settings nodeSettings */ public static TestAnalysis createTestAnalysis(IndexSettings indexSettings, Settings nodeSettings, AnalysisPlugin... analysisPlugins) throws IOException { - Environment env = new Environment(nodeSettings); + Environment env = TestEnvironment.newEnvironment(nodeSettings); AnalysisModule analysisModule = new AnalysisModule(env, Arrays.asList(analysisPlugins)); AnalysisRegistry analysisRegistry = analysisModule.getAnalysisRegistry(); return new TestAnalysis(analysisRegistry.build(indexSettings),