1   /**
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase;
21  
22  
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertFalse;
25  import static org.junit.Assert.assertTrue;
26  
27  import java.io.File;
28  import java.io.IOException;
29  
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.apache.hadoop.fs.FileSystem;
33  import org.apache.hadoop.fs.Path;
34  import org.apache.hadoop.hbase.client.Get;
35  import org.apache.hadoop.hbase.client.HTable;
36  import org.apache.hadoop.hbase.client.Put;
37  import org.apache.hadoop.hbase.client.Result;
38  import org.apache.hadoop.hbase.util.Bytes;
39  import org.apache.hadoop.hbase.zookeeper.MiniZooKeeperCluster;
40  import org.apache.hadoop.hdfs.MiniDFSCluster;
41  import org.junit.After;
42  import org.junit.AfterClass;
43  import org.junit.Before;
44  import org.junit.BeforeClass;
45  import org.junit.Test;
46  
47  /**
48   * Test our testing utility class
49   */
50  public class TestHBaseTestingUtility {
51    private final Log LOG = LogFactory.getLog(this.getClass());
52  
53    /**
54     * Basic sanity test that spins up multiple HDFS and HBase clusters that share
55     * the same ZK ensemble. We then create the same table in both and make sure
56     * that what we insert in one place doesn't end up in the other.
57     * @throws Exception
58     */
59    @Test (timeout=180000)
60    public void testMultiClusters() throws Exception {
61      // Create three clusters
62  
63      // Cluster 1.
64      HBaseTestingUtility htu1 = new HBaseTestingUtility();
65      // Set a different zk path for each cluster
66      htu1.getConfiguration().set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/1");
67      htu1.startMiniZKCluster();
68  
69      // Cluster 2
70      HBaseTestingUtility htu2 = new HBaseTestingUtility();
71      htu2.getConfiguration().set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/2");
72      htu2.getConfiguration().set("hbase.zookeeper.property.clientPort",
73        htu1.getConfiguration().get("hbase.zookeeper.property.clientPort", "-1"));
74      htu2.setZkCluster(htu1.getZkCluster());
75  
76      // Cluster 3; seed it with the conf from htu1 so we pickup the 'right'
77      // zk cluster config; it is set back into the config. as part of the
78      // start of minizkcluster.
79      HBaseTestingUtility htu3 = new HBaseTestingUtility();
80      htu3.getConfiguration().set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/3");
81      htu3.getConfiguration().set("hbase.zookeeper.property.clientPort",
82        htu1.getConfiguration().get("hbase.zookeeper.property.clientPort", "-1"));
83      htu3.setZkCluster(htu1.getZkCluster());
84  
85      try {
86        htu1.startMiniCluster();
87        htu2.startMiniCluster();
88        htu3.startMiniCluster();
89  
90        final byte[] TABLE_NAME = Bytes.toBytes("test");
91        final byte[] FAM_NAME = Bytes.toBytes("fam");
92        final byte[] ROW = Bytes.toBytes("row");
93        final byte[] QUAL_NAME = Bytes.toBytes("qual");
94        final byte[] VALUE = Bytes.toBytes("value");
95  
96        HTable table1 = htu1.createTable(TABLE_NAME, FAM_NAME);
97        HTable table2 = htu2.createTable(TABLE_NAME, FAM_NAME);
98  
99        Put put = new Put(ROW);
100       put.add(FAM_NAME, QUAL_NAME, VALUE);
101       table1.put(put);
102 
103       Get get = new Get(ROW);
104       get.addColumn(FAM_NAME, QUAL_NAME);
105       Result res = table1.get(get);
106       assertEquals(1, res.size());
107 
108       res = table2.get(get);
109       assertEquals(0, res.size());
110 
111     } finally {
112       htu3.shutdownMiniCluster();
113       htu2.shutdownMiniCluster();
114       htu1.shutdownMiniCluster();
115     }
116   }
117 
118   @Test public void testMiniCluster() throws Exception {
119     HBaseTestingUtility hbt = new HBaseTestingUtility();
120 
121     MiniHBaseCluster cluster = hbt.startMiniCluster();
122     try {
123       assertEquals(1, cluster.getLiveRegionServerThreads().size());
124     } finally {
125       hbt.shutdownMiniCluster();
126     }
127   }
128 
129   /**
130    *  Test that we can start and stop multiple time a cluster
131    *   with the same HBaseTestingUtility.
132    */
133   @Test public void testMultipleStartStop() throws Exception{
134     HBaseTestingUtility htu1 = new HBaseTestingUtility();
135     Path foo = new Path("foo");
136 
137     htu1.startMiniCluster();
138     htu1.getDFSCluster().getFileSystem().create(foo);
139     assertTrue( htu1.getDFSCluster().getFileSystem().exists(foo));
140     htu1.shutdownMiniCluster();
141 
142     htu1.startMiniCluster();
143     assertFalse( htu1.getDFSCluster().getFileSystem().exists(foo));
144     htu1.getDFSCluster().getFileSystem().create(foo);
145     assertTrue( htu1.getDFSCluster().getFileSystem().exists(foo));
146     htu1.shutdownMiniCluster();
147   }
148 
149 
150   @Test public void testMiniZooKeeper() throws Exception {
151     HBaseTestingUtility hbt = new HBaseTestingUtility();
152     MiniZooKeeperCluster cluster1 = hbt.startMiniZKCluster();
153     try {
154       assertEquals(0, cluster1.getBackupZooKeeperServerNum());
155       assertTrue((cluster1.killCurrentActiveZooKeeperServer() == -1));
156     } finally {
157       hbt.shutdownMiniZKCluster();
158     }
159 
160     // set up zookeeper cluster with 5 zk servers
161     MiniZooKeeperCluster cluster2 = hbt.startMiniZKCluster(5);
162     int defaultClientPort = 21818;
163     cluster2.setDefaultClientPort(defaultClientPort);
164     try {
165       assertEquals(4, cluster2.getBackupZooKeeperServerNum());
166 
167       // killing the current active zk server
168       assertTrue((cluster2.killCurrentActiveZooKeeperServer() >= defaultClientPort));
169       assertTrue((cluster2.killCurrentActiveZooKeeperServer() >= defaultClientPort));
170       assertEquals(2, cluster2.getBackupZooKeeperServerNum());
171       assertEquals(3, cluster2.getZooKeeperServerNum());
172 
173       // killing the backup zk servers
174       cluster2.killOneBackupZooKeeperServer();
175       cluster2.killOneBackupZooKeeperServer();
176       assertEquals(0, cluster2.getBackupZooKeeperServerNum());
177       assertEquals(1, cluster2.getZooKeeperServerNum());
178 
179       // killing the last zk server
180       assertTrue((cluster2.killCurrentActiveZooKeeperServer() == -1));
181       // this should do nothing.
182       cluster2.killOneBackupZooKeeperServer();
183       assertEquals(-1, cluster2.getBackupZooKeeperServerNum());
184       assertEquals(0, cluster2.getZooKeeperServerNum());
185     } finally {
186       hbt.shutdownMiniZKCluster();
187     }
188   }
189 
190   @Test public void testMiniDFSCluster() throws Exception {
191     HBaseTestingUtility hbt = new HBaseTestingUtility();
192     MiniDFSCluster cluster = hbt.startMiniDFSCluster(1);
193     FileSystem dfs = cluster.getFileSystem();
194     Path dir = new Path("dir");
195     Path qualifiedDir = dfs.makeQualified(dir);
196     LOG.info("dir=" + dir + ", qualifiedDir=" + qualifiedDir);
197     assertFalse(dfs.exists(qualifiedDir));
198     assertTrue(dfs.mkdirs(qualifiedDir));
199     assertTrue(dfs.delete(qualifiedDir, true));
200     hbt.shutdownMiniCluster();
201   }
202 
203   @Test public void testSetupClusterTestBuildDir() throws Exception {
204     HBaseTestingUtility hbt = new HBaseTestingUtility();
205     Path testdir = hbt.getClusterTestDir();
206     LOG.info("uuid-subdir=" + testdir);
207     FileSystem fs = hbt.getTestFileSystem();
208 
209     assertFalse(fs.exists(testdir));
210 
211     hbt.startMiniDFSCluster(1);
212     assertTrue(fs.exists(testdir));
213 
214     hbt.shutdownMiniCluster();
215     assertFalse(fs.exists(testdir));
216 
217   }
218 
219   @Test public void testTestDir() throws Exception {
220     HBaseTestingUtility hbt = new HBaseTestingUtility();
221     Path testdir = hbt.getDataTestDir();
222     LOG.info("testdir=" + testdir);
223     FileSystem fs = hbt.getTestFileSystem();
224     assertTrue(!fs.exists(testdir));
225     assertTrue(fs.mkdirs(testdir));
226     assertTrue(hbt.cleanupTestDir());
227   }
228 }