1 package org.broadinstitute.hellbender.testutils;
2 
3 import org.apache.hadoop.conf.Configuration;
4 import org.apache.hadoop.fs.Path;
5 import org.apache.hadoop.hdfs.MiniDFSCluster;
6 import org.testng.Assert;
7 
8 import java.io.File;
9 import java.io.IOException;
10 import java.util.UUID;
11 
12 /**
13  * Utilities to help manage a {@link MiniDFSCluster} for use in tests.
14  */
15 public final class MiniClusterUtils {
16 
17     /**
18      * @return a new empty cluster with the given Configuration
19      * @throws IOException
20      */
getMiniCluster(Configuration otherConf)21     public static MiniDFSCluster getMiniCluster(Configuration otherConf) throws IOException {
22         final File baseDir = BaseTest.createTempDir("minicluster_storage");
23         final Configuration configuration;
24         if (otherConf != null) {
25             configuration = new Configuration(otherConf);
26         } else {
27             configuration = new Configuration();
28         }
29         configuration.set(MiniDFSCluster.HDFS_MINIDFS_BASEDIR, baseDir.getAbsolutePath());
30         return new MiniDFSCluster.Builder(configuration).build();
31     }
32 
33     /**
34      * @return a new empty cluster
35      * @throws IOException
36      */
getMiniCluster()37     public static MiniDFSCluster getMiniCluster() throws IOException {
38         return getMiniCluster(null);
39     }
40 
41     /**
42      * @return a unique path in the filesystem on the given cluster
43      * @throws IOException
44      */
getTempPath(MiniDFSCluster cluster, String prefix, String extension)45     public static Path getTempPath(MiniDFSCluster cluster, String prefix, String extension) throws IOException {
46         return new Path(cluster.getFileSystem().getWorkingDirectory(), prefix + "_" + UUID.randomUUID() + extension);
47     }
48 
49     /**
50      * @return path to the working directory in the given cluster
51      * @throws IOException
52      */
getWorkingDir(MiniDFSCluster cluster)53     public static Path getWorkingDir(MiniDFSCluster cluster) throws IOException {
54         return cluster.getFileSystem().getWorkingDirectory();
55     }
56 
57     /**
58      * shut down the cluster
59      */
stopCluster(MiniDFSCluster cluster)60     public static void stopCluster(MiniDFSCluster cluster){
61         if(cluster != null){
62             cluster.shutdown(true);
63         }
64     }
65 
66     /**
67      * Create a new isolated {@link MiniDFSCluster}, run a {@link MiniClusterTest} on it and then shut it down.
68      * @param test a function to run on the cluster, this should indicate success or failure by using {@link Assert}
69      * @throws Exception
70      */
runOnIsolatedMiniCluster(MiniClusterTest test)71     public static void runOnIsolatedMiniCluster(MiniClusterTest test) throws Exception {
72         MiniDFSCluster cluster = null;
73         try {
74             cluster = getMiniCluster();
75             test.test(cluster);
76         } finally {
77             stopCluster(cluster);
78         }
79     }
80 
81     /**
82      * An interface for writing tests that run on a minicluster.
83      * Implementers should write test to make use of the passed in cluster
84      */
85     @FunctionalInterface
86     public interface MiniClusterTest {
87         /**
88          * A test to be run using an hdfs MiniCluster
89          * It's alright for this to make destructive changes to the cluster since it is given it's own isolated setup.
90          *
91          * Test failure is indicated use standard {@link Assert} methods
92          *
93          * @param cluster an isolated MiniDFSCluster instance
94          * @throws Exception in order to allow any checked exception to be thrown
95          */
test(MiniDFSCluster cluster)96         void test(MiniDFSCluster cluster) throws Exception;
97     }
98 
99 
100 }
101