1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.client.replication;
19  
20  import java.util.concurrent.atomic.AtomicBoolean;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.fs.FileSystem;
26  import org.apache.hadoop.fs.Path;
27  import org.apache.hadoop.hbase.*;
28  import org.apache.hadoop.hbase.replication.regionserver.ReplicationSourceManager;
29  import org.junit.BeforeClass;
30  import org.junit.Test;
31  import org.junit.experimental.categories.Category;
32  
33  import static org.junit.Assert.fail;
34  import static org.junit.Assert.assertEquals;
35  
36  /**
37   * Unit testing of ReplicationAdmin
38   */
39  @Category(MediumTests.class)
40  public class TestReplicationAdmin {
41  
42    private static final Log LOG =
43        LogFactory.getLog(TestReplicationAdmin.class);
44    private final static HBaseTestingUtility TEST_UTIL =
45        new HBaseTestingUtility();
46  
47    private final String ID_ONE = "1";
48    private final String KEY_ONE = "127.0.0.1:2181:/hbase";
49    private final String ID_SECOND = "2";
50    private final String KEY_SECOND = "127.0.0.1:2181:/hbase2";
51  
52    private static ReplicationSourceManager manager;
53    private static ReplicationAdmin admin;
54    private static AtomicBoolean replicating = new AtomicBoolean(true);
55  
56    /**
57     * @throws java.lang.Exception
58     */
59    @BeforeClass
60    public static void setUpBeforeClass() throws Exception {
61      TEST_UTIL.startMiniZKCluster();
62      Configuration conf = TEST_UTIL.getConfiguration();
63      conf.setBoolean(HConstants.REPLICATION_ENABLE_KEY, true);
64      admin = new ReplicationAdmin(conf);
65      Path oldLogDir = new Path(TEST_UTIL.getDataTestDir(),
66          HConstants.HREGION_OLDLOGDIR_NAME);
67      Path logDir = new Path(TEST_UTIL.getDataTestDir(),
68          HConstants.HREGION_LOGDIR_NAME);
69      manager = new ReplicationSourceManager(admin.getReplicationZk(), conf,
70          // The following stopper never stops so that we can respond
71          // to zk notification
72          new Stoppable() {
73            @Override
74            public void stop(String why) {}
75            @Override
76            public boolean isStopped() {return false;}
77          }, FileSystem.get(conf), replicating, logDir, oldLogDir);
78    }
79  
80    /**
81     * Simple testing of adding and removing peers, basically shows that
82     * all interactions with ZK work
83     * @throws Exception
84     */
85    @Test
86    public void testAddRemovePeer() throws Exception {
87      assertEquals(0, manager.getSources().size());
88      // Add a valid peer
89      admin.addPeer(ID_ONE, KEY_ONE);
90      // try adding the same (fails)
91      try {
92        admin.addPeer(ID_ONE, KEY_ONE);
93      } catch (IllegalArgumentException iae) {
94        // OK!
95      }
96      assertEquals(1, admin.getPeersCount());
97      // Try to remove an inexisting peer
98      try {
99        admin.removePeer(ID_SECOND);
100       fail();
101     } catch (IllegalArgumentException iae) {
102       // OK!
103     }
104     assertEquals(1, admin.getPeersCount());
105     // Add a second since multi-slave is supported
106     try {
107       admin.addPeer(ID_SECOND, KEY_SECOND);
108     } catch (IllegalStateException iae) {
109       fail();
110       // OK!
111     }
112     assertEquals(2, admin.getPeersCount());
113     // Remove the first peer we added
114     admin.removePeer(ID_ONE);
115     assertEquals(1, admin.getPeersCount());
116   }
117 
118   @org.junit.Rule
119   public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
120     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
121 }
122