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