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.junit.Test;
29  import org.junit.experimental.categories.Category;
30  
31  @Category(SmallTests.class)
32  public class TestZooKeeperMainServer {
33    // ZKMS calls System.exit.  Catch the call and prevent exit using trick described up in
34    // http://stackoverflow.com/questions/309396/java-how-to-test-methods-that-call-system-exit
35    protected static class ExitException extends SecurityException {
36      private static final long serialVersionUID = 1L;
37      public final int status;
38      public ExitException(int status) {
39        super("There is no escape!");
40        this.status = status;
41      }
42    }
43  
44    private static class NoExitSecurityManager extends SecurityManager {
45      @Override
46      public void checkPermission(Permission perm) {
47        // allow anything.
48      }
49  
50      @Override
51      public void checkPermission(Permission perm, Object context) {
52        // allow anything.
53      }
54  
55      @Override
56      public void checkExit(int status) {
57        super.checkExit(status);
58        throw new ExitException(status);
59      }
60    }
61  
62    /**
63     * We need delete of a znode to work at least.
64     * @throws Exception
65     */
66    @Test
67    public void testCommandLineWorks() throws Exception {
68      System.setSecurityManager(new NoExitSecurityManager());
69      HBaseTestingUtility htu = new HBaseTestingUtility();
70      htu.getConfiguration().setInt(HConstants.ZK_SESSION_TIMEOUT, 1000);
71      htu.startMiniZKCluster();
72      try {
73        ZooKeeperWatcher zkw = htu.getZooKeeperWatcher();
74        String znode = "/testCommandLineWorks";
75        ZKUtil.createWithParents(zkw, znode, HConstants.EMPTY_BYTE_ARRAY);
76        ZKUtil.checkExists(zkw, znode);
77        boolean exception = false;
78        try {
79          ZooKeeperMainServer.main(new String [] {"-server",
80            "localhost:" + htu.getZkCluster().getClientPort(), "delete", znode});
81        } catch (ExitException ee) {
82          // ZKMS calls System.exit which should trigger this exception.
83          exception = true;
84        }
85        assertTrue(exception);
86        assertEquals(-1, ZKUtil.checkExists(zkw, znode));
87      } finally {
88        htu.shutdownMiniZKCluster();
89        System.setSecurityManager(null); // or save and restore original
90      }
91    }
92  
93    @Test
94    public void testHostPortParse() {
95      ZooKeeperMainServer parser = new ZooKeeperMainServer();
96      Configuration c = HBaseConfiguration.create();
97      assertEquals("localhost:" + c.get(HConstants.ZOOKEEPER_CLIENT_PORT), parser.parse(c));
98      final String port = "1234";
99      c.set(HConstants.ZOOKEEPER_CLIENT_PORT, port);
100     c.set("hbase.zookeeper.quorum", "example.com");
101     assertEquals("example.com:" + port, parser.parse(c));
102     c.set("hbase.zookeeper.quorum", "example1.com,example2.com,example3.com");
103     String ensemble = parser.parse(c);
104     assertTrue(port, ensemble.matches("(example[1-3]\\.com:1234,){2}example[1-3]\\.com:" + port));
105   }
106 }