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;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import org.apache.hadoop.fs.FileSystem;
024import org.apache.hadoop.fs.Path;
025import org.apache.hadoop.hbase.client.Put;
026import org.apache.hadoop.hbase.client.Table;
027import org.apache.hadoop.hbase.testclassification.MediumTests;
028import org.apache.hadoop.hbase.util.Bytes;
029import org.apache.hadoop.hdfs.MiniDFSCluster;
030import org.junit.ClassRule;
031import org.junit.Rule;
032import org.junit.Test;
033import org.junit.experimental.categories.Category;
034import org.junit.rules.TestName;
035
036/**
037 * Test that an HBase cluster can run on top of an existing MiniDfsCluster
038 */
039@Category(MediumTests.class)
040public class TestHBaseOnOtherDfsCluster {
041
042  @ClassRule
043  public static final HBaseClassTestRule CLASS_RULE =
044    HBaseClassTestRule.forClass(TestHBaseOnOtherDfsCluster.class);
045
046  @Rule
047  public TestName name = new TestName();
048
049  @Test
050  public void testOveralyOnOtherCluster() throws Exception {
051    // just run HDFS
052    HBaseTestingUtility util1 = new HBaseTestingUtility();
053    MiniDFSCluster dfs = util1.startMiniDFSCluster(1);
054
055    // run HBase on that HDFS
056    HBaseTestingUtility util2 = new HBaseTestingUtility();
057    // set the dfs
058    util2.setDFSCluster(dfs, false);
059    util2.startMiniCluster();
060
061    // ensure that they are pointed at the same place
062    FileSystem fs = dfs.getFileSystem();
063    FileSystem targetFs = util2.getDFSCluster().getFileSystem();
064    assertFsSameUri(fs, targetFs);
065
066    fs = FileSystem.get(util1.getConfiguration());
067    targetFs = FileSystem.get(util2.getConfiguration());
068    assertFsSameUri(fs, targetFs);
069
070    Path randomFile = new Path("/" + util1.getRandomUUID());
071    assertTrue(targetFs.createNewFile(randomFile));
072    assertTrue(fs.exists(randomFile));
073
074    // do a simple create/write to ensure the cluster works as expected
075    byte[] family = Bytes.toBytes("testfamily");
076    final TableName tablename = TableName.valueOf(name.getMethodName());
077    Table table = util2.createTable(tablename, family);
078    Put p = new Put(new byte[] { 1, 2, 3 });
079    p.addColumn(family, null, new byte[] { 1 });
080    table.put(p);
081
082    // shutdown and make sure cleanly shutting down
083    util2.shutdownMiniCluster();
084    util1.shutdownMiniDFSCluster();
085  }
086
087  private void assertFsSameUri(FileSystem sourceFs, FileSystem targetFs) {
088    Path source = new Path(sourceFs.getUri());
089    Path target = new Path(targetFs.getUri());
090    assertEquals(source, target);
091  }
092}