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