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.junit.ClassRule;
033import org.junit.Test;
034import org.junit.experimental.categories.Category;
035
036import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableSet;
037
038@Category({ MiscTests.class, SmallTests.class })
039public class TestZKConfig {
040
041  @ClassRule
042  public static final HBaseClassTestRule CLASS_RULE =
043    HBaseClassTestRule.forClass(TestZKConfig.class);
044
045  /** Supported ZooKeeper client TLS properties */
046  private static final Set<String> ZOOKEEPER_CLIENT_TLS_PROPERTIES = ImmutableSet.of(
047    "client.secure", "clientCnxnSocket", "ssl.keyStore.location", "ssl.keyStore.password",
048    "ssl.keyStore.passwordPath", "ssl.keyStore.type", "ssl.trustStore.location",
049    "ssl.trustStore.password", "ssl.trustStore.passwordPath", "ssl.trustStore.type");
050
051  @Test
052  public void testZKConfigLoading() throws Exception {
053    Configuration conf = HBaseConfiguration.create();
054    // Test that we read only from the config instance
055    // (i.e. via hbase-default.xml and hbase-site.xml)
056    conf.setInt(HConstants.ZOOKEEPER_CLIENT_PORT, 2181);
057    Properties props = ZKConfig.makeZKProps(conf);
058    assertEquals("Property client port should have been default from the HBase config", "2181",
059      props.getProperty("clientPort"));
060  }
061
062  @Test
063  public void testGetZooKeeperClusterKey() {
064    Configuration conf = HBaseConfiguration.create();
065    conf.set(HConstants.ZOOKEEPER_QUORUM, "\tlocalhost\n");
066    conf.set(HConstants.ZOOKEEPER_CLIENT_PORT, "3333");
067    conf.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "hbase");
068    String clusterKey = ZKConfig.getZooKeeperClusterKey(conf, "test");
069    assertTrue(!clusterKey.contains("\t") && !clusterKey.contains("\n"));
070    assertEquals("localhost:3333:hbase,test", clusterKey);
071  }
072
073  @Test
074  public void testClusterKey() throws Exception {
075    testKey("server", 2181, "/hbase");
076    testKey("server1,server2,server3", 2181, "/hbase");
077    try {
078      ZKConfig.validateClusterKey("2181:/hbase");
079    } catch (IOException ex) {
080      // OK
081    }
082  }
083
084  @Test
085  public void testClusterKeyWithMultiplePorts() throws Exception {
086    // server has different port than the default port
087    testKey("server1:2182", 2181, "/hbase", true);
088    // multiple servers have their own port
089    testKey("server1:2182,server2:2183,server3:2184", 2181, "/hbase", true);
090    // one server has no specified port, should use default port
091    testKey("server1:2182,server2,server3:2184", 2181, "/hbase", true);
092    // the last server has no specified port, should use default port
093    testKey("server1:2182,server2:2183,server3", 2181, "/hbase", true);
094    // multiple servers have no specified port, should use default port for those servers
095    testKey("server1:2182,server2,server3:2184,server4", 2181, "/hbase", true);
096    // same server, different ports
097    testKey("server1:2182,server1:2183,server1", 2181, "/hbase", true);
098    // mix of same server/different port and different server
099    testKey("server1:2182,server2:2183,server1", 2181, "/hbase", true);
100  }
101
102  @Test
103  public void testZooKeeperTlsPropertiesClient() {
104    // Arrange
105    Configuration conf = HBaseConfiguration.create();
106    for (String p : ZOOKEEPER_CLIENT_TLS_PROPERTIES) {
107      conf.set(HConstants.ZK_CFG_PROPERTY_PREFIX + p, p);
108      String zkprop = "zookeeper." + p;
109      System.clearProperty(zkprop);
110    }
111
112    // Act
113    ZKConfig.getClientZKQuorumServersString(conf);
114
115    // Assert
116    for (String p : ZOOKEEPER_CLIENT_TLS_PROPERTIES) {
117      String zkprop = "zookeeper." + p;
118      assertEquals("Invalid or unset system property: " + zkprop, p, System.getProperty(zkprop));
119      System.clearProperty(zkprop);
120    }
121  }
122
123  @Test
124  public void testZooKeeperTlsPropertiesServer() {
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    ZKConfig.getZKQuorumServersString(conf);
135
136    // Assert
137    for (String p : ZOOKEEPER_CLIENT_TLS_PROPERTIES) {
138      String zkprop = "zookeeper." + p;
139      assertEquals("Invalid or unset system property: " + zkprop, p, System.getProperty(zkprop));
140      System.clearProperty(zkprop);
141    }
142  }
143
144  @Test
145  public void testZooKeeperPropertiesDoesntOverwriteSystem() {
146    // Arrange
147    System.setProperty("zookeeper.a.b.c", "foo");
148    Configuration conf = HBaseConfiguration.create();
149    conf.set(HConstants.ZK_CFG_PROPERTY_PREFIX + "a.b.c", "bar");
150
151    // Act
152    ZKConfig.getZKQuorumServersString(conf);
153
154    // Assert
155    assertEquals("foo", System.getProperty("zookeeper.a.b.c"));
156    System.clearProperty("zookeeper.a.b.c");
157  }
158
159  private void testKey(String ensemble, int port, String znode) throws IOException {
160    testKey(ensemble, port, znode, false); // not support multiple client ports
161  }
162
163  private void testKey(String ensemble, int port, String znode, Boolean multiplePortSupport)
164    throws IOException {
165    Configuration conf = new Configuration();
166    String key = ensemble + ":" + port + ":" + znode;
167    String ensemble2 = null;
168    ZKConfig.ZKClusterKey zkClusterKey = ZKConfig.transformClusterKey(key);
169    if (multiplePortSupport) {
170      ensemble2 = ZKConfig.standardizeZKQuorumServerString(ensemble, Integer.toString(port));
171      assertEquals(ensemble2, zkClusterKey.getQuorumString());
172    } else {
173      assertEquals(ensemble, zkClusterKey.getQuorumString());
174    }
175    assertEquals(port, zkClusterKey.getClientPort());
176    assertEquals(znode, zkClusterKey.getZnodeParent());
177
178    conf = HBaseConfiguration.createClusterConf(conf, key);
179    assertEquals(zkClusterKey.getQuorumString(), conf.get(HConstants.ZOOKEEPER_QUORUM));
180    assertEquals(zkClusterKey.getClientPort(), conf.getInt(HConstants.ZOOKEEPER_CLIENT_PORT, -1));
181    assertEquals(zkClusterKey.getZnodeParent(), conf.get(HConstants.ZOOKEEPER_ZNODE_PARENT));
182
183    String reconstructedKey = ZKConfig.getZooKeeperClusterKey(conf);
184    if (multiplePortSupport) {
185      String key2 = ensemble2 + ":" + port + ":" + znode;
186      assertEquals(key2, reconstructedKey);
187    } else {
188      assertEquals(key, reconstructedKey);
189    }
190  }
191}