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    }
110
111    // Act
112    ZKClientConfig zkClientConfig = ZKConfig.getZKClientConfig(conf);
113
114    // Assert
115    for (String p : ZOOKEEPER_CLIENT_TLS_PROPERTIES) {
116      assertEquals("Invalid or unset system property: " + p, p, zkClientConfig.getProperty(p));
117    }
118  }
119
120  private void testKey(String ensemble, int port, String znode) throws IOException {
121    testKey(ensemble, port, znode, false); // not support multiple client ports
122  }
123
124  private void testKey(String ensemble, int port, String znode, Boolean multiplePortSupport)
125    throws IOException {
126    Configuration conf = new Configuration();
127    String key = ensemble + ":" + port + ":" + znode;
128    String ensemble2 = null;
129    ZKConfig.ZKClusterKey zkClusterKey = ZKConfig.transformClusterKey(key);
130    if (multiplePortSupport) {
131      ensemble2 = ZKConfig.standardizeZKQuorumServerString(ensemble, Integer.toString(port));
132      assertEquals(ensemble2, zkClusterKey.getQuorumString());
133    } else {
134      assertEquals(ensemble, zkClusterKey.getQuorumString());
135    }
136    assertEquals(port, zkClusterKey.getClientPort());
137    assertEquals(znode, zkClusterKey.getZnodeParent());
138
139    conf = HBaseConfiguration.createClusterConf(conf, key);
140    assertEquals(zkClusterKey.getQuorumString(), conf.get(HConstants.ZOOKEEPER_QUORUM));
141    assertEquals(zkClusterKey.getClientPort(), conf.getInt(HConstants.ZOOKEEPER_CLIENT_PORT, -1));
142    assertEquals(zkClusterKey.getZnodeParent(), conf.get(HConstants.ZOOKEEPER_ZNODE_PARENT));
143
144    String reconstructedKey = ZKConfig.getZooKeeperClusterKey(conf);
145    if (multiplePortSupport) {
146      String key2 = ensemble2 + ":" + port + ":" + znode;
147      assertEquals(key2, reconstructedKey);
148    } else {
149      assertEquals(key, reconstructedKey);
150    }
151  }
152}