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 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     *
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
80     * the test.  Useful for tests that do not use a cluster.
81     * Creates it if it does not exist already.
82     */
83    public Path getDataTestDir() {
84      if (this.dataTestDir == null) {
85        setupDataTestDir();
86      }
87      return new Path(this.dataTestDir.getAbsolutePath());
88    }
89  
90    /**
91     * @param subdirName
92     * @return Path to a subdirectory named <code>subdirName</code> under
93     * {@link #getDataTestDir()}.
94     * Does *NOT* create it if it does not exist.
95     */
96    public Path getDataTestDir(final String subdirName) {
97      return new Path(getDataTestDir(), subdirName);
98    }
99  
100   /**
101    * Sets up a directory for a test to use.
102    *
103    * @return New directory path, if created.
104    */
105   protected Path setupDataTestDir() {
106     if (this.dataTestDir != null) {
107       LOG.warn("Data test dir already setup in " +
108           dataTestDir.getAbsolutePath());
109       return null;
110     }
111 
112     String randomStr = UUID.randomUUID().toString();
113     Path testPath = new Path(getBaseTestDir(), randomStr);
114 
115     this.dataTestDir = new File(testPath.toString()).getAbsoluteFile();
116     // Set this property so if mapreduce jobs run, they will use this as their home dir.
117     System.setProperty("test.build.dir", this.dataTestDir.toString());
118     if (deleteOnExit()) this.dataTestDir.deleteOnExit();
119 
120     createSubDir("hbase.local.dir", testPath, "hbase-local-dir");
121 
122     return testPath;
123   }
124 
125   protected void createSubDir(String propertyName, Path parent, String subDirName) {
126     Path newPath = new Path(parent, subDirName);
127     File newDir = new File(newPath.toString()).getAbsoluteFile();
128     if (deleteOnExit()) newDir.deleteOnExit();
129     conf.set(propertyName, newDir.getAbsolutePath());
130   }
131 
132   /**
133    * @return True if we should delete testing dirs on exit.
134    */
135   boolean deleteOnExit() {
136     String v = System.getProperty("hbase.testing.preserve.testdir");
137     // Let default be true, to delete on exit.
138     return v == null ? true : !Boolean.parseBoolean(v);
139   }
140 
141   /**
142    * @return True if we removed the test dirs
143    * @throws IOException
144    */
145   public boolean cleanupTestDir() throws IOException {
146     if (deleteDir(this.dataTestDir)) {
147       this.dataTestDir = null;
148       return true;
149     }
150     return false;
151   }
152 
153   /**
154    * @param subdir Test subdir name.
155    * @return True if we removed the test dir
156    * @throws IOException
157    */
158   boolean cleanupTestDir(final String subdir) throws IOException {
159     if (this.dataTestDir == null) {
160       return false;
161     }
162     return deleteDir(new File(this.dataTestDir, subdir));
163   }
164 
165   /**
166    * @return Where to write test data on local filesystem; usually
167    * {@link #DEFAULT_BASE_TEST_DIRECTORY}
168    * Should not be used by the unit tests, hence its's private.
169    * Unit test will use a subdirectory of this directory.
170    * @see #setupDataTestDir()
171    */
172   private Path getBaseTestDir() {
173     String PathName = System.getProperty(
174         BASE_TEST_DIRECTORY_KEY, DEFAULT_BASE_TEST_DIRECTORY);
175 
176     return new Path(PathName);
177   }
178 
179   /**
180    * @param dir Directory to delete
181    * @return True if we deleted it.
182    * @throws IOException
183    */
184   boolean deleteDir(final File dir) throws IOException {
185     if (dir == null || !dir.exists()) {
186       return true;
187     }
188     int ntries = 0;
189     do {
190       ntries += 1;
191       try {
192         if (deleteOnExit()) FileUtils.deleteDirectory(dir);
193         return true;
194       } catch (IOException ex) {
195         LOG.warn("Failed to delete " + dir.getAbsolutePath());
196       } catch (IllegalArgumentException ex) {
197         LOG.warn("Failed to delete " + dir.getAbsolutePath(), ex);
198       }
199     } while (ntries < 30);
200     return ntries < 30;
201   }
202 }