|
@@ -18,12 +18,19 @@
|
|
|
package org.apache.hadoop.hdfs.server.namenode;
|
|
|
|
|
|
import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_NAMENODE_NAME_DIR_KEY;
|
|
|
+import static org.junit.Assert.assertEquals;
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
import static org.junit.Assert.assertFalse;
|
|
|
+import static org.junit.Assert.fail;
|
|
|
+
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.PrintStream;
|
|
|
import java.net.URI;
|
|
|
-import java.util.ArrayList;
|
|
|
+import java.security.Permission;
|
|
|
import java.util.Collection;
|
|
|
import java.util.Iterator;
|
|
|
import java.util.List;
|
|
@@ -40,11 +47,11 @@ import org.junit.After;
|
|
|
import org.junit.Before;
|
|
|
import org.junit.Test;
|
|
|
|
|
|
-
|
|
|
public class TestClusterId {
|
|
|
private static final Log LOG = LogFactory.getLog(TestClusterId.class);
|
|
|
File hdfsDir;
|
|
|
-
|
|
|
+ Configuration config;
|
|
|
+
|
|
|
private String getClusterId(Configuration config) throws IOException {
|
|
|
// see if cluster id not empty.
|
|
|
Collection<URI> dirsToFormat = FSNamesystem.getNamespaceDirs(config);
|
|
@@ -59,33 +66,41 @@ public class TestClusterId {
|
|
|
LOG.info("successfully formated : sd="+sd.getCurrentDir() + ";cid="+cid);
|
|
|
return cid;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@Before
|
|
|
public void setUp() throws IOException {
|
|
|
+ System.setSecurityManager(new NoExitSecurityManager());
|
|
|
+
|
|
|
String baseDir = System.getProperty("test.build.data", "build/test/data");
|
|
|
|
|
|
- hdfsDir = new File(baseDir, "dfs");
|
|
|
- if ( hdfsDir.exists() && !FileUtil.fullyDelete(hdfsDir) ) {
|
|
|
- throw new IOException("Could not delete test directory '" +
|
|
|
- hdfsDir + "'");
|
|
|
+ hdfsDir = new File(baseDir, "dfs/name");
|
|
|
+ if (hdfsDir.exists() && !FileUtil.fullyDelete(hdfsDir)) {
|
|
|
+ throw new IOException("Could not delete test directory '" + hdfsDir + "'");
|
|
|
}
|
|
|
LOG.info("hdfsdir is " + hdfsDir.getAbsolutePath());
|
|
|
+
|
|
|
+ // as some tests might change these values we reset them to defaults before
|
|
|
+ // every test
|
|
|
+ StartupOption.FORMAT.setForceFormat(false);
|
|
|
+ StartupOption.FORMAT.setInteractiveFormat(true);
|
|
|
+
|
|
|
+ config = new Configuration();
|
|
|
+ config.set(DFS_NAMENODE_NAME_DIR_KEY, hdfsDir.getPath());
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@After
|
|
|
public void tearDown() throws IOException {
|
|
|
- if ( hdfsDir.exists() && !FileUtil.fullyDelete(hdfsDir) ) {
|
|
|
- throw new IOException("Could not tearDown test directory '" +
|
|
|
- hdfsDir + "'");
|
|
|
+ System.setSecurityManager(null);
|
|
|
+
|
|
|
+ if (hdfsDir.exists() && !FileUtil.fullyDelete(hdfsDir)) {
|
|
|
+ throw new IOException("Could not tearDown test directory '" + hdfsDir
|
|
|
+ + "'");
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@Test
|
|
|
public void testFormatClusterIdOption() throws IOException {
|
|
|
- Configuration config = new Configuration();
|
|
|
|
|
|
- config.set(DFS_NAMENODE_NAME_DIR_KEY, new File(hdfsDir, "name").getPath());
|
|
|
-
|
|
|
// 1. should format without cluster id
|
|
|
//StartupOption.FORMAT.setClusterId("");
|
|
|
NameNode.format(config);
|
|
@@ -107,4 +122,356 @@ public class TestClusterId {
|
|
|
String newCid = getClusterId(config);
|
|
|
assertFalse("ClusterId should not be the same", newCid.equals(cid));
|
|
|
}
|
|
|
-}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test namenode format with -format option. Format should succeed.
|
|
|
+ *
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void testFormat() throws IOException {
|
|
|
+ String[] argv = { "-format" };
|
|
|
+ try {
|
|
|
+ NameNode.createNameNode(argv, config);
|
|
|
+ fail("createNameNode() did not call System.exit()");
|
|
|
+ } catch (ExitException e) {
|
|
|
+ assertEquals("Format should have succeeded", 0, e.status);
|
|
|
+ }
|
|
|
+
|
|
|
+ String cid = getClusterId(config);
|
|
|
+ assertTrue("Didn't get new ClusterId", (cid != null && !cid.equals("")));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test namenode format with -format option when an empty name directory
|
|
|
+ * exists. Format should succeed.
|
|
|
+ *
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void testFormatWithEmptyDir() throws IOException {
|
|
|
+
|
|
|
+ if (!hdfsDir.mkdirs()) {
|
|
|
+ fail("Failed to create dir " + hdfsDir.getPath());
|
|
|
+ }
|
|
|
+
|
|
|
+ String[] argv = { "-format" };
|
|
|
+ try {
|
|
|
+ NameNode.createNameNode(argv, config);
|
|
|
+ fail("createNameNode() did not call System.exit()");
|
|
|
+ } catch (ExitException e) {
|
|
|
+ assertEquals("Format should have succeeded", 0, e.status);
|
|
|
+ }
|
|
|
+
|
|
|
+ String cid = getClusterId(config);
|
|
|
+ assertTrue("Didn't get new ClusterId", (cid != null && !cid.equals("")));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test namenode format with -format -force options when name directory
|
|
|
+ * exists. Format should succeed.
|
|
|
+ *
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void testFormatWithForce() throws IOException {
|
|
|
+
|
|
|
+ if (!hdfsDir.mkdirs()) {
|
|
|
+ fail("Failed to create dir " + hdfsDir.getPath());
|
|
|
+ }
|
|
|
+
|
|
|
+ String[] argv = { "-format", "-force" };
|
|
|
+ try {
|
|
|
+ NameNode.createNameNode(argv, config);
|
|
|
+ fail("createNameNode() did not call System.exit()");
|
|
|
+ } catch (ExitException e) {
|
|
|
+ assertEquals("Format should have succeeded", 0, e.status);
|
|
|
+ }
|
|
|
+
|
|
|
+ String cid = getClusterId(config);
|
|
|
+ assertTrue("Didn't get new ClusterId", (cid != null && !cid.equals("")));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test namenode format with -format -force -clusterid option when name
|
|
|
+ * directory exists. Format should succeed.
|
|
|
+ *
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void testFormatWithForceAndClusterId() throws IOException {
|
|
|
+
|
|
|
+ if (!hdfsDir.mkdirs()) {
|
|
|
+ fail("Failed to create dir " + hdfsDir.getPath());
|
|
|
+ }
|
|
|
+
|
|
|
+ String myId = "testFormatWithForceAndClusterId";
|
|
|
+ String[] argv = { "-format", "-force", "-clusterid", myId };
|
|
|
+ try {
|
|
|
+ NameNode.createNameNode(argv, config);
|
|
|
+ fail("createNameNode() did not call System.exit()");
|
|
|
+ } catch (ExitException e) {
|
|
|
+ assertEquals("Format should have succeeded", 0, e.status);
|
|
|
+ }
|
|
|
+
|
|
|
+ String cId = getClusterId(config);
|
|
|
+ assertEquals("ClusterIds do not match", myId, cId);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test namenode format with -clusterid -force option. Format command should
|
|
|
+ * fail as no cluster id was provided.
|
|
|
+ *
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void testFormatWithInvalidClusterIdOption() throws IOException {
|
|
|
+
|
|
|
+ String[] argv = { "-format", "-clusterid", "-force" };
|
|
|
+ PrintStream origErr = System.err;
|
|
|
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
+ PrintStream stdErr = new PrintStream(baos);
|
|
|
+ System.setErr(stdErr);
|
|
|
+
|
|
|
+ NameNode.createNameNode(argv, config);
|
|
|
+
|
|
|
+ // Check if usage is printed
|
|
|
+ assertTrue(baos.toString("UTF-8").contains("Usage: java NameNode"));
|
|
|
+ System.setErr(origErr);
|
|
|
+
|
|
|
+ // check if the version file does not exists.
|
|
|
+ File version = new File(hdfsDir, "current/VERSION");
|
|
|
+ assertFalse("Check version should not exist", version.exists());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test namenode format with -format -clusterid options. Format should fail
|
|
|
+ * was no clusterid was sent.
|
|
|
+ *
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void testFormatWithNoClusterIdOption() throws IOException {
|
|
|
+
|
|
|
+ String[] argv = { "-format", "-clusterid" };
|
|
|
+ PrintStream origErr = System.err;
|
|
|
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
+ PrintStream stdErr = new PrintStream(baos);
|
|
|
+ System.setErr(stdErr);
|
|
|
+
|
|
|
+ NameNode.createNameNode(argv, config);
|
|
|
+
|
|
|
+ // Check if usage is printed
|
|
|
+ assertTrue(baos.toString("UTF-8").contains("Usage: java NameNode"));
|
|
|
+ System.setErr(origErr);
|
|
|
+
|
|
|
+ // check if the version file does not exists.
|
|
|
+ File version = new File(hdfsDir, "current/VERSION");
|
|
|
+ assertFalse("Check version should not exist", version.exists());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test namenode format with -format -clusterid and empty clusterid. Format
|
|
|
+ * should fail as no valid if was provided.
|
|
|
+ *
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void testFormatWithEmptyClusterIdOption() throws IOException {
|
|
|
+
|
|
|
+ String[] argv = { "-format", "-clusterid", "" };
|
|
|
+
|
|
|
+ PrintStream origErr = System.err;
|
|
|
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
+ PrintStream stdErr = new PrintStream(baos);
|
|
|
+ System.setErr(stdErr);
|
|
|
+
|
|
|
+ NameNode.createNameNode(argv, config);
|
|
|
+
|
|
|
+ // Check if usage is printed
|
|
|
+ assertTrue(baos.toString("UTF-8").contains("Usage: java NameNode"));
|
|
|
+ System.setErr(origErr);
|
|
|
+
|
|
|
+ // check if the version file does not exists.
|
|
|
+ File version = new File(hdfsDir, "current/VERSION");
|
|
|
+ assertFalse("Check version should not exist", version.exists());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test namenode format with -format -nonInteractive options when a non empty
|
|
|
+ * name directory exists. Format should not succeed.
|
|
|
+ *
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void testFormatWithNonInteractive() throws IOException {
|
|
|
+
|
|
|
+ // we check for a non empty dir, so create a child path
|
|
|
+ File data = new File(hdfsDir, "file");
|
|
|
+ if (!data.mkdirs()) {
|
|
|
+ fail("Failed to create dir " + data.getPath());
|
|
|
+ }
|
|
|
+
|
|
|
+ String[] argv = { "-format", "-nonInteractive" };
|
|
|
+ try {
|
|
|
+ NameNode.createNameNode(argv, config);
|
|
|
+ fail("createNameNode() did not call System.exit()");
|
|
|
+ } catch (ExitException e) {
|
|
|
+ assertEquals("Format should have been aborted with exit code 1", 1,
|
|
|
+ e.status);
|
|
|
+ }
|
|
|
+
|
|
|
+ // check if the version file does not exists.
|
|
|
+ File version = new File(hdfsDir, "current/VERSION");
|
|
|
+ assertFalse("Check version should not exist", version.exists());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test namenode format with -format -nonInteractive options when name
|
|
|
+ * directory does not exist. Format should succeed.
|
|
|
+ *
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void testFormatWithNonInteractiveNameDirDoesNotExit()
|
|
|
+ throws IOException {
|
|
|
+
|
|
|
+ String[] argv = { "-format", "-nonInteractive" };
|
|
|
+ try {
|
|
|
+ NameNode.createNameNode(argv, config);
|
|
|
+ fail("createNameNode() did not call System.exit()");
|
|
|
+ } catch (ExitException e) {
|
|
|
+ assertEquals("Format should have succeeded", 0, e.status);
|
|
|
+ }
|
|
|
+
|
|
|
+ String cid = getClusterId(config);
|
|
|
+ assertTrue("Didn't get new ClusterId", (cid != null && !cid.equals("")));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test namenode format with -force -nonInteractive -force option. Format
|
|
|
+ * should succeed.
|
|
|
+ *
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void testFormatWithNonInteractiveAndForce() throws IOException {
|
|
|
+
|
|
|
+ if (!hdfsDir.mkdirs()) {
|
|
|
+ fail("Failed to create dir " + hdfsDir.getPath());
|
|
|
+ }
|
|
|
+
|
|
|
+ String[] argv = { "-format", "-nonInteractive", "-force" };
|
|
|
+ try {
|
|
|
+ NameNode.createNameNode(argv, config);
|
|
|
+ fail("createNameNode() did not call System.exit()");
|
|
|
+ } catch (ExitException e) {
|
|
|
+ assertEquals("Format should have succeeded", 0, e.status);
|
|
|
+ }
|
|
|
+
|
|
|
+ String cid = getClusterId(config);
|
|
|
+ assertTrue("Didn't get new ClusterId", (cid != null && !cid.equals("")));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test namenode format with -format option when a non empty name directory
|
|
|
+ * exists. Enter Y when prompted and the format should succeed.
|
|
|
+ *
|
|
|
+ * @throws IOException
|
|
|
+ * @throws InterruptedException
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void testFormatWithoutForceEnterYes() throws IOException,
|
|
|
+ InterruptedException {
|
|
|
+
|
|
|
+ // we check for a non empty dir, so create a child path
|
|
|
+ File data = new File(hdfsDir, "file");
|
|
|
+ if (!data.mkdirs()) {
|
|
|
+ fail("Failed to create dir " + data.getPath());
|
|
|
+ }
|
|
|
+
|
|
|
+ // capture the input stream
|
|
|
+ InputStream origIn = System.in;
|
|
|
+ ByteArrayInputStream bins = new ByteArrayInputStream("Y\n".getBytes());
|
|
|
+ System.setIn(bins);
|
|
|
+
|
|
|
+ String[] argv = { "-format" };
|
|
|
+
|
|
|
+ try {
|
|
|
+ NameNode.createNameNode(argv, config);
|
|
|
+ fail("createNameNode() did not call System.exit()");
|
|
|
+ } catch (ExitException e) {
|
|
|
+ assertEquals("Format should have succeeded", 0, e.status);
|
|
|
+ }
|
|
|
+
|
|
|
+ System.setIn(origIn);
|
|
|
+
|
|
|
+ String cid = getClusterId(config);
|
|
|
+ assertTrue("Didn't get new ClusterId", (cid != null && !cid.equals("")));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test namenode format with -format option when a non empty name directory
|
|
|
+ * exists. Enter N when prompted and format should be aborted.
|
|
|
+ *
|
|
|
+ * @throws IOException
|
|
|
+ * @throws InterruptedException
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void testFormatWithoutForceEnterNo() throws IOException,
|
|
|
+ InterruptedException {
|
|
|
+
|
|
|
+ // we check for a non empty dir, so create a child path
|
|
|
+ File data = new File(hdfsDir, "file");
|
|
|
+ if (!data.mkdirs()) {
|
|
|
+ fail("Failed to create dir " + data.getPath());
|
|
|
+ }
|
|
|
+
|
|
|
+ // capture the input stream
|
|
|
+ InputStream origIn = System.in;
|
|
|
+ ByteArrayInputStream bins = new ByteArrayInputStream("N\n".getBytes());
|
|
|
+ System.setIn(bins);
|
|
|
+
|
|
|
+ String[] argv = { "-format" };
|
|
|
+ try {
|
|
|
+ NameNode.createNameNode(argv, config);
|
|
|
+ fail("createNameNode() did not call System.exit()");
|
|
|
+ } catch (ExitException e) {
|
|
|
+ assertEquals("Format should not have succeeded", 1, e.status);
|
|
|
+ }
|
|
|
+
|
|
|
+ System.setIn(origIn);
|
|
|
+
|
|
|
+ // check if the version file does not exists.
|
|
|
+ File version = new File(hdfsDir, "current/VERSION");
|
|
|
+ assertFalse("Check version should not exist", version.exists());
|
|
|
+ }
|
|
|
+
|
|
|
+ private static class ExitException extends SecurityException {
|
|
|
+ private static final long serialVersionUID = 1L;
|
|
|
+ public final int status;
|
|
|
+
|
|
|
+ public ExitException(int status) {
|
|
|
+ super("There is no escape!");
|
|
|
+ this.status = status;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static class NoExitSecurityManager extends SecurityManager {
|
|
|
+ @Override
|
|
|
+ public void checkPermission(Permission perm) {
|
|
|
+ // allow anything.
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void checkPermission(Permission perm, Object context) {
|
|
|
+ // allow anything.
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void checkExit(int status) {
|
|
|
+ super.checkExit(status);
|
|
|
+ throw new ExitException(status);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|