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