1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
43 import java.io.File;
44 import java.util.List;
45
46
47
48
49 @Category(LargeTests.class)
50 public class TestHBaseTestingUtility {
51 private static final Log LOG = LogFactory.getLog(TestHBaseTestingUtility.class);
52
53
54
55
56
57
58
59 @Test (timeout=180000)
60 public void testMultiClusters() throws Exception {
61
62
63
64 HBaseTestingUtility htu1 = new HBaseTestingUtility();
65
66 htu1.getConfiguration().set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/1");
67 htu1.startMiniZKCluster();
68
69
70 HBaseTestingUtility htu2 = new HBaseTestingUtility();
71 htu2.getConfiguration().set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/2");
72 htu2.getConfiguration().set(HConstants.ZOOKEEPER_CLIENT_PORT,
73 htu1.getConfiguration().get(HConstants.ZOOKEEPER_CLIENT_PORT, "-1"));
74 htu2.setZkCluster(htu1.getZkCluster());
75
76
77
78
79 HBaseTestingUtility htu3 = new HBaseTestingUtility();
80 htu3.getConfiguration().set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/3");
81 htu3.getConfiguration().set(HConstants.ZOOKEEPER_CLIENT_PORT,
82 htu1.getConfiguration().get(HConstants.ZOOKEEPER_CLIENT_PORT, "-1"));
83 htu3.setZkCluster(htu1.getZkCluster());
84
85 try {
86 htu1.startMiniCluster();
87 htu2.startMiniCluster();
88 htu3.startMiniCluster();
89
90 final TableName TABLE_NAME = TableName.valueOf("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 Table table1 = htu1.createTable(TABLE_NAME, FAM_NAME);
97 Table 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 table1.close();
112 table2.close();
113
114 } finally {
115 htu3.shutdownMiniCluster();
116 htu2.shutdownMiniCluster();
117 htu1.shutdownMiniCluster();
118 }
119 }
120
121 @Test public void testMiniCluster() throws Exception {
122 HBaseTestingUtility hbt = new HBaseTestingUtility();
123
124 MiniHBaseCluster cluster = hbt.startMiniCluster();
125 try {
126 assertEquals(1, cluster.getLiveRegionServerThreads().size());
127 } finally {
128 hbt.shutdownMiniCluster();
129 }
130 }
131
132 @Test
133 public void testMiniClusterBindToWildcard() throws Exception {
134 HBaseTestingUtility hbt = new HBaseTestingUtility();
135 hbt.getConfiguration().set("hbase.regionserver.ipc.address", "0.0.0.0");
136 MiniHBaseCluster cluster = hbt.startMiniCluster();
137 try {
138 assertEquals(1, cluster.getLiveRegionServerThreads().size());
139 } finally {
140 hbt.shutdownMiniCluster();
141 }
142 }
143
144 @Test
145 public void testMiniClusterWithSSLOn() throws Exception {
146 final String BASEDIR = System.getProperty("test.build.dir",
147 "target/test-dir") + "/" + TestHBaseTestingUtility.class.getSimpleName();
148 String sslConfDir = KeyStoreTestUtil.getClasspathDir(TestHBaseTestingUtility.class);
149 String keystoresDir = new File(BASEDIR).getAbsolutePath();
150
151 HBaseTestingUtility hbt = new HBaseTestingUtility();
152 File base = new File(BASEDIR);
153 FileUtil.fullyDelete(base);
154 base.mkdirs();
155
156 KeyStoreTestUtil.setupSSLConfig(keystoresDir, sslConfDir, hbt.getConfiguration(), false);
157
158 hbt.getConfiguration().set("hbase.ssl.enabled", "true");
159 hbt.getConfiguration().addResource("ssl-server.xml");
160 hbt.getConfiguration().addResource("ssl-client.xml");
161
162 MiniHBaseCluster cluster = hbt.startMiniCluster();
163 try {
164 assertEquals(1, cluster.getLiveRegionServerThreads().size());
165 } finally {
166 hbt.shutdownMiniCluster();
167 }
168 }
169
170
171
172
173
174 @Test public void testMultipleStartStop() throws Exception{
175 HBaseTestingUtility htu1 = new HBaseTestingUtility();
176 Path foo = new Path("foo");
177
178 htu1.startMiniCluster();
179 htu1.getDFSCluster().getFileSystem().create(foo);
180 assertTrue( htu1.getDFSCluster().getFileSystem().exists(foo));
181 htu1.shutdownMiniCluster();
182
183 htu1.startMiniCluster();
184 assertFalse( htu1.getDFSCluster().getFileSystem().exists(foo));
185 htu1.getDFSCluster().getFileSystem().create(foo);
186 assertTrue( htu1.getDFSCluster().getFileSystem().exists(foo));
187 htu1.shutdownMiniCluster();
188 }
189
190 @Test
191 public void testMiniZooKeeperWithOneServer() throws Exception {
192 HBaseTestingUtility hbt = new HBaseTestingUtility();
193 MiniZooKeeperCluster cluster1 = hbt.startMiniZKCluster();
194 try {
195 assertEquals(0, cluster1.getBackupZooKeeperServerNum());
196 assertTrue((cluster1.killCurrentActiveZooKeeperServer() == -1));
197 } finally {
198 hbt.shutdownMiniZKCluster();
199 }
200 }
201
202 @Test
203 public void testMiniZooKeeperWithMultipleServers() throws Exception {
204 HBaseTestingUtility hbt = new HBaseTestingUtility();
205
206 MiniZooKeeperCluster cluster2 = hbt.startMiniZKCluster(5);
207 int defaultClientPort = 21818;
208 cluster2.setDefaultClientPort(defaultClientPort);
209 try {
210 assertEquals(4, cluster2.getBackupZooKeeperServerNum());
211
212
213 int currentActivePort = cluster2.killCurrentActiveZooKeeperServer();
214 assertTrue(currentActivePort >= defaultClientPort);
215
216 assertTrue(cluster2.getClientPort() == currentActivePort);
217
218
219 currentActivePort = cluster2.killCurrentActiveZooKeeperServer();
220 assertTrue(currentActivePort >= defaultClientPort);
221 assertTrue(cluster2.getClientPort() == currentActivePort);
222 assertEquals(2, cluster2.getBackupZooKeeperServerNum());
223 assertEquals(3, cluster2.getZooKeeperServerNum());
224
225
226 cluster2.killOneBackupZooKeeperServer();
227 cluster2.killOneBackupZooKeeperServer();
228 assertEquals(0, cluster2.getBackupZooKeeperServerNum());
229 assertEquals(1, cluster2.getZooKeeperServerNum());
230
231
232 currentActivePort = cluster2.killCurrentActiveZooKeeperServer();
233 assertTrue(currentActivePort == -1);
234 assertTrue(cluster2.getClientPort() == currentActivePort);
235
236 cluster2.killOneBackupZooKeeperServer();
237 assertEquals(-1, cluster2.getBackupZooKeeperServerNum());
238 assertEquals(0, cluster2.getZooKeeperServerNum());
239 } finally {
240 hbt.shutdownMiniZKCluster();
241 }
242 }
243
244 @Test
245 public void testMiniZooKeeperWithMultipleClientPorts() throws Exception {
246 int defaultClientPort = 8888;
247 int i, j;
248 HBaseTestingUtility hbt = new HBaseTestingUtility();
249
250
251 int [] clientPortList1 = {1111, 1112, 1113};
252 MiniZooKeeperCluster cluster1 = hbt.startMiniZKCluster(clientPortList1.length, clientPortList1);
253 try {
254 List<Integer> clientPortListInCluster = cluster1.getClientPortList();
255
256 for (i = 0; i < clientPortListInCluster.size(); i++) {
257 assertEquals(clientPortListInCluster.get(i).intValue(), clientPortList1[i]);
258 }
259 } finally {
260 hbt.shutdownMiniZKCluster();
261 }
262
263
264 hbt.getConfiguration().setInt("test.hbase.zookeeper.property.clientPort", defaultClientPort);
265 int [] clientPortList2 = {2222, 2223};
266 MiniZooKeeperCluster cluster2 =
267 hbt.startMiniZKCluster(clientPortList2.length + 2, clientPortList2);
268
269 try {
270 List<Integer> clientPortListInCluster = cluster2.getClientPortList();
271
272 for (i = 0, j = 0; i < clientPortListInCluster.size(); i++) {
273 if (i < clientPortList2.length) {
274 assertEquals(clientPortListInCluster.get(i).intValue(), clientPortList2[i]);
275 } else {
276
277
278 assertEquals(clientPortListInCluster.get(i).intValue(), defaultClientPort + j);
279 j++;
280 }
281 }
282 } finally {
283 hbt.shutdownMiniZKCluster();
284 }
285
286
287 hbt.getConfiguration().setInt("test.hbase.zookeeper.property.clientPort", defaultClientPort);
288 int [] clientPortList3 = {3333, -3334, 3335, 0};
289 MiniZooKeeperCluster cluster3 =
290 hbt.startMiniZKCluster(clientPortList3.length + 1, clientPortList3);
291
292 try {
293 List<Integer> clientPortListInCluster = cluster3.getClientPortList();
294
295 for (i = 0, j = 0; i < clientPortListInCluster.size(); i++) {
296
297
298 if (i < clientPortList3.length && clientPortList3[i] > 0) {
299 assertEquals(clientPortListInCluster.get(i).intValue(), clientPortList3[i]);
300 } else {
301 assertEquals(clientPortListInCluster.get(i).intValue(), defaultClientPort + j);
302 j++;
303 }
304 }
305 } finally {
306 hbt.shutdownMiniZKCluster();
307 }
308
309
310
311
312
313 hbt.getConfiguration().setInt("test.hbase.zookeeper.property.clientPort", defaultClientPort);
314 int [] clientPortList4 = {-4444, defaultClientPort+2, 4446, defaultClientPort};
315 MiniZooKeeperCluster cluster4 =
316 hbt.startMiniZKCluster(clientPortList4.length + 1, clientPortList4);
317
318 try {
319 List<Integer> clientPortListInCluster = cluster4.getClientPortList();
320
321 for (i = 0, j = 1; i < clientPortListInCluster.size(); i++) {
322
323
324 if (i < clientPortList4.length && clientPortList4[i] > 0) {
325 assertEquals(clientPortListInCluster.get(i).intValue(), clientPortList4[i]);
326 } else {
327 assertEquals(clientPortListInCluster.get(i).intValue(), defaultClientPort + j);
328 j +=2;
329 }
330 }
331 } finally {
332 hbt.shutdownMiniZKCluster();
333 }
334
335
336 int [] clientPortList5 = {5555, 5556, 5556};
337
338 try {
339 MiniZooKeeperCluster cluster5 =
340 hbt.startMiniZKCluster(clientPortList5.length, clientPortList5);
341 assertTrue(cluster5.getClientPort() == -1);
342 } catch (Exception e) {
343
344 } finally {
345 hbt.shutdownMiniZKCluster();
346 }
347 }
348
349 @Test public void testMiniDFSCluster() throws Exception {
350 HBaseTestingUtility hbt = new HBaseTestingUtility();
351 MiniDFSCluster cluster = hbt.startMiniDFSCluster(null);
352 FileSystem dfs = cluster.getFileSystem();
353 Path dir = new Path("dir");
354 Path qualifiedDir = dfs.makeQualified(dir);
355 LOG.info("dir=" + dir + ", qualifiedDir=" + qualifiedDir);
356 assertFalse(dfs.exists(qualifiedDir));
357 assertTrue(dfs.mkdirs(qualifiedDir));
358 assertTrue(dfs.delete(qualifiedDir, true));
359 hbt.shutdownMiniCluster();
360 }
361
362 @Test public void testSetupClusterTestBuildDir() throws Exception {
363 HBaseTestingUtility hbt = new HBaseTestingUtility();
364 Path testdir = hbt.getClusterTestDir();
365 LOG.info("uuid-subdir=" + testdir);
366 FileSystem fs = hbt.getTestFileSystem();
367
368 assertFalse(fs.exists(testdir));
369
370 hbt.startMiniDFSCluster(null);
371 assertTrue(fs.exists(testdir));
372
373 hbt.shutdownMiniCluster();
374 assertFalse(fs.exists(testdir));
375 }
376
377 @Test public void testTestDir() throws Exception {
378 HBaseTestingUtility hbt = new HBaseTestingUtility();
379 Path testdir = hbt.getDataTestDir();
380 LOG.info("testdir=" + testdir);
381 FileSystem fs = hbt.getTestFileSystem();
382 assertTrue(!fs.exists(testdir));
383 assertTrue(fs.mkdirs(testdir));
384 assertTrue(hbt.cleanupTestDir());
385 }
386
387 }
388