Skip to content

Commit c019d28

Browse files
committed
add tests
1 parent d23e10d commit c019d28

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

distribution/tools/server-cli/src/main/java/org/elasticsearch/server/cli/ServerProcessBuilder.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.IOException;
2424
import java.io.OutputStream;
2525
import java.io.UncheckedIOException;
26+
import java.nio.file.FileAlreadyExistsException;
2627
import java.nio.file.Files;
2728
import java.nio.file.Path;
2829
import java.util.HashMap;
@@ -135,15 +136,14 @@ private String getCommand() {
135136
* @throws UserException If the process failed during bootstrap
136137
*/
137138
public ServerProcess start() throws UserException {
138-
ensureWorkingDirExists();
139139
return start(ProcessBuilder::start);
140140
}
141141

142142
private void ensureWorkingDirExists() throws UserException {
143143
Path workingDir = serverArgs.logsDir();
144144
try {
145145
Files.createDirectories(workingDir);
146-
} catch (FileNotFoundException e) {
146+
} catch (FileAlreadyExistsException e) {
147147
throw new UserException(ExitCodes.CONFIG, "Logs dir [" + workingDir + "] exists but is not a directory", e);
148148
} catch (IOException e) {
149149
throw new UserException(ExitCodes.CONFIG, "Unable to create logs dir [" + workingDir + "]", e);
@@ -166,6 +166,8 @@ ServerProcess start(ProcessStarter processStarter) throws UserException {
166166
checkRequiredArgument(jvmOptions, "jvmOptions");
167167
checkRequiredArgument(terminal, "terminal");
168168

169+
ensureWorkingDirExists();
170+
169171
Process jvmProcess = null;
170172
ErrorPumpThread errorPump;
171173

distribution/tools/server-cli/src/test/java/org/elasticsearch/server/cli/ServerProcessTests.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.elasticsearch.common.settings.SecureSettings;
2121
import org.elasticsearch.common.settings.Settings;
2222
import org.elasticsearch.core.IOUtils;
23+
import org.elasticsearch.env.Environment;
2324
import org.elasticsearch.test.ESTestCase;
2425
import org.junit.AfterClass;
2526
import org.junit.Before;
@@ -32,6 +33,7 @@
3233
import java.io.PrintStream;
3334
import java.io.UncheckedIOException;
3435
import java.nio.charset.StandardCharsets;
36+
import java.nio.file.FileAlreadyExistsException;
3537
import java.nio.file.Files;
3638
import java.nio.file.Path;
3739
import java.nio.file.Paths;
@@ -65,6 +67,7 @@ public class ServerProcessTests extends ESTestCase {
6567
protected final Map<String, String> sysprops = new HashMap<>();
6668
protected final Map<String, String> envVars = new HashMap<>();
6769
Path esHomeDir;
70+
Path logsDir;
6871
Settings.Builder nodeSettings;
6972
ProcessValidator processValidator;
7073
MainMethod mainCallback;
@@ -93,6 +96,7 @@ public void resetEnv() {
9396
sysprops.put("os.name", "Linux");
9497
sysprops.put("java.home", "javahome");
9598
sysprops.put("es.path.home", esHomeDir.toString());
99+
logsDir = esHomeDir.resolve("logs");
96100
envVars.clear();
97101
nodeSettings = Settings.builder();
98102
processValidator = null;
@@ -212,7 +216,7 @@ ServerArgs createServerArgs(boolean daemonize, boolean quiet) {
212216
secrets,
213217
nodeSettings.build(),
214218
esHomeDir.resolve("config"),
215-
esHomeDir.resolve("logs")
219+
logsDir
216220
);
217221
}
218222

@@ -429,4 +433,26 @@ public void testProcessDies() throws Exception {
429433
int exitCode = server.waitFor();
430434
assertThat(exitCode, equalTo(-9));
431435
}
436+
437+
public void testLogsDirIsFile() throws Exception {
438+
Files.createFile(logsDir);
439+
var e = expectThrows(UserException.class, this::runForeground);
440+
assertThat(e.getMessage(), containsString("exists but is not a directory"));
441+
}
442+
443+
public void testLogsDirCreateParents() throws Exception {
444+
Path testDir = createTempDir();
445+
logsDir = testDir.resolve("subdir/logs");
446+
processValidator = pb -> assertThat(pb.directory().toString(), equalTo(logsDir.toString()));
447+
runForeground();
448+
}
449+
450+
public void testLogsCreateFailure() throws Exception {
451+
Path testDir = createTempDir();
452+
Path parentFile = testDir.resolve("exists");
453+
Files.createFile(parentFile);
454+
logsDir = parentFile.resolve("logs");
455+
var e = expectThrows(UserException.class, this::runForeground);
456+
assertThat(e.getMessage(), containsString("Unable to create logs dir"));
457+
}
432458
}

0 commit comments

Comments
 (0)