diff --git a/distribution/tools/launchers/build.gradle b/distribution/tools/launchers/build.gradle index aee205a24dea3..aa03e5f656522 100644 --- a/distribution/tools/launchers/build.gradle +++ b/distribution/tools/launchers/build.gradle @@ -35,6 +35,7 @@ dependencies { compileOnly project(':distribution:tools:java-version-checker') testImplementation "com.carrotsearch.randomizedtesting:randomizedtesting-runner:${versions.randomizedrunner}" testImplementation "junit:junit:${versions.junit}" + testImplementation "org.mockito:mockito-core:${versions.mockito}" testImplementation "org.hamcrest:hamcrest:${versions.hamcrest}" } diff --git a/distribution/tools/launchers/src/main/java/org/opensearch/tools/launchers/Launchers.java b/distribution/tools/launchers/src/main/java/org/opensearch/tools/launchers/Launchers.java index 815615cd936b9..ff86f1e25b889 100644 --- a/distribution/tools/launchers/src/main/java/org/opensearch/tools/launchers/Launchers.java +++ b/distribution/tools/launchers/src/main/java/org/opensearch/tools/launchers/Launchers.java @@ -75,8 +75,8 @@ static void exit(final int status) { } @SuppressForbidden(reason = "Files#createTempDirectory(String, FileAttribute...)") - static Path createTempDirectory(final String prefix, final FileAttribute... attrs) throws IOException { - return Files.createTempDirectory(prefix, attrs); + static Path createTempDirectory(final Path rootDir, final String prefix, final FileAttribute... attrs) throws IOException { + return Files.createTempDirectory(rootDir, prefix, attrs); } } diff --git a/distribution/tools/launchers/src/main/java/org/opensearch/tools/launchers/TempDirectory.java b/distribution/tools/launchers/src/main/java/org/opensearch/tools/launchers/TempDirectory.java index b282e27bc0340..1728ada007fc0 100644 --- a/distribution/tools/launchers/src/main/java/org/opensearch/tools/launchers/TempDirectory.java +++ b/distribution/tools/launchers/src/main/java/org/opensearch/tools/launchers/TempDirectory.java @@ -64,7 +64,7 @@ public static void main(final String[] args) throws IOException { path = Paths.get(System.getProperty("java.io.tmpdir"), "opensearch"); Files.createDirectories(path); } else { - path = Launchers.createTempDirectory("opensearch-"); + path = Launchers.createTempDirectory(Paths.get(System.getProperty("java.io.tmpdir")), "opensearch-"); } Launchers.outPrintln(path.toString()); } diff --git a/distribution/tools/launchers/src/test/java/org/opensearch/tools/launchers/TempDirectoryTests.java b/distribution/tools/launchers/src/test/java/org/opensearch/tools/launchers/TempDirectoryTests.java new file mode 100644 index 0000000000000..c6d482e391c05 --- /dev/null +++ b/distribution/tools/launchers/src/test/java/org/opensearch/tools/launchers/TempDirectoryTests.java @@ -0,0 +1,72 @@ +package org.opensearch.tools.launchers; + +import com.carrotsearch.randomizedtesting.RandomizedTest; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +import static org.junit.Assert.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +public class TempDirectoryTests extends LaunchersTestCase { + + @Rule + public TemporaryFolder tempFolder = new TemporaryFolder(); + + private Path createdTempDir; + + @Before + public void setUp() throws IOException{ + System.setProperty("java.io.tmpdir", tempFolder.newFolder().toString()); + } + + @After + public void tearDown() throws IOException { + if (createdTempDir != null && Files.exists(createdTempDir)) { + Files.delete(createdTempDir); + } + } + + @Test + public void testMainWithArguments() { + String[] args = {RandomizedTest.randomAsciiLettersOfLengthBetween(1, 10)}; + assertThrows(IllegalArgumentException.class, () -> TempDirectory.main(args)); + } + + @Test + public void testMainOnWindows() throws IOException { + System.setProperty("os.name", "Windows " + RandomizedTest.randomIntBetween(7, 10)); + Launchers mockLaunchers = mock(Launchers.class); + doNothing().when(mockLaunchers).outPrintln(any(String.class)); + + TempDirectory.main(new String[]{}); + + createdTempDir = Paths.get(System.getProperty("java.io.tmpdir"), "opensearch"); + verify(mockLaunchers, times(1)).outPrintln(createdTempDir.toString()); + assert Files.exists(createdTempDir); + } + + @Test + public void testMainOnNonWindows() throws IOException { + String [] osNames = {"Linux", "Mac OS X", "Unix"}; + String osName = RandomizedTest.randomFrom(osNames); + System.setProperty("os.name", osName); + Launchers mockLaunchers = mock(Launchers.class); + doNothing().when(mockLaunchers).outPrintln(any(String.class)); + when(mockLaunchers.createTempDirectory(any(Path.class), any(String.class))).thenReturn(tempFolder.newFolder().toPath().resolve("opensearch-" + RandomizedTest.randomAsciiLettersOfLength(3))); + + TempDirectory.main(new String[]{}); + + createdTempDir = tempFolder.newFolder().toPath().resolve("opensearch-" + RandomizedTest.randomAsciiLettersOfLength(3)); + verify(mockLaunchers, times(1)).outPrintln(createdTempDir.toString()); + assert Files.exists(createdTempDir); + } +}