HADOOP-10927. Fix CredentialShell help behavior and error codes. Contributed by Josh Elser.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1615827 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andrew Wang 2014-08-05 00:20:30 +00:00
parent d7b5709bcb
commit c8abf5f20a
4 changed files with 46 additions and 16 deletions

View File

@ -519,6 +519,9 @@ Release 2.6.0 - UNRELEASED
HADOOP-10928. Incorrect usage on `hadoop credential list`. HADOOP-10928. Incorrect usage on `hadoop credential list`.
(Josh Elser via wang) (Josh Elser via wang)
HADOOP-10927. Fix CredentialShell help behavior and error codes.
(Josh Elser via wang)
Release 2.5.0 - UNRELEASED Release 2.5.0 - UNRELEASED
INCOMPATIBLE CHANGES INCOMPATIBLE CHANGES

View File

@ -35,6 +35,7 @@ function print_usage(){
echo " distcp <srcurl> <desturl> copy file or directories recursively" echo " distcp <srcurl> <desturl> copy file or directories recursively"
echo " archive -archiveName NAME -p <parent path> <src>* <dest> create a hadoop archive" echo " archive -archiveName NAME -p <parent path> <src>* <dest> create a hadoop archive"
echo " classpath prints the class path needed to get the" echo " classpath prints the class path needed to get the"
echo " credential interact with credential providers"
echo " Hadoop jar and the required libraries" echo " Hadoop jar and the required libraries"
echo " daemonlog get/set the log level for each daemon" echo " daemonlog get/set the log level for each daemon"
echo " or" echo " or"

View File

@ -67,11 +67,11 @@ public class CredentialShell extends Configured implements Tool {
if (command.validate()) { if (command.validate()) {
command.execute(); command.execute();
} else { } else {
exitCode = -1; exitCode = 1;
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(err); e.printStackTrace(err);
return -1; return 1;
} }
return exitCode; return exitCode;
} }
@ -79,29 +79,36 @@ public class CredentialShell extends Configured implements Tool {
/** /**
* Parse the command line arguments and initialize the data * Parse the command line arguments and initialize the data
* <pre> * <pre>
* % hadoop alias create alias [-provider providerPath] * % hadoop credential create alias [-provider providerPath]
* % hadoop alias list [-provider providerPath] * % hadoop credential list [-provider providerPath]
* % hadoop alias delete alias [-provider providerPath] [-i] * % hadoop credential delete alias [-provider providerPath] [-i]
* </pre> * </pre>
* @param args * @param args
* @return * @return 0 if the argument(s) were recognized, 1 otherwise
* @throws IOException * @throws IOException
*/ */
private int init(String[] args) throws IOException { protected int init(String[] args) throws IOException {
// no args should print the help message
if (0 == args.length) {
printCredShellUsage();
ToolRunner.printGenericCommandUsage(System.err);
return 1;
}
for (int i = 0; i < args.length; i++) { // parse command line for (int i = 0; i < args.length; i++) { // parse command line
if (args[i].equals("create")) { if (args[i].equals("create")) {
String alias = args[++i]; String alias = args[++i];
command = new CreateCommand(alias); command = new CreateCommand(alias);
if (alias.equals("-help")) { if (alias.equals("-help")) {
printCredShellUsage(); printCredShellUsage();
return -1; return 0;
} }
} else if (args[i].equals("delete")) { } else if (args[i].equals("delete")) {
String alias = args[++i]; String alias = args[++i];
command = new DeleteCommand(alias); command = new DeleteCommand(alias);
if (alias.equals("-help")) { if (alias.equals("-help")) {
printCredShellUsage(); printCredShellUsage();
return -1; return 0;
} }
} else if (args[i].equals("list")) { } else if (args[i].equals("list")) {
command = new ListCommand(); command = new ListCommand();
@ -115,11 +122,11 @@ public class CredentialShell extends Configured implements Tool {
value = args[++i]; value = args[++i];
} else if (args[i].equals("-help")) { } else if (args[i].equals("-help")) {
printCredShellUsage(); printCredShellUsage();
return -1; return 0;
} else { } else {
printCredShellUsage(); printCredShellUsage();
ToolRunner.printGenericCommandUsage(System.err); ToolRunner.printGenericCommandUsage(System.err);
return -1; return 1;
} }
} }
return 0; return 0;

View File

@ -17,16 +17,18 @@
*/ */
package org.apache.hadoop.security.alias; package org.apache.hadoop.security.alias;
import static org.junit.Assert.*; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.PrintStream; import java.io.PrintStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.alias.CredentialShell.PasswordReader;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -87,7 +89,7 @@ public class TestCredShell {
CredentialShell cs = new CredentialShell(); CredentialShell cs = new CredentialShell();
cs.setConf(new Configuration()); cs.setConf(new Configuration());
rc = cs.run(args1); rc = cs.run(args1);
assertEquals(-1, rc); assertEquals(1, rc);
assertTrue(outContent.toString().contains("There are no valid " + assertTrue(outContent.toString().contains("There are no valid " +
"CredentialProviders configured.")); "CredentialProviders configured."));
} }
@ -122,7 +124,7 @@ public class TestCredShell {
config.set(CredentialProviderFactory.CREDENTIAL_PROVIDER_PATH, "user:///"); config.set(CredentialProviderFactory.CREDENTIAL_PROVIDER_PATH, "user:///");
cs.setConf(config); cs.setConf(config);
rc = cs.run(args1); rc = cs.run(args1);
assertEquals(-1, rc); assertEquals(1, rc);
assertTrue(outContent.toString().contains("There are no valid " + assertTrue(outContent.toString().contains("There are no valid " +
"CredentialProviders configured.")); "CredentialProviders configured."));
} }
@ -139,7 +141,7 @@ public class TestCredShell {
shell.setConf(new Configuration()); shell.setConf(new Configuration());
shell.setPasswordReader(new MockPasswordReader(passwords)); shell.setPasswordReader(new MockPasswordReader(passwords));
rc = shell.run(args1); rc = shell.run(args1);
assertEquals(outContent.toString(), -1, rc); assertEquals(outContent.toString(), 1, rc);
assertTrue(outContent.toString().contains("Passwords don't match")); assertTrue(outContent.toString().contains("Passwords don't match"));
} }
@ -186,4 +188,21 @@ public class TestCredShell {
System.out.println(message); System.out.println(message);
} }
} }
@Test
public void testEmptyArgList() throws Exception {
CredentialShell shell = new CredentialShell();
shell.setConf(new Configuration());
assertEquals(1, shell.init(new String[0]));
}
@Test
public void testCommandHelpExitsNormally() throws Exception {
for (String cmd : Arrays.asList("create", "list", "delete")) {
CredentialShell shell = new CredentialShell();
shell.setConf(new Configuration());
assertEquals("Expected help argument on " + cmd + " to return 0",
0, shell.init(new String[] {cmd, "-help"}));
}
}
} }