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.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
35
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
49 }
50
51 @Override
52 public void checkPermission(Permission perm, Object context) {
53
54 }
55
56 @Override
57 public void checkExit(int status) {
58 super.checkExit(status);
59 throw new ExitException(status);
60 }
61 }
62
63
64
65
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
84 exception = true;
85 }
86 assertTrue(exception);
87 assertEquals(-1, ZKUtil.checkExists(zkw, znode));
88 } finally {
89 htu.shutdownMiniZKCluster();
90 System.setSecurityManager(null);
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 }