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