View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.zookeeper;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.security.Permission;
25  
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.hadoop.hbase.*;
28  import org.apache.hadoop.hbase.testclassification.SmallTests;
29  import org.junit.Test;
30  import org.junit.experimental.categories.Category;
31  
32  @Category(SmallTests.class)
33  public class TestZooKeeperMainServer {
34    // ZKMS calls System.exit.  Catch the call and prevent exit using trick described up in
35    // http://stackoverflow.com/questions/309396/java-how-to-test-methods-that-call-system-exit
36    protected static class ExitException extends SecurityException {
37      private static final long serialVersionUID = 1L;
38      public final int status;
39      public ExitException(int status) {
40        super("There is no escape!");
41        this.status = status;
42      }
43    }
44  
45    private static class NoExitSecurityManager extends SecurityManager {
46      @Override
47      public void checkPermission(Permission perm) {
48        // allow anything.
49      }
50  
51      @Override
52      public void checkPermission(Permission perm, Object context) {
53        // allow anything.
54      }
55  
56      @Override
57      public void checkExit(int status) {
58        super.checkExit(status);
59        throw new ExitException(status);
60      }
61    }
62  
63    /**
64     * We need delete of a znode to work at least.
65     * @throws Exception
66     */
67    @Test
68    public void testCommandLineWorks() throws Exception {
69      System.setSecurityManager(new NoExitSecurityManager());
70      HBaseTestingUtility htu = new HBaseTestingUtility();
71      htu.getConfiguration().setInt(HConstants.ZK_SESSION_TIMEOUT, 1000);
72      htu.startMiniZKCluster();
73      try {
74        ZooKeeperWatcher zkw = htu.getZooKeeperWatcher();
75        String znode = "/testCommandLineWorks";
76        ZKUtil.createWithParents(zkw, znode, HConstants.EMPTY_BYTE_ARRAY);
77        ZKUtil.checkExists(zkw, znode);
78        boolean exception = false;
79        try {
80          ZooKeeperMainServer.main(new String [] {"-server",
81            "localhost:" + htu.getZkCluster().getClientPort(), "delete", znode});
82        } catch (ExitException ee) {
83          // ZKMS calls System.exit which should trigger this exception.
84          exception = true;
85        }
86        assertTrue(exception);
87        assertEquals(-1, ZKUtil.checkExists(zkw, znode));
88      } finally {
89        htu.shutdownMiniZKCluster();
90        System.setSecurityManager(null); // or save and restore original
91      }
92    }
93  
94    @Test
95    public void testHostPortParse() {
96      ZooKeeperMainServer parser = new ZooKeeperMainServer();
97      Configuration c = HBaseConfiguration.create();
98      assertEquals("localhost:" + c.get(HConstants.ZOOKEEPER_CLIENT_PORT), parser.parse(c));
99      final String port = "1234";
100     c.set(HConstants.ZOOKEEPER_CLIENT_PORT, port);
101     c.set("hbase.zookeeper.quorum", "example.com");
102     assertEquals("example.com:" + port, parser.parse(c));
103     c.set("hbase.zookeeper.quorum", "example1.com,example2.com,example3.com");
104     String ensemble = parser.parse(c);
105     assertTrue(port, ensemble.matches("(example[1-3]\\.com:1234,){2}example[1-3]\\.com:" + port));
106   }
107 }