Explorar o código

HADOOP-4708. Add support for dfsadmin commands in TestCLI. Contributed by Boris Shkolnik.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/core/trunk@722584 13f79535-47bb-0310-9956-ffa450edef68
Christopher Douglas %!s(int64=16) %!d(string=hai) anos
pai
achega
2a01f2ac45

+ 3 - 0
CHANGES.txt

@@ -152,6 +152,9 @@ Trunk (unreleased changes)
     HADOOP-3770. Add gridmix2, an iteration on the gridmix benchmark. (Runping
     Qi via cdouglas)
 
+    HADOOP-4708. Add support for dfsadmin commands in TestCLI. (Boris Shkolnik
+    via cdouglas)
+
   OPTIMIZATIONS
 
     HADOOP-3293. Fixes FileInputFormat to do provide locations for splits

+ 39 - 23
src/test/org/apache/hadoop/cli/TestCLI.java

@@ -23,21 +23,25 @@ import java.util.ArrayList;
 
 import javax.xml.parsers.SAXParser;
 import javax.xml.parsers.SAXParserFactory;
+
 import junit.framework.TestCase;
+
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.apache.hadoop.cli.util.ComparatorBase;
-import org.apache.hadoop.cli.util.ComparatorData;
 import org.apache.hadoop.cli.util.CLITestData;
-import org.xml.sax.Attributes;
-import org.xml.sax.SAXException;
-import org.xml.sax.helpers.DefaultHandler;
-
 import org.apache.hadoop.cli.util.CommandExecutor;
+import org.apache.hadoop.cli.util.ComparatorBase;
+import org.apache.hadoop.cli.util.ComparatorData;
+import org.apache.hadoop.cli.util.CLITestData.TestCmd;
+import org.apache.hadoop.cli.util.CLITestData.TestCmd.CommandType;
 import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.hdfs.DistributedFileSystem;
 import org.apache.hadoop.hdfs.MiniDFSCluster;
-import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.util.StringUtils;
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.DefaultHandler;
 
 /**
  * Tests for the Command Line Interface (CLI)
@@ -168,17 +172,17 @@ public class TestCLI extends TestCase {
         LOG.info("           Test Description: [" + td.getTestDesc() + "]");
         LOG.info("");
 
-        ArrayList<String> testCommands = td.getTestCommands();
+        ArrayList<TestCmd> testCommands = td.getTestCommands();
         for (int j = 0; j < testCommands.size(); j++) {
           LOG.info("              Test Commands: [" + 
-              expandCommand((String) testCommands.get(j)) + "]");
+              expandCommand(testCommands.get(j).getCmd()) + "]");
         }
 
         LOG.info("");
-        ArrayList<String> cleanupCommands = td.getCleanupCommands();
+        ArrayList<TestCmd> cleanupCommands = td.getCleanupCommands();
         for (int j = 0; j < cleanupCommands.size(); j++) {
           LOG.info("           Cleanup Commands: [" +
-              expandCommand((String) cleanupCommands.get(j)) + "]");
+              expandCommand(cleanupCommands.get(j).getCmd()) + "]");
         }
 
         LOG.info("");
@@ -314,10 +318,13 @@ public class TestCLI extends TestCase {
       CLITestData testdata = (CLITestData) testsFromConfigFile.get(index);
    
       // Execute the test commands
-      ArrayList<String> testCommands = testdata.getTestCommands();
+      ArrayList<TestCmd> testCommands = testdata.getTestCommands();
       for (int i = 0; i < testCommands.size(); i++) {
-        CommandExecutor.executeFSCommand(testCommands.get(i), 
-        		namenode);
+      try {
+        CommandExecutor.executeCommand(testCommands.get(i), namenode);
+      } catch (Exception e) {
+        fail(StringUtils.stringifyException(e));
+      }
       }
       
       boolean overallTCResult = true;
@@ -341,10 +348,13 @@ public class TestCLI extends TestCase {
       testdata.setTestResult(overallTCResult);
       
       // Execute the cleanup commands
-      ArrayList<String> cleanupCommands = testdata.getCleanupCommands();
+      ArrayList<TestCmd> cleanupCommands = testdata.getCleanupCommands();
       for (int i = 0; i < cleanupCommands.size(); i++) {
-        CommandExecutor.executeFSCommand(cleanupCommands.get(i), 
-        		namenode);
+      try { 
+        CommandExecutor.executeCommand(cleanupCommands.get(i), namenode);
+      } catch (Exception e) {
+        fail(StringUtils.stringifyException(e));
+      }
       }
     }
   }
@@ -355,8 +365,8 @@ public class TestCLI extends TestCase {
   static class TestConfigFileParser extends DefaultHandler {
     String charString = null;
     CLITestData td = null;
-    ArrayList<String> testCommands = null;
-    ArrayList<String> cleanupCommands = null;
+    ArrayList<TestCmd> testCommands = null;
+    ArrayList<TestCmd> cleanupCommands = null;
     
     @Override
     public void startDocument() throws SAXException {
@@ -371,9 +381,9 @@ public class TestCLI extends TestCase {
       if (qName.equals("test")) {
         td = new CLITestData();
       } else if (qName.equals("test-commands")) {
-        testCommands = new ArrayList<String>();
+        testCommands = new ArrayList<TestCmd>();
       } else if (qName.equals("cleanup-commands")) {
-        cleanupCommands = new ArrayList<String>();
+        cleanupCommands = new ArrayList<TestCmd>();
       } else if (qName.equals("comparators")) {
         testComparators = new ArrayList<ComparatorData>();
       } else if (qName.equals("comparator")) {
@@ -396,10 +406,16 @@ public class TestCLI extends TestCase {
         cleanupCommands = null;
       } else if (qName.equals("command")) {
         if (testCommands != null) {
-          testCommands.add(charString);
+          testCommands.add(new TestCmd(charString, CommandType.FS));
         } else if (cleanupCommands != null) {
-          cleanupCommands.add(charString);
+          cleanupCommands.add(new TestCmd(charString, CommandType.FS));
         }
+      } else if (qName.equals("admin-command")) {
+          if (testCommands != null) {
+              testCommands.add(new TestCmd(charString,CommandType.ADMIN));
+            } else if (cleanupCommands != null) {
+              cleanupCommands.add(new TestCmd(charString, CommandType.ADMIN));
+            } 
       } else if (qName.equals("comparators")) {
         td.setComparatorData(testComparators);
       } else if (qName.equals("comparator")) {

+ 34 - 6
src/test/org/apache/hadoop/cli/util/CLITestData.java

@@ -26,8 +26,8 @@ import java.util.ArrayList;
  */
 public class CLITestData {
   private String testDesc = null;
-  private ArrayList<String> testCommands = null;
-  private ArrayList<String> cleanupCommands = null;
+  private ArrayList<TestCmd> testCommands = null;
+  private ArrayList<TestCmd> cleanupCommands = null;
   private ArrayList<ComparatorData> comparatorData = null;
   private boolean testResult = false;
   
@@ -35,6 +35,34 @@ public class CLITestData {
 
   }
 
+  /**
+   * Class to define Test Command. includes type of the command and command itself
+   * Valid types FS and Admin (for dfsadmin commands)
+   *
+   */
+  static public class TestCmd {
+    public enum CommandType {
+        FS,
+        ADMIN
+    }
+    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
    */
@@ -52,14 +80,14 @@ public class CLITestData {
   /**
    * @return the testCommands
    */
-  public ArrayList<String> getTestCommands() {
+  public ArrayList<TestCmd> getTestCommands() {
     return testCommands;
   }
 
   /**
    * @param testCommands the testCommands to set
    */
-  public void setTestCommands(ArrayList<String> testCommands) {
+  public void setTestCommands(ArrayList<TestCmd> testCommands) {
     this.testCommands = testCommands;
   }
 
@@ -94,14 +122,14 @@ public class CLITestData {
   /**
    * @return the cleanupCommands
    */
-  public ArrayList<String> getCleanupCommands() {
+  public ArrayList<TestCmd> getCleanupCommands() {
     return cleanupCommands;
   }
 
   /**
    * @param cleanupCommands the cleanupCommands to set
    */
-  public void setCleanupCommands(ArrayList<String> cleanupCommands) {
+  public void setCleanupCommands(ArrayList<TestCmd> cleanupCommands) {
     this.cleanupCommands = cleanupCommands;
   }
 }

+ 45 - 1
src/test/org/apache/hadoop/cli/util/CommandExecutor.java

@@ -23,9 +23,12 @@ import java.io.File;
 import java.io.PrintStream;
 import java.util.StringTokenizer;
 
+import org.apache.hadoop.cli.TestCLI;
+import org.apache.hadoop.cli.util.CLITestData.TestCmd;
+import org.apache.hadoop.cli.util.CLITestData.TestCmd.CommandType;
 import org.apache.hadoop.fs.FsShell;
+import org.apache.hadoop.hdfs.tools.DFSAdmin;
 import org.apache.hadoop.util.ToolRunner;
-import org.apache.hadoop.cli.TestCLI;
 
 /**
  *
@@ -58,6 +61,47 @@ public class CommandExecutor {
     return args;
   }
   
+  public static int executeCommand(final TestCmd cmd, final String namenode) throws Exception {
+    switch(cmd.getType()) {
+    case ADMIN:
+      return CommandExecutor.executeDFSAdminCommand(cmd.getCmd(),namenode);
+    case FS:
+      return CommandExecutor.executeFSCommand(cmd.getCmd(),namenode);
+    default:
+      throw new Exception("Unknow type of Test command:"+ cmd.getType()); 
+    }
+  }
+  
+  public static int executeDFSAdminCommand(final String cmd, final String namenode) {
+      exitCode = 0;
+      
+      ByteArrayOutputStream bao = new ByteArrayOutputStream();
+      PrintStream origOut = System.out;
+      PrintStream origErr = System.err;
+      
+      System.setOut(new PrintStream(bao));
+      System.setErr(new PrintStream(bao));
+      
+      DFSAdmin shell = new DFSAdmin();
+      String[] args = getFSCommandAsArgs(cmd, namenode);
+      cmdExecuted = cmd;
+     
+      try {
+        ToolRunner.run(shell, args);
+      } catch (Exception e) {
+        e.printStackTrace();
+        lastException = e;
+        exitCode = -1;
+      } finally {
+        System.setOut(origOut);
+        System.setErr(origErr);
+      }
+      
+      commandOutput = bao.toString();
+      
+      return exitCode;
+  }
+  
   public static int executeFSCommand(final String cmd, final String namenode) {
     exitCode = 0;