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.security.Permission; 024import org.apache.hadoop.conf.Configuration; 025import org.apache.hadoop.hbase.HBaseClassTestRule; 026import org.apache.hadoop.hbase.HBaseConfiguration; 027import org.apache.hadoop.hbase.HBaseZKTestingUtility; 028import org.apache.hadoop.hbase.HConstants; 029import org.apache.hadoop.hbase.testclassification.SmallTests; 030import org.apache.hadoop.hbase.testclassification.ZKTests; 031import org.junit.ClassRule; 032import org.junit.Test; 033import org.junit.experimental.categories.Category; 034 035@Category({ ZKTests.class, SmallTests.class }) 036public class TestZKMainServer { 037 038 @ClassRule 039 public static final HBaseClassTestRule CLASS_RULE = 040 HBaseClassTestRule.forClass(TestZKMainServer.class); 041 042 // ZKMS calls System.exit. Catch the call and prevent exit using trick described up in 043 // http://stackoverflow.com/questions/309396/java-how-to-test-methods-that-call-system-exit 044 protected static class ExitException extends SecurityException { 045 private static final long serialVersionUID = 1L; 046 private final int status; 047 public ExitException(int status) { 048 super("There is no escape!"); 049 this.status = status; 050 } 051 } 052 053 private static class NoExitSecurityManager extends SecurityManager { 054 @Override 055 public void checkPermission(Permission perm) { 056 // allow anything. 057 } 058 059 @Override 060 public void checkPermission(Permission perm, Object context) { 061 // allow anything. 062 } 063 064 @Override 065 public void checkExit(int status) { 066 super.checkExit(status); 067 throw new ExitException(status); 068 } 069 } 070 071 /** 072 * We need delete of a znode to work at least. 073 */ 074 @Test 075 public void testCommandLineWorks() throws Exception { 076 System.setSecurityManager(new NoExitSecurityManager()); 077 HBaseZKTestingUtility htu = new HBaseZKTestingUtility(); 078 // Make it long so for sure succeeds. 079 htu.getConfiguration().setInt(HConstants.ZK_SESSION_TIMEOUT, 30000); 080 htu.startMiniZKCluster(); 081 try { 082 ZKWatcher zkw = htu.getZooKeeperWatcher(); 083 String znode = "/testCommandLineWorks"; 084 ZKUtil.createWithParents(zkw, znode, HConstants.EMPTY_BYTE_ARRAY); 085 ZKUtil.checkExists(zkw, znode); 086 boolean exception = false; 087 try { 088 ZKMainServer.main(new String [] {"-server", 089 "localhost:" + htu.getZkCluster().getClientPort(), "delete", znode}); 090 } catch (ExitException ee) { 091 // ZKMS calls System.exit which should trigger this exception. 092 exception = true; 093 } 094 assertTrue(exception); 095 assertEquals(-1, ZKUtil.checkExists(zkw, znode)); 096 } finally { 097 htu.shutdownMiniZKCluster(); 098 System.setSecurityManager(null); // or save and restore original 099 } 100 } 101 102 @Test 103 public void testHostPortParse() { 104 ZKMainServer parser = new ZKMainServer(); 105 Configuration c = HBaseConfiguration.create(); 106 assertEquals("localhost:" + c.get(HConstants.ZOOKEEPER_CLIENT_PORT), parser.parse(c)); 107 final String port = "1234"; 108 c.set(HConstants.ZOOKEEPER_CLIENT_PORT, port); 109 c.set("hbase.zookeeper.quorum", "example.com"); 110 assertEquals("example.com:" + port, parser.parse(c)); 111 c.set("hbase.zookeeper.quorum", "example1.com,example2.com,example3.com"); 112 String ensemble = parser.parse(c); 113 assertTrue(port, ensemble.matches("(example[1-3]\\.com:1234,){2}example[1-3]\\.com:" + port)); 114 115 // multiple servers with its own port 116 c.set("hbase.zookeeper.quorum", "example1.com:5678,example2.com:9012,example3.com:3456"); 117 ensemble = parser.parse(c); 118 assertEquals(ensemble, "example1.com:5678,example2.com:9012,example3.com:3456"); 119 120 // some servers without its own port, which will be assigned the default client port 121 c.set("hbase.zookeeper.quorum", "example1.com:5678,example2.com:9012,example3.com"); 122 ensemble = parser.parse(c); 123 assertEquals(ensemble, "example1.com:5678,example2.com:9012,example3.com:" + port); 124 } 125}