1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.zookeeper;
19
20 import java.io.IOException;
21 import java.util.Properties;
22
23 import org.apache.hadoop.conf.Configuration;
24 import org.apache.hadoop.hbase.HBaseConfiguration;
25 import org.apache.hadoop.hbase.HConstants;
26 import org.apache.hadoop.hbase.testclassification.SmallTests;
27 import org.junit.Assert;
28 import org.junit.Test;
29 import org.junit.experimental.categories.Category;
30
31 import static org.junit.Assert.assertEquals;
32 import static org.junit.Assert.assertTrue;
33
34 @Category({SmallTests.class})
35 public class TestZKConfig {
36
37 @Test
38 public void testZKConfigLoading() throws Exception {
39 Configuration conf = HBaseConfiguration.create();
40
41
42 conf.setInt(HConstants.ZOOKEEPER_CLIENT_PORT, 2181);
43 Properties props = ZKConfig.makeZKProps(conf);
44 assertEquals("Property client port should have been default from the HBase config",
45 "2181",
46 props.getProperty("clientPort"));
47 }
48
49 @Test
50 public void testGetZooKeeperClusterKey() {
51 Configuration conf = HBaseConfiguration.create();
52 conf.set(HConstants.ZOOKEEPER_QUORUM, "\tlocalhost\n");
53 conf.set(HConstants.ZOOKEEPER_CLIENT_PORT, "3333");
54 conf.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "hbase");
55 String clusterKey = ZKConfig.getZooKeeperClusterKey(conf, "test");
56 assertTrue(!clusterKey.contains("\t") && !clusterKey.contains("\n"));
57 assertEquals("localhost:3333:hbase,test", clusterKey);
58 }
59
60 @Test
61 public void testClusterKey() throws Exception {
62 testKey("server", 2181, "hbase");
63 testKey("server1,server2,server3", 2181, "hbase");
64 try {
65 ZKConfig.validateClusterKey("2181:hbase");
66 } catch (IOException ex) {
67
68 }
69 }
70
71 @Test
72 public void testClusterKeyWithMultiplePorts() throws Exception {
73
74 testKey("server1:2182", 2181, "hbase", true);
75
76 testKey("server1:2182,server2:2183,server3:2184", 2181, "hbase", true);
77
78 testKey("server1:2182,server2,server3:2184", 2181, "hbase", true);
79
80 testKey("server1:2182,server2:2183,server3", 2181, "hbase", true);
81
82 testKey("server1:2182,server2,server3:2184,server4", 2181, "hbase", true);
83
84 testKey("server1:2182,server1:2183,server1", 2181, "hbase", true);
85
86 testKey("server1:2182,server2:2183,server1", 2181, "hbase", true);
87 }
88
89 private void testKey(String ensemble, int port, String znode)
90 throws IOException {
91 testKey(ensemble, port, znode, false);
92 }
93
94 private void testKey(String ensemble, int port, String znode, Boolean multiplePortSupport)
95 throws IOException {
96 Configuration conf = new Configuration();
97 String key = ensemble+":"+port+":"+znode;
98 String ensemble2 = null;
99 ZKConfig.ZKClusterKey zkClusterKey = ZKConfig.transformClusterKey(key);
100 if (multiplePortSupport) {
101 ensemble2 = ZKConfig.standardizeZKQuorumServerString(ensemble,
102 Integer.toString(port));
103 assertEquals(ensemble2, zkClusterKey.getQuorumString());
104 }
105 else {
106 assertEquals(ensemble, zkClusterKey.getQuorumString());
107 }
108 assertEquals(port, zkClusterKey.getClientPort());
109 assertEquals(znode, zkClusterKey.getZnodeParent());
110
111 conf = HBaseConfiguration.createClusterConf(conf, key);
112 assertEquals(zkClusterKey.getQuorumString(), conf.get(HConstants.ZOOKEEPER_QUORUM));
113 assertEquals(zkClusterKey.getClientPort(), conf.getInt(HConstants.ZOOKEEPER_CLIENT_PORT, -1));
114 assertEquals(zkClusterKey.getZnodeParent(), conf.get(HConstants.ZOOKEEPER_ZNODE_PARENT));
115
116 String reconstructedKey = ZKConfig.getZooKeeperClusterKey(conf);
117 if (multiplePortSupport) {
118 String key2 = ensemble2 + ":" + port + ":" + znode;
119 assertEquals(key2, reconstructedKey);
120 }
121 else {
122 assertEquals(key, reconstructedKey);
123 }
124 }
125 }