HADOOP-7014. Generalize CLITest structure and interfaces to facilitate

upstream adoption (e.g. for web testing). Contributed by Konstantin Boudnik


git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1092847 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Konstantin Boudnik 2011-04-16 00:23:46 +00:00
parent 625b099235
commit 58ca2272bc
4 changed files with 53 additions and 88 deletions

View File

@ -90,6 +90,9 @@ Trunk (unreleased changes)
HADOOP-7224. Add CommandFactory to shell. (Daryn Sharp via szetszwo)
HADOOP-7014. Generalize CLITest structure and interfaces to facilitate
upstream adoption (e.g. for web testing). (cos)
OPTIMIZATIONS
BUG FIXES

View File

@ -20,16 +20,12 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.cli.util.CLITestData;
import org.apache.hadoop.cli.util.CLITestData.TestCmd;
import org.apache.hadoop.cli.util.CLITestData.TestCmd.CommandType;
import org.apache.hadoop.cli.util.CommandExecutor;
import org.apache.hadoop.cli.util.*;
import org.apache.hadoop.cli.util.CLITestCmd;
import org.apache.hadoop.cli.util.CLICommand;
import org.apache.hadoop.cli.util.CommandExecutor.Result;
import org.apache.hadoop.cli.util.ComparatorBase;
import org.apache.hadoop.cli.util.ComparatorData;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.CommonConfigurationKeys;
import org.apache.hadoop.security.authorize.ServiceAuthorizationManager;
import org.apache.hadoop.util.StringUtils;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@ -72,7 +68,6 @@ public class CLITestHelper {
protected Configuration conf = null;
protected String clitestDataDir = null;
protected String username = null;
/**
* Read the test config file - testConfig.xml
*/
@ -83,7 +78,7 @@ protected void readTestConfigFile() {
testConfigFile = TEST_CACHE_DATA_DIR + File.separator + testConfigFile;
try {
SAXParser p = (SAXParserFactory.newInstance()).newSAXParser();
p.parse(testConfigFile, new TestConfigFileParser());
p.parse(testConfigFile, getConfigParser());
success = true;
} catch (Exception e) {
LOG.info("File: " + testConfigFile + " not found");
@ -92,7 +87,19 @@ protected void readTestConfigFile() {
assertTrue("Error reading test config file", success);
}
}
/**
* Method decides what is a proper configuration file parser for this type
* of CLI tests.
* Ancestors need to override the implementation if a parser with additional
* features is needed. Also, such ancestor has to provide its own
* TestConfigParser implementation
* @return an instance of TestConfigFileParser class
*/
protected TestConfigFileParser getConfigParser () {
return new TestConfigFileParser();
}
protected String getTestFile() {
return "";
}
@ -151,15 +158,15 @@ private void displayResults() {
LOG.info(" Test Description: [" + td.getTestDesc() + "]");
LOG.info("");
ArrayList<TestCmd> testCommands = td.getTestCommands();
for (TestCmd cmd : testCommands) {
ArrayList<CLICommand> testCommands = td.getTestCommands();
for (CLICommand cmd : testCommands) {
LOG.info(" Test Commands: [" +
expandCommand(cmd.getCmd()) + "]");
}
LOG.info("");
ArrayList<TestCmd> cleanupCommands = td.getCleanupCommands();
for (TestCmd cmd : cleanupCommands) {
ArrayList<CLICommand> cleanupCommands = td.getCleanupCommands();
for (CLICommand cmd : cleanupCommands) {
LOG.info(" Cleanup Commands: [" +
expandCommand(cmd.getCmd()) + "]");
}
@ -303,12 +310,12 @@ public void testAll() {
// Run the tests defined in the testConf.xml config file.
for (int index = 0; index < testsFromConfigFile.size(); index++) {
CLITestData testdata = (CLITestData) testsFromConfigFile.get(index);
CLITestData testdata = testsFromConfigFile.get(index);
// Execute the test commands
ArrayList<TestCmd> testCommands = testdata.getTestCommands();
ArrayList<CLICommand> testCommands = testdata.getTestCommands();
Result cmdResult = null;
for (TestCmd cmd : testCommands) {
for (CLICommand cmd : testCommands) {
try {
cmdResult = execute(cmd);
} catch (Exception e) {
@ -336,8 +343,8 @@ public void testAll() {
testdata.setTestResult(overallTCResult);
// Execute the cleanup commands
ArrayList<TestCmd> cleanupCommands = testdata.getCleanupCommands();
for (TestCmd cmd : cleanupCommands) {
ArrayList<CLICommand> cleanupCommands = testdata.getCleanupCommands();
for (CLICommand cmd : cleanupCommands) {
try {
execute(cmd);
} catch (Exception e) {
@ -346,9 +353,12 @@ public void testAll() {
}
}
}
protected CommandExecutor.Result execute(TestCmd cmd) throws Exception {
throw new Exception("Unknow type of Test command:"+ cmd.getType());
/**
* this method has to be overridden by an ancestor
*/
protected CommandExecutor.Result execute(CLICommand cmd) throws Exception {
throw new Exception("Unknown type of test command:"+ cmd.getType());
}
/*
@ -357,8 +367,8 @@ protected CommandExecutor.Result execute(TestCmd cmd) throws Exception {
class TestConfigFileParser extends DefaultHandler {
String charString = null;
CLITestData td = null;
ArrayList<TestCmd> testCommands = null;
ArrayList<TestCmd> cleanupCommands = null;
ArrayList<CLICommand> testCommands = null;
ArrayList<CLICommand> cleanupCommands = null;
@Override
public void startDocument() throws SAXException {
@ -373,9 +383,9 @@ public void startElement(String uri,
if (qName.equals("test")) {
td = new CLITestData();
} else if (qName.equals("test-commands")) {
testCommands = new ArrayList<TestCmd>();
testCommands = new ArrayList<CLICommand>();
} else if (qName.equals("cleanup-commands")) {
cleanupCommands = new ArrayList<TestCmd>();
cleanupCommands = new ArrayList<CLICommand>();
} else if (qName.equals("comparators")) {
testComparators = new ArrayList<ComparatorData>();
} else if (qName.equals("comparator")) {
@ -385,9 +395,8 @@ public void startElement(String uri,
}
@Override
public void endElement(String uri,
String localName,
String qName) throws SAXException {
public void endElement(String uri, String localName,String qName)
throws SAXException {
if (qName.equals("description")) {
td.setTestDesc(charString);
} else if (qName.equals("test-commands")) {
@ -398,27 +407,9 @@ public void endElement(String uri,
cleanupCommands = null;
} else if (qName.equals("command")) {
if (testCommands != null) {
testCommands.add(new TestCmd(charString, CommandType.FS));
testCommands.add(new CLITestCmd(charString, new CLICommandFS()));
} else if (cleanupCommands != null) {
cleanupCommands.add(new TestCmd(charString, CommandType.FS));
}
} else if (qName.equals("dfs-admin-command")) {
if (testCommands != null) {
testCommands.add(new TestCmd(charString, CommandType.DFSADMIN));
} else if (cleanupCommands != null) {
cleanupCommands.add(new TestCmd(charString, CommandType.DFSADMIN));
}
} else if (qName.equals("mr-admin-command")) {
if (testCommands != null) {
testCommands.add(new TestCmd(charString, CommandType.MRADMIN));
} else if (cleanupCommands != null) {
cleanupCommands.add(new TestCmd(charString, CommandType.MRADMIN));
}
} else if (qName.equals("archive-command")) {
if (testCommands != null) {
testCommands.add(new TestCmd(charString, CommandType.ARCHIVE));
} else if (cleanupCommands != null) {
cleanupCommands.add(new TestCmd(charString, CommandType.ARCHIVE));
cleanupCommands.add(new CLITestCmd(charString, new CLICommandFS()));
}
} else if (qName.equals("comparators")) {
td.setComparatorData(testComparators);

View File

@ -18,8 +18,7 @@
package org.apache.hadoop.cli;
import org.apache.hadoop.cli.util.CLITestData;
import org.apache.hadoop.cli.util.CmdFactory;
import org.apache.hadoop.cli.util.CLICommand;
import org.apache.hadoop.cli.util.CommandExecutor;
import org.junit.After;
import org.junit.Before;
@ -42,8 +41,9 @@ public void tearDown() throws Exception {
}
@Override
protected CommandExecutor.Result execute(CLITestData.TestCmd cmd) throws Exception {
return CmdFactory.getCommandExecutor(cmd, "").executeCommand(cmd.getCmd());
protected CommandExecutor.Result execute(CLICommand cmd) throws Exception {
return cmd.getExecutor("").executeCommand(cmd.getCmd());
}
@Override

View File

@ -26,8 +26,8 @@
*/
public class CLITestData {
private String testDesc = null;
private ArrayList<TestCmd> testCommands = null;
private ArrayList<TestCmd> cleanupCommands = null;
private ArrayList<CLICommand> testCommands = null;
private ArrayList<CLICommand> cleanupCommands = null;
private ArrayList<ComparatorData> comparatorData = null;
private boolean testResult = false;
@ -35,35 +35,6 @@ public CLITestData() {
}
/**
* Class to define Test Command. includes type of the command and command itself
* Valid types FS, DFSADMIN, MRADMIN and ARCHIVE.
*/
static public class TestCmd {
public enum CommandType {
FS,
DFSADMIN,
MRADMIN,
ARCHIVE
}
private final CommandType type;
private final String cmd;
public TestCmd(String str, CommandType type) {
cmd = str;
this.type = type;
}
public CommandType getType() {
return type;
}
public String getCmd() {
return cmd;
}
public String toString() {
return cmd;
}
}
/**
* @return the testDesc
*/
@ -81,14 +52,14 @@ public void setTestDesc(String testDesc) {
/**
* @return the testCommands
*/
public ArrayList<TestCmd> getTestCommands() {
public ArrayList<CLICommand> getTestCommands() {
return testCommands;
}
/**
* @param testCommands the testCommands to set
*/
public void setTestCommands(ArrayList<TestCmd> testCommands) {
public void setTestCommands(ArrayList<CLICommand> testCommands) {
this.testCommands = testCommands;
}
@ -123,14 +94,14 @@ public void setTestResult(boolean testResult) {
/**
* @return the cleanupCommands
*/
public ArrayList<TestCmd> getCleanupCommands() {
public ArrayList<CLICommand> getCleanupCommands() {
return cleanupCommands;
}
/**
* @param cleanupCommands the cleanupCommands to set
*/
public void setCleanupCommands(ArrayList<TestCmd> cleanupCommands) {
public void setCleanupCommands(ArrayList<CLICommand> cleanupCommands) {
this.cleanupCommands = cleanupCommands;
}
}