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 5284b50436
commit 9f77e17540
2 changed files with 30 additions and 9 deletions

View File

@ -95,8 +95,8 @@ public class TestGenerateOzoneRequiredConfigurations {
try (PrintStream ps = new PrintStream(outContent)) { try (PrintStream ps = new PrintStream(outContent)) {
System.setOut(ps); System.setOut(ps);
GenerateOzoneRequiredConfigurations.main(args); GenerateOzoneRequiredConfigurations.main(args);
Assert.assertThat(outContent.toString(), Assert.assertThat(outContent.toString(), CoreMatchers.containsString(
CoreMatchers.containsString("ozone-site.xml has been generated at")); "ozone-site.xml has been generated at"));
System.setOut(oldStream); System.setOut(oldStream);
} }
} }
@ -123,8 +123,29 @@ public class TestGenerateOzoneRequiredConfigurations {
tempPath.setWritable(true); 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 { private File getRandomTempDir() throws IOException {
File tempDir = new File(outputBaseDir, RandomStringUtils.randomAlphanumeric(5)); File tempDir = new File(outputBaseDir,
RandomStringUtils.randomAlphanumeric(5));
FileUtils.forceMkdir(tempDir); FileUtils.forceMkdir(tempDir);
return tempDir; return tempDir;
} }

View File

@ -25,6 +25,7 @@ import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller; import javax.xml.bind.Marshaller;
import java.io.File; import java.io.File;
import java.net.URL; import java.net.URL;
import java.nio.file.Files;
import java.nio.file.InvalidPathException; import java.nio.file.InvalidPathException;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.ArrayList; 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 * @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) { public static boolean isValidPath(String path) {
try { try {
Paths.get(path); return Files.isDirectory(Paths.get(path));
} catch (InvalidPathException | NullPointerException ex) { } catch (InvalidPathException | NullPointerException ex) {
return false; return false;
} }
return true;
} }
/** /**
@ -129,12 +129,12 @@ public final class GenerateOzoneRequiredConfigurations {
public static int generateConfigurations(String path) throws JAXBException { public static int generateConfigurations(String path) throws JAXBException {
if (!isValidPath(path)) { if (!isValidPath(path)) {
System.out.println("Invalid path or insufficient permission"); System.out.println("Invalid directory path.");
return FAILURE; return FAILURE;
} }
if (!canWrite(path)) { if (!canWrite(path)) {
System.out.println("Invalid path or insufficient permission"); System.out.println("Insufficient permission.");
return FAILURE; return FAILURE;
} }