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.FSDataOutputStream;
027import org.apache.hadoop.fs.FileSystem;
028import org.apache.hadoop.fs.Path;
029import org.apache.hadoop.hbase.HBaseClassTestRule;
030import org.apache.hadoop.hbase.HBaseCommonTestingUtility;
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 HBaseCommonTestingUtility htu;
049  private Configuration conf;
050
051  @Before
052  public void setUp() throws IOException {
053    htu = new HBaseCommonTestingUtility();
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  private void WriteDataToHDFS(FileSystem fs, Path file, int dataSize)
083    throws Exception {
084    FSDataOutputStream out = fs.create(file);
085    byte [] data = new byte[dataSize];
086    out.write(data, 0, dataSize);
087    out.close();
088  }
089
090  @Test
091  public void testSetWALRootDir() throws Exception {
092    Path p = new Path("file:///hbase/root");
093    CommonFSUtils.setWALRootDir(conf, p);
094    assertEquals(p.toString(), conf.get(CommonFSUtils.HBASE_WAL_DIR));
095  }
096
097  @Test
098  public void testGetWALRootDir() throws IOException {
099    Path root = new Path("file:///hbase/root");
100    Path walRoot = new Path("file:///hbase/logroot");
101    CommonFSUtils.setRootDir(conf, root);
102    assertEquals(root, CommonFSUtils.getRootDir(conf));
103    assertEquals(root, CommonFSUtils.getWALRootDir(conf));
104    CommonFSUtils.setWALRootDir(conf, walRoot);
105    assertEquals(walRoot, CommonFSUtils.getWALRootDir(conf));
106  }
107
108  @Test(expected=IllegalStateException.class)
109  public void testGetWALRootDirIllegalWALDir() throws IOException {
110    Path root = new Path("file:///hbase/root");
111    Path invalidWALDir = new Path("file:///hbase/root/logroot");
112    CommonFSUtils.setRootDir(conf, root);
113    CommonFSUtils.setWALRootDir(conf, invalidWALDir);
114    CommonFSUtils.getWALRootDir(conf);
115  }
116
117  @Test
118  public void testRemoveWALRootPath() throws Exception {
119    CommonFSUtils.setRootDir(conf, new Path("file:///user/hbase"));
120    Path testFile = new Path(CommonFSUtils.getRootDir(conf), "test/testfile");
121    Path tmpFile = new Path("file:///test/testfile");
122    assertEquals("test/testfile", CommonFSUtils.removeWALRootPath(testFile, conf));
123    assertEquals(tmpFile.toString(), CommonFSUtils.removeWALRootPath(tmpFile, conf));
124    CommonFSUtils.setWALRootDir(conf, new Path("file:///user/hbaseLogDir"));
125    assertEquals(testFile.toString(), CommonFSUtils.removeWALRootPath(testFile, conf));
126    Path logFile = new Path(CommonFSUtils.getWALRootDir(conf), "test/testlog");
127    assertEquals("test/testlog", CommonFSUtils.removeWALRootPath(logFile, conf));
128  }
129}