1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
34
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
48 }
49
50 @Override
51 public void checkPermission(Permission perm, Object context) {
52
53 }
54
55 @Override
56 public void checkExit(int status) {
57 super.checkExit(status);
58 throw new ExitException(status);
59 }
60 }
61
62
63
64
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
83 exception = true;
84 }
85 assertTrue(exception);
86 assertEquals(-1, ZKUtil.checkExists(zkw, znode));
87 } finally {
88 htu.shutdownMiniZKCluster();
89 System.setSecurityManager(null);
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 }