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