diff --git a/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/tools/federation/RouterAdmin.java b/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/tools/federation/RouterAdmin.java index 46be3732c26..ef8d7c15162 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/tools/federation/RouterAdmin.java +++ b/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/tools/federation/RouterAdmin.java @@ -146,6 +146,43 @@ public class RouterAdmin extends Configured implements Tool { return getUsage(null); } + /** + * Usage: validates the maximum number of arguments for a command. + * @param arg List of of command line parameters. + */ + private void validateMax(String[] arg) { + if (arg[0].equals("-rm")) { + if (arg.length > 2) { + throw new IllegalArgumentException( + "Too many arguments, Max=1 argument allowed"); + } + } else if (arg[0].equals("-ls")) { + if (arg.length > 2) { + throw new IllegalArgumentException( + "Too many arguments, Max=1 argument allowed"); + } + } else if (arg[0].equals("-clrQuota")) { + if (arg.length > 2) { + throw new IllegalArgumentException( + "Too many arguments, Max=1 argument allowed"); + } + } else if (arg[0].equals("-safemode")) { + if (arg.length > 2) { + throw new IllegalArgumentException( + "Too many arguments, Max=1 argument allowed only"); + } + } else if (arg[0].equals("-nameservice")) { + if (arg.length > 3) { + throw new IllegalArgumentException( + "Too many arguments, Max=2 arguments allowed"); + } + } else if (arg[0].equals("-getDisabledNameservices")) { + if (arg.length > 1) { + throw new IllegalArgumentException("No arguments allowed"); + } + } + } + @Override public int run(String[] argv) throws Exception { if (argv.length < 1) { @@ -222,6 +259,7 @@ public class RouterAdmin extends Configured implements Tool { Exception debugException = null; exitCode = 0; try { + validateMax(argv); if ("-add".equals(cmd)) { if (addMount(argv, i)) { System.out.println("Successfully added mount point " + argv[i]); @@ -251,10 +289,6 @@ public class RouterAdmin extends Configured implements Tool { "Successfully clear quota for mount point " + argv[i]); } } else if ("-safemode".equals(cmd)) { - if (argv.length > 2) { - throw new IllegalArgumentException( - "Too many arguments, Max=1 argument allowed only"); - } manageSafeMode(argv[i]); } else if ("-nameservice".equals(cmd)) { String subcmd = argv[i]; @@ -641,6 +675,9 @@ public class RouterAdmin extends Configured implements Tool { throw new IllegalArgumentException( "Cannot parse ssQuota: " + parameters[i]); } + } else { + throw new IllegalArgumentException( + "Invalid argument : " + parameters[i]); } i++; diff --git a/hadoop-hdfs-project/hadoop-hdfs-rbf/src/test/java/org/apache/hadoop/hdfs/server/federation/router/TestRouterAdminCLI.java b/hadoop-hdfs-project/hadoop-hdfs-rbf/src/test/java/org/apache/hadoop/hdfs/server/federation/router/TestRouterAdminCLI.java index 0c7321f7cc9..fa29cd9d849 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-rbf/src/test/java/org/apache/hadoop/hdfs/server/federation/router/TestRouterAdminCLI.java +++ b/hadoop-hdfs-project/hadoop-hdfs-rbf/src/test/java/org/apache/hadoop/hdfs/server/federation/router/TestRouterAdminCLI.java @@ -267,6 +267,13 @@ public class TestRouterAdminCLI { assertEquals(0, ToolRunner.run(admin, argv)); assertTrue(out.toString().contains(src)); + // Test with wrong number of arguments + argv = new String[] {"-ls", srcWithSlash, "check", "check2"}; + System.setErr(new PrintStream(err)); + ToolRunner.run(admin, argv); + assertTrue( + err.toString().contains("Too many arguments, Max=1 argument allowed")); + out.reset(); GetMountTableEntriesRequest getRequest = GetMountTableEntriesRequest .newInstance("/"); @@ -317,6 +324,13 @@ public class TestRouterAdminCLI { assertEquals(0, ToolRunner.run(admin, argv)); assertTrue(out.toString().contains( "Cannot remove mount point " + invalidPath)); + + // test wrong number of arguments + System.setErr(new PrintStream(err)); + argv = new String[] {"-rm", src, "check" }; + ToolRunner.run(admin, argv); + assertTrue(err.toString() + .contains("Too many arguments, Max=1 argument allowed")); } @Test @@ -572,6 +586,18 @@ public class TestRouterAdminCLI { // verify if quota unset successfully assertEquals(HdfsConstants.QUOTA_RESET, quotaUsage.getQuota()); assertEquals(HdfsConstants.QUOTA_RESET, quotaUsage.getSpaceQuota()); + + // verify wrong arguments + System.setErr(new PrintStream(err)); + argv = new String[] {"-clrQuota", src, "check"}; + ToolRunner.run(admin, argv); + assertTrue(err.toString(), + err.toString().contains("Too many arguments, Max=1 argument allowed")); + + argv = new String[] {"-setQuota", src, "check", "check2"}; + err.reset(); + ToolRunner.run(admin, argv); + assertTrue(err.toString().contains("Invalid argument : check")); } @Test @@ -686,6 +712,15 @@ public class TestRouterAdminCLI { new String[] {"-nameservice", "wrong", "ns0"})); assertTrue("Got error: " + err.toString(), err.toString().startsWith("nameservice: Unknown command: wrong")); + + err.reset(); + ToolRunner.run(admin, + new String[] {"-nameservice", "enable", "ns0", "check"}); + assertTrue( + err.toString().contains("Too many arguments, Max=2 arguments allowed")); + err.reset(); + ToolRunner.run(admin, new String[] {"-getDisabledNameservices", "check"}); + assertTrue(err.toString().contains("No arguments allowed")); } /**