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