HDFS-11516. Admin command line should print message to stderr in failure case. Contributed by Kai Sasaki.

This commit is contained in:
Andrew Wang 2017-03-17 11:26:02 -07:00
parent 86035c1644
commit 92ea6d74ec
10 changed files with 77 additions and 15 deletions

View File

@ -298,6 +298,11 @@ private boolean compareTestOutput(ComparatorData compdata, Result cmdResult) {
return compareOutput;
}
private boolean compareTextExitCode(ComparatorData compdata,
Result cmdResult) {
return compdata.getExitCode() == cmdResult.getExitCode();
}
/***********************************
************* TESTS RUNNER
@ -330,10 +335,17 @@ public void testAll() {
final String comptype = cd.getComparatorType();
boolean compareOutput = false;
boolean compareExitCode = false;
if (! comptype.equalsIgnoreCase("none")) {
compareOutput = compareTestOutput(cd, cmdResult);
overallTCResult &= compareOutput;
if (cd.getExitCode() == -1) {
// No need to check exit code if not specified
compareExitCode = true;
} else {
compareExitCode = compareTextExitCode(cd, cmdResult);
}
overallTCResult &= (compareOutput & compareExitCode);
}
cd.setExitCode(cmdResult.getExitCode());
@ -391,6 +403,7 @@ public void startElement(String uri,
testComparators = new ArrayList<ComparatorData>();
} else if (qName.equals("comparator")) {
comparatorData = new ComparatorData();
comparatorData.setExitCode(-1);
}
charString = "";
}
@ -422,6 +435,8 @@ public void endElement(String uri, String localName,String qName)
comparatorData.setComparatorType(charString);
} else if (qName.equals("expected-output")) {
comparatorData.setExpectedOutput(charString);
} else if (qName.equals("expected-exit-code")) {
comparatorData.setExitCode(Integer.valueOf(charString));
} else if (qName.equals("test")) {
if (!Shell.WINDOWS || runOnWindows) {
testsFromConfigFile.add(td);

View File

@ -75,7 +75,7 @@ public Result executeCommand(final String cmd) throws Exception {
System.setErr(new PrintStream(bao));
try {
execute(cmd);
exitCode = execute(cmd);
} catch (Exception e) {
e.printStackTrace();
lastException = e;
@ -87,7 +87,7 @@ public Result executeCommand(final String cmd) throws Exception {
return new Result(bao.toString(), exitCode, lastException, cmd);
}
protected abstract void execute(final String cmd) throws Exception;
protected abstract int execute(String cmd) throws Exception;
public static class Result {
final String commandOutput;

View File

@ -30,8 +30,8 @@ public FSCmdExecutor(String namenode, FsShell shell) {
}
@Override
protected void execute(final String cmd) throws Exception{
protected int execute(final String cmd) throws Exception{
String[] args = getCommandAsArgs(cmd, "NAMENODE", this.namenode);
ToolRunner.run(shell, args);
return ToolRunner.run(shell, args);
}
}

View File

@ -176,11 +176,11 @@ public int run(Configuration conf, List<String> args) throws IOException {
for (AdminHelper.Command command : commands) {
System.err.println(command.getLongUsage());
}
return 0;
return 1;
}
if (args.size() != 1) {
System.out.println("You must give exactly one argument to -help.");
return 0;
System.err.println("You must give exactly one argument to -help.");
return 1;
}
final String commandName = args.get(0);
// prepend a dash to match against the command names

View File

@ -233,7 +233,7 @@ public int run(Configuration conf, List<String> args) throws IOException {
final FileEncryptionInfo fei =
admin.getFileEncryptionInfo(p);
if (fei == null) {
System.out.println("No FileEncryptionInfo found for path " + path);
System.err.println("No FileEncryptionInfo found for path " + path);
return 2;
}
System.out.println(fei.toStringStable());

View File

@ -30,8 +30,8 @@ public CacheAdminCmdExecutor(String namenode, CacheAdmin admin) {
}
@Override
protected void execute(final String cmd) throws Exception {
protected int execute(final String cmd) throws Exception {
String[] args = getCommandAsArgs(cmd, "NAMENODE", this.namenode);
ToolRunner.run(admin, args);
return ToolRunner.run(admin, args);
}
}

View File

@ -30,8 +30,8 @@ public CryptoAdminCmdExecutor(String namenode, CryptoAdmin admin) {
}
@Override
protected void execute(final String cmd) throws Exception {
protected int execute(final String cmd) throws Exception {
String[] args = getCommandAsArgs(cmd, "NAMENODE", this.namenode);
ToolRunner.run(admin, args);
return ToolRunner.run(admin, args);
}
}

View File

@ -30,8 +30,8 @@ public ErasureCodingCliCmdExecutor(String namenode, ECAdmin admin) {
}
@Override
protected void execute(final String cmd) throws Exception {
protected int execute(final String cmd) throws Exception {
String[] args = getCommandAsArgs(cmd, "NAMENODE", this.namenode);
ToolRunner.run(admin, args);
return ToolRunner.run(admin, args);
}
}

View File

@ -46,6 +46,37 @@
</comparators>
</test>
<test>
<description>Test help usage</description>
<test-commands>
<crypto-admin-command>-help</crypto-admin-command>
</test-commands>
<cleanup-commands>
</cleanup-commands>
<comparators>
<comparator>
<type>SubstringComparator</type>
<expected-output>[-createZone -keyName</expected-output>
</comparator>
</comparators>
</test>
<test>
<description>Test extra help argument</description>
<test-commands>
<crypto-admin-command>-help arg1 arg2</crypto-admin-command>
</test-commands>
<cleanup-commands>
</cleanup-commands>
<comparators>
<comparator>
<type>SubstringComparator</type>
<expected-output>You must give exactly one argument to -help.</expected-output>
<expected-exit-code>1</expected-exit-code>
</comparator>
</comparators>
</test>
<test>
<description>Test create ez, dir doesn't exist</description>
<test-commands>

View File

@ -71,6 +71,22 @@
</comparators>
</test>
<test>
<description>help: help with extra argument</description>
<test-commands>
<ec-admin-command>-help arg1 arg2</ec-admin-command>
</test-commands>
<cleanup-commands>
</cleanup-commands>
<comparators>
<comparator>
<type>SubstringComparator</type>
<expected-output>You must give exactly one argument to -help.</expected-output>
<expected-exit-code>1</expected-exit-code>
</comparator>
</comparators>
</test>
<test>
<description>help: setPolicy command</description>
<test-commands>