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.client;
019
020import static org.junit.Assert.fail;
021
022import java.net.UnknownHostException;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.HBaseTestingUtility;
025import org.apache.hadoop.hbase.HConstants;
026import org.apache.hadoop.hbase.ServerName;
027import org.apache.hadoop.hbase.testclassification.ClientTests;
028import org.apache.hadoop.hbase.testclassification.MediumTests;
029import org.junit.AfterClass;
030import org.junit.BeforeClass;
031import org.junit.ClassRule;
032import org.junit.Test;
033import org.junit.experimental.categories.Category;
034
035/**
036 * Tests that we fail fast when hostname resolution is not working and do not cache
037 * unresolved InetSocketAddresses.
038 */
039@Category({MediumTests.class, ClientTests.class})
040public class TestCIBadHostname {
041
042  @ClassRule
043  public static final HBaseClassTestRule CLASS_RULE =
044      HBaseClassTestRule.forClass(TestCIBadHostname.class);
045
046  private static HBaseTestingUtility testUtil;
047  private static ConnectionImplementation conn;
048
049  @BeforeClass
050  public static void setupBeforeClass() throws Exception {
051    testUtil = HBaseTestingUtility.createLocalHTU();
052    testUtil.startMiniCluster();
053    conn = (ConnectionImplementation) testUtil.getConnection();
054  }
055
056  @AfterClass
057  public static void teardownAfterClass() throws Exception {
058    conn.close();
059    testUtil.shutdownMiniCluster();
060  }
061
062  @Test(expected = UnknownHostException.class)
063  public void testGetAdminBadHostname() throws Exception {
064    // verify that we can get an instance with the cluster hostname
065    ServerName master = testUtil.getHBaseCluster().getMaster().getServerName();
066    try {
067      conn.getAdmin(master);
068    } catch (UnknownHostException uhe) {
069      fail("Obtaining admin to the cluster master should have succeeded");
070    }
071
072    // test that we fail to get a client to an unresolvable hostname, which
073    // means it won't be cached
074    ServerName badHost =
075        ServerName.valueOf("unknownhost.invalid:" + HConstants.DEFAULT_MASTER_PORT,
076        System.currentTimeMillis());
077    conn.getAdmin(badHost);
078    fail("Obtaining admin to unresolvable hostname should have failed");
079  }
080
081  @Test(expected = UnknownHostException.class)
082  public void testGetClientBadHostname() throws Exception {
083    // verify that we can get an instance with the cluster hostname
084    ServerName rs = testUtil.getHBaseCluster().getRegionServer(0).getServerName();
085    try {
086      conn.getClient(rs);
087    } catch (UnknownHostException uhe) {
088      fail("Obtaining client to the cluster regionserver should have succeeded");
089    }
090
091    // test that we fail to get a client to an unresolvable hostname, which
092    // means it won't be cached
093    ServerName badHost =
094        ServerName.valueOf("unknownhost.invalid:" + HConstants.DEFAULT_REGIONSERVER_PORT,
095        System.currentTimeMillis());
096    conn.getAdmin(badHost);
097    fail("Obtaining client to unresolvable hostname should have failed");
098  }
099}