diff --git a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/genconf/TestGenerateOzoneRequiredConfigurations.java b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/genconf/TestGenerateOzoneRequiredConfigurations.java index 75c15a4058..8c75ebb1cb 100644 --- a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/genconf/TestGenerateOzoneRequiredConfigurations.java +++ b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/genconf/TestGenerateOzoneRequiredConfigurations.java @@ -95,8 +95,8 @@ public void testGenerateConfigurationsThroughMainMethod() throws Exception { try (PrintStream ps = new PrintStream(outContent)) { System.setOut(ps); GenerateOzoneRequiredConfigurations.main(args); - Assert.assertThat(outContent.toString(), - CoreMatchers.containsString("ozone-site.xml has been generated at")); + Assert.assertThat(outContent.toString(), CoreMatchers.containsString( + "ozone-site.xml has been generated at")); System.setOut(oldStream); } } @@ -123,8 +123,29 @@ public void generateConfigurationsFailure() throws Exception { tempPath.setWritable(true); } + /** + * Test to avoid generating ozone-site.xml when invalid permission. + * @throws Exception + */ + @Test + public void generateConfigurationsFailureForInvalidPath() throws Exception { + File tempPath = getRandomTempDir(); + tempPath.setReadOnly(); + String[] args = new String[]{"-output", + tempPath.getAbsolutePath() + "/ozone-site.xml"}; + GenerateOzoneRequiredConfigurations.main(args); + + Assert.assertEquals("Path is invalid", false, + GenerateOzoneRequiredConfigurations.isValidPath(args[1])); + + Assert.assertEquals("Config file not generated", 1, + GenerateOzoneRequiredConfigurations.generateConfigurations(args[1])); + tempPath.setWritable(true); + } + private File getRandomTempDir() throws IOException { - File tempDir = new File(outputBaseDir, RandomStringUtils.randomAlphanumeric(5)); + File tempDir = new File(outputBaseDir, + RandomStringUtils.randomAlphanumeric(5)); FileUtils.forceMkdir(tempDir); return tempDir; } diff --git a/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/genconf/GenerateOzoneRequiredConfigurations.java b/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/genconf/GenerateOzoneRequiredConfigurations.java index 6296c9dc57..688b735849 100644 --- a/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/genconf/GenerateOzoneRequiredConfigurations.java +++ b/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/genconf/GenerateOzoneRequiredConfigurations.java @@ -25,6 +25,7 @@ import javax.xml.bind.Marshaller; import java.io.File; import java.net.URL; +import java.nio.file.Files; import java.nio.file.InvalidPathException; import java.nio.file.Paths; import java.util.ArrayList; @@ -94,18 +95,17 @@ public static void main(String[] args) { } /** - * Check if the path is valid. + * Check if the path is valid directory. * * @param path - * @return true, if path is valid, else return false + * @return true, if path is valid directory, else return false */ public static boolean isValidPath(String path) { try { - Paths.get(path); + return Files.isDirectory(Paths.get(path)); } catch (InvalidPathException | NullPointerException ex) { return false; } - return true; } /** @@ -129,12 +129,12 @@ public static boolean canWrite(String path) { public static int generateConfigurations(String path) throws JAXBException { if (!isValidPath(path)) { - System.out.println("Invalid path or insufficient permission"); + System.out.println("Invalid directory path."); return FAILURE; } if (!canWrite(path)) { - System.out.println("Invalid path or insufficient permission"); + System.out.println("Insufficient permission."); return FAILURE; }