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 @Test
171 public void testMiniClusterWithSSLOnWithOldPropery() throws Exception {
172 final String BASEDIR = System.getProperty("test.build.dir",
173 "target/test-dir") + "/" + TestHBaseTestingUtility.class.getSimpleName();
174 String sslConfDir = KeyStoreTestUtil.getClasspathDir(TestHBaseTestingUtility.class);
175 String keystoresDir = new File(BASEDIR).getAbsolutePath();
176
177 HBaseTestingUtility hbt = new HBaseTestingUtility();
178 File base = new File(BASEDIR);
179 FileUtil.fullyDelete(base);
180 base.mkdirs();
181
182 KeyStoreTestUtil.setupSSLConfig(keystoresDir, sslConfDir, hbt.getConfiguration(), false);
183
184 hbt.getConfiguration().set("hadoop.ssl.enabled", "true");
185 hbt.getConfiguration().addResource("ssl-server.xml");
186 hbt.getConfiguration().addResource("ssl-client.xml");
187
188 MiniHBaseCluster cluster = hbt.startMiniCluster();
189 try {
190 assertEquals(1, cluster.getLiveRegionServerThreads().size());
191 } finally {
192 hbt.shutdownMiniCluster();
193 }
194 }
195
196
197
198
199 @Test public void testMultipleStartStop() throws Exception{
200 HBaseTestingUtility htu1 = new HBaseTestingUtility();
201 Path foo = new Path("foo");
202
203 htu1.startMiniCluster();
204 htu1.getDFSCluster().getFileSystem().create(foo);
205 assertTrue( htu1.getDFSCluster().getFileSystem().exists(foo));
206 htu1.shutdownMiniCluster();
207
208 htu1.startMiniCluster();
209 assertFalse( htu1.getDFSCluster().getFileSystem().exists(foo));
210 htu1.getDFSCluster().getFileSystem().create(foo);
211 assertTrue( htu1.getDFSCluster().getFileSystem().exists(foo));
212 htu1.shutdownMiniCluster();
213 }
214
215 @Test
216 public void testMiniZooKeeperWithOneServer() throws Exception {
217 HBaseTestingUtility hbt = new HBaseTestingUtility();
218 MiniZooKeeperCluster cluster1 = hbt.startMiniZKCluster();
219 try {
220 assertEquals(0, cluster1.getBackupZooKeeperServerNum());
221 assertTrue((cluster1.killCurrentActiveZooKeeperServer() == -1));
222 } finally {
223 hbt.shutdownMiniZKCluster();
224 }
225 }
226
227 @Test
228 public void testMiniZooKeeperWithMultipleServers() throws Exception {
229 HBaseTestingUtility hbt = new HBaseTestingUtility();
230
231 MiniZooKeeperCluster cluster2 = hbt.startMiniZKCluster(5);
232 int defaultClientPort = 21818;
233 cluster2.setDefaultClientPort(defaultClientPort);
234 try {
235 assertEquals(4, cluster2.getBackupZooKeeperServerNum());
236
237
238 int currentActivePort = cluster2.killCurrentActiveZooKeeperServer();
239 assertTrue(currentActivePort >= defaultClientPort);
240
241 assertTrue(cluster2.getClientPort() == currentActivePort);
242
243
244 currentActivePort = cluster2.killCurrentActiveZooKeeperServer();
245 assertTrue(currentActivePort >= defaultClientPort);
246 assertTrue(cluster2.getClientPort() == currentActivePort);
247 assertEquals(2, cluster2.getBackupZooKeeperServerNum());
248 assertEquals(3, cluster2.getZooKeeperServerNum());
249
250
251 cluster2.killOneBackupZooKeeperServer();
252 cluster2.killOneBackupZooKeeperServer();
253 assertEquals(0, cluster2.getBackupZooKeeperServerNum());
254 assertEquals(1, cluster2.getZooKeeperServerNum());
255
256
257 currentActivePort = cluster2.killCurrentActiveZooKeeperServer();
258 assertTrue(currentActivePort == -1);
259 assertTrue(cluster2.getClientPort() == currentActivePort);
260
261 cluster2.killOneBackupZooKeeperServer();
262 assertEquals(-1, cluster2.getBackupZooKeeperServerNum());
263 assertEquals(0, cluster2.getZooKeeperServerNum());
264 } finally {
265 hbt.shutdownMiniZKCluster();
266 }
267 }
268
269 @Test
270 public void testMiniZooKeeperWithMultipleClientPorts() throws Exception {
271 int defaultClientPort = 8888;
272 int i, j;
273 HBaseTestingUtility hbt = new HBaseTestingUtility();
274
275
276 int [] clientPortList1 = {1111, 1112, 1113};
277 MiniZooKeeperCluster cluster1 = hbt.startMiniZKCluster(clientPortList1.length, clientPortList1);
278 try {
279 List<Integer> clientPortListInCluster = cluster1.getClientPortList();
280
281 for (i = 0; i < clientPortListInCluster.size(); i++) {
282 assertEquals(clientPortListInCluster.get(i).intValue(), clientPortList1[i]);
283 }
284 } finally {
285 hbt.shutdownMiniZKCluster();
286 }
287
288
289 hbt.getConfiguration().setInt("test.hbase.zookeeper.property.clientPort", defaultClientPort);
290 int [] clientPortList2 = {2222, 2223};
291 MiniZooKeeperCluster cluster2 =
292 hbt.startMiniZKCluster(clientPortList2.length + 2, clientPortList2);
293
294 try {
295 List<Integer> clientPortListInCluster = cluster2.getClientPortList();
296
297 for (i = 0, j = 0; i < clientPortListInCluster.size(); i++) {
298 if (i < clientPortList2.length) {
299 assertEquals(clientPortListInCluster.get(i).intValue(), clientPortList2[i]);
300 } else {
301
302
303 assertEquals(clientPortListInCluster.get(i).intValue(), defaultClientPort + j);
304 j++;
305 }
306 }
307 } finally {
308 hbt.shutdownMiniZKCluster();
309 }
310
311
312 hbt.getConfiguration().setInt("test.hbase.zookeeper.property.clientPort", defaultClientPort);
313 int [] clientPortList3 = {3333, -3334, 3335, 0};
314 MiniZooKeeperCluster cluster3 =
315 hbt.startMiniZKCluster(clientPortList3.length + 1, clientPortList3);
316
317 try {
318 List<Integer> clientPortListInCluster = cluster3.getClientPortList();
319
320 for (i = 0, j = 0; i < clientPortListInCluster.size(); i++) {
321
322
323 if (i < clientPortList3.length && clientPortList3[i] > 0) {
324 assertEquals(clientPortListInCluster.get(i).intValue(), clientPortList3[i]);
325 } else {
326 assertEquals(clientPortListInCluster.get(i).intValue(), defaultClientPort + j);
327 j++;
328 }
329 }
330 } finally {
331 hbt.shutdownMiniZKCluster();
332 }
333
334
335
336
337
338 hbt.getConfiguration().setInt("test.hbase.zookeeper.property.clientPort", defaultClientPort);
339 int [] clientPortList4 = {-4444, defaultClientPort+2, 4446, defaultClientPort};
340 MiniZooKeeperCluster cluster4 =
341 hbt.startMiniZKCluster(clientPortList4.length + 1, clientPortList4);
342
343 try {
344 List<Integer> clientPortListInCluster = cluster4.getClientPortList();
345
346 for (i = 0, j = 1; i < clientPortListInCluster.size(); i++) {
347
348
349 if (i < clientPortList4.length && clientPortList4[i] > 0) {
350 assertEquals(clientPortListInCluster.get(i).intValue(), clientPortList4[i]);
351 } else {
352 assertEquals(clientPortListInCluster.get(i).intValue(), defaultClientPort + j);
353 j +=2;
354 }
355 }
356 } finally {
357 hbt.shutdownMiniZKCluster();
358 }
359
360
361 int [] clientPortList5 = {5555, 5556, 5556};
362
363 try {
364 MiniZooKeeperCluster cluster5 =
365 hbt.startMiniZKCluster(clientPortList5.length, clientPortList5);
366 assertTrue(cluster5.getClientPort() == -1);
367 } catch (Exception e) {
368
369 } finally {
370 hbt.shutdownMiniZKCluster();
371 }
372 }
373
374 @Test public void testMiniDFSCluster() throws Exception {
375 HBaseTestingUtility hbt = new HBaseTestingUtility();
376 MiniDFSCluster cluster = hbt.startMiniDFSCluster(null);
377 FileSystem dfs = cluster.getFileSystem();
378 Path dir = new Path("dir");
379 Path qualifiedDir = dfs.makeQualified(dir);
380 LOG.info("dir=" + dir + ", qualifiedDir=" + qualifiedDir);
381 assertFalse(dfs.exists(qualifiedDir));
382 assertTrue(dfs.mkdirs(qualifiedDir));
383 assertTrue(dfs.delete(qualifiedDir, true));
384 hbt.shutdownMiniCluster();
385 }
386
387 @Test public void testSetupClusterTestBuildDir() throws Exception {
388 HBaseTestingUtility hbt = new HBaseTestingUtility();
389 Path testdir = hbt.getClusterTestDir();
390 LOG.info("uuid-subdir=" + testdir);
391 FileSystem fs = hbt.getTestFileSystem();
392
393 assertFalse(fs.exists(testdir));
394
395 hbt.startMiniDFSCluster(null);
396 assertTrue(fs.exists(testdir));
397
398 hbt.shutdownMiniCluster();
399 assertFalse(fs.exists(testdir));
400 }
401
402 @Test public void testTestDir() throws Exception {
403 HBaseTestingUtility hbt = new HBaseTestingUtility();
404 Path testdir = hbt.getDataTestDir();
405 LOG.info("testdir=" + testdir);
406 FileSystem fs = hbt.getTestFileSystem();
407 assertTrue(!fs.exists(testdir));
408 assertTrue(fs.mkdirs(testdir));
409 assertTrue(hbt.cleanupTestDir());
410 }
411
412 }
413