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.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertNotNull;
022import static org.junit.jupiter.api.Assertions.assertNull;
023
024import java.io.IOException;
025import java.util.Properties;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.fs.FileSystem;
028import org.apache.hadoop.fs.Path;
029import org.apache.hadoop.hbase.HBaseConfiguration;
030import org.apache.hadoop.hbase.HBaseZKTestingUtil;
031import org.apache.hadoop.hbase.HConstants;
032import org.apache.hadoop.hbase.testclassification.SmallTests;
033import org.apache.hadoop.hbase.testclassification.ZKTests;
034import org.junit.jupiter.api.BeforeEach;
035import org.junit.jupiter.api.Tag;
036import org.junit.jupiter.api.Test;
037
038/**
039 * Test for HQuorumPeer.
040 */
041@Tag(ZKTests.TAG)
042@Tag(SmallTests.TAG)
043public class TestHQuorumPeer {
044
045  private static final HBaseZKTestingUtil TEST_UTIL = new HBaseZKTestingUtil();
046  private static int PORT_NO = 21818;
047  private Path dataDir;
048
049  @BeforeEach
050  public void setup() throws IOException {
051    // Set it to a non-standard port.
052    TEST_UTIL.getConfiguration().setInt(HConstants.ZOOKEEPER_CLIENT_PORT, PORT_NO);
053    this.dataDir = TEST_UTIL.getDataTestDir(this.getClass().getName());
054    FileSystem fs = FileSystem.get(TEST_UTIL.getConfiguration());
055    if (fs.exists(this.dataDir)) {
056      if (!fs.delete(this.dataDir, true)) {
057        throw new IOException("Failed cleanup of " + this.dataDir);
058      }
059    }
060    if (!fs.mkdirs(this.dataDir)) {
061      throw new IOException("Failed create of " + this.dataDir);
062    }
063  }
064
065  @Test
066  public void testMakeZKProps() {
067    Configuration conf = new Configuration(TEST_UTIL.getConfiguration());
068    conf.set(HConstants.ZOOKEEPER_DATA_DIR, this.dataDir.toString());
069    Properties properties = ZKConfig.makeZKProps(conf);
070    assertEquals(dataDir.toString(), (String) properties.get("dataDir"));
071    assertEquals(Integer.valueOf(PORT_NO), Integer.valueOf(properties.getProperty("clientPort")));
072    assertEquals("127.0.0.1:2888:3888", properties.get("server.0"));
073    assertNull(properties.get("server.1"));
074
075    String oldValue = conf.get(HConstants.ZOOKEEPER_QUORUM);
076    conf.set(HConstants.ZOOKEEPER_QUORUM, "a.foo.bar,b.foo.bar,c.foo.bar");
077    properties = ZKConfig.makeZKProps(conf);
078    assertEquals(dataDir.toString(), properties.get("dataDir"));
079    assertEquals(Integer.valueOf(PORT_NO), Integer.valueOf(properties.getProperty("clientPort")));
080    assertEquals("a.foo.bar:2888:3888", properties.get("server.0"));
081    assertEquals("b.foo.bar:2888:3888", properties.get("server.1"));
082    assertEquals("c.foo.bar:2888:3888", properties.get("server.2"));
083    assertNull(properties.get("server.3"));
084    conf.set(HConstants.ZOOKEEPER_QUORUM, oldValue);
085  }
086
087  @Test
088  public void testShouldAssignDefaultZookeeperClientPort() {
089    Configuration config = HBaseConfiguration.create();
090    config.clear();
091    Properties p = ZKConfig.makeZKProps(config);
092    assertNotNull(p);
093    assertEquals(2181, p.get("clientPort"));
094  }
095
096  @Test
097  public void testGetZKQuorumServersString() {
098    Configuration config = new Configuration(TEST_UTIL.getConfiguration());
099    config.setInt(HConstants.ZOOKEEPER_CLIENT_PORT, 8888);
100    config.set(HConstants.ZOOKEEPER_QUORUM, "foo:1234,bar:5678,baz,qux:9012");
101
102    String s = ZKConfig.getZKQuorumServersString(config);
103    assertEquals("foo:1234,bar:5678,baz:8888,qux:9012", s);
104  }
105}