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.hbase.classification.InterfaceAudience;
29  import org.apache.hadoop.hbase.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 final Configuration conf;
44  
45    public HBaseCommonTestingUtility() {
46      this(null);
47    }
48  
49    public HBaseCommonTestingUtility(Configuration conf) {
50      this.conf = (conf == null ? HBaseConfiguration.create() : conf);
51    }
52  
53    /**
54     * Returns this classes's instance of {@link Configuration}.
55     *
56     * @return Instance of Configuration.
57     */
58    public Configuration getConfiguration() {
59      return this.conf;
60    }
61  
62    /**
63     * System property key to get base test directory value
64     */
65    public static final String BASE_TEST_DIRECTORY_KEY =
66        "test.build.data.basedirectory";
67  
68    /**
69     * Default base directory for test output.
70     */
71    public static final String DEFAULT_BASE_TEST_DIRECTORY = "target/test-data";
72  
73    /**
74     * Directory where we put the data for this instance of HBaseTestingUtility
75     */
76    private File dataTestDir = null;
77  
78    /**
79     * @return Where to write test data on local filesystem, specific to the test. Useful for tests
80     *    that do not use a cluster. Creates it if it does not exist already.
81     */
82    public Path getDataTestDir() {
83      if (this.dataTestDir == null) {
84        setupDataTestDir();
85      }
86      return new Path(this.dataTestDir.getAbsolutePath());
87    }
88  
89    /**
90     * @param subdirName the name of the subdirectory in the test data directory
91     * @return Path to a subdirectory named {code subdirName} under
92     *  {@link #getDataTestDir()}. 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 
117     if (deleteOnExit()) {
118       this.dataTestDir.deleteOnExit();
119     }
120 
121     createSubDir("hbase.local.dir", testPath, "hbase-local-dir");
122 
123     return testPath;
124   }
125 
126   protected void createSubDir(String propertyName, Path parent, String subDirName) {
127     Path newPath = new Path(parent, subDirName);
128     File newDir = new File(newPath.toString()).getAbsoluteFile();
129 
130     if (deleteOnExit()) {
131       newDir.deleteOnExit();
132     }
133 
134     conf.set(propertyName, newDir.getAbsolutePath());
135   }
136 
137   /**
138    * @return True if we should delete testing dirs on exit.
139    */
140   boolean deleteOnExit() {
141     String v = System.getProperty("hbase.testing.preserve.testdir");
142     // Let default be true, to delete on exit.
143     return v == null ? true : !Boolean.parseBoolean(v);
144   }
145 
146   /**
147    * @return True if we removed the test dirs
148    */
149   public boolean cleanupTestDir() {
150     if (deleteDir(this.dataTestDir)) {
151       this.dataTestDir = null;
152       return true;
153     }
154     return false;
155   }
156 
157   /**
158    * @param subdir Test subdir name.
159    * @return True if we removed the test dir
160    */
161   boolean cleanupTestDir(final String subdir) {
162     if (this.dataTestDir == null) {
163       return false;
164     }
165     return deleteDir(new File(this.dataTestDir, subdir));
166   }
167 
168   /**
169    * @return Where to write test data on local filesystem; usually
170    *    {@link #DEFAULT_BASE_TEST_DIRECTORY}
171    *    Should not be used by the unit tests, hence its's private.
172    *    Unit test will use a subdirectory of this directory.
173    * @see #setupDataTestDir()
174    */
175   private Path getBaseTestDir() {
176     String PathName = System.getProperty(
177         BASE_TEST_DIRECTORY_KEY, DEFAULT_BASE_TEST_DIRECTORY);
178 
179     return new Path(PathName);
180   }
181 
182   public Path getRandomDir() {
183     String randomStr = UUID.randomUUID().toString();
184     Path testPath = new Path(getBaseTestDir(), randomStr);
185     return testPath;
186   }
187 
188   /**
189    * @param dir Directory to delete
190    * @return True if we deleted it.
191    */
192   boolean deleteDir(final File dir) {
193     if (dir == null || !dir.exists()) {
194       return true;
195     }
196     int ntries = 0;
197     do {
198       ntries += 1;
199       try {
200         if (deleteOnExit()) {
201           FileUtils.deleteDirectory(dir);
202         }
203 
204         return true;
205       } catch (IOException ex) {
206         LOG.warn("Failed to delete " + dir.getAbsolutePath());
207       } catch (IllegalArgumentException ex) {
208         LOG.warn("Failed to delete " + dir.getAbsolutePath(), ex);
209       }
210     } while (ntries < 30);
211 
212     return false;
213   }
214 }