HDDS-130. TestGenerateOzoneRequiredConfigurations should use 
GenericTestUtils#getTempPath to set output directory. Contributed by Sandeep 
Nemuri.


Project: http://git-wip-us.apache.org/repos/asf/hadoop/repo
Commit: http://git-wip-us.apache.org/repos/asf/hadoop/commit/e9ea9022
Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/e9ea9022
Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/e9ea9022

Branch: refs/heads/HADOOP-15461
Commit: e9ea9022999f83aca000a10edbb76489e6b79d12
Parents: 652bcbb
Author: Xiaoyu Yao <x...@apache.org>
Authored: Tue Jun 12 09:03:42 2018 -0700
Committer: Xiaoyu Yao <x...@apache.org>
Committed: Tue Jun 12 09:03:55 2018 -0700

----------------------------------------------------------------------
 ...TestGenerateOzoneRequiredConfigurations.java | 72 ++++++++++++++------
 1 file changed, 50 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hadoop/blob/e9ea9022/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/genconf/TestGenerateOzoneRequiredConfigurations.java
----------------------------------------------------------------------
diff --git 
a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/genconf/TestGenerateOzoneRequiredConfigurations.java
 
b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/genconf/TestGenerateOzoneRequiredConfigurations.java
index 82582a6..cfd1159 100644
--- 
a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/genconf/TestGenerateOzoneRequiredConfigurations.java
+++ 
b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/genconf/TestGenerateOzoneRequiredConfigurations.java
@@ -18,56 +18,57 @@
 
 package org.apache.hadoop.ozone.genconf;
 
-import org.apache.hadoop.hdds.conf.OzoneConfiguration;
-import org.apache.hadoop.ozone.MiniOzoneCluster;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang.RandomStringUtils;
+import org.apache.hadoop.test.GenericTestUtils;
+import org.hamcrest.CoreMatchers;
 import org.junit.AfterClass;
 import org.junit.Assert;
 import org.junit.BeforeClass;
 import org.junit.Test;
 
+import java.io.ByteArrayOutputStream;
+import java.io.File;
 import java.io.IOException;
+import java.io.PrintStream;
 
 
 /**
  * Tests GenerateOzoneRequiredConfigurations.
  */
 public class TestGenerateOzoneRequiredConfigurations {
-  private static MiniOzoneCluster cluster;
-  private static OzoneConfiguration conf;
-
+  private static File outputBaseDir;
   /**
-   * Create a MiniDFSCluster for testing.
-   * <p>
-   * Ozone is made active by setting OZONE_ENABLED = true and
-   * OZONE_HANDLER_TYPE_KEY = "distributed"
+   * Creates output directory which will be used by the test-cases.
+   * If a test-case needs a separate directory, it has to create a random
+   * directory inside {@code outputBaseDir}.
    *
-   * @throws IOException
+   * @throws Exception In case of exception while creating output directory.
    */
   @BeforeClass
   public static void init() throws Exception {
-    conf = new OzoneConfiguration();
-    cluster = MiniOzoneCluster.newBuilder(conf).setNumDatanodes(1).build();
-    cluster.waitForClusterToBeReady();
+    outputBaseDir = GenericTestUtils.getTestDir();
+    FileUtils.forceMkdir(outputBaseDir);
   }
 
   /**
-   * Shutdown MiniDFSCluster.
+   * Cleans up the output base directory.
    */
   @AfterClass
-  public static void shutdown() {
-    if (cluster != null) {
-      cluster.shutdown();
-    }
+  public static void cleanup() throws IOException {
+    FileUtils.deleteDirectory(outputBaseDir);
   }
 
   /**
-   * Tests a valid path and generates ozone-site.xml.
+   * Tests a valid path and generates ozone-site.xml by calling
+   * {@code GenerateOzoneRequiredConfigurations#generateConfigurations}.
+   *
    * @throws Exception
    */
   @Test
-  public void generateConfigurationsSuccess() throws Exception {
-    String[] args = new String[]{"-output", "."};
-    GenerateOzoneRequiredConfigurations.main(args);
+  public void testGenerateConfigurations() throws Exception {
+    File tempPath = getRandomTempDir();
+    String[] args = new String[]{"-output", tempPath.getAbsolutePath()};
 
     Assert.assertEquals("Path is valid",
         true, GenerateOzoneRequiredConfigurations.isValidPath(args[1]));
@@ -80,6 +81,27 @@ public class TestGenerateOzoneRequiredConfigurations {
   }
 
   /**
+   * Tests ozone-site.xml generation by calling
+   * {@code GenerateOzoneRequiredConfigurations#main}.
+   *
+   * @throws Exception
+   */
+  @Test
+  public void testGenerateConfigurationsThroughMainMethod() throws Exception {
+    File tempPath = getRandomTempDir();
+    String[] args = new String[]{"-output", tempPath.getAbsolutePath()};
+    ByteArrayOutputStream outContent = new ByteArrayOutputStream();
+    PrintStream oldStream = System.out;
+    try (PrintStream ps = new PrintStream(outContent)) {
+      System.setOut(ps);
+      GenerateOzoneRequiredConfigurations.main(args);
+      Assert.assertThat(outContent.toString(),
+              CoreMatchers.containsString("ozone-site.xml has been generated 
at"));
+      System.setOut(oldStream);
+    }
+  }
+
+  /**
    * Test to avoid generating ozone-site.xml when invalid permission.
    * @throws Exception
    */
@@ -97,4 +119,10 @@ public class TestGenerateOzoneRequiredConfigurations {
     Assert.assertEquals("Config file not generated",
         1, 
GenerateOzoneRequiredConfigurations.generateConfigurations(args[1]));
   }
+
+  private File getRandomTempDir() throws IOException {
+    File tempDir = new File(outputBaseDir, 
RandomStringUtils.randomAlphanumeric(5));
+    FileUtils.forceMkdir(tempDir);
+    return tempDir;
+  }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: common-commits-unsubscr...@hadoop.apache.org
For additional commands, e-mail: common-commits-h...@hadoop.apache.org

Reply via email to