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

This commit is contained in:
Xiaoyu Yao 2018-06-12 09:03:42 -07:00
parent 652bcbb3e4
commit e9ea902299

View File

@ -18,56 +18,57 @@
package org.apache.hadoop.ozone.genconf; package org.apache.hadoop.ozone.genconf;
import org.apache.hadoop.hdds.conf.OzoneConfiguration; import org.apache.commons.io.FileUtils;
import org.apache.hadoop.ozone.MiniOzoneCluster; import org.apache.commons.lang.RandomStringUtils;
import org.apache.hadoop.test.GenericTestUtils;
import org.hamcrest.CoreMatchers;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.Assert; import org.junit.Assert;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream;
/** /**
* Tests GenerateOzoneRequiredConfigurations. * Tests GenerateOzoneRequiredConfigurations.
*/ */
public class TestGenerateOzoneRequiredConfigurations { public class TestGenerateOzoneRequiredConfigurations {
private static MiniOzoneCluster cluster; private static File outputBaseDir;
private static OzoneConfiguration conf;
/** /**
* Create a MiniDFSCluster for testing. * Creates output directory which will be used by the test-cases.
* <p> * If a test-case needs a separate directory, it has to create a random
* Ozone is made active by setting OZONE_ENABLED = true and * directory inside {@code outputBaseDir}.
* OZONE_HANDLER_TYPE_KEY = "distributed"
* *
* @throws IOException * @throws Exception In case of exception while creating output directory.
*/ */
@BeforeClass @BeforeClass
public static void init() throws Exception { public static void init() throws Exception {
conf = new OzoneConfiguration(); outputBaseDir = GenericTestUtils.getTestDir();
cluster = MiniOzoneCluster.newBuilder(conf).setNumDatanodes(1).build(); FileUtils.forceMkdir(outputBaseDir);
cluster.waitForClusterToBeReady();
} }
/** /**
* Shutdown MiniDFSCluster. * Cleans up the output base directory.
*/ */
@AfterClass @AfterClass
public static void shutdown() { public static void cleanup() throws IOException {
if (cluster != null) { FileUtils.deleteDirectory(outputBaseDir);
cluster.shutdown();
}
} }
/** /**
* 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 * @throws Exception
*/ */
@Test @Test
public void generateConfigurationsSuccess() throws Exception { public void testGenerateConfigurations() throws Exception {
String[] args = new String[]{"-output", "."}; File tempPath = getRandomTempDir();
GenerateOzoneRequiredConfigurations.main(args); String[] args = new String[]{"-output", tempPath.getAbsolutePath()};
Assert.assertEquals("Path is valid", Assert.assertEquals("Path is valid",
true, GenerateOzoneRequiredConfigurations.isValidPath(args[1])); true, GenerateOzoneRequiredConfigurations.isValidPath(args[1]));
@ -79,6 +80,27 @@ public void generateConfigurationsSuccess() throws Exception {
0, GenerateOzoneRequiredConfigurations.generateConfigurations(args[1])); 0, GenerateOzoneRequiredConfigurations.generateConfigurations(args[1]));
} }
/**
* 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. * Test to avoid generating ozone-site.xml when invalid permission.
* @throws Exception * @throws Exception
@ -97,4 +119,10 @@ public void generateConfigurationsFailure() throws Exception {
Assert.assertEquals("Config file not generated", Assert.assertEquals("Config file not generated",
1, GenerateOzoneRequiredConfigurations.generateConfigurations(args[1])); 1, GenerateOzoneRequiredConfigurations.generateConfigurations(args[1]));
} }
private File getRandomTempDir() throws IOException {
File tempDir = new File(outputBaseDir, RandomStringUtils.randomAlphanumeric(5));
FileUtils.forceMkdir(tempDir);
return tempDir;
}
} }