HDDS-417. Ambiguous error message when using genconf tool. Contributed by Dinesh Chitlangia.

This commit is contained in:
Márton Elek 2018-09-10 13:16:28 +02:00
parent 0da49642fc
commit 585a4f96d7
2 changed files with 30 additions and 9 deletions

View File

@ -95,8 +95,8 @@ public class TestGenerateOzoneRequiredConfigurations {
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 class TestGenerateOzoneRequiredConfigurations {
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;
}

View File

@ -25,6 +25,7 @@ import javax.xml.bind.JAXBException;
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 final class GenerateOzoneRequiredConfigurations {
}
/**
* 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 final class GenerateOzoneRequiredConfigurations {
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;
}