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.master;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021
022import java.io.IOException;
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hbase.HBaseTestingUtil;
025import org.apache.hadoop.hbase.HConstants;
026import org.apache.hadoop.hbase.ServerName;
027import org.apache.hadoop.hbase.client.Admin;
028import org.apache.hadoop.hbase.testclassification.MediumTests;
029import org.junit.jupiter.api.AfterAll;
030import org.junit.jupiter.api.BeforeAll;
031import org.junit.jupiter.api.Tag;
032import org.junit.jupiter.api.Test;
033
034@Tag(MediumTests.TAG)
035public class TestUnknownServers {
036
037  private static HBaseTestingUtil UTIL;
038  private static Admin ADMIN;
039  private final static int SLAVES = 1;
040  private static boolean IS_UNKNOWN_SERVER = true;
041
042  @BeforeAll
043  public static void setUpBeforeClass() throws Exception {
044    UTIL = new HBaseTestingUtil();
045    UTIL.getConfiguration().setClass(HConstants.MASTER_IMPL,
046      TestUnknownServers.HMasterForTest.class, HMaster.class);
047    UTIL.startMiniCluster(SLAVES);
048    ADMIN = UTIL.getAdmin();
049  }
050
051  @Test
052  public void testListUnknownServers() throws Exception {
053    assertEquals(ADMIN.listUnknownServers().size(), SLAVES);
054    IS_UNKNOWN_SERVER = false;
055    assertEquals(ADMIN.listUnknownServers().size(), 0);
056  }
057
058  @AfterAll
059  public static void tearDownAfterClass() throws Exception {
060    if (ADMIN != null) {
061      ADMIN.close();
062    }
063    if (UTIL != null) {
064      UTIL.shutdownMiniCluster();
065    }
066  }
067
068  public static final class HMasterForTest extends HMaster {
069
070    public HMasterForTest(Configuration conf) throws IOException {
071      super(conf);
072    }
073
074    @Override
075    protected ServerManager createServerManager(MasterServices master, RegionServerList storage)
076      throws IOException {
077      setupClusterConnection();
078      return new TestUnknownServers.ServerManagerForTest(master, storage);
079    }
080  }
081
082  private static final class ServerManagerForTest extends ServerManager {
083
084    public ServerManagerForTest(MasterServices master, RegionServerList storage) {
085      super(master, storage);
086    }
087
088    @Override
089    public boolean isServerUnknown(ServerName serverName) {
090      return IS_UNKNOWN_SERVER;
091    }
092  }
093}