1   package org.apache.hadoop.hbase.client.replication;
2   
3   import java.util.concurrent.atomic.AtomicBoolean;
4   
5   import org.apache.commons.logging.Log;
6   import org.apache.commons.logging.LogFactory;
7   import org.apache.hadoop.conf.Configuration;
8   import org.apache.hadoop.fs.FileSystem;
9   import org.apache.hadoop.fs.Path;
10  import org.apache.hadoop.hbase.HBaseTestingUtility;
11  import org.apache.hadoop.hbase.HConstants;
12  import org.apache.hadoop.hbase.replication.regionserver.ReplicationSourceManager;
13  import org.junit.BeforeClass;
14  import org.junit.Test;
15  
16  import static org.junit.Assert.fail;
17  import static org.junit.Assert.assertEquals;
18  
19  /**
20   * Unit testing of ReplicationAdmin
21   */
22  public class TestReplicationAdmin {
23  
24    private static final Log LOG =
25        LogFactory.getLog(TestReplicationAdmin.class);
26    private final static HBaseTestingUtility TEST_UTIL =
27        new HBaseTestingUtility();
28  
29    private final String ID_ONE = "1";
30    private final String KEY_ONE = "127.0.0.1:2181:/hbase";
31    private final String ID_SECOND = "2";
32    private final String KEY_SECOND = "127.0.0.1:2181:/hbase2";
33  
34    private static ReplicationSourceManager manager;
35    private static ReplicationAdmin admin;
36    private static AtomicBoolean replicating = new AtomicBoolean(true);
37  
38    /**
39     * @throws java.lang.Exception
40     */
41    @BeforeClass
42    public static void setUpBeforeClass() throws Exception {
43      TEST_UTIL.startMiniZKCluster();
44      Configuration conf = TEST_UTIL.getConfiguration();
45      conf.setBoolean(HConstants.REPLICATION_ENABLE_KEY, true);
46      admin = new ReplicationAdmin(conf);
47      Path oldLogDir = new Path(TEST_UTIL.getTestDir(),
48          HConstants.HREGION_OLDLOGDIR_NAME);
49      Path logDir = new Path(TEST_UTIL.getTestDir(),
50          HConstants.HREGION_LOGDIR_NAME);
51      manager = new ReplicationSourceManager(admin.getReplicationZk(),
52          conf, null, FileSystem.get(conf), replicating, logDir, oldLogDir);
53    }
54  
55    /**
56     * Simple testing of adding and removing peers, basically shows that
57     * all interactions with ZK work
58     * @throws Exception
59     */
60    @Test
61    public void testAddRemovePeer() throws Exception {
62      assertEquals(0, manager.getSources().size());
63      // Add a valid peer
64      admin.addPeer(ID_ONE, KEY_ONE);
65      // try adding the same (fails)
66      try {
67        admin.addPeer(ID_ONE, KEY_ONE);
68      } catch (IllegalArgumentException iae) {
69        // OK!
70      }
71      assertEquals(1, admin.getPeersCount());
72      // Try to remove an inexisting peer
73      try {
74        admin.removePeer(ID_SECOND);
75        fail();
76      } catch (IllegalArgumentException iae) {
77        // OK!
78      }
79      assertEquals(1, admin.getPeersCount());
80      // Add a second, returns illegal since multi-slave isn't supported
81      try {
82        admin.addPeer(ID_SECOND, KEY_SECOND);
83        fail();
84      } catch (IllegalStateException iae) {
85        // OK!
86      }
87      assertEquals(1, admin.getPeersCount());
88      // Remove the first peer we added
89      admin.removePeer(ID_ONE);
90      assertEquals(0, admin.getPeersCount());
91    }
92  }