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.assertNotEquals;
023import static org.junit.Assert.assertTrue;
024
025import java.io.ByteArrayOutputStream;
026import java.io.IOException;
027import org.apache.hadoop.conf.Configuration;
028import org.apache.hadoop.fs.FSDataOutputStream;
029import org.apache.hadoop.fs.FileSystem;
030import org.apache.hadoop.fs.Path;
031import org.apache.hadoop.hbase.HBaseClassTestRule;
032import org.apache.hadoop.hbase.HBaseCommonTestingUtility;
033import org.apache.hadoop.hbase.testclassification.MediumTests;
034import org.apache.hadoop.hbase.testclassification.MiscTests;
035import org.junit.Before;
036import org.junit.ClassRule;
037import org.junit.Test;
038import org.junit.experimental.categories.Category;
039import org.slf4j.Logger;
040import org.slf4j.LoggerFactory;
041
042/**
043 * Test {@link CommonFSUtils}.
044 */
045@Category({MiscTests.class, MediumTests.class})
046public class TestCommonFSUtils {
047
048  @ClassRule
049  public static final HBaseClassTestRule CLASS_RULE =
050      HBaseClassTestRule.forClass(TestCommonFSUtils.class);
051
052  private static final Logger LOG = LoggerFactory.getLogger(TestCommonFSUtils.class);
053
054  private HBaseCommonTestingUtility htu;
055  private Configuration conf;
056
057  @Before
058  public void setUp() throws IOException {
059    htu = new HBaseCommonTestingUtility();
060    conf = htu.getConfiguration();
061  }
062
063  /**
064   * Test path compare and prefix checking.
065   */
066  @Test
067  public void testMatchingTail() throws IOException {
068    Path rootdir = htu.getDataTestDir();
069    final FileSystem fs = rootdir.getFileSystem(conf);
070    assertTrue(rootdir.depth() > 1);
071    Path partPath = new Path("a", "b");
072    Path fullPath = new Path(rootdir, partPath);
073    Path fullyQualifiedPath = fs.makeQualified(fullPath);
074    assertFalse(CommonFSUtils.isMatchingTail(fullPath, partPath));
075    assertFalse(CommonFSUtils.isMatchingTail(fullPath, partPath.toString()));
076    assertTrue(CommonFSUtils.isStartingWithPath(rootdir, fullPath.toString()));
077    assertTrue(CommonFSUtils.isStartingWithPath(fullyQualifiedPath, fullPath.toString()));
078    assertFalse(CommonFSUtils.isStartingWithPath(rootdir, partPath.toString()));
079    assertFalse(CommonFSUtils.isMatchingTail(fullyQualifiedPath, partPath));
080    assertTrue(CommonFSUtils.isMatchingTail(fullyQualifiedPath, fullPath));
081    assertTrue(CommonFSUtils.isMatchingTail(fullyQualifiedPath, fullPath.toString()));
082    assertTrue(CommonFSUtils.isMatchingTail(fullyQualifiedPath, fs.makeQualified(fullPath)));
083    assertTrue(CommonFSUtils.isStartingWithPath(rootdir, fullyQualifiedPath.toString()));
084    assertFalse(CommonFSUtils.isMatchingTail(fullPath, new Path("x")));
085    assertFalse(CommonFSUtils.isMatchingTail(new Path("x"), fullPath));
086  }
087
088  private void WriteDataToHDFS(FileSystem fs, Path file, int dataSize)
089    throws Exception {
090    FSDataOutputStream out = fs.create(file);
091    byte [] data = new byte[dataSize];
092    out.write(data, 0, dataSize);
093    out.close();
094  }
095
096  @Test
097  public void testSetWALRootDir() throws Exception {
098    Path p = new Path("file:///hbase/root");
099    CommonFSUtils.setWALRootDir(conf, p);
100    assertEquals(p.toString(), conf.get(CommonFSUtils.HBASE_WAL_DIR));
101  }
102
103  @Test
104  public void testGetWALRootDir() throws IOException {
105    Path root = new Path("file:///hbase/root");
106    Path walRoot = new Path("file:///hbase/logroot");
107    CommonFSUtils.setRootDir(conf, root);
108    assertEquals(root, CommonFSUtils.getRootDir(conf));
109    assertEquals(root, CommonFSUtils.getWALRootDir(conf));
110    CommonFSUtils.setWALRootDir(conf, walRoot);
111    assertEquals(walRoot, CommonFSUtils.getWALRootDir(conf));
112  }
113
114  @Test(expected=IllegalStateException.class)
115  public void testGetWALRootDirIllegalWALDir() throws IOException {
116    Path root = new Path("file:///hbase/root");
117    Path invalidWALDir = new Path("file:///hbase/root/logroot");
118    CommonFSUtils.setRootDir(conf, root);
119    CommonFSUtils.setWALRootDir(conf, invalidWALDir);
120    CommonFSUtils.getWALRootDir(conf);
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
136  @Test(expected=NullPointerException.class)
137  public void streamCapabilitiesDoesNotAllowNullStream() {
138    CommonFSUtils.hasCapability(null, "hopefully any string");
139  }
140
141  private static final boolean STREAM_CAPABILITIES_IS_PRESENT;
142  static {
143    boolean tmp = false;
144    try {
145      Class.forName("org.apache.hadoop.fs.StreamCapabilities");
146      tmp = true;
147      LOG.debug("Test thought StreamCapabilities class was present.");
148    } catch (ClassNotFoundException exception) {
149      LOG.debug("Test didn't think StreamCapabilities class was present.");
150    } finally {
151      STREAM_CAPABILITIES_IS_PRESENT = tmp;
152    }
153  }
154
155  @Test
156  public void checkStreamCapabilitiesOnKnownNoopStream() throws IOException {
157    FSDataOutputStream stream = new FSDataOutputStream(new ByteArrayOutputStream(), null);
158    assertNotEquals("We expect our dummy FSDOS to claim capabilities iff the StreamCapabilities " +
159        "class is not defined.", STREAM_CAPABILITIES_IS_PRESENT,
160        CommonFSUtils.hasCapability(stream, "hsync"));
161    assertNotEquals("We expect our dummy FSDOS to claim capabilities iff the StreamCapabilities " +
162        "class is not defined.", STREAM_CAPABILITIES_IS_PRESENT,
163        CommonFSUtils.hasCapability(stream, "hflush"));
164    assertNotEquals("We expect our dummy FSDOS to claim capabilities iff the StreamCapabilities " +
165        "class is not defined.", STREAM_CAPABILITIES_IS_PRESENT,
166        CommonFSUtils.hasCapability(stream, "a capability that hopefully no filesystem will " +
167            "implement."));
168  }
169}