001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.testing;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertFalse;
022import static org.junit.Assert.assertThrows;
023import static org.junit.Assert.assertTrue;
024
025import java.util.Collection;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.ServerName;
028import org.apache.hadoop.hbase.Waiter;
029import org.apache.hadoop.hbase.client.Admin;
030import org.apache.hadoop.hbase.client.Connection;
031import org.apache.hadoop.hbase.client.ConnectionFactory;
032import org.apache.hadoop.hbase.testclassification.LargeTests;
033import org.apache.hadoop.hbase.testclassification.MiscTests;
034import org.apache.hadoop.hbase.util.DNS;
035import org.apache.hadoop.hbase.util.DNS.ServerType;
036import org.junit.After;
037import org.junit.AfterClass;
038import org.junit.Before;
039import org.junit.BeforeClass;
040import org.junit.ClassRule;
041import org.junit.Test;
042import org.junit.experimental.categories.Category;
043
044import org.apache.hbase.thirdparty.com.google.common.collect.Iterables;
045import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
046
047@Category({ MiscTests.class, LargeTests.class })
048public class TestTestingHBaseCluster {
049
050  @ClassRule
051  public static final HBaseClassTestRule CLASS_RULE =
052    HBaseClassTestRule.forClass(TestTestingHBaseCluster.class);
053
054  private static TestingHBaseCluster CLUSTER;
055
056  private Connection conn;
057
058  private Admin admin;
059
060  @BeforeClass
061  public static void setUpBeforeClass() throws Exception {
062    CLUSTER = TestingHBaseCluster.create(TestingHBaseClusterOption.builder().numMasters(2)
063      .numRegionServers(3).numDataNodes(3).build());
064  }
065
066  @AfterClass
067  public static void tearDownAfterClass() throws Exception {
068    if (CLUSTER.isClusterRunning()) {
069      CLUSTER.stop();
070    }
071  }
072
073  @Before
074  public void setUp() throws Exception {
075    if (!CLUSTER.isClusterRunning()) {
076      CLUSTER.start();
077    }
078    if (!CLUSTER.isHBaseClusterRunning()) {
079      CLUSTER.startHBaseCluster();
080    }
081    conn = ConnectionFactory.createConnection(CLUSTER.getConf());
082    admin = conn.getAdmin();
083  }
084
085  @After
086  public void tearDown() throws Exception {
087    Closeables.close(admin, true);
088    Closeables.close(conn, true);
089    if (CLUSTER.isHBaseClusterRunning()) {
090      CLUSTER.stopHBaseCluster();
091    }
092  }
093
094  @Test
095  public void testStartStop() throws Exception {
096    assertTrue(CLUSTER.isClusterRunning());
097    assertTrue(CLUSTER.isHBaseClusterRunning());
098    assertThrows(IllegalStateException.class, () -> CLUSTER.start());
099    CLUSTER.stop();
100    assertFalse(CLUSTER.isClusterRunning());
101    assertFalse(CLUSTER.isHBaseClusterRunning());
102    assertThrows(IllegalStateException.class, () -> CLUSTER.stop());
103  }
104
105  @Test
106  public void testStartStopHBaseCluster() throws Exception {
107    assertTrue(CLUSTER.isHBaseClusterRunning());
108    assertThrows(IllegalStateException.class, () -> CLUSTER.startHBaseCluster());
109    CLUSTER.stopHBaseCluster();
110    assertTrue(CLUSTER.isClusterRunning());
111    assertFalse(CLUSTER.isHBaseClusterRunning());
112    assertThrows(IllegalStateException.class, () -> CLUSTER.stopHBaseCluster());
113  }
114
115  @Test
116  public void testStartStopMaster() throws Exception {
117    ServerName master = admin.getMaster();
118    CLUSTER.stopMaster(master).join();
119    // wait until the backup master becomes active master.
120    Waiter.waitFor(CLUSTER.getConf(), 30000, () -> {
121      try {
122        return admin.getMaster() != null;
123      } catch (Exception e) {
124        // ignore
125        return false;
126      }
127    });
128    // should have no backup master
129    assertTrue(admin.getBackupMasters().isEmpty());
130    CLUSTER.startMaster();
131    Waiter.waitFor(CLUSTER.getConf(), 30000, () -> !admin.getBackupMasters().isEmpty());
132    CLUSTER.startMaster(DNS.getHostname(CLUSTER.getConf(), ServerType.MASTER), 0);
133    Waiter.waitFor(CLUSTER.getConf(), 30000, () -> admin.getBackupMasters().size() == 2);
134  }
135
136  @Test
137  public void testStartStopRegionServer() throws Exception {
138    Collection<ServerName> regionServers = admin.getRegionServers();
139    assertEquals(3, regionServers.size());
140    CLUSTER.stopRegionServer(Iterables.get(regionServers, 0)).join();
141    Waiter.waitFor(CLUSTER.getConf(), 30000, () -> admin.getRegionServers().size() == 2);
142    CLUSTER.startRegionServer();
143    Waiter.waitFor(CLUSTER.getConf(), 30000, () -> admin.getRegionServers().size() == 3);
144    CLUSTER.startRegionServer(DNS.getHostname(CLUSTER.getConf(), ServerType.REGIONSERVER), 0);
145    Waiter.waitFor(CLUSTER.getConf(), 30000, () -> admin.getRegionServers().size() == 4);
146  }
147
148  @Test
149  public void testGetAddresses() throws Exception {
150    assertTrue(CLUSTER.getActiveMasterAddress().isPresent());
151    assertEquals(1, CLUSTER.getBackupMasterAddresses().size());
152    assertEquals(3, CLUSTER.getRegionServerAddresses().size());
153  }
154}