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