View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.mapreduce;
20  
21  import static org.junit.Assert.assertTrue;
22  import static org.junit.Assume.assumeTrue;
23  
24  import java.io.IOException;
25  
26  import org.apache.hadoop.conf.Configurable;
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.hbase.HBaseConfiguration;
29  import org.apache.hadoop.hbase.IntegrationTestingUtility;
30  import org.apache.hadoop.hbase.IntegrationTests;
31  import org.apache.hadoop.mapreduce.Job;
32  import org.apache.hadoop.util.Tool;
33  import org.apache.hadoop.util.ToolRunner;
34  import org.junit.Before;
35  import org.junit.BeforeClass;
36  import org.junit.Test;
37  import org.junit.experimental.categories.Category;
38  
39  /**
40   * Test that we add tmpjars correctly including the named dependencies. Runs
41   * as an integration test so that classpath is realistic.
42   */
43  @Category(IntegrationTests.class)
44  public class IntegrationTestTableMapReduceUtil implements Configurable, Tool {
45  
46    private static IntegrationTestingUtility util;
47  
48    @BeforeClass
49    public static void provisionCluster() throws Exception {
50      if (null == util) {
51        util = new IntegrationTestingUtility();
52      }
53    }
54  
55    @Before
56    public void skipMiniCluster() {
57      // test probably also works with a local cluster, but
58      // IntegrationTestingUtility doesn't support this concept.
59      assumeTrue("test requires a distributed cluster.", util.isDistributedCluster());
60    }
61  
62    /**
63     * Look for jars we expect to be on the classpath by name.
64     */
65    @Test
66    public void testAddDependencyJars() throws Exception {
67      Job job = new Job();
68      TableMapReduceUtil.addDependencyJars(job);
69      String tmpjars = job.getConfiguration().get("tmpjars");
70  
71      // verify presence of modules
72      assertTrue(tmpjars.contains("hbase-common"));
73      assertTrue(tmpjars.contains("hbase-protocol"));
74      assertTrue(tmpjars.contains("hbase-client"));
75      assertTrue(tmpjars.contains("hbase-hadoop-compat"));
76      assertTrue(tmpjars.contains("hbase-server"));
77  
78      // verify presence of 3rd party dependencies.
79      assertTrue(tmpjars.contains("zookeeper"));
80      assertTrue(tmpjars.contains("netty"));
81      assertTrue(tmpjars.contains("protobuf"));
82      assertTrue(tmpjars.contains("guava"));
83      assertTrue(tmpjars.contains("htrace"));
84    }
85  
86    @Override
87    public int run(String[] args) throws Exception {
88      provisionCluster();
89      skipMiniCluster();
90      testAddDependencyJars();
91      return 0;
92    }
93  
94    public void setConf(Configuration conf) {
95      if (util != null) {
96        throw new IllegalArgumentException(
97            "setConf not supported after the test has been initialized.");
98      }
99      util = new IntegrationTestingUtility(conf);
100   }
101 
102   @Override
103   public Configuration getConf() {
104     return util.getConfiguration();
105   }
106 
107   public static void main(String[] args) throws Exception {
108     Configuration conf = HBaseConfiguration.create();
109     IntegrationTestingUtility.setUseDistributedCluster(conf);
110     int status = ToolRunner.run(conf, new IntegrationTestTableMapReduceUtil(), args);
111     System.exit(status);
112   }
113 }