001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.zookeeper; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertTrue; 022 023import java.io.IOException; 024import java.util.Properties; 025import java.util.Set; 026import org.apache.hadoop.conf.Configuration; 027import org.apache.hadoop.hbase.HBaseClassTestRule; 028import org.apache.hadoop.hbase.HBaseConfiguration; 029import org.apache.hadoop.hbase.HConstants; 030import org.apache.hadoop.hbase.testclassification.MiscTests; 031import org.apache.hadoop.hbase.testclassification.SmallTests; 032import org.apache.zookeeper.client.ZKClientConfig; 033import org.junit.ClassRule; 034import org.junit.Test; 035import org.junit.experimental.categories.Category; 036 037import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableSet; 038 039@Category({ MiscTests.class, SmallTests.class }) 040public class TestZKConfig { 041 042 @ClassRule 043 public static final HBaseClassTestRule CLASS_RULE = 044 HBaseClassTestRule.forClass(TestZKConfig.class); 045 046 /** Supported ZooKeeper client TLS properties */ 047 private static final Set<String> ZOOKEEPER_CLIENT_TLS_PROPERTIES = ImmutableSet.of( 048 "client.secure", "clientCnxnSocket", "ssl.keyStore.location", "ssl.keyStore.password", 049 "ssl.keyStore.passwordPath", "ssl.keyStore.type", "ssl.trustStore.location", 050 "ssl.trustStore.password", "ssl.trustStore.passwordPath", "ssl.trustStore.type"); 051 052 @Test 053 public void testZKConfigLoading() throws Exception { 054 Configuration conf = HBaseConfiguration.create(); 055 // Test that we read only from the config instance 056 // (i.e. via hbase-default.xml and hbase-site.xml) 057 conf.setInt(HConstants.ZOOKEEPER_CLIENT_PORT, 2181); 058 Properties props = ZKConfig.makeZKProps(conf); 059 assertEquals("Property client port should have been default from the HBase config", "2181", 060 props.getProperty("clientPort")); 061 } 062 063 @Test 064 public void testGetZooKeeperClusterKey() { 065 Configuration conf = HBaseConfiguration.create(); 066 conf.set(HConstants.ZOOKEEPER_QUORUM, "\tlocalhost\n"); 067 conf.set(HConstants.ZOOKEEPER_CLIENT_PORT, "3333"); 068 conf.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "hbase"); 069 String clusterKey = ZKConfig.getZooKeeperClusterKey(conf, "test"); 070 assertTrue(!clusterKey.contains("\t") && !clusterKey.contains("\n")); 071 assertEquals("localhost:3333:hbase,test", clusterKey); 072 } 073 074 @Test 075 public void testClusterKey() throws Exception { 076 testKey("server", 2181, "/hbase"); 077 testKey("server1,server2,server3", 2181, "/hbase"); 078 try { 079 ZKConfig.validateClusterKey("2181:/hbase"); 080 } catch (IOException ex) { 081 // OK 082 } 083 } 084 085 @Test 086 public void testClusterKeyWithMultiplePorts() throws Exception { 087 // server has different port than the default port 088 testKey("server1:2182", 2181, "/hbase", true); 089 // multiple servers have their own port 090 testKey("server1:2182,server2:2183,server3:2184", 2181, "/hbase", true); 091 // one server has no specified port, should use default port 092 testKey("server1:2182,server2,server3:2184", 2181, "/hbase", true); 093 // the last server has no specified port, should use default port 094 testKey("server1:2182,server2:2183,server3", 2181, "/hbase", true); 095 // multiple servers have no specified port, should use default port for those servers 096 testKey("server1:2182,server2,server3:2184,server4", 2181, "/hbase", true); 097 // same server, different ports 098 testKey("server1:2182,server1:2183,server1", 2181, "/hbase", true); 099 // mix of same server/different port and different server 100 testKey("server1:2182,server2:2183,server1", 2181, "/hbase", true); 101 } 102 103 @Test 104 public void testZooKeeperTlsProperties() { 105 // Arrange 106 Configuration conf = HBaseConfiguration.create(); 107 for (String p : ZOOKEEPER_CLIENT_TLS_PROPERTIES) { 108 conf.set(HConstants.ZK_CFG_PROPERTY_PREFIX + p, p); 109 String zkprop = "zookeeper." + p; 110 System.clearProperty(zkprop); 111 } 112 113 // Act 114 ZKClientConfig zkClientConfig = ZKConfig.getZKClientConfig(conf); 115 116 // Assert 117 for (String p : ZOOKEEPER_CLIENT_TLS_PROPERTIES) { 118 assertEquals("Invalid or unset system property: " + p, p, 119 zkClientConfig.getProperty("zookeeper." + p)); 120 } 121 } 122 123 @Test 124 public void testZooKeeperTlsPropertiesHQuorumPeer() { 125 // Arrange 126 Configuration conf = HBaseConfiguration.create(); 127 for (String p : ZOOKEEPER_CLIENT_TLS_PROPERTIES) { 128 conf.set(HConstants.ZK_CFG_PROPERTY_PREFIX + p, p); 129 String zkprop = "zookeeper." + p; 130 System.clearProperty(zkprop); 131 } 132 133 // Act 134 Properties zkProps = ZKConfig.makeZKProps(conf); 135 136 // Assert 137 for (String p : ZOOKEEPER_CLIENT_TLS_PROPERTIES) { 138 assertEquals("Invalid or unset system property: " + p, p, zkProps.getProperty(p)); 139 } 140 } 141 142 private void testKey(String ensemble, int port, String znode) throws IOException { 143 testKey(ensemble, port, znode, false); // not support multiple client ports 144 } 145 146 private void testKey(String ensemble, int port, String znode, Boolean multiplePortSupport) 147 throws IOException { 148 Configuration conf = new Configuration(); 149 String key = ensemble + ":" + port + ":" + znode; 150 String ensemble2 = null; 151 ZKConfig.ZKClusterKey zkClusterKey = ZKConfig.transformClusterKey(key); 152 if (multiplePortSupport) { 153 ensemble2 = ZKConfig.standardizeZKQuorumServerString(ensemble, Integer.toString(port)); 154 assertEquals(ensemble2, zkClusterKey.getQuorumString()); 155 } else { 156 assertEquals(ensemble, zkClusterKey.getQuorumString()); 157 } 158 assertEquals(port, zkClusterKey.getClientPort()); 159 assertEquals(znode, zkClusterKey.getZnodeParent()); 160 161 conf = HBaseConfiguration.createClusterConf(conf, key); 162 assertEquals(zkClusterKey.getQuorumString(), conf.get(HConstants.ZOOKEEPER_QUORUM)); 163 assertEquals(zkClusterKey.getClientPort(), conf.getInt(HConstants.ZOOKEEPER_CLIENT_PORT, -1)); 164 assertEquals(zkClusterKey.getZnodeParent(), conf.get(HConstants.ZOOKEEPER_ZNODE_PARENT)); 165 166 String reconstructedKey = ZKConfig.getZooKeeperClusterKey(conf); 167 if (multiplePortSupport) { 168 String key2 = ensemble2 + ":" + port + ":" + znode; 169 assertEquals(key2, reconstructedKey); 170 } else { 171 assertEquals(key, reconstructedKey); 172 } 173 } 174}