From 9265d1a61a947b33b40d7c03e69e2274505b5c07 Mon Sep 17 00:00:00 2001 From: Vinayakumar B Date: Tue, 28 Aug 2018 09:21:07 +0530 Subject: [PATCH] HDFS-13858. RBF: Add check to have single valid argument to safemode command. Contributed by Ayush Saxena. (cherry picked from commit 75691ad600473d4d315434b0876d6d10d3050a6b) --- .../hadoop/hdfs/tools/federation/RouterAdmin.java | 6 ++++++ .../federation/router/TestRouterAdminCLI.java | 14 ++++++++++++++ 2 files changed, 20 insertions(+) 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 91e1669ed9d..f88d0a60eb6 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 @@ -218,6 +218,10 @@ 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]; @@ -712,6 +716,8 @@ public class RouterAdmin extends Configured implements Tool { } else if (cmd.equals("get")) { boolean result = getSafeMode(); System.out.println("Safe Mode: " + result); + } else { + throw new IllegalArgumentException("Invalid argument: " + cmd); } } 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 2da5fb95402..2682e9a2d88 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 @@ -519,6 +519,7 @@ public class TestRouterAdminCLI { assertTrue(routerContext.getRouter().getSafemodeService().isInSafeMode()); System.setOut(new PrintStream(out)); + System.setErr(new PrintStream(err)); assertEquals(0, ToolRunner.run(admin, new String[] {"-safemode", "get"})); assertTrue(out.toString().contains("true")); @@ -534,6 +535,19 @@ public class TestRouterAdminCLI { assertEquals(0, ToolRunner.run(admin, new String[] {"-safemode", "get"})); assertTrue(out.toString().contains("false")); + + out.reset(); + assertEquals(-1, ToolRunner.run(admin, + new String[] {"-safemode", "get", "-random", "check" })); + assertTrue(err.toString(), err.toString() + .contains("safemode: Too many arguments, Max=1 argument allowed only")); + err.reset(); + + assertEquals(-1, + ToolRunner.run(admin, new String[] {"-safemode", "check" })); + assertTrue(err.toString(), + err.toString().contains("safemode: Invalid argument: check")); + err.reset(); } @Test