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.util;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertFalse;
022import static org.junit.jupiter.api.Assertions.assertThrows;
023import static org.junit.jupiter.api.Assertions.assertTrue;
024
025import java.io.IOException;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.fs.FileSystem;
028import org.apache.hadoop.fs.Path;
029import org.apache.hadoop.hbase.HBaseCommonTestingUtil;
030import org.apache.hadoop.hbase.HConstants;
031import org.apache.hadoop.hbase.testclassification.MiscTests;
032import org.apache.hadoop.hbase.testclassification.SmallTests;
033import org.junit.jupiter.api.BeforeEach;
034import org.junit.jupiter.api.Tag;
035import org.junit.jupiter.api.Test;
036
037/**
038 * Test {@link CommonFSUtils}.
039 */
040@Tag(MiscTests.TAG)
041@Tag(SmallTests.TAG)
042public class TestCommonFSUtils {
043
044  private HBaseCommonTestingUtil htu;
045  private Configuration conf;
046
047  @BeforeEach
048  public void setUp() throws IOException {
049    htu = new HBaseCommonTestingUtil();
050    conf = htu.getConfiguration();
051  }
052
053  /**
054   * Test path compare and prefix checking.
055   */
056  @Test
057  public void testMatchingTail() throws IOException {
058    Path rootdir = htu.getDataTestDir();
059    final FileSystem fs = rootdir.getFileSystem(conf);
060    assertTrue(rootdir.depth() > 1);
061    Path partPath = new Path("a", "b");
062    Path fullPath = new Path(rootdir, partPath);
063    Path fullyQualifiedPath = fs.makeQualified(fullPath);
064    assertFalse(CommonFSUtils.isMatchingTail(fullPath, partPath));
065    assertFalse(CommonFSUtils.isMatchingTail(fullPath, partPath.toString()));
066    assertTrue(CommonFSUtils.isStartingWithPath(rootdir, fullPath.toString()));
067    assertTrue(CommonFSUtils.isStartingWithPath(fullyQualifiedPath, fullPath.toString()));
068    assertFalse(CommonFSUtils.isStartingWithPath(rootdir, partPath.toString()));
069    assertFalse(CommonFSUtils.isMatchingTail(fullyQualifiedPath, partPath));
070    assertTrue(CommonFSUtils.isMatchingTail(fullyQualifiedPath, fullPath));
071    assertTrue(CommonFSUtils.isMatchingTail(fullyQualifiedPath, fullPath.toString()));
072    assertTrue(CommonFSUtils.isMatchingTail(fullyQualifiedPath, fs.makeQualified(fullPath)));
073    assertTrue(CommonFSUtils.isStartingWithPath(rootdir, fullyQualifiedPath.toString()));
074    assertFalse(CommonFSUtils.isMatchingTail(fullPath, new Path("x")));
075    assertFalse(CommonFSUtils.isMatchingTail(new Path("x"), fullPath));
076  }
077
078  @Test
079  public void testSetWALRootDir() throws Exception {
080    Path p = new Path("file:///hbase/root");
081    CommonFSUtils.setWALRootDir(conf, p);
082    assertEquals(p.toString(), conf.get(CommonFSUtils.HBASE_WAL_DIR));
083  }
084
085  @Test
086  public void testGetWALRootDir() throws IOException {
087    Path root = new Path("file:///hbase/root");
088    Path walRoot = new Path("file:///hbase/logroot");
089    CommonFSUtils.setRootDir(conf, root);
090    assertEquals(root, CommonFSUtils.getRootDir(conf));
091    assertEquals(root, CommonFSUtils.getWALRootDir(conf));
092    CommonFSUtils.setWALRootDir(conf, walRoot);
093    assertEquals(walRoot, CommonFSUtils.getWALRootDir(conf));
094  }
095
096  @Test
097  public void testGetWALRootDirUsingUri() throws IOException {
098    Path root = new Path("file:///hbase/root");
099    conf.set(HConstants.HBASE_DIR, root.toString());
100    Path walRoot = new Path("file:///hbase/logroot");
101    conf.set(CommonFSUtils.HBASE_WAL_DIR, walRoot.toString());
102    String walDirUri = CommonFSUtils.getDirUri(conf, walRoot);
103    String rootDirUri = CommonFSUtils.getDirUri(conf, root);
104    CommonFSUtils.setFsDefault(this.conf, rootDirUri);
105    CommonFSUtils.setRootDir(conf, root);
106    assertEquals(root, CommonFSUtils.getRootDir(conf));
107    CommonFSUtils.setFsDefault(this.conf, walDirUri);
108    CommonFSUtils.setWALRootDir(conf, walRoot);
109    assertEquals(walRoot, CommonFSUtils.getWALRootDir(conf));
110  }
111
112  @Test
113  public void testGetWALRootDirIllegalWALDir() throws IOException {
114    Path root = new Path("file:///hbase/root");
115    Path invalidWALDir = new Path("file:///hbase/root/logroot");
116    CommonFSUtils.setRootDir(conf, root);
117    CommonFSUtils.setWALRootDir(conf, invalidWALDir);
118    assertThrows(IllegalStateException.class, () -> {
119      CommonFSUtils.getWALRootDir(conf);
120    });
121  }
122
123  @Test
124  public void testRemoveWALRootPath() throws Exception {
125    CommonFSUtils.setRootDir(conf, new Path("file:///user/hbase"));
126    Path testFile = new Path(CommonFSUtils.getRootDir(conf), "test/testfile");
127    Path tmpFile = new Path("file:///test/testfile");
128    assertEquals("test/testfile", CommonFSUtils.removeWALRootPath(testFile, conf));
129    assertEquals(tmpFile.toString(), CommonFSUtils.removeWALRootPath(tmpFile, conf));
130    CommonFSUtils.setWALRootDir(conf, new Path("file:///user/hbaseLogDir"));
131    assertEquals(testFile.toString(), CommonFSUtils.removeWALRootPath(testFile, conf));
132    Path logFile = new Path(CommonFSUtils.getWALRootDir(conf), "test/testlog");
133    assertEquals("test/testlog", CommonFSUtils.removeWALRootPath(logFile, conf));
134  }
135}