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