forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EnvironmentTests.java
289 lines (253 loc) · 14.8 KB
/
EnvironmentTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
package org.elasticsearch.env;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.core.PathUtils;
import org.elasticsearch.test.ESTestCase;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Path;
import java.util.List;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.Matchers.arrayWithSize;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.hasToString;
import static org.hamcrest.Matchers.is;
/**
* Simple unit-tests for Environment.java
*/
public class EnvironmentTests extends ESTestCase {
public void testRepositoryResolution() throws IOException {
Environment environment = newEnvironment();
assertThat(environment.resolveRepoFile("/test/repos/repo1"), nullValue());
assertThat(environment.resolveRepoFile("test/repos/repo1"), nullValue());
environment = newEnvironment(
Settings.builder()
.putList(Environment.PATH_REPO_SETTING.getKey(), "/test/repos", "/another/repos", "/test/repos/../other")
.build()
);
assertThat(environment.resolveRepoFile("/test/repos/repo1"), notNullValue());
assertThat(environment.resolveRepoFile("test/repos/repo1"), notNullValue());
assertThat(environment.resolveRepoFile("/another/repos/repo1"), notNullValue());
assertThat(environment.resolveRepoFile("/test/repos/../repo1"), nullValue());
assertThat(environment.resolveRepoFile("/test/repos/../repos/repo1"), notNullValue());
assertThat(environment.resolveRepoFile("/somethingeles/repos/repo1"), nullValue());
assertThat(environment.resolveRepoFile("/test/other/repo"), notNullValue());
assertThat(environment.resolveRepoURL(new URL("file:///test/repos/repo1")), notNullValue());
assertThat(environment.resolveRepoURL(new URL("file:/test/repos/repo1")), notNullValue());
assertThat(environment.resolveRepoURL(new URL("file://test/repos/repo1")), nullValue());
assertThat(environment.resolveRepoURL(new URL("file:///test/repos/../repo1")), nullValue());
assertThat(environment.resolveRepoURL(new URL("http://localhost/test/")), nullValue());
assertThat(environment.resolveRepoURL(new URL("jar:file:///test/repos/repo1!/repo/")), notNullValue());
assertThat(environment.resolveRepoURL(new URL("jar:file:/test/repos/repo1!/repo/")), notNullValue());
assertThat(environment.resolveRepoURL(new URL("jar:file:///test/repos/repo1!/repo/")).toString(), endsWith("repo1!/repo/"));
assertThat(environment.resolveRepoURL(new URL("jar:file:///test/repos/../repo1!/repo/")), nullValue());
assertThat(environment.resolveRepoURL(new URL("jar:http://localhost/test/../repo1?blah!/repo/")), nullValue());
}
public void testPathDataWhenNotSet() {
final Path pathHome = createTempDir().toAbsolutePath();
final Settings settings = Settings.builder().put("path.home", pathHome).build();
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, null);
assertFalse(Environment.PATH_DATA_SETTING.exists(environment.settings()));
}
public void testPathDataLegacyCommaList() {
final Settings settings = Settings.builder()
.put("path.home", createTempDir().toAbsolutePath())
.put("path.data", createTempDir().toAbsolutePath() + "," + createTempDir().toAbsolutePath())
.build();
final Environment environment = new Environment(settings, null);
assertThat(environment.dataFiles(), arrayWithSize(2));
}
public void testPathLogsWhenNotSet() {
final Path pathHome = createTempDir().toAbsolutePath();
final Settings settings = Settings.builder().put("path.home", pathHome).build();
final Environment environment = new Environment(settings, null);
assertThat(environment.logsFile(), equalTo(pathHome.resolve("logs")));
}
public void testDefaultConfigPath() {
final Path path = createTempDir().toAbsolutePath();
final Settings settings = Settings.builder().put("path.home", path).build();
final Environment environment = new Environment(settings, null);
assertThat(environment.configFile(), equalTo(path.resolve("config")));
}
public void testConfigPath() {
final Path configPath = createTempDir().toAbsolutePath();
final Settings settings = Settings.builder().put("path.home", createTempDir().toAbsolutePath()).build();
final Environment environment = new Environment(settings, configPath);
assertThat(environment.configFile(), equalTo(configPath));
}
public void testConfigPathWhenNotSet() {
final Path pathHome = createTempDir().toAbsolutePath();
final Settings settings = Settings.builder().put("path.home", pathHome).build();
final Environment environment = new Environment(settings, null);
assertThat(environment.configFile(), equalTo(pathHome.resolve("config")));
}
public void testNodeDoesNotRequireLocalStorage() {
final Path pathHome = createTempDir().toAbsolutePath();
final Settings settings = Settings.builder().put("path.home", pathHome).put("node.master", false).put("node.data", false).build();
final Environment environment = new Environment(settings, null, false);
assertThat(environment.dataFiles(), arrayWithSize(0));
}
public void testNodeDoesNotRequireLocalStorageButHasPathData() {
final Path pathHome = createTempDir().toAbsolutePath();
final Path pathData = pathHome.resolve("data");
final Settings settings = Settings.builder()
.put("path.home", pathHome)
.put("path.data", pathData)
.put("node.master", false)
.put("node.data", false)
.build();
final IllegalStateException e = expectThrows(IllegalStateException.class, () -> new Environment(settings, null, false));
assertThat(e, hasToString(containsString("node does not require local storage yet path.data is set to [" + pathData + "]")));
}
public void testNonExistentTempPathValidation() {
Settings build = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()).build();
Environment environment = new Environment(build, null, true, createTempDir().resolve("this_does_not_exist"));
FileNotFoundException e = expectThrows(FileNotFoundException.class, environment::validateTmpFile);
assertThat(e.getMessage(), startsWith("Temporary directory ["));
assertThat(e.getMessage(), endsWith("this_does_not_exist] does not exist or is not accessible"));
}
public void testTempPathValidationWhenRegularFile() throws IOException {
Settings build = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()).build();
Environment environment = new Environment(build, null, true, createTempFile("something", ".test"));
IOException e = expectThrows(IOException.class, environment::validateTmpFile);
assertThat(e.getMessage(), startsWith("Temporary directory ["));
assertThat(e.getMessage(), endsWith(".test] is not a directory"));
}
public void testNonExistentTempPathValidationForNatives() {
Settings build = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()).build();
Environment environment = new Environment(build, null, true, createTempDir().resolve("this_does_not_exist"));
FileNotFoundException e = expectThrows(FileNotFoundException.class, environment::validateNativesConfig);
assertThat(e.getMessage(), startsWith("Temporary directory ["));
assertThat(e.getMessage(), endsWith("this_does_not_exist] does not exist or is not accessible"));
}
public void testTempPathValidationWhenRegularFileForNatives() throws IOException {
Settings build = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()).build();
Environment environment = new Environment(build, null, true, createTempFile("something", ".test"));
IOException e = expectThrows(IOException.class, environment::validateNativesConfig);
assertThat(e.getMessage(), startsWith("Temporary directory ["));
assertThat(e.getMessage(), endsWith(".test] is not a directory"));
}
// test that environment paths are absolute and normalized
public void testPathNormalization() throws IOException {
final Setting<String> pidFileSetting;
if (randomBoolean()) {
pidFileSetting = Environment.NODE_PIDFILE_SETTING;
} else {
pidFileSetting = Environment.PIDFILE_SETTING;
}
final Settings settings = Settings.builder()
.put(Environment.PATH_HOME_SETTING.getKey(), "home")
.put(Environment.PATH_DATA_SETTING.getKey(), "./home/../home/data")
.put(Environment.PATH_LOGS_SETTING.getKey(), "./home/../home/logs")
.put(Environment.PATH_REPO_SETTING.getKey(), "./home/../home/repo")
.put(Environment.PATH_SHARED_DATA_SETTING.getKey(), "./home/../home/shared_data")
.put(pidFileSetting.getKey(), "./home/../home/pidfile")
.build();
// the above paths will be treated as relative to the working directory
final Path workingDirectory = PathUtils.get(System.getProperty("user.dir"));
final Environment environment = new Environment(settings, null, true, createTempDir());
final String homePath = Environment.PATH_HOME_SETTING.get(environment.settings());
assertPath(homePath, workingDirectory.resolve("home"));
final Path home = PathUtils.get(homePath);
final List<String> dataPaths = Environment.PATH_DATA_SETTING.get(environment.settings());
assertThat(dataPaths, hasSize(1));
assertPath(dataPaths.get(0), home.resolve("data"));
final String logPath = Environment.PATH_LOGS_SETTING.get(environment.settings());
assertPath(logPath, home.resolve("logs"));
final List<String> repoPaths = Environment.PATH_REPO_SETTING.get(environment.settings());
assertThat(repoPaths, hasSize(1));
assertPath(repoPaths.get(0), home.resolve("repo"));
final String sharedDataPath = Environment.PATH_SHARED_DATA_SETTING.get(environment.settings());
assertPath(sharedDataPath, home.resolve("shared_data"));
final String pidFile = pidFileSetting.get(environment.settings());
assertPath(pidFile, home.resolve("pidfile"));
if (pidFileSetting.isDeprecated()) {
assertSettingDeprecationsAndWarnings(new Setting<?>[] { pidFileSetting });
}
}
public void testSingleDataPathListCheck() {
{
final Settings settings = Settings.builder().build();
assertThat(Environment.dataPathUsesList(settings), is(false));
}
{
final Settings settings = Settings.builder()
.putList(Environment.PATH_DATA_SETTING.getKey(), createTempDir().toString(), createTempDir().toString())
.build();
assertThat(Environment.dataPathUsesList(settings), is(true));
}
{
final Settings settings = Settings.builder()
.putList(Environment.PATH_DATA_SETTING.getKey(), createTempDir().toString())
.build();
assertThat(Environment.dataPathUsesList(settings), is(true));
}
{
final Settings settings = Settings.builder()
.put(Environment.PATH_DATA_SETTING.getKey(), createTempDir().toString() + "," + createTempDir().toString())
.build();
assertThat(Environment.dataPathUsesList(settings), is(true));
}
}
public void testLegacyDataPathListPropagation() {
{
final Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()).build();
Environment env = new Environment(settings, null);
assertThat(Environment.dataPathUsesList(env.settings()), is(false));
}
{
final Settings settings = Settings.builder()
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir())
.putList(Environment.PATH_DATA_SETTING.getKey(), createTempDir().toString(), createTempDir().toString())
.build();
Environment env = new Environment(settings, null);
assertThat(Environment.dataPathUsesList(env.settings()), is(true));
}
{
final Settings settings = Settings.builder()
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir())
.putList(Environment.PATH_DATA_SETTING.getKey(), createTempDir().toString())
.build();
Environment env = new Environment(settings, null);
assertThat(Environment.dataPathUsesList(env.settings()), is(true));
}
{
final Settings settings = Settings.builder()
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir())
.put(Environment.PATH_DATA_SETTING.getKey(), createTempDir().toString())
.build();
Environment env = new Environment(settings, null);
assertThat(Environment.dataPathUsesList(env.settings()), is(false));
}
}
private void assertPath(final String actual, final Path expected) {
assertIsAbsolute(actual);
assertIsNormalized(actual);
assertThat(PathUtils.get(actual), equalTo(expected));
}
private void assertIsAbsolute(final String path) {
assertTrue("path [" + path + "] is not absolute", PathUtils.get(path).isAbsolute());
}
private void assertIsNormalized(final String path) {
assertThat("path [" + path + "] is not normalized", PathUtils.get(path), equalTo(PathUtils.get(path).normalize()));
}
}