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.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
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
74
75
76
77
78 @Test (timeout=180000)
79 public void multiClusters() throws Exception {
80
81
82
83 HBaseTestingUtility htu1 = new HBaseTestingUtility();
84
85 htu1.getConfiguration().set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/1");
86 htu1.startMiniZKCluster();
87
88
89 HBaseTestingUtility htu2 = new HBaseTestingUtility();
90 htu2.getConfiguration().set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/2");
91 htu2.setZkCluster(htu1.getZkCluster());
92
93
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 }