MAPREDUCE-2202. Generalize CLITest structure and interfaces to facilitate upstream adoption (e.g. for web or system testing). Contributed by Konstantin Boudnik


git-svn-id: https://svn.apache.org/repos/asf/hadoop/mapreduce/trunk@1094093 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/CHANGES.txt b/CHANGES.txt
index 8c3919e..39e8af9 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -62,6 +62,9 @@
 
     MAPREDUCE-2367. Allow using a file to exclude certain tests from build.
     (todd)
+
+    MAPREDUCE-2202. Generalize CLITest structure and interfaces to faciliate
+    upstream adoption (e.g. for web or system testing). (cos)
     
   OPTIMIZATIONS
     
diff --git a/src/test/mapred/org/apache/hadoop/cli/CLITestCmdMR.java b/src/test/mapred/org/apache/hadoop/cli/CLITestCmdMR.java
new file mode 100644
index 0000000..f9bc943
--- /dev/null
+++ b/src/test/mapred/org/apache/hadoop/cli/CLITestCmdMR.java
@@ -0,0 +1,41 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.cli;
+
+import org.apache.hadoop.cli.util.CLICommandTypes;
+import org.apache.hadoop.cli.util.CLITestCmd;
+import org.apache.hadoop.cli.util.CommandExecutor;
+
+public class CLITestCmdMR extends CLITestCmd {
+  public CLITestCmdMR(String str, CLICommandTypes type) {
+    super(str, type);
+  }
+
+  /**
+   * This is not implemented because HadoopArchive constructor requires JobConf
+   * to create an archive object. Because TestMRCLI uses setup method from
+   * TestHDFSCLI the initialization of executor objects happens before a config
+   * is created and updated. Thus, actual calls to executors happen in the body
+   * of the test method.
+   */
+  @Override
+  public CommandExecutor getExecutor(String tag)
+      throws IllegalArgumentException {
+    throw new IllegalArgumentException("Method isn't supported");
+  }
+}
diff --git a/src/test/mapred/org/apache/hadoop/cli/TestMRCLI.java b/src/test/mapred/org/apache/hadoop/cli/TestMRCLI.java
index aaa7738..3c2b2b9 100644
--- a/src/test/mapred/org/apache/hadoop/cli/TestMRCLI.java
+++ b/src/test/mapred/org/apache/hadoop/cli/TestMRCLI.java
@@ -18,8 +18,7 @@
 
 package org.apache.hadoop.cli;
 
-import org.apache.hadoop.cli.util.CommandExecutor;
-import org.apache.hadoop.cli.util.CLITestData.TestCmd;
+import org.apache.hadoop.cli.util.*;
 import org.apache.hadoop.cli.util.CommandExecutor.Result;
 import org.apache.hadoop.tools.HadoopArchives;
 import org.apache.hadoop.mapred.JobConf;
@@ -29,51 +28,59 @@
 import org.apache.hadoop.security.authorize.HadoopPolicyProvider;
 import org.apache.hadoop.security.authorize.PolicyProvider;
 import org.apache.hadoop.util.ToolRunner;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.xml.sax.SAXException;
 
-public class TestMRCLI extends TestHDFSCLI{
+public class TestMRCLI extends TestHDFSCLI {
 
   protected MiniMRCluster mrCluster = null;
   protected String jobtracker = null;
-  protected MRCmdExecutor cmdExecutor = null;
-  protected ArchiveCmdExecutor archiveCmdExecutor = null;
-  
+  private JobConf mrConf;
+
+  @Before
   public void setUp() throws Exception {
     super.setUp();
     conf.setClass(PolicyProvider.POLICY_PROVIDER_CONFIG,
         HadoopPolicyProvider.class, PolicyProvider.class);
-    JobConf mrConf = new JobConf(conf);
+    mrConf = new JobConf(conf);
     mrCluster = new MiniMRCluster(1, dfsCluster.getFileSystem().getUri().toString(), 1, 
                            null, null, mrConf);
     jobtracker = mrCluster.createJobConf().get(JTConfig.JT_IPC_ADDRESS, "local");
-    cmdExecutor = new MRCmdExecutor(jobtracker);
-    archiveCmdExecutor = new ArchiveCmdExecutor(namenode, mrConf);
   }
 
-  
+  @After
   public void tearDown() throws Exception {
     mrCluster.shutdown();
     super.tearDown();
   }
-  
-  protected String getTestFile() {
+
+    @Override
+    protected TestConfigFileParser getConfigParser() {
+        return new TestConfigFileParserMR();
+    }
+
+    protected String getTestFile() {
     return "testMRConf.xml";
   }
-  
+
+  @Override
   protected String expandCommand(final String cmd) {
     String expCmd = cmd;
     expCmd = expCmd.replaceAll("JOBTRACKER", jobtracker);
-    expCmd = super.expandCommand(cmd);
+    expCmd = super.expandCommand(expCmd);
     return expCmd;
   }
-  
-  protected Result execute(TestCmd cmd) throws Exception {
-    if(cmd.getType() == TestCmd.CommandType.MRADMIN) {
-      return cmdExecutor.executeCommand(cmd.getCmd());
-    } else if(cmd.getType() == TestCmd.CommandType.ARCHIVE) {
-      return archiveCmdExecutor.executeCommand(cmd.getCmd());
-    } else {
+
+  @Override
+  protected Result execute(CLICommand cmd) throws Exception {
+    if (cmd.getType() instanceof CLICommandMRAdmin)
+      return new TestMRCLI.MRCmdExecutor(jobtracker).executeCommand(cmd.getCmd());
+    else if (cmd.getType() instanceof CLICommandArchive)
+      return new TestMRCLI.ArchiveCmdExecutor(namenode, mrConf).executeCommand(cmd.getCmd());
+    else
       return super.execute(cmd);
-    }
   }
   
   public static class MRCmdExecutor extends CommandExecutor {
@@ -81,11 +88,13 @@
     public MRCmdExecutor(String jobtracker) {
       this.jobtracker = jobtracker;
     }
+    @Override
     protected void execute(final String cmd) throws Exception{
       MRAdmin mradmin = new MRAdmin();
       String[] args = getCommandAsArgs(cmd, "JOBTRACKER", jobtracker);
       ToolRunner.run(mradmin, args);
     }
+
   }
   
   public static class ArchiveCmdExecutor extends CommandExecutor {
@@ -95,11 +104,43 @@
       this.namenode = namenode;
       this.jobConf = jobConf;
     }
+    @Override
     protected void execute(final String cmd) throws Exception {
-//      JobConf job=new JobConf(conf);
       HadoopArchives archive = new HadoopArchives(jobConf);
       String[] args = getCommandAsArgs(cmd, "NAMENODE", namenode);
       ToolRunner.run(archive, args);
     }
   }
+
+  @Test
+  @Override
+  public void testAll () {
+    super.testAll();
+  }
+
+  class TestConfigFileParserMR extends CLITestHelper.TestConfigFileParser {
+    @Override
+    public void endElement(String uri, String localName, String qName)
+        throws SAXException {
+      if (qName.equals("mr-admin-command")) {
+        if (testCommands != null) {
+          testCommands.add(new CLITestCmdMR(charString,
+              new CLICommandMRAdmin()));
+        } else if (cleanupCommands != null) {
+          cleanupCommands.add(new CLITestCmdMR(charString,
+              new CLICommandMRAdmin()));
+        }
+      } else if (qName.equals("archive-command")) {
+        if (testCommands != null) {
+          testCommands.add(new CLITestCmdMR(charString,
+              new CLICommandArchive()));
+        } else if (cleanupCommands != null) {
+          cleanupCommands.add(new CLITestCmdMR(charString,
+              new CLICommandArchive()));
+        }
+      } else {
+        super.endElement(uri, localName, qName);
+      }
+    }
+  }
 }
diff --git a/src/test/mapred/org/apache/hadoop/cli/util/CLICommandArchive.java b/src/test/mapred/org/apache/hadoop/cli/util/CLICommandArchive.java
new file mode 100644
index 0000000..45104cb
--- /dev/null
+++ b/src/test/mapred/org/apache/hadoop/cli/util/CLICommandArchive.java
@@ -0,0 +1,21 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.cli.util;
+
+public class CLICommandArchive implements CLICommandTypes {
+}
diff --git a/src/test/mapred/org/apache/hadoop/cli/util/CLICommandMRAdmin.java b/src/test/mapred/org/apache/hadoop/cli/util/CLICommandMRAdmin.java
new file mode 100644
index 0000000..a737e33
--- /dev/null
+++ b/src/test/mapred/org/apache/hadoop/cli/util/CLICommandMRAdmin.java
@@ -0,0 +1,21 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.cli.util;
+
+public class CLICommandMRAdmin implements CLICommandTypes {
+}