1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 import org.junit.experimental.categories.Category;
47
48
49
50
51 @Category(LargeTests.class)
52 public class TestHBaseTestingUtility {
53 private final Log LOG = LogFactory.getLog(this.getClass());
54
55
56
57
58
59
60
61 @Test (timeout=180000)
62 public void testMultiClusters() throws Exception {
63
64
65
66 HBaseTestingUtility htu1 = new HBaseTestingUtility();
67
68 htu1.getConfiguration().set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/1");
69 htu1.startMiniZKCluster();
70
71
72 HBaseTestingUtility htu2 = new HBaseTestingUtility();
73 htu2.getConfiguration().set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/2");
74 htu2.getConfiguration().set(HConstants.ZOOKEEPER_CLIENT_PORT,
75 htu1.getConfiguration().get(HConstants.ZOOKEEPER_CLIENT_PORT, "-1"));
76 htu2.setZkCluster(htu1.getZkCluster());
77
78
79
80
81 HBaseTestingUtility htu3 = new HBaseTestingUtility();
82 htu3.getConfiguration().set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/3");
83 htu3.getConfiguration().set(HConstants.ZOOKEEPER_CLIENT_PORT,
84 htu1.getConfiguration().get(HConstants.ZOOKEEPER_CLIENT_PORT, "-1"));
85 htu3.setZkCluster(htu1.getZkCluster());
86
87 try {
88 htu1.startMiniCluster();
89 htu2.startMiniCluster();
90 htu3.startMiniCluster();
91
92 final byte[] TABLE_NAME = Bytes.toBytes("test");
93 final byte[] FAM_NAME = Bytes.toBytes("fam");
94 final byte[] ROW = Bytes.toBytes("row");
95 final byte[] QUAL_NAME = Bytes.toBytes("qual");
96 final byte[] VALUE = Bytes.toBytes("value");
97
98 HTable table1 = htu1.createTable(TABLE_NAME, FAM_NAME);
99 HTable table2 = htu2.createTable(TABLE_NAME, FAM_NAME);
100
101 Put put = new Put(ROW);
102 put.add(FAM_NAME, QUAL_NAME, VALUE);
103 table1.put(put);
104
105 Get get = new Get(ROW);
106 get.addColumn(FAM_NAME, QUAL_NAME);
107 Result res = table1.get(get);
108 assertEquals(1, res.size());
109
110 res = table2.get(get);
111 assertEquals(0, res.size());
112
113 table1.close();
114 table2.close();
115
116 } finally {
117 htu3.shutdownMiniCluster();
118 htu2.shutdownMiniCluster();
119 htu1.shutdownMiniCluster();
120 }
121 }
122
123 @Test public void testMiniCluster() throws Exception {
124 HBaseTestingUtility hbt = new HBaseTestingUtility();
125
126 MiniHBaseCluster cluster = hbt.startMiniCluster();
127 try {
128 assertEquals(1, cluster.getLiveRegionServerThreads().size());
129 } finally {
130 hbt.shutdownMiniCluster();
131 }
132 }
133
134
135
136
137
138 @Test public void testMultipleStartStop() throws Exception{
139 HBaseTestingUtility htu1 = new HBaseTestingUtility();
140 Path foo = new Path("foo");
141
142 htu1.startMiniCluster();
143 htu1.getDFSCluster().getFileSystem().create(foo);
144 assertTrue( htu1.getDFSCluster().getFileSystem().exists(foo));
145 htu1.shutdownMiniCluster();
146
147 htu1.startMiniCluster();
148 assertFalse( htu1.getDFSCluster().getFileSystem().exists(foo));
149 htu1.getDFSCluster().getFileSystem().create(foo);
150 assertTrue( htu1.getDFSCluster().getFileSystem().exists(foo));
151 htu1.shutdownMiniCluster();
152 }
153
154
155 @Test public void testMiniZooKeeper() throws Exception {
156 HBaseTestingUtility hbt = new HBaseTestingUtility();
157 MiniZooKeeperCluster cluster1 = hbt.startMiniZKCluster();
158 try {
159 assertEquals(0, cluster1.getBackupZooKeeperServerNum());
160 assertTrue((cluster1.killCurrentActiveZooKeeperServer() == -1));
161 } finally {
162 hbt.shutdownMiniZKCluster();
163 }
164
165
166 MiniZooKeeperCluster cluster2 = hbt.startMiniZKCluster(5);
167 int defaultClientPort = 21818;
168 cluster2.setDefaultClientPort(defaultClientPort);
169 try {
170 assertEquals(4, cluster2.getBackupZooKeeperServerNum());
171
172
173 assertTrue((cluster2.killCurrentActiveZooKeeperServer() >= defaultClientPort));
174 assertTrue((cluster2.killCurrentActiveZooKeeperServer() >= defaultClientPort));
175 assertEquals(2, cluster2.getBackupZooKeeperServerNum());
176 assertEquals(3, cluster2.getZooKeeperServerNum());
177
178
179 cluster2.killOneBackupZooKeeperServer();
180 cluster2.killOneBackupZooKeeperServer();
181 assertEquals(0, cluster2.getBackupZooKeeperServerNum());
182 assertEquals(1, cluster2.getZooKeeperServerNum());
183
184
185 assertTrue((cluster2.killCurrentActiveZooKeeperServer() == -1));
186
187 cluster2.killOneBackupZooKeeperServer();
188 assertEquals(-1, cluster2.getBackupZooKeeperServerNum());
189 assertEquals(0, cluster2.getZooKeeperServerNum());
190 } finally {
191 hbt.shutdownMiniZKCluster();
192 }
193 }
194
195 @Test public void testMiniDFSCluster() throws Exception {
196 HBaseTestingUtility hbt = new HBaseTestingUtility();
197 MiniDFSCluster cluster = hbt.startMiniDFSCluster(1);
198 FileSystem dfs = cluster.getFileSystem();
199 Path dir = new Path("dir");
200 Path qualifiedDir = dfs.makeQualified(dir);
201 LOG.info("dir=" + dir + ", qualifiedDir=" + qualifiedDir);
202 assertFalse(dfs.exists(qualifiedDir));
203 assertTrue(dfs.mkdirs(qualifiedDir));
204 assertTrue(dfs.delete(qualifiedDir, true));
205 hbt.shutdownMiniCluster();
206 }
207
208 @Test public void testSetupClusterTestBuildDir() throws Exception {
209 HBaseTestingUtility hbt = new HBaseTestingUtility();
210 Path testdir = hbt.getClusterTestDir();
211 LOG.info("uuid-subdir=" + testdir);
212 FileSystem fs = hbt.getTestFileSystem();
213
214 assertFalse(fs.exists(testdir));
215
216 hbt.startMiniDFSCluster(1);
217 assertTrue(fs.exists(testdir));
218
219 hbt.shutdownMiniCluster();
220 assertFalse(fs.exists(testdir));
221
222 }
223
224 @Test public void testTestDir() throws Exception {
225 HBaseTestingUtility hbt = new HBaseTestingUtility();
226 Path testdir = hbt.getDataTestDir();
227 LOG.info("testdir=" + testdir);
228 FileSystem fs = hbt.getTestFileSystem();
229 assertTrue(!fs.exists(testdir));
230 assertTrue(fs.mkdirs(testdir));
231 assertTrue(hbt.cleanupTestDir());
232 }
233
234 @org.junit.Rule
235 public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
236 new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
237 }
238