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;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.UUID;
24  
25  import org.apache.commons.io.FileUtils;
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.classification.InterfaceAudience;
29  import org.apache.hadoop.classification.InterfaceStability;
30  import org.apache.hadoop.conf.Configuration;
31  import org.apache.hadoop.fs.Path;
32  
33  /**
34   * Common helpers for testing HBase that do not depend on specific server/etc. things.
35   * @see {@link HBaseTestingUtility}
36   *
37   */
38  @InterfaceAudience.Public
39  @InterfaceStability.Unstable
40  public class HBaseCommonTestingUtility {
41    protected static final Log LOG = LogFactory.getLog(HBaseCommonTestingUtility.class);
42  
43    protected Configuration conf;
44  
45    public HBaseCommonTestingUtility() {
46      this(HBaseConfiguration.create());
47    }
48  
49    public HBaseCommonTestingUtility(Configuration conf) {
50      this.conf = conf;
51    }
52  
53    /**
54     * Returns this classes's instance of {@link Configuration}.
55     * @return Instance of Configuration.
56     */
57    public Configuration getConfiguration() {
58      return this.conf;
59    }
60  
61    /**
62     * System property key to get base test directory value
63     */
64    public static final String BASE_TEST_DIRECTORY_KEY =
65      "test.build.data.basedirectory";
66  
67    /**
68     * Default base directory for test output.
69     */
70    public static final String DEFAULT_BASE_TEST_DIRECTORY = "target/test-data";
71  
72    /** Directory where we put the data for this instance of HBaseTestingUtility*/
73    private File dataTestDir = null;
74  
75    /**
76     * @return Where to write test data on local filesystem, specific to
77     *  the test.  Useful for tests that do not use a cluster.
78     * Creates it if it does not exist already.
79     * @see #getTestFileSystem()
80     */
81    public Path getDataTestDir() {
82      if (this.dataTestDir == null){
83        setupDataTestDir();
84      }
85      return new Path(this.dataTestDir.getAbsolutePath());
86    }
87  
88    /**
89     * @param subdirName
90     * @return Path to a subdirectory named <code>subdirName</code> under
91     * {@link #getDataTestDir()}.
92     * Does *NOT* create it if it does not exist.
93     */
94    public Path getDataTestDir(final String subdirName) {
95      return new Path(getDataTestDir(), subdirName);
96    }
97  
98    /**
99     * Sets up a directory for a test to use.
100    *
101    * @return New directory path, if created.
102    */
103   protected Path setupDataTestDir() {
104     if (this.dataTestDir != null) {
105       LOG.warn("Data test dir already setup in " +
106         dataTestDir.getAbsolutePath());
107       return null;
108     }
109 
110     String randomStr = UUID.randomUUID().toString();
111     Path testPath = new Path(getBaseTestDir(), randomStr);
112 
113     this.dataTestDir = new File(testPath.toString()).getAbsoluteFile();
114     // Set this property so if mapreduce jobs run, they will use this as their home dir.
115     System.setProperty("test.build.dir", this.dataTestDir.toString());
116     if (deleteOnExit()) this.dataTestDir.deleteOnExit();
117 
118     createSubDir("hbase.local.dir", testPath, "hbase-local-dir");
119 
120     return testPath;
121   }
122 
123   protected void createSubDir(String propertyName, Path parent, String subDirName){
124     Path newPath= new Path(parent, subDirName);
125     File newDir = new File(newPath.toString()).getAbsoluteFile();
126     if (deleteOnExit()) newDir.deleteOnExit();
127     conf.set(propertyName, newDir.getAbsolutePath());
128   }
129 
130   /**
131    * @return True if we should delete testing dirs on exit.
132    */
133   boolean deleteOnExit() {
134     String v = System.getProperty("hbase.testing.preserve.testdir");
135     // Let default be true, to delete on exit.
136     return v == null? true: !Boolean.parseBoolean(v);
137   }
138 
139   /**
140    * @return True if we removed the test dirs
141    * @throws IOException
142    */
143   public boolean cleanupTestDir() throws IOException {
144     if (deleteDir(this.dataTestDir)) {
145       this.dataTestDir = null;
146       return true;
147     }
148     return false;
149   }
150 
151   /**
152    * @param subdir Test subdir name.
153    * @return True if we removed the test dir
154    * @throws IOException
155    */
156   boolean cleanupTestDir(final String subdir) throws IOException {
157     if (this.dataTestDir == null){
158       return false;
159     }
160     return deleteDir(new File(this.dataTestDir, subdir));
161   }
162 
163   /**
164    * @return Where to write test data on local filesystem; usually
165    * {@link #DEFAULT_BASE_TEST_DIRECTORY}
166    * Should not be used by the unit tests, hence its's private.
167    * Unit test will use a subdirectory of this directory.
168    * @see #setupDataTestDir()
169    * @see #getTestFileSystem()
170    */
171   private Path getBaseTestDir() {
172     String PathName = System.getProperty(
173       BASE_TEST_DIRECTORY_KEY, DEFAULT_BASE_TEST_DIRECTORY);
174 
175     return new Path(PathName);
176   }
177 
178   /**
179    * @param dir Directory to delete
180    * @return True if we deleted it.
181    * @throws IOException
182    */
183   boolean deleteDir(final File dir) throws IOException {
184     if (dir == null || !dir.exists()) {
185       return true;
186     }
187     try {
188       if (deleteOnExit()) FileUtils.deleteDirectory(dir);
189       return true;
190     } catch (IOException ex) {
191       LOG.warn("Failed to delete " + dir.getAbsolutePath());
192       return false;
193     }
194   }
195 }