diff --git a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/ozShell/TestOzoneShell.java b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/ozShell/TestOzoneShell.java index 508287082e..a4b30f00c7 100644 --- a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/ozShell/TestOzoneShell.java +++ b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/ozShell/TestOzoneShell.java @@ -38,6 +38,7 @@ import java.util.UUID; import java.util.stream.Collectors; +import com.google.common.base.Strings; import org.apache.commons.lang3.RandomStringUtils; import org.apache.hadoop.fs.FileUtil; import org.apache.hadoop.hdds.client.ReplicationFactor; @@ -203,13 +204,32 @@ public void reset() { public void testCreateVolume() throws Exception { LOG.info("Running testCreateVolume"); String volumeName = "volume" + RandomStringUtils.randomNumeric(5); + testCreateVolume(volumeName, ""); + volumeName = "volume" + RandomStringUtils.randomNumeric(5); + testCreateVolume("/////" + volumeName, ""); + testCreateVolume("/////", "Volume name is required to create a volume"); + testCreateVolume("/////vol/123", + "Illegal argument: Bucket or Volume name has an unsupported character : /"); + } + + private void testCreateVolume(String volumeName, String errorMsg) throws Exception { + err.reset(); String userName = "bilbo"; String[] args = new String[] {"-createVolume", url + "/" + volumeName, "-user", userName, "-root"}; - assertEquals(0, ToolRunner.run(shell, args)); - OzoneVolume volumeInfo = client.getVolumeDetails(volumeName); - assertEquals(volumeName, volumeInfo.getName()); + if (Strings.isNullOrEmpty(errorMsg)) { + assertEquals(0, ToolRunner.run(shell, args)); + } else { + assertEquals(1, ToolRunner.run(shell, args)); + assertTrue(err.toString().contains(errorMsg)); + return; + } + + String truncatedVolumeName = + volumeName.substring(volumeName.lastIndexOf('/') + 1); + OzoneVolume volumeInfo = client.getVolumeDetails(truncatedVolumeName); + assertEquals(truncatedVolumeName, volumeInfo.getName()); assertEquals(userName, volumeInfo.getOwner()); } diff --git a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/web/ozShell/volume/CreateVolumeHandler.java b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/web/ozShell/volume/CreateVolumeHandler.java index 74fdbb0a5d..0057282631 100644 --- a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/web/ozShell/volume/CreateVolumeHandler.java +++ b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/web/ozShell/volume/CreateVolumeHandler.java @@ -60,15 +60,15 @@ protected void execute(CommandLine cmd) String ozoneURIString = cmd.getOptionValue(Shell.CREATE_VOLUME); URI ozoneURI = verifyURI(ozoneURIString); - if (ozoneURI.getPath().isEmpty()) { + + // we need to skip the slash in the URI path + // getPath returns /volumeName needs to remove the initial slash. + volumeName = ozoneURI.getPath().replaceAll("^/+", ""); + if (volumeName.isEmpty()) { throw new OzoneClientException( "Volume name is required to create a volume"); } - // we need to skip the slash in the URI path - // getPath returns /volumeName needs to remove the first slash. - volumeName = ozoneURI.getPath().substring(1); - if (cmd.hasOption(Shell.VERBOSE)) { System.out.printf("Volume name : %s%n", volumeName); }