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