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