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.hdfs.MiniDFSCluster;
40  import org.junit.After;
41  import org.junit.AfterClass;
42  import org.junit.Before;
43  import org.junit.BeforeClass;
44  import org.junit.Test;
45  
46  /**
47   * Test our testing utility class
48   */
49  public class TestHBaseTestingUtility {
50    private final Log LOG = LogFactory.getLog(this.getClass());
51  
52    private HBaseTestingUtility hbt;
53  
54    @BeforeClass
55    public static void setUpBeforeClass() throws Exception {
56    }
57  
58    @AfterClass
59    public static void tearDownAfterClass() throws Exception {
60    }
61  
62    @Before
63    public void setUp() throws Exception {
64      this.hbt = new HBaseTestingUtility();
65      this.hbt.cleanupTestDir();
66    }
67  
68    @After
69    public void tearDown() throws Exception {
70    }
71  
72    /**
73     * Basic sanity test that spins up multiple HDFS and HBase clusters that share
74     * the same ZK ensemble. We then create the same table in both and make sure
75     * that what we insert in one place doesn't end up in the other.
76     * @throws Exception
77     */
78    @Test (timeout=180000)
79    public void multiClusters() throws Exception {
80      // Create three clusters
81  
82      // Cluster 1.
83      HBaseTestingUtility htu1 = new HBaseTestingUtility();
84      // Set a different zk path for each cluster
85      htu1.getConfiguration().set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/1");
86      htu1.startMiniZKCluster();
87  
88      // Cluster 2
89      HBaseTestingUtility htu2 = new HBaseTestingUtility();
90      htu2.getConfiguration().set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/2");
91      htu2.setZkCluster(htu1.getZkCluster());
92  
93      // Cluster 3.
94      HBaseTestingUtility htu3 = new HBaseTestingUtility();
95      htu3.getConfiguration().set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/3");
96      htu3.setZkCluster(htu1.getZkCluster());
97  
98      try {
99        htu1.startMiniCluster();
100       htu2.startMiniCluster();
101       htu3.startMiniCluster();
102 
103       final byte[] TABLE_NAME = Bytes.toBytes("test");
104       final byte[] FAM_NAME = Bytes.toBytes("fam");
105       final byte[] ROW = Bytes.toBytes("row");
106       final byte[] QUAL_NAME = Bytes.toBytes("qual");
107       final byte[] VALUE = Bytes.toBytes("value");
108 
109       HTable table1 = htu1.createTable(TABLE_NAME, FAM_NAME);
110       HTable table2 = htu2.createTable(TABLE_NAME, FAM_NAME);
111 
112       Put put = new Put(ROW);
113       put.add(FAM_NAME, QUAL_NAME, VALUE);
114       table1.put(put);
115 
116       Get get = new Get(ROW);
117       get.addColumn(FAM_NAME, QUAL_NAME);
118       Result res = table1.get(get);
119       assertEquals(1, res.size());
120 
121       res = table2.get(get);
122       assertEquals(0, res.size());
123 
124     } finally {
125       htu3.shutdownMiniCluster();
126       htu2.shutdownMiniCluster();
127       htu1.shutdownMiniCluster();
128     }
129   }
130 
131   @Test public void testMiniCluster() throws Exception {
132     MiniHBaseCluster cluster = this.hbt.startMiniCluster();
133     try {
134       assertEquals(1, cluster.getLiveRegionServerThreads().size());
135     } finally {
136       cluster.shutdown();
137     }
138   }
139 
140   @Test public void testMiniDFSCluster() throws Exception {
141     MiniDFSCluster cluster = this.hbt.startMiniDFSCluster(1);
142     FileSystem dfs = cluster.getFileSystem();
143     Path dir = new Path("dir");
144     Path qualifiedDir = dfs.makeQualified(dir);
145     LOG.info("dir=" + dir + ", qualifiedDir=" + qualifiedDir);
146     assertFalse(dfs.exists(qualifiedDir));
147     assertTrue(dfs.mkdirs(qualifiedDir));
148     assertTrue(dfs.delete(qualifiedDir, true));
149     try {
150     } finally {
151       cluster.shutdown();
152     }
153   }
154 
155   @Test public void testSetupClusterTestBuildDir() {
156     File testdir = this.hbt.setupClusterTestBuildDir();
157     LOG.info("uuid-subdir=" + testdir);
158     assertFalse(testdir.exists());
159     assertTrue(testdir.mkdirs());
160     assertTrue(testdir.exists());
161   }
162 
163   @Test public void testTestDir() throws IOException {
164     Path testdir = HBaseTestingUtility.getTestDir();
165     LOG.info("testdir=" + testdir);
166     FileSystem fs = this.hbt.getTestFileSystem();
167     assertTrue(!fs.exists(testdir));
168     assertTrue(fs.mkdirs(testdir));
169     assertTrue(this.hbt.cleanupTestDir());
170   }
171 }