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;
20  
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertFalse;
24  import static org.junit.Assert.assertTrue;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.fs.FileSystem;
29  import org.apache.hadoop.fs.FileUtil;
30  import org.apache.hadoop.fs.Path;
31  import org.apache.hadoop.hbase.client.Get;
32  import org.apache.hadoop.hbase.client.Put;
33  import org.apache.hadoop.hbase.client.Result;
34  import org.apache.hadoop.hbase.client.Table;
35  import org.apache.hadoop.hbase.testclassification.LargeTests;
36  import org.apache.hadoop.hbase.util.Bytes;
37  import org.apache.hadoop.hbase.zookeeper.MiniZooKeeperCluster;
38  import org.apache.hadoop.hdfs.MiniDFSCluster;
39  import org.apache.hadoop.hbase.http.ssl.KeyStoreTestUtil;
40  import org.junit.Test;
41  import org.junit.experimental.categories.Category;
42  import java.io.File;
43  
44  /**
45   * Test our testing utility class
46   */
47  @Category(LargeTests.class)
48  public class TestHBaseTestingUtility {
49    private final Log LOG = LogFactory.getLog(this.getClass());
50  
51    /**
52     * Basic sanity test that spins up multiple HDFS and HBase clusters that share
53     * the same ZK ensemble. We then create the same table in both and make sure
54     * that what we insert in one place doesn't end up in the other.
55     * @throws Exception
56     */
57    @Test (timeout=180000)
58    public void testMultiClusters() throws Exception {
59      // Create three clusters
60  
61      // Cluster 1.
62      HBaseTestingUtility htu1 = new HBaseTestingUtility();
63      // Set a different zk path for each cluster
64      htu1.getConfiguration().set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/1");
65      htu1.startMiniZKCluster();
66  
67      // Cluster 2
68      HBaseTestingUtility htu2 = new HBaseTestingUtility();
69      htu2.getConfiguration().set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/2");
70      htu2.getConfiguration().set(HConstants.ZOOKEEPER_CLIENT_PORT,
71        htu1.getConfiguration().get(HConstants.ZOOKEEPER_CLIENT_PORT, "-1"));
72      htu2.setZkCluster(htu1.getZkCluster());
73  
74      // Cluster 3; seed it with the conf from htu1 so we pickup the 'right'
75      // zk cluster config; it is set back into the config. as part of the
76      // start of minizkcluster.
77      HBaseTestingUtility htu3 = new HBaseTestingUtility();
78      htu3.getConfiguration().set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/3");
79      htu3.getConfiguration().set(HConstants.ZOOKEEPER_CLIENT_PORT,
80        htu1.getConfiguration().get(HConstants.ZOOKEEPER_CLIENT_PORT, "-1"));
81      htu3.setZkCluster(htu1.getZkCluster());
82  
83      try {
84        htu1.startMiniCluster();
85        htu2.startMiniCluster();
86        htu3.startMiniCluster();
87  
88        final TableName TABLE_NAME = TableName.valueOf("test");
89        final byte[] FAM_NAME = Bytes.toBytes("fam");
90        final byte[] ROW = Bytes.toBytes("row");
91        final byte[] QUAL_NAME = Bytes.toBytes("qual");
92        final byte[] VALUE = Bytes.toBytes("value");
93  
94        Table table1 = htu1.createTable(TABLE_NAME, FAM_NAME);
95        Table table2 = htu2.createTable(TABLE_NAME, FAM_NAME);
96  
97        Put put = new Put(ROW);
98        put.add(FAM_NAME, QUAL_NAME, VALUE);
99        table1.put(put);
100 
101       Get get = new Get(ROW);
102       get.addColumn(FAM_NAME, QUAL_NAME);
103       Result res = table1.get(get);
104       assertEquals(1, res.size());
105 
106       res = table2.get(get);
107       assertEquals(0, res.size());
108 
109       table1.close();
110       table2.close();
111 
112     } finally {
113       htu3.shutdownMiniCluster();
114       htu2.shutdownMiniCluster();
115       htu1.shutdownMiniCluster();
116     }
117   }
118 
119   @Test public void testMiniCluster() throws Exception {
120     HBaseTestingUtility hbt = new HBaseTestingUtility();
121 
122     MiniHBaseCluster cluster = hbt.startMiniCluster();
123     try {
124       assertEquals(1, cluster.getLiveRegionServerThreads().size());
125     } finally {
126       hbt.shutdownMiniCluster();
127     }
128   }
129 
130   @Test
131   public void testMiniClusterBindToWildcard() throws Exception {
132     HBaseTestingUtility hbt = new HBaseTestingUtility();
133     hbt.getConfiguration().set("hbase.regionserver.ipc.address", "0.0.0.0");
134     MiniHBaseCluster cluster = hbt.startMiniCluster();
135     try {
136       assertEquals(1, cluster.getLiveRegionServerThreads().size());
137     } finally {
138       hbt.shutdownMiniCluster();
139     }
140   }
141 
142   @Test
143   public void testMiniClusterWithSSLOn() throws Exception {
144     final String BASEDIR = System.getProperty("test.build.dir",
145         "target/test-dir") + "/" + TestHBaseTestingUtility.class.getSimpleName();
146     String sslConfDir = KeyStoreTestUtil.getClasspathDir(TestHBaseTestingUtility.class);
147     String keystoresDir = new File(BASEDIR).getAbsolutePath();
148 
149     HBaseTestingUtility hbt = new HBaseTestingUtility();
150     File base = new File(BASEDIR);
151     FileUtil.fullyDelete(base);
152     base.mkdirs();
153 
154     KeyStoreTestUtil.setupSSLConfig(keystoresDir, sslConfDir, hbt.getConfiguration(), false);
155 
156     hbt.getConfiguration().set("hbase.ssl.enabled", "true");
157     hbt.getConfiguration().addResource("ssl-server.xml");
158     hbt.getConfiguration().addResource("ssl-client.xml");
159 
160     MiniHBaseCluster cluster = hbt.startMiniCluster();
161     try {
162       assertEquals(1, cluster.getLiveRegionServerThreads().size());
163     } finally {
164       hbt.shutdownMiniCluster();
165     }
166   }
167 
168   /**
169    *  Test that we can start and stop multiple time a cluster
170    *   with the same HBaseTestingUtility.
171    */
172   @Test public void testMultipleStartStop() throws Exception{
173     HBaseTestingUtility htu1 = new HBaseTestingUtility();
174     Path foo = new Path("foo");
175 
176     htu1.startMiniCluster();
177     htu1.getDFSCluster().getFileSystem().create(foo);
178     assertTrue( htu1.getDFSCluster().getFileSystem().exists(foo));
179     htu1.shutdownMiniCluster();
180 
181     htu1.startMiniCluster();
182     assertFalse( htu1.getDFSCluster().getFileSystem().exists(foo));
183     htu1.getDFSCluster().getFileSystem().create(foo);
184     assertTrue( htu1.getDFSCluster().getFileSystem().exists(foo));
185     htu1.shutdownMiniCluster();
186   }
187 
188 
189   @Test public void testMiniZooKeeper() throws Exception {
190     HBaseTestingUtility hbt = new HBaseTestingUtility();
191     MiniZooKeeperCluster cluster1 = hbt.startMiniZKCluster();
192     try {
193       assertEquals(0, cluster1.getBackupZooKeeperServerNum());
194       assertTrue((cluster1.killCurrentActiveZooKeeperServer() == -1));
195     } finally {
196       hbt.shutdownMiniZKCluster();
197     }
198 
199     // set up zookeeper cluster with 5 zk servers
200     MiniZooKeeperCluster cluster2 = hbt.startMiniZKCluster(5);
201     int defaultClientPort = 21818;
202     cluster2.setDefaultClientPort(defaultClientPort);
203     try {
204       assertEquals(4, cluster2.getBackupZooKeeperServerNum());
205 
206       // killing the current active zk server
207       int currentActivePort = cluster2.killCurrentActiveZooKeeperServer();
208       assertTrue(currentActivePort >= defaultClientPort);
209       // Check if the client port is returning a proper value
210       assertTrue(cluster2.getClientPort() == currentActivePort);
211 
212       // kill another active zk server
213       currentActivePort = cluster2.killCurrentActiveZooKeeperServer();
214       assertTrue(currentActivePort >= defaultClientPort);
215       assertTrue(cluster2.getClientPort() == currentActivePort);      
216       assertEquals(2, cluster2.getBackupZooKeeperServerNum());
217       assertEquals(3, cluster2.getZooKeeperServerNum());
218 
219       // killing the backup zk servers
220       cluster2.killOneBackupZooKeeperServer();
221       cluster2.killOneBackupZooKeeperServer();
222       assertEquals(0, cluster2.getBackupZooKeeperServerNum());
223       assertEquals(1, cluster2.getZooKeeperServerNum());
224 
225       // killing the last zk server
226       currentActivePort = cluster2.killCurrentActiveZooKeeperServer();
227       assertTrue(currentActivePort == -1);
228       assertTrue(cluster2.getClientPort() == currentActivePort);
229       // this should do nothing.
230       cluster2.killOneBackupZooKeeperServer();
231       assertEquals(-1, cluster2.getBackupZooKeeperServerNum());
232       assertEquals(0, cluster2.getZooKeeperServerNum());
233     } finally {
234       hbt.shutdownMiniZKCluster();
235     }
236   }
237 
238   @Test public void testMiniDFSCluster() throws Exception {
239     HBaseTestingUtility hbt = new HBaseTestingUtility();
240     MiniDFSCluster cluster = hbt.startMiniDFSCluster(null);
241     FileSystem dfs = cluster.getFileSystem();
242     Path dir = new Path("dir");
243     Path qualifiedDir = dfs.makeQualified(dir);
244     LOG.info("dir=" + dir + ", qualifiedDir=" + qualifiedDir);
245     assertFalse(dfs.exists(qualifiedDir));
246     assertTrue(dfs.mkdirs(qualifiedDir));
247     assertTrue(dfs.delete(qualifiedDir, true));
248     hbt.shutdownMiniCluster();
249   }
250 
251   @Test public void testSetupClusterTestBuildDir() throws Exception {
252     HBaseTestingUtility hbt = new HBaseTestingUtility();
253     Path testdir = hbt.getClusterTestDir();
254     LOG.info("uuid-subdir=" + testdir);
255     FileSystem fs = hbt.getTestFileSystem();
256 
257     assertFalse(fs.exists(testdir));
258 
259     hbt.startMiniDFSCluster(null);
260     assertTrue(fs.exists(testdir));
261 
262     hbt.shutdownMiniCluster();
263     assertFalse(fs.exists(testdir));
264   }
265 
266   @Test public void testTestDir() throws Exception {
267     HBaseTestingUtility hbt = new HBaseTestingUtility();
268     Path testdir = hbt.getDataTestDir();
269     LOG.info("testdir=" + testdir);
270     FileSystem fs = hbt.getTestFileSystem();
271     assertTrue(!fs.exists(testdir));
272     assertTrue(fs.mkdirs(testdir));
273     assertTrue(hbt.cleanupTestDir());
274   }
275 
276 }
277