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