View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.util;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertFalse;
23  import static org.junit.Assert.assertNotEquals;
24  import static org.junit.Assert.assertTrue;
25  
26  import java.io.ByteArrayOutputStream;
27  import java.io.IOException;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.hadoop.conf.Configuration;
32  import org.apache.hadoop.fs.FSDataOutputStream;
33  import org.apache.hadoop.fs.FileSystem;
34  import org.apache.hadoop.fs.Path;
35  import org.apache.hadoop.hbase.HBaseCommonTestingUtility;
36  import org.apache.hadoop.hbase.testclassification.MediumTests;
37  import org.apache.hadoop.hbase.testclassification.MiscTests;
38  import org.junit.Before;
39  import org.junit.Test;
40  import org.junit.experimental.categories.Category;
41  
42  /**
43   * Test {@link CommonFSUtils}.
44   */
45  @Category({MiscTests.class, MediumTests.class})
46  public class TestCommonFSUtils {
47    private static final Log LOG = LogFactory.getLog(TestCommonFSUtils.class);
48  
49    private HBaseCommonTestingUtility htu;
50    private Configuration conf;
51  
52    @Before
53    public void setUp() throws IOException {
54      htu = new HBaseCommonTestingUtility();
55      conf = htu.getConfiguration();
56    }
57  
58    /**
59     * Test path compare and prefix checking.
60     */
61    @Test
62    public void testMatchingTail() throws IOException {
63      Path rootdir = htu.getDataTestDir();
64      final FileSystem fs = rootdir.getFileSystem(conf);
65      assertTrue(rootdir.depth() > 1);
66      Path partPath = new Path("a", "b");
67      Path fullPath = new Path(rootdir, partPath);
68      Path fullyQualifiedPath = fs.makeQualified(fullPath);
69      assertFalse(CommonFSUtils.isMatchingTail(fullPath, partPath));
70      assertFalse(CommonFSUtils.isMatchingTail(fullPath, partPath.toString()));
71      assertTrue(CommonFSUtils.isStartingWithPath(rootdir, fullPath.toString()));
72      assertTrue(CommonFSUtils.isStartingWithPath(fullyQualifiedPath, fullPath.toString()));
73      assertFalse(CommonFSUtils.isStartingWithPath(rootdir, partPath.toString()));
74      assertFalse(CommonFSUtils.isMatchingTail(fullyQualifiedPath, partPath));
75      assertTrue(CommonFSUtils.isMatchingTail(fullyQualifiedPath, fullPath));
76      assertTrue(CommonFSUtils.isMatchingTail(fullyQualifiedPath, fullPath.toString()));
77      assertTrue(CommonFSUtils.isMatchingTail(fullyQualifiedPath, fs.makeQualified(fullPath)));
78      assertTrue(CommonFSUtils.isStartingWithPath(rootdir, fullyQualifiedPath.toString()));
79      assertFalse(CommonFSUtils.isMatchingTail(fullPath, new Path("x")));
80      assertFalse(CommonFSUtils.isMatchingTail(new Path("x"), fullPath));
81    }
82  
83    private void WriteDataToHDFS(FileSystem fs, Path file, int dataSize)
84      throws Exception {
85      FSDataOutputStream out = fs.create(file);
86      byte [] data = new byte[dataSize];
87      out.write(data, 0, dataSize);
88      out.close();
89    }
90  
91    @Test
92    public void testSetWALRootDir() throws Exception {
93      Path p = new Path("file:///hbase/root");
94      CommonFSUtils.setWALRootDir(conf, p);
95      assertEquals(p.toString(), conf.get(CommonFSUtils.HBASE_WAL_DIR));
96    }
97  
98    @Test
99    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(CommonFSUtils.getRootDir(conf), root);
104     assertEquals(CommonFSUtils.getWALRootDir(conf), root);
105     CommonFSUtils.setWALRootDir(conf, walRoot);
106     assertEquals(CommonFSUtils.getWALRootDir(conf), walRoot);
107   }
108 
109   @Test(expected=IllegalStateException.class)
110   public void testGetWALRootDirIllegalWALDir() throws IOException {
111     Path root = new Path("file:///hbase/root");
112     Path invalidWALDir = new Path("file:///hbase/root/logroot");
113     CommonFSUtils.setRootDir(conf, root);
114     CommonFSUtils.setWALRootDir(conf, invalidWALDir);
115     CommonFSUtils.getWALRootDir(conf);
116   }
117 
118   @Test
119   public void testRemoveWALRootPath() throws Exception {
120     CommonFSUtils.setRootDir(conf, new Path("file:///user/hbase"));
121     Path testFile = new Path(CommonFSUtils.getRootDir(conf), "test/testfile");
122     Path tmpFile = new Path("file:///test/testfile");
123     assertEquals(CommonFSUtils.removeWALRootPath(testFile, conf), "test/testfile");
124     assertEquals(CommonFSUtils.removeWALRootPath(tmpFile, conf), tmpFile.toString());
125     CommonFSUtils.setWALRootDir(conf, new Path("file:///user/hbaseLogDir"));
126     assertEquals(CommonFSUtils.removeWALRootPath(testFile, conf), testFile.toString());
127     Path logFile = new Path(CommonFSUtils.getWALRootDir(conf), "test/testlog");
128     assertEquals(CommonFSUtils.removeWALRootPath(logFile, conf), "test/testlog");
129   }
130 
131   @Test(expected=NullPointerException.class)
132   public void streamCapabilitiesDoesNotAllowNullStream() {
133     CommonFSUtils.hasCapability(null, "hopefully any string");
134   }
135 
136   private static final boolean STREAM_CAPABILITIES_IS_PRESENT;
137   static {
138     boolean tmp = false;
139     try {
140       Class.forName("org.apache.hadoop.fs.StreamCapabilities");
141       tmp = true;
142       LOG.debug("Test thought StreamCapabilities class was present.");
143     } catch (ClassNotFoundException exception) {
144       LOG.debug("Test didn't think StreamCapabilities class was present.");
145     } finally {
146       STREAM_CAPABILITIES_IS_PRESENT = tmp;
147     }
148   }
149 
150   @Test
151   public void checkStreamCapabilitiesOnKnownNoopStream() throws IOException {
152     FSDataOutputStream stream = new FSDataOutputStream(new ByteArrayOutputStream(), null);
153     assertNotEquals("We expect our dummy FSDOS to claim capabilities iff the StreamCapabilities " +
154         "class is not defined.", STREAM_CAPABILITIES_IS_PRESENT,
155         CommonFSUtils.hasCapability(stream, "hsync"));
156     assertNotEquals("We expect our dummy FSDOS to claim capabilities iff the StreamCapabilities " +
157         "class is not defined.", STREAM_CAPABILITIES_IS_PRESENT,
158         CommonFSUtils.hasCapability(stream, "hflush"));
159     assertNotEquals("We expect our dummy FSDOS to claim capabilities iff the StreamCapabilities " +
160         "class is not defined.", STREAM_CAPABILITIES_IS_PRESENT,
161         CommonFSUtils.hasCapability(stream, "a capability that hopefully no filesystem will " +
162             "implement."));
163   }
164 }