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.assertSame;
023
024import java.io.IOException;
025import java.util.List;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.ServerName;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.client.Admin;
030import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
031import org.apache.hadoop.hbase.client.Connection;
032import org.apache.hadoop.hbase.client.ConnectionFactory;
033import org.apache.hadoop.hbase.client.RegionInfo;
034import org.apache.hadoop.hbase.client.RegionInfoBuilder;
035import org.apache.hadoop.hbase.client.RegionLocator;
036import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
037import org.apache.hadoop.hbase.regionserver.OnlineRegions;
038import org.apache.hadoop.hbase.regionserver.Region;
039import org.apache.hadoop.hbase.testclassification.LargeTests;
040import org.apache.hadoop.hbase.testclassification.MiscTests;
041import org.apache.hadoop.hbase.util.Bytes;
042import org.junit.AfterClass;
043import org.junit.BeforeClass;
044import org.junit.ClassRule;
045import org.junit.Test;
046import org.junit.experimental.categories.Category;
047
048import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
049
050@Category({ MiscTests.class, LargeTests.class })
051public class TestTestingHBaseClusterImplForCPs {
052
053  @ClassRule
054  public static final HBaseClassTestRule CLASS_RULE =
055    HBaseClassTestRule.forClass(TestTestingHBaseClusterImplForCPs.class);
056
057  private static TestingHBaseCluster CLUSTER;
058
059  private static TableName NAME = TableName.valueOf("test");
060
061  private static byte[] CF = Bytes.toBytes("cf");
062
063  private static Connection CONN;
064
065  private static Admin ADMIN;
066
067  @BeforeClass
068  public static void setUpBeforeClass() throws Exception {
069    CLUSTER = TestingHBaseCluster.create(TestingHBaseClusterOption.builder().numMasters(2)
070      .numRegionServers(3).numDataNodes(3).build());
071    CLUSTER.start();
072    CONN = ConnectionFactory.createConnection(CLUSTER.getConf());
073    ADMIN = CONN.getAdmin();
074    ADMIN.createTable(TableDescriptorBuilder.newBuilder(NAME)
075      .setColumnFamily(ColumnFamilyDescriptorBuilder.of(CF)).build());
076    ADMIN.balancerSwitch(false, true);
077  }
078
079  @AfterClass
080  public static void tearDownAfterClass() throws Exception {
081    Closeables.close(ADMIN, true);
082    Closeables.close(CONN, true);
083    if (CLUSTER.isClusterRunning()) {
084      CLUSTER.stop();
085    }
086  }
087
088  @Test
089  public void testGetRegion() throws IOException {
090    List<RegionInfo> infos = ADMIN.getRegions(NAME);
091    assertEquals(1, infos.size());
092    RegionInfo info = infos.get(0);
093    Region region = CLUSTER.getRegion(info).get();
094    ServerName loc;
095    try (RegionLocator locator = CONN.getRegionLocator(NAME)) {
096      loc = locator.getRegionLocation(info.getStartKey()).getServerName();
097    }
098    OnlineRegions onlineRegionsInterface = CLUSTER.getOnlineRegionsInterface(loc).get();
099    List<? extends Region> regions = onlineRegionsInterface.getRegions(NAME);
100    assertEquals(1, regions.size());
101    assertSame(region, regions.get(0));
102
103    assertFalse(CLUSTER
104      .getRegion(RegionInfoBuilder.newBuilder(TableName.valueOf("whatever")).build()).isPresent());
105    assertFalse(CLUSTER.getOnlineRegionsInterface(ServerName.valueOf("whatever,1,1")).isPresent());
106  }
107}