View Javadoc

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 org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.hadoop.conf.Configuration;
23  import org.apache.hadoop.hbase.*;
24  import org.junit.BeforeClass;
25  import org.junit.Test;
26  import org.junit.experimental.categories.Category;
27  
28  import static org.junit.Assert.fail;
29  import static org.junit.Assert.assertEquals;
30  import static org.junit.Assert.assertTrue;
31  import static org.junit.Assert.assertFalse;
32  
33  /**
34   * Unit testing of ReplicationAdmin
35   */
36  @Category(MediumTests.class)
37  public class TestReplicationAdmin {
38  
39    private static final Log LOG =
40        LogFactory.getLog(TestReplicationAdmin.class);
41    private final static HBaseTestingUtility TEST_UTIL =
42        new HBaseTestingUtility();
43  
44    private final String ID_ONE = "1";
45    private final String KEY_ONE = "127.0.0.1:2181:/hbase";
46    private final String ID_SECOND = "2";
47    private final String KEY_SECOND = "127.0.0.1:2181:/hbase2";
48  
49    private static ReplicationAdmin admin;
50  
51    /**
52     * @throws java.lang.Exception
53     */
54    @BeforeClass
55    public static void setUpBeforeClass() throws Exception {
56      TEST_UTIL.startMiniZKCluster();
57      Configuration conf = TEST_UTIL.getConfiguration();
58      conf.setBoolean(HConstants.REPLICATION_ENABLE_KEY, HConstants.REPLICATION_ENABLE_DEFAULT);
59      admin = new ReplicationAdmin(conf);
60    }
61  
62    /**
63     * Simple testing of adding and removing peers, basically shows that
64     * all interactions with ZK work
65     * @throws Exception
66     */
67    @Test
68    public void testAddRemovePeer() throws Exception {
69      // Add a valid peer
70      admin.addPeer(ID_ONE, KEY_ONE);
71      // try adding the same (fails)
72      try {
73        admin.addPeer(ID_ONE, KEY_ONE);
74      } catch (IllegalArgumentException iae) {
75        // OK!
76      }
77      assertEquals(1, admin.getPeersCount());
78      // Try to remove an inexisting peer
79      try {
80        admin.removePeer(ID_SECOND);
81        fail();
82      } catch (IllegalArgumentException iae) {
83        // OK!
84      }
85      assertEquals(1, admin.getPeersCount());
86      // Add a second since multi-slave is supported
87      try {
88        admin.addPeer(ID_SECOND, KEY_SECOND);
89      } catch (IllegalStateException iae) {
90        fail();
91      }
92      assertEquals(2, admin.getPeersCount());
93      // Remove the first peer we added
94      admin.removePeer(ID_ONE);
95      assertEquals(1, admin.getPeersCount());
96      admin.removePeer(ID_SECOND);
97      assertEquals(0, admin.getPeersCount());
98    }
99  
100   /**
101    * basic checks that when we add a peer that it is enabled, and that we can disable
102    * @throws Exception
103    */
104   @Test
105   public void testEnableDisable() throws Exception {
106     admin.addPeer(ID_ONE, KEY_ONE);
107     assertEquals(1, admin.getPeersCount());
108     assertTrue(admin.getPeerState(ID_ONE));
109     admin.disablePeer(ID_ONE);
110 
111     assertFalse(admin.getPeerState(ID_ONE));
112     try {
113       admin.getPeerState(ID_SECOND);
114     } catch (IllegalArgumentException iae) {
115       // OK!
116     }
117     admin.removePeer(ID_ONE);
118   }
119 
120 }
121